Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var fibonacci = require( '@stdlib/math/base/special/fibonacci' );
var pkg = require( './../package.json' ).name;
Expand All @@ -37,14 +37,16 @@ bench( pkg, function benchmark( b ) {
var y;
var i;

FN = new Array( 76 );
FN = zeros( 76 );
for ( i = 3; i < 79; i++ ) {
FN[ i ] = fibonacci( i );
}

x = discreteUniform( 100, 3, 78 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( (randu()*75.0) + 3.0 );
y = fibonacciIndex( FN[ x ] );
y = fibonacciIndex( FN[ 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,8 @@

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 zeros = require( '@stdlib/array/base/zeros' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var fibonacci = require( '@stdlib/math/base/special/fibonacci' );
var tryRequire = require( '@stdlib/utils/try-require' );
Expand All @@ -46,14 +46,16 @@ bench( pkg+'::native', opts, function benchmark( b ) {
var y;
var i;

FN = new Array( 76 );
FN = zeros( 76 );
for ( i = 3; i < 79; i++ ) {
FN[ i ] = fibonacci( i );
}

x = discreteUniform( 100, 3, 78 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( ( randu() * 75.0 ) + 3.0 );
y = fibonacciIndex( FN[ x ] );
y = fibonacciIndex( FN[ 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 @@ -102,15 +102,18 @@ int fibonacci_index( int F ) {
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( (100000.0*rand_double()) + 2.0 );
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
// note: using actual Fibonacci numbers is not important
x = (int)floor( (100000.0*rand_double()) + 2.0 );
y = fibonacci_index( x );
y = fibonacci_index( x[ i%100 ] );
if ( y < 0 ) {
printf( "should return a nonnegative integer\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;
double x;
double x[ 100 ];
double y;
double t;
int i;

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

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = (int)floor( ( 75.0 * rand_double() ) + 3.0 );
y = stdlib_base_fibonacci_index( x );
y = stdlib_base_fibonacci_index( 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 @@ -37,32 +37,32 @@ tape( 'main export is a function', function test( t ) {

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

tape( 'if provided `+infinity`, the function returns `NaN`', function test( t ) {
var n = fibonacciIndex( PINF );
t.strictEqual( isnan( n ), true, 'returns NaN' );
t.strictEqual( isnan( n ), true, 'returns expected value' );
t.end();
});

tape( 'if provided a number less than or equal to 1, the function returns `NaN`', function test( t ) {
var n;
var i;

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

for ( i = 1; i > -100; i-- ) {
n = fibonacciIndex( i );
t.strictEqual( isnan( n ), true, 'returns NaN when provided ' + i );
t.strictEqual( isnan( n ), true, 'returns expected value when provided ' + i );
}
t.end();
});

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,32 @@ tape( 'main export is a function', opts, function test( t ) {

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

tape( 'if provided `+infinity`, the function returns `NaN`', opts, function test( t ) {
var n = fibonacciIndex( PINF );
t.strictEqual( isnan( n ), true, 'returns NaN' );
t.strictEqual( isnan( n ), true, 'returns expected value' );
t.end();
});

tape( 'if provided a number less than or equal to 1, the function returns `NaN`', opts, function test( t ) {
var n;
var i;

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

for ( i = 1; i > -100; i-- ) {
n = fibonacciIndex( i );
t.strictEqual( isnan( n ), true, 'returns NaN when provided ' + i );
t.strictEqual( isnan( n ), true, 'returns expected value when provided ' + i );
}
t.end();
});

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
// 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 round = require( '@stdlib/math/base/special/round' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var pow = require( '@stdlib/math/base/special/pow' );
var zeros = require( '@stdlib/array/base/zeros' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var PHI = require( '@stdlib/constants/float64/phi' );
var pkg = require( './../package.json' ).name;
Expand All @@ -45,10 +45,11 @@ bench( pkg, function benchmark( b ) {
var y;
var i;

x = discreteUniform( 100, 0, 79 );

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

x = discreteUniform( 100, 0, 79 );

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

x = discreteUniform( 100, 0, 79 );

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

x = discreteUniform( 100, 0, 40 ); // limit upper bound

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( randu()*40.0 ); // limit upper bound
y = fibonacci( x );
y = fibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -142,7 +146,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 @@ -156,10 +160,11 @@ bench( pkg+'::recursion_memoized', function benchmark( b ) {
return arr[ n ];
}

x = discreteUniform( 100, 0, 40 ); // limit upper bound

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

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

x = discreteUniform( 100, 0, 79 ); // limit upper bound

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

x = discreteUniform( 100, 0, 79 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( randu()*79.0 );
y = fibonacci( x );
y = fibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -251,7 +258,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 @@ -268,10 +275,11 @@ bench( pkg+'::iterative_memoized', function benchmark( b ) {
return arr[ n ];
}

x = discreteUniform( 100, 0, 79 );

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

x = discreteUniform( 100, 0, 79 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( randu()*79.0 );
y = fibonacci( x );
y = fibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -337,7 +346,7 @@ bench( pkg+'::iterative_doubling_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 Down Expand Up @@ -377,10 +386,11 @@ bench( pkg+'::iterative_doubling_memoized', function benchmark( b ) {
return a + b;
}

x = discreteUniform( 100, 0, 79 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( randu()*79.0 );
y = fibonacci( x );
y = fibonacci( 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, 0, 79 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( randu()*79.0 );
y = fibonacci( x );
y = fibonacci( x[ i%x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Loading