Skip to content

bench: updates random value generation and docs #6658

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 13, 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
20 changes: 8 additions & 12 deletions lib/node_modules/@stdlib/math/base/special/fast/hypot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,17 @@ var h = hypot( -5.0, 12.0 );
<!-- 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 logEachMap = require( '@stdlib/console/log-each-map' );
var hypot = require( '@stdlib/math/base/special/fast/hypot' );

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

for ( i = 0; i < 100; i++ ) {
x = round( randu()*100.0 ) - 50.0;
y = round( randu()*100.0 ) - 50.0;
h = hypot( x, y );
console.log( 'hypot(%d,%d) = %d', x, y, h );
}
logEachMap( 'hypot(%d,%d) = %0.4f', x, y, hypot );
```

</section>
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var hypot = require( './../lib' );
Expand All @@ -30,16 +30,21 @@ var hypot = require( './../lib' );
// MAIN //

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

opts = {
'dtype': 'float64'
};
x = uniform( 100, -50.0, 50.0, opts );
y = uniform( 100, -50.0, 50.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100.0 ) - 50.0;
y = ( randu()*100.0 ) - 50.0;
h = hypot( x, y );
h = hypot( x[ i%x.length ], y[ i%y.length ] );
if ( isnan( h ) ) {
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
Expand All @@ -39,16 +39,21 @@ var opts = {
// MAIN //

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

opts = {
'dtype': 'float64'
};
x = uniform( 100, -50.0, 50.0, opts );
y = uniform( 100, -50.0, 50.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() * 100.0 ) - 50.0;
y = ( randu() * 100.0 ) - 50.0;
h = hypot( x, y );
h = hypot( x[ i%x.length ], y[ i%y.length ] );
if ( isnan( h ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,20 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
double x;
double y;
double x[ 100 ];
double y[ 100 ];
double z;
double t;
int i;

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

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( 100.0 * rand_double() ) - 50.0;
y = ( 100.0 * rand_double() ) - 50.0;
z = stdlib_base_fast_hypot( x, y );
z = stdlib_base_fast_hypot( x[ i%100 ], y[ i%100 ] );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@

'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 logEachMap = require( '@stdlib/console/log-each-map' );
var hypot = require( './../lib' );

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

for ( i = 0; i < 100; i++ ) {
x = round( randu()*100.0 ) - 50.0;
y = round( randu()*100.0 ) - 50.0;
h = hypot( x, y );
console.log( 'hypot(%d,%d) = %d', x, y, h );
}
logEachMap( 'hypot(%d,%d) = %0.4f', x, y, hypot );
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,25 @@ tape( 'the function computes the hypotenuse (canonical inputs)', function test(
var h;

h = hypot( 3.0, 4.0 );
t.strictEqual( h, 5.0, 'returns 5.0' );
t.strictEqual( h, 5.0, 'returns expected value' );

h = hypot( 6.0, 8.0 );
t.strictEqual( h, 10.0, 'returns 10.0' );
t.strictEqual( h, 10.0, 'returns expected value' );

h = hypot( 5.0, 12.0 );
t.strictEqual( h, 13.0, 'returns 13.0' );
t.strictEqual( h, 13.0, 'returns expected value' );

t.end();
});

tape( 'the function can overflow', function test( t ) {
var h = hypot( 1.0e308, 1.0e308 );
t.strictEqual( h, PINF, 'overflows' );
t.strictEqual( h, PINF, 'returns expected value' );
t.end();
});

tape( 'the function can underflow', function test( t ) {
var h = hypot( 1.0e-200, 1.0e-200 );
t.strictEqual( h, 0.0, 'underflows' );
t.strictEqual( h, 0.0, 'returns expected value' );
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,25 @@ tape( 'the function computes the hypotenuse (canonical inputs)', opts, function
var h;

h = hypot( 3.0, 4.0 );
t.strictEqual( h, 5.0, 'returns 5.0' );
t.strictEqual( h, 5.0, 'returns expected value' );

h = hypot( 6.0, 8.0 );
t.strictEqual( h, 10.0, 'returns 10.0' );
t.strictEqual( h, 10.0, 'returns expected value' );

h = hypot( 5.0, 12.0 );
t.strictEqual( h, 13.0, 'returns 13.0' );
t.strictEqual( h, 13.0, 'returns expected value' );

t.end();
});

tape( 'the function can overflow', opts, function test( t ) {
var h = hypot( 1.0e308, 1.0e308 );
t.strictEqual( h, PINF, 'overflows' );
t.strictEqual( h, PINF, 'returns expected value' );
t.end();
});

tape( 'the function can underflow', opts, function test( t ) {
var h = hypot( 1.0e-200, 1.0e-200 );
t.strictEqual( h, 0.0, 'underflows' );
t.strictEqual( h, 0.0, 'returns expected value' );
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,31 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var floor = require( '@stdlib/math/base/special/floor' );
var pkg = require( './../package.json' ).name;
var pow = require( './../lib' );


// MAIN //

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

opts = {
'dtype': 'float64'
};
x = uniform( 100, -50.0, 50.0, opts );
y = discreteUniform( 100, -50.0, 50.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100.0 ) - 50.0;
y = floor( randu()*100.0 ) - 50;
z = pow( x, y );
z = pow( x[ i%x.length ], y[ i%y.length ] );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var floor = require( '@stdlib/math/base/special/floor' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;

Expand All @@ -40,16 +40,21 @@ var opts = {
// MAIN //

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

opts = {
'dtype': 'float64'
};
x = uniform( 100, -50.0, 50.0, opts );
y = discreteUniform( 100, -50.0, 50.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() * 100.0 ) - 50.0;
y = floor( randu() * 100.0 ) - 50;
z = pow( x, y );
z = pow( x[ i%x.length ], y[ i%y.length ] );
if ( isnan( z ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,20 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
int32_t x;
int32_t x[ 100 ];
double y[ 100 ];
double t;
double y;
double z;
int i;

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

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( 100.0 * rand_double() ) - 50.00;
y = floor( 100.0 * rand_double() ) - 50.00;
z = stdlib_base_fast_pow( x, y );
z = stdlib_base_fast_pow( x[ i%100 ], y[ i%100 ] );
if ( z != z ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ tape( 'the function returns `NaN` if provided `NaN` for the base', function test
var v;

v = pow( NaN, 5 );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

v = pow( NaN, 1 );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

v = pow( NaN, 0 );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand Down Expand Up @@ -295,7 +295,7 @@ tape( 'the function returns `0` if `+infinity` is raised to any negative finite
for ( i = 0; i < 100; i++ ) {
y = -round( randu()*1.0e5 ) - 1;
v = pow( PINF, y );
t.strictEqual( isPositiveZero( v ), true, 'returns 0' );
t.strictEqual( isPositiveZero( v ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -307,7 +307,7 @@ tape( 'the function returns `+infinity` if `+infinity` is raised to any positive
for ( i = 0; i < 100; i++ ) {
y = round( randu()*1.0e5 ) + 1;
v = pow( PINF, y );
t.strictEqual( v, PINF, 'returns +infinity' );
t.strictEqual( v, PINF, 'returns expected value' );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ tape( 'the function returns `NaN` if provided `NaN` for the base', opts, functio
var v;

v = pow( NaN, 5 );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

v = pow( NaN, 1 );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

v = pow( NaN, 0 );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand Down Expand Up @@ -304,7 +304,7 @@ tape( 'the function returns `0` if `+infinity` is raised to any negative finite
for ( i = 0; i < 100; i++ ) {
y = -round( randu() * 1.0e5 ) - 1;
v = pow( PINF, y );
t.strictEqual( isPositiveZero( v ), true, 'returns 0' );
t.strictEqual( isPositiveZero( v ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -316,7 +316,7 @@ tape( 'the function returns `+infinity` if `+infinity` is raised to any positive
for ( i = 0; i < 100; i++ ) {
y = round( randu() * 1.0e5 ) + 1;
v = pow( PINF, y );
t.strictEqual( v, PINF, 'returns +infinity' );
t.strictEqual( v, PINF, 'returns expected value' );
}
t.end();
});