Skip to content
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

bench: update random value generation #6430

Merged
merged 2 commits into from
Mar 30, 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 @@ -21,8 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var floor = require( '@stdlib/math/base/special/floor' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var zeros = require( '@stdlib/array/base/zeros' );
var round = require( '@stdlib/math/base/special/round' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var pow = require( '@stdlib/math/base/special/pow' );
Expand All @@ -46,10 +46,11 @@ bench( pkg, function benchmark( b ) {
var y;
var i;

x = discreteUniform( 100, -78, 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = -floor( randu()*79.0 );
y = negafibonacci( x );
y = negafibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -72,10 +73,11 @@ bench( pkg+'::analytic', function benchmark( b ) {
return pow( -1.0, an+1 ) * round( pow( PHI, an ) / SQRT_5 );
}

x = discreteUniform( 100, -78, 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = -floor( randu()*79.0 );
y = negafibonacci( x );
y = negafibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -93,10 +95,11 @@ bench( pkg+'::table', function benchmark( b ) {
var y;
var i;

x = discreteUniform( 100, -78, 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = -floor( randu()*79.0 );
y = NEGAFIBONACCI[ abs( x ) ];
y = NEGAFIBONACCI[ abs( x[ i%x.length ] ) ];
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down Expand Up @@ -124,10 +127,11 @@ bench( pkg+'::naive_recursion', function benchmark( b ) {
return negafibonacci( n+2 ) - negafibonacci( n+1 );
}

x = discreteUniform( 100, -40, 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = -floor( randu()*40.0 ); // limit lower bound
y = negafibonacci( x );
y = negafibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -147,7 +151,7 @@ bench( pkg+'::recursion_memoized', function benchmark( b ) {
var y;
var i;

arr = new Array( 79 );
arr = zeros( 79 );
arr[ 0 ] = 0;
arr[ 1 ] = 1;
arr[ 2 ] = -1;
Expand All @@ -162,10 +166,11 @@ bench( pkg+'::recursion_memoized', function benchmark( b ) {
return arr[ an ];
}

x = discreteUniform( 100, -40, 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = -floor( randu()*40.0 ); // limit lower bound
y = negafibonacci( x );
y = negafibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -190,7 +195,7 @@ bench( pkg+'::naive_iterative', function benchmark( b ) {

an = abs( n );

arr = new Array( an+1 );
arr = zeros( an+1 );
arr[ 0 ] = 0;
arr[ 1 ] = 1;
arr[ 2 ] = -1;
Expand All @@ -200,10 +205,11 @@ bench( pkg+'::naive_iterative', function benchmark( b ) {
return arr[ an ];
}

x = discreteUniform( 100, -40, 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = -floor( randu()*79.0 );
y = negafibonacci( x );
y = negafibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down Expand Up @@ -240,10 +246,11 @@ bench( pkg+'::iterative', function benchmark( b ) {
return b;
}

x = discreteUniform( 100, -78, 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = -floor( randu()*79.0 );
y = negafibonacci( x );
y = negafibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -263,7 +270,7 @@ bench( pkg+'::iterative_memoized', function benchmark( b ) {
var y;
var i;

arr = new Array( 79 );
arr = zeros( 79 );
arr[ 0 ] = 0;
arr[ 1 ] = 1;
arr[ 2 ] = -1;
Expand All @@ -283,10 +290,11 @@ bench( pkg+'::iterative_memoized', function benchmark( b ) {
return arr[ an ];
}

x = discreteUniform( 100, -78, 0 );

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

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var floor = require( '@stdlib/math/base/special/floor' );
var discreteUniform = require( '@stdlib/random/array/discrete-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 @@ -44,10 +43,11 @@ bench( pkg+'::native', opts, function benchmark( b ) {
var y;
var i;

x = discreteUniform( 100, -78, 0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = -floor( randu() * 79.0 );
y = negafibonacci( x );
y = negafibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,17 @@ int negafibonacci( int n ) {
static double benchmark( void ) {
double elapsed;
double t;
int x;
int x[ 100 ];
int y;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = (int)floor( 40.0*rand_double() );
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = (int)floor( 40.0*rand_double() );
y = negafibonacci( -x );
y = negafibonacci( -x[ i%100 ] );
if ( y == 7 ) {
printf( "should not return 7\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,18 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
int32_t x;
int32_t x[ 100 ];
double t;
double y;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = (int32_t)floor( 40.0 * rand_double() );
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = (int32_t)floor( 40.0 * rand_double() );
y = stdlib_base_negafibonacci( -x );
y = stdlib_base_negafibonacci( -x[ i%100 ] );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,51 +46,51 @@
var v;
var i;

t.strictEqual( isnan( negafibonacci( 3.14 ) ), true, 'returns NaN' );
t.strictEqual( isnan( negafibonacci( 3.14 ) ), true, 'returns expected value' );

for ( i = 1; i < 100; i++ ) {
v = negafibonacci( i );
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
}
t.end();
});

tape( 'if provided negative infinity, the function returns `NaN`', function test( t ) {
var v = negafibonacci( NINF );
t.strictEqual( isnan( v ), true, 'returns NaN when provided -infinity' );
t.strictEqual( isnan( v ), true, 'returns expected value' );
t.end();
});

tape( 'if provided `NaN`, the function returns `NaN`', function test( t ) {
var v = negafibonacci( NaN );
t.strictEqual( isnan( v ), true, 'returns NaN when provided a NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );
t.end();
});

tape( 'if provided a non-integer, the function returns `NaN`', function test( t ) {
var v = negafibonacci( -3.14 );
t.strictEqual( isnan( v ), true, 'returns NaN' );
t.strictEqual( isnan( v ), true, 'returns expected value' );
t.end();
});

tape( 'the function returns the nth negaFibonacci number', function test( t ) {

Check warning on line 76 in lib/node_modules/@stdlib/math/base/special/negafibonacci/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "nega"
var v;
var i;
for ( i = 0; i > -79; i-- ) {
v = negafibonacci( i );
t.strictEqual( v, NEGAFIBONACCI[ abs(i) ], 'returns the '+i+'th negaFibonacci number' );

Check warning on line 81 in lib/node_modules/@stdlib/math/base/special/negafibonacci/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "nega"
}
t.end();
});

tape( 'the function returns the nth negaFibonacci number (Fibonacci relationship)', function test( t ) {

Check warning on line 86 in lib/node_modules/@stdlib/math/base/special/negafibonacci/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "nega"
var v;
var f;
var i;
for ( i = 0; i > -79; i-- ) {
v = negafibonacci( i );
f = pow( -1.0, abs(i)+1 ) * fibonacci( abs(i) );
t.strictEqual( v, f, 'returns the '+i+'th negaFibonacci number' );

Check warning on line 93 in lib/node_modules/@stdlib/math/base/special/negafibonacci/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "nega"
}
t.end();
});
Expand All @@ -100,7 +100,7 @@
var v;
for ( i = -79; i > -500; i-- ) {
v = negafibonacci( i );
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
}
t.end();
});
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,33 @@
var v;
var i;

t.strictEqual( isnan( negafibonacci( 3.14 ) ), true, 'returns NaN' );
t.strictEqual( isnan( negafibonacci( 3.14 ) ), true, 'returns expected value' );

for ( i = 1; i < 100; i++ ) {
v = negafibonacci( i );
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
}
t.end();
});

tape( 'the function returns the nth negaFibonacci number', opts, function test( t ) {

Check warning on line 66 in lib/node_modules/@stdlib/math/base/special/negafibonacci/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "nega"
var v;
var i;
for ( i = 0; i > -79; i-- ) {
v = negafibonacci( i );
t.strictEqual( v, NEGAFIBONACCI[ abs(i) ], 'returns the '+i+'th negaFibonacci number' );

Check warning on line 71 in lib/node_modules/@stdlib/math/base/special/negafibonacci/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "nega"
}
t.end();
});

tape( 'the function returns the nth negaFibonacci number (Fibonacci relationship)', opts, function test( t ) {

Check warning on line 76 in lib/node_modules/@stdlib/math/base/special/negafibonacci/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "nega"
var v;
var f;
var i;
for ( i = 0; i > -79; i-- ) {
v = negafibonacci( i );
f = pow( -1.0, abs(i) + 1 ) * fibonacci( abs(i) );
t.strictEqual( v, f, 'returns the '+i+'th negaFibonacci number' );

Check warning on line 83 in lib/node_modules/@stdlib/math/base/special/negafibonacci/test/test.native.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "nega"
}
t.end();
});
Expand All @@ -90,7 +90,7 @@
var v;
for ( i = -79; i > -500; i-- ) {
v = negafibonacci( i );
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
}
t.end();
});
Loading