Skip to content

Commit

Permalink
refactor: update math/base/special/hypotf to follow latest project …
Browse files Browse the repository at this point in the history
…conventions

PR-URL: stdlib-js#4783
Co-authored-by: Athan Reines <[email protected]>
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
2 people authored and saurabhraghuvanshii committed Jan 19, 2025
1 parent 90bf564 commit 8104e98
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 166 deletions.
21 changes: 10 additions & 11 deletions lib/node_modules/@stdlib/math/base/special/hypotf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,19 @@ h = hypotf( 5.0, NaN );
<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var hypotf = require( '@stdlib/math/base/special/hypotf' );

var x;
var y;
var h;
var i;
var len = 100;
var opts = {
'dtype': 'float32'
};
var x = discreteUniform( len, -50, 50, opts );
var y = discreteUniform( len, -50, 50, opts );

for ( i = 0; i < 100; i++ ) {
x = round( randu()*100.0 ) - 50.0;
y = round( randu()*100.0 ) - 50.0;
h = hypotf( x, y );
console.log( 'h(%d,%d) = %d', x, y, h );
var i;
for ( i = 0; i < len; i++ ) {
console.log( 'h(%d,%d) = %d', x[ i ], y[ i ], hypotf( x[ i ], y[ i ] ) );
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pkg = require( './../package.json' ).name;
var hypotf = require( './../lib' );
Expand All @@ -37,16 +37,24 @@ var opts = {
// MAIN //

bench( pkg, function benchmark( b ) {
var opts;
var len;
var x;
var y;
var z;
var i;

opts = {
'dtype': 'float32'
};

len = 100;
x = uniform( len, -50, 50, opts );
y = uniform( len, -50, 50, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100.0 ) - 50.0;
y = ( randu()*100.0 ) - 50.0;
z = hypotf( x, y );
z = hypotf( x[ i % len ], y[ i % len ] );
if ( isnanf( z ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -60,16 +68,24 @@ bench( pkg, function benchmark( b ) {
});

bench( pkg+'::built-in', opts, function benchmark( b ) {
var opts;
var len;
var x;
var y;
var z;
var i;

opts = {
'dtype': 'float32'
};

len = 100;
x = uniform( len, -50, 50, opts );
y = uniform( len, -50, 50, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100.0 ) - 50.0;
y = ( randu()*100.0 ) - 50.0;
z = Math.hypot( x, y ); // eslint-disable-line stdlib/no-builtin-math
z = Math.hypot( x[ i % len ], y[ i % len ] ); // eslint-disable-line stdlib/no-builtin-math
if ( isnanf( z ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
Expand All @@ -39,16 +39,24 @@ var opts = {
// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var opts;
var len;
var x;
var y;
var z;
var i;

opts = {
'dtype': 'float32'
};

len = 100;
x = uniform( len, -50, 50, opts );
y = uniform( len, -50, 50, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100.0 ) - 50.0;
y = ( randu()*100.0 ) - 50.0;
z = hypotf( x, y );
z = hypotf( x[ i % len ], y[ i % len ] );
if ( isnanf( z ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ static float rand_float( void ) {
static double benchmark( void ) {
double elapsed;
double t;
float x;
float y;
float x[ 100 ];
float y[ 100 ];
float z;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = ( 100.0f * rand_float() ) - 50.0f;
y[ i ] = ( 100.0f * rand_float() ) - 50.0f;
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( 100.0f*rand_float() ) - 50.0f;
y = ( 100.0f*rand_float() ) - 50.0f;
z = stdlib_base_hypotf( x, y );
z = stdlib_base_hypotf( x[ i % 100 ], y[ i % 100 ] );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
Expand Down
21 changes: 10 additions & 11 deletions lib/node_modules/@stdlib/math/base/special/hypotf/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@

'use strict';

var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var hypotf = require( './../lib' );

var x;
var y;
var h;
var i;
var len = 100;
var opts = {
'dtype': 'float32'
};
var x = discreteUniform( len, -50, 50, opts );
var y = discreteUniform( len, -50, 50, opts );

for ( i = 0; i < 100; i++ ) {
x = round( randu()*100.0 ) - 50.0;
y = round( randu()*100.0 ) - 50.0;
h = hypotf( x, y );
console.log( 'h(%d,%d) = %d', x, y, h );
var i;
for ( i = 0; i < len; i++ ) {
console.log( 'h(%d,%d) = %d', x[ i ], y[ i ], hypotf( x[ i ], y[ i ] ) );
}
167 changes: 85 additions & 82 deletions lib/node_modules/@stdlib/math/base/special/hypotf/manifest.json
Original file line number Diff line number Diff line change
@@ -1,84 +1,87 @@
{
"options": {
"task": "build"
},
"fields": [
{
"field": "src",
"resolve": true,
"relative": true
},
{
"field": "include",
"resolve": true,
"relative": true
},
{
"field": "libraries",
"resolve": false,
"relative": false
},
{
"field": "libpath",
"resolve": true,
"relative": false
}
],
"confs": [
{
"task": "build",
"src": [
"./src/hypotf.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/binary",
"@stdlib/math/base/assert/is-nanf",
"@stdlib/math/base/assert/is-infinitef",
"@stdlib/math/base/special/sqrtf"
]
},
{
"task": "benchmark",
"src": [
"./src/hypotf.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nanf",
"@stdlib/math/base/assert/is-infinitef",
"@stdlib/math/base/special/sqrtf"
]
},
{
"task": "examples",
"src": [
"./src/hypotf.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nanf",
"@stdlib/math/base/assert/is-infinitef",
"@stdlib/math/base/special/sqrtf"
]
}
]
"options": {
"task": "build"
},
"fields": [
{
"field": "src",
"resolve": true,
"relative": true
},
{
"field": "include",
"resolve": true,
"relative": true
},
{
"field": "libraries",
"resolve": false,
"relative": false
},
{
"field": "libpath",
"resolve": true,
"relative": false
}
],
"confs": [
{
"task": "build",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/binary",
"@stdlib/math/base/assert/is-nanf",
"@stdlib/math/base/assert/is-infinitef",
"@stdlib/math/base/special/sqrtf",
"@stdlib/constants/float32/pinf"
]
},
{
"task": "benchmark",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nanf",
"@stdlib/math/base/assert/is-infinitef",
"@stdlib/math/base/special/sqrtf",
"@stdlib/constants/float32/pinf"
]
},
{
"task": "examples",
"src": [
"./src/main.c"
],
"include": [
"./include"
],
"libraries": [
"-lm"
],
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-nanf",
"@stdlib/math/base/assert/is-infinitef",
"@stdlib/math/base/special/sqrtf",
"@stdlib/constants/float32/pinf"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "stdlib/math/base/assert/is_nanf.h"
#include "stdlib/math/base/assert/is_infinitef.h"
#include "stdlib/math/base/special/sqrtf.h"
#include <math.h>
#include "stdlib/constants/float32/pinf.h"

/**
* Computes the hypotenuse avoiding overflow and underflow (single-precision).
Expand All @@ -41,7 +41,7 @@ float stdlib_base_hypotf( const float x, const float y ) {
return 0.0f / 0.0f; // NaN
}
if ( stdlib_base_is_infinitef( x ) || stdlib_base_is_infinitef( y ) ) {
return INFINITY;
return STDLIB_CONSTANT_FLOAT32_PINF;
}
a = x;
b = y;
Expand All @@ -60,5 +60,5 @@ float stdlib_base_hypotf( const float x, const float y ) {
return 0.0f;
}
b /= a;
return a * stdlib_base_sqrtf( 1.0f + (b*b) );
return a * stdlib_base_sqrtf( 1.0f + ( b * b ) );
}
Loading

0 comments on commit 8104e98

Please sign in to comment.