Skip to content

Commit 0dc5e1f

Browse files
fix: update hypot to follow the IEEE 754-2019 standard
PR-URL: #6509 Reviewed-by: Athan Reines <[email protected]>
1 parent 8297683 commit 0dc5e1f

File tree

6 files changed

+39
-10
lines changed

6 files changed

+39
-10
lines changed

Diff for: lib/node_modules/@stdlib/math/base/special/hypot/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ h = hypot( -0.0, -0.0 );
5252
// returns +0.0
5353
```
5454

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

5757
```javascript
5858
var h = hypot( NaN, 12.0 );

Diff for: lib/node_modules/@stdlib/math/base/special/hypot/docs/repl.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{{alias}}( x, y )
33
Computes the hypotenuse avoiding overflow and underflow.
44

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

78
Parameters
89
----------

Diff for: lib/node_modules/@stdlib/math/base/special/hypot/lib/main.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' );
4949
*/
5050
function hypot( x, y ) {
5151
var tmp;
52-
if ( isnan( x ) || isnan( y ) ) {
53-
return NaN;
54-
}
52+
53+
// If one of the arguments is `+-infinity`, return `+infinity` even if the other argument is `NaN` (IEEE 754-2019)...
5554
if ( isInfinite( x ) || isInfinite( y ) ) {
5655
return PINF;
5756
}
57+
if ( isnan( x ) || isnan( y ) ) {
58+
return NaN;
59+
}
5860
if ( x < 0.0 ) {
5961
x = -x;
6062
}

Diff for: lib/node_modules/@stdlib/math/base/special/hypot/src/main.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ double stdlib_base_hypot( const double x, const double y ) {
3737
double tmp;
3838
double a;
3939
double b;
40-
if ( stdlib_base_is_nan( x ) || stdlib_base_is_nan( y ) ) {
41-
return 0.0 / 0.0; // NaN
42-
}
40+
41+
// If one of the arguments is `+-infinity`, return `+infinity` even if the other argument is `NaN` (IEEE 754-2019)...
4342
if ( stdlib_base_is_infinite( x ) || stdlib_base_is_infinite( y ) ) {
4443
return STDLIB_CONSTANT_FLOAT64_PINF;
4544
}
45+
if ( stdlib_base_is_nan( x ) || stdlib_base_is_nan( y ) ) {
46+
return 0.0 / 0.0; // NaN
47+
}
4648
a = x;
4749
b = y;
4850
if ( a < 0.0 ) {

Diff for: lib/node_modules/@stdlib/math/base/special/hypot/test/test.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,22 @@ tape( 'the function returns `+infinity` if either argument is `+-infinity`', fun
7272
h = hypot( NINF, NINF );
7373
t.strictEqual( h, PINF, 'returns expected value' );
7474

75+
h = hypot( NaN, PINF );
76+
t.strictEqual( h, PINF, 'returns expected value' );
77+
78+
h = hypot( PINF, NaN );
79+
t.strictEqual( h, PINF, 'returns expected value' );
80+
81+
h = hypot( NINF, NaN );
82+
t.strictEqual( h, PINF, 'returns expected value' );
83+
84+
h = hypot( NaN, NINF );
85+
t.strictEqual( h, PINF, 'returns expected value' );
86+
7587
t.end();
7688
});
7789

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

8193
h = hypot( NaN, 3.14 );

Diff for: lib/node_modules/@stdlib/math/base/special/hypot/test/test.native.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,22 @@ tape( 'the function returns `+infinity` if either argument is `+-infinity`', opt
8181
h = hypot( NINF, NINF );
8282
t.strictEqual( h, PINF, 'returns expected value' );
8383

84+
h = hypot( NaN, PINF );
85+
t.strictEqual( h, PINF, 'returns expected value' );
86+
87+
h = hypot( PINF, NaN );
88+
t.strictEqual( h, PINF, 'returns expected value' );
89+
90+
h = hypot( NINF, NaN );
91+
t.strictEqual( h, PINF, 'returns expected value' );
92+
93+
h = hypot( NaN, NINF );
94+
t.strictEqual( h, PINF, 'returns expected value' );
95+
8496
t.end();
8597
});
8698

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

90102
h = hypot( NaN, 3.14 );

0 commit comments

Comments
 (0)