Skip to content

fix: update hypot to follow the IEEE 754-2019 standard #6509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/math/base/special/hypot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ h = hypot( -0.0, -0.0 );
// returns +0.0
```

If either argument is `NaN`, the function returns `NaN`.
If either argument is `NaN` and the other argument is not `+-Infinity`, the function returns `NaN`.

```javascript
var h = hypot( NaN, 12.0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{{alias}}( x, y )
Computes the hypotenuse avoiding overflow and underflow.

If either argument is `NaN`, the function returns `NaN`.
If either argument is `NaN` and the other argument is not `+-Infinity`,
the function returns `NaN`.

Parameters
----------
Expand Down
8 changes: 5 additions & 3 deletions lib/node_modules/@stdlib/math/base/special/hypot/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' );
*/
function hypot( x, y ) {
var tmp;
if ( isnan( x ) || isnan( y ) ) {
return NaN;
}

// If one of the arguments is `+-infinity`, return `+infinity` even if the other argument is `NaN` (IEEE 754-2019)...
if ( isInfinite( x ) || isInfinite( y ) ) {
return PINF;
}
if ( isnan( x ) || isnan( y ) ) {
return NaN;
}
if ( x < 0.0 ) {
x = -x;
}
Expand Down
8 changes: 5 additions & 3 deletions lib/node_modules/@stdlib/math/base/special/hypot/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ double stdlib_base_hypot( const double x, const double y ) {
double tmp;
double a;
double b;
if ( stdlib_base_is_nan( x ) || stdlib_base_is_nan( y ) ) {
return 0.0 / 0.0; // NaN
}

// If one of the arguments is `+-infinity`, return `+infinity` even if the other argument is `NaN` (IEEE 754-2019)...
if ( stdlib_base_is_infinite( x ) || stdlib_base_is_infinite( y ) ) {
return STDLIB_CONSTANT_FLOAT64_PINF;
}
if ( stdlib_base_is_nan( x ) || stdlib_base_is_nan( y ) ) {
return 0.0 / 0.0; // NaN
}
a = x;
b = y;
if ( a < 0.0 ) {
Expand Down
14 changes: 13 additions & 1 deletion lib/node_modules/@stdlib/math/base/special/hypot/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,22 @@ tape( 'the function returns `+infinity` if either argument is `+-infinity`', fun
h = hypot( NINF, NINF );
t.strictEqual( h, PINF, 'returns expected value' );

h = hypot( NaN, PINF );
t.strictEqual( h, PINF, 'returns expected value' );

h = hypot( PINF, NaN );
t.strictEqual( h, PINF, 'returns expected value' );

h = hypot( NINF, NaN );
t.strictEqual( h, PINF, 'returns expected value' );

h = hypot( NaN, NINF );
t.strictEqual( h, PINF, 'returns expected value' );

t.end();
});

tape( 'the function returns `NaN` if either argument is `NaN`', function test( t ) {
tape( 'the function returns `NaN` if either argument is `NaN` but not `+-infinity`', function test( t ) {
var h;

h = hypot( NaN, 3.14 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,22 @@ tape( 'the function returns `+infinity` if either argument is `+-infinity`', opt
h = hypot( NINF, NINF );
t.strictEqual( h, PINF, 'returns expected value' );

h = hypot( NaN, PINF );
t.strictEqual( h, PINF, 'returns expected value' );

h = hypot( PINF, NaN );
t.strictEqual( h, PINF, 'returns expected value' );

h = hypot( NINF, NaN );
t.strictEqual( h, PINF, 'returns expected value' );

h = hypot( NaN, NINF );
t.strictEqual( h, PINF, 'returns expected value' );

t.end();
});

tape( 'the function returns `NaN` if either argument is `NaN`', opts, function test( t ) {
tape( 'the function returns `NaN` if either argument is `NaN` but not `+-infinity`', opts, function test( t ) {
var h;

h = hypot( NaN, 3.14 );
Expand Down