Skip to content

fix: update hypotf to follow the IEEE 754-2019 standard #6511

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 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ h = hypotf( -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 = hypotf( 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 (single-precision).

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
12 changes: 7 additions & 5 deletions lib/node_modules/@stdlib/math/base/special/hypotf/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var isInfinitef = require( '@stdlib/math/base/assert/is-infinitef' );
var PINF = require( '@stdlib/constants/float32/pinf' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var sqrtf = require( '@stdlib/math/base/special/sqrtf' );


// MAIN //
Expand All @@ -50,12 +50,14 @@ var sqrt = require( '@stdlib/math/base/special/sqrt' );
*/
function hypotf( x, y ) {
var tmp;
if ( isnanf( x ) || isnanf( y ) ) {
return NaN;
}

// If one of the arguments is `+-infinity`, return `+infinity` even if the other argument is `NaN` (IEEE 754-2019)...
if ( isInfinitef( x ) || isInfinitef( y ) ) {
return PINF;
}
if ( isnanf( x ) || isnanf( y ) ) {
return NaN;
}
x = float64ToFloat32( x );
y = float64ToFloat32( y );
if ( x < 0.0 ) {
Expand All @@ -73,7 +75,7 @@ function hypotf( x, y ) {
return 0.0;
}
y = float64ToFloat32( y / x );
return float64ToFloat32( x * float64ToFloat32( sqrt( float64ToFloat32( 1.0 + float64ToFloat32(y*y) ) ) ) ); // eslint-disable-line max-len
return float64ToFloat32( x * sqrtf( float64ToFloat32( 1.0 + float64ToFloat32(y*y) ) ) ); // eslint-disable-line max-len
}


Expand Down
8 changes: 5 additions & 3 deletions lib/node_modules/@stdlib/math/base/special/hypotf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ float stdlib_base_hypotf( const float x, const float y ) {
float tmp;
float a;
float b;
if ( stdlib_base_is_nanf( x ) || stdlib_base_is_nanf( y ) ) {
return 0.0f / 0.0f; // NaN
}

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

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

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

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

h = hypotf( 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 = hypotf( NaN, 3.14 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,22 @@ tape( 'the function returns `+infinity` if either argument is `+-infinity`', opt
h = hypotf( NINF, NINF );
t.strictEqual( h, PINF, 'returns expected value' );

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

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

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

h = hypotf( 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 = hypotf( NaN, 3.14 );
Expand Down Expand Up @@ -145,6 +157,8 @@ tape( 'the function computes the hypotenuse', opts, function test( t ) {
});

tape( 'the function computes the hypotenuse (canonical inputs)', opts, function test( t ) {
var delta;
var tol;
var h;

h = hypotf( 3.0, 4.0 );
Expand All @@ -153,8 +167,11 @@ tape( 'the function computes the hypotenuse (canonical inputs)', opts, function
h = hypotf( 6.0, 8.0 );
t.strictEqual( h, 10.0, 'returns expected value' );

// NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running it with

CFLAGS="-ffp-contract=off" make install-node-addons NODE_ADDONS_PATTERN="@stdlib/math/base/special/hypotf"

makes the test pass without any tolerance check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @kgryte

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, interesting. Thanks for tracking this down!

h = hypotf( 5.0, 12.0 );
t.strictEqual( h, 13.0, 'returns expected value' );
delta = absf( h - 13.0 );
tol = EPS * absf( 13.0 );
t.strictEqual( delta <= tol, true, 'within tolerance. h: '+h+'. Expected: 13.0. Delta: '+delta+'. Tol: '+tol+'.' );

t.end();
});
Expand Down