Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c8b2a70

Browse files
feat: add math/base/special/fast/hypotf
PR-URL: #7998 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent b5a845c commit c8b2a70

File tree

27 files changed

+1904
-0
lines changed

27 files changed

+1904
-0
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# hypotf
22+
23+
> Compute the [hypotenuse][hypotenuse] of a single-precision floating-point number.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var hypotf = require( '@stdlib/math/base/special/fast/hypotf' );
41+
```
42+
43+
#### hypotf( x, y )
44+
45+
Computes the [hypotenuse][hypotenuse] of a single-precision floating-point number.
46+
47+
```javascript
48+
var h = hypotf( -5.0, 12.0 );
49+
// returns 13.0
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
## Notes
61+
62+
- For a sufficiently large `x` and/or `y`, computing the hypotenuse will overflow.
63+
64+
```javascript
65+
var h = hypotf( 1.0e38, 1.0e38 );
66+
// returns Infinity
67+
```
68+
69+
Similarly, for sufficiently small `x` and/or `y`, computing the hypotenuse will underflow.
70+
71+
```javascript
72+
var h = hypotf( 1.0e-45, 1.0e-45 );
73+
// returns 0.0
74+
```
75+
76+
</section>
77+
78+
<!-- /.notes -->
79+
80+
<!-- Package usage examples. -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint no-undef: "error" -->
87+
88+
```javascript
89+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
90+
var logEachMap = require( '@stdlib/console/log-each-map' );
91+
var hypotf = require( '@stdlib/math/base/special/fast/hypotf' );
92+
93+
var opts = {
94+
'dtype': 'float32'
95+
};
96+
var x = discreteUniform( 100, -50, 50, opts );
97+
var y = discreteUniform( 100, -50, 50, opts );
98+
99+
logEachMap( 'h(%d,%d) = %0.4f', x, y, hypotf );
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- C interface documentation. -->
107+
108+
* * *
109+
110+
<section class="c">
111+
112+
## C APIs
113+
114+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
115+
116+
<section class="intro">
117+
118+
</section>
119+
120+
<!-- /.intro -->
121+
122+
<!-- C usage documentation. -->
123+
124+
<section class="usage">
125+
126+
### Usage
127+
128+
```c
129+
#include "stdlib/math/base/special/fast/hypotf.h"
130+
```
131+
132+
#### stdlib_base_fast_hypotf( x, y )
133+
134+
Computes the hypotenuse of a single-precision floating-point number.
135+
136+
```c
137+
float h = stdlib_base_fast_hypotf( 5.0f, 12.0f );
138+
// returns 13.0f
139+
```
140+
141+
The function accepts the following arguments:
142+
143+
- **x**: `[in] float` input value.
144+
- **y**: `[in] float` input value.
145+
146+
```c
147+
float stdlib_base_fast_hypotf( const float x, const float y );
148+
```
149+
150+
</section>
151+
152+
<!-- /.usage -->
153+
154+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
155+
156+
<section class="notes">
157+
158+
</section>
159+
160+
<!-- /.notes -->
161+
162+
<!-- C API usage examples. -->
163+
164+
<section class="examples">
165+
166+
### Examples
167+
168+
```c
169+
#include "stdlib/math/base/special/fast/hypotf.h"
170+
#include <stdio.h>
171+
172+
int main( void ) {
173+
const float x[] = { 3.0f, 4.0f, 5.0f, 12.0f };
174+
175+
float y;
176+
int i;
177+
for ( i = 0; i < 4; i += 2 ) {
178+
y = stdlib_base_fast_hypotf( x[ i ], x[ i+1 ] );
179+
printf( "hypot(%f, %f) = %f\n", x[ i ], x[ i+1 ], y );
180+
}
181+
}
182+
```
183+
184+
</section>
185+
186+
<!-- /.examples -->
187+
188+
</section>
189+
190+
<!-- /.c -->
191+
192+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
193+
194+
<section class="references">
195+
196+
</section>
197+
198+
<!-- /.references -->
199+
200+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
201+
202+
<section class="related">
203+
204+
205+
</section>
206+
207+
<!-- /.related -->
208+
209+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
210+
211+
<section class="links">
212+
213+
[hypotenuse]: https://en.wikipedia.org/wiki/Pythagorean_theorem
214+
215+
216+
</section>
217+
218+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var hypotf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var opts;
34+
var x;
35+
var y;
36+
var h;
37+
var i;
38+
39+
opts = {
40+
'dtype': 'float32'
41+
};
42+
x = uniform( 100, -50, 50, opts );
43+
y = uniform( 100, -50, 50, opts );
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
h = hypotf( x[ i % x.length ], y[ i % y.length ] );
48+
if ( isnanf( h ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnanf( h ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var hypotf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( hypotf instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var opts;
43+
var x;
44+
var y;
45+
var h;
46+
var i;
47+
48+
opts = {
49+
'dtype': 'float32'
50+
};
51+
x = uniform( 100, -50, 50, opts );
52+
y = uniform( 100, -50, 50, opts );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
h = hypotf( x[ i % x.length ], y[ i % y.length ] );
57+
if ( isnanf( h ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnanf( h ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /