Skip to content

Commit

Permalink
refactor: return 0.5 when n is 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gunjjoshi committed Nov 13, 2024
1 parent b8259d3 commit 3e9c7d7
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var v = bernoulli( 0 );
// returns 1.0

v = bernoulli( 1 );
// returns 0.0
// returns 0.5

v = bernoulli( 2 );
// returns ~0.167
Expand Down Expand Up @@ -158,7 +158,7 @@ double out = stdlib_base_bernoulli( 0 );
// returns 1.0

out = stdlib_base_bernoulli( 1 );
// returns 0.0
// returns 0.5
```

The function accepts the following arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var floor = require( '@stdlib/math/base/special/floor' );
var randu = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var bernoulli = require( './../lib' );
Expand All @@ -35,10 +34,11 @@ bench( pkg, function benchmark( b ) {
var y;
var i;

x = randu( 100, 0, 500 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( randu()*500.0 );
y = bernoulli( x );
y = bernoulli( 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 randu = 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 = randu( 100, 0, 500 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floor( randu() * 500.0 );
y = bernoulli( x );
y = bernoulli( 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 @@ -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 ] = ( 500.0 * rand_double() );
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = ( 500.0 * rand_double() );
y = stdlib_base_bernoulli( x );
y = stdlib_base_bernoulli( 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 @@ -21,7 +21,7 @@
> var y = {{alias}}( 0 )
1.0
> y = {{alias}}( 1 )
0.0
0.5
> y = {{alias}}( 2 )
~0.167
> y = {{alias}}( 3 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @example
* var y = bernoulli( 1 );
* // returns 0.0
* // returns 0.5
*
* @example
* var y = bernoulli( 2 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* // returns 1.0
*
* y = bernoulli( 1 );
* // returns 0.0
* // returns 0.5
*
* y = bernoulli( 2 );
* // returns ~0.166
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var MAX_BERNOULLI = 258|0; // asm type annotation
*
* @example
* var y = bernoulli( 1 );
* // returns 0.0
* // returns 0.5
*
* @example
* var y = bernoulli( 2 );
Expand Down Expand Up @@ -85,6 +85,9 @@ function bernoulli( n ) {
if ( isnan( n ) || !isNonNegativeInteger( n ) ) {
return NaN;
}
if ( n === 1 ) {
return 0.5;
}
if ( isOdd( n ) ) {
return 0.0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var addon = require( './../src/addon.node' );
*
* @example
* var y = bernoulli( 1 );
* // returns 0.0
* // returns 0.5
*
* @example
* var y = bernoulli( 2 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ double stdlib_base_bernoulli( const int32_t n ) {
if ( stdlib_base_is_nan( n ) || !stdlib_base_is_nonnegative_integer( n ) ) {
return 0.0 / 0.0; // NaN
}
if ( n == 1 ) {
return 0.5;
}
if ( stdlib_base_is_odd( n ) ) {
return 0.0;
}
Expand Down
20 changes: 13 additions & 7 deletions lib/node_modules/@stdlib/math/base/special/bernoulli/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,37 @@ tape( 'if provided a negative number, the function returns `NaN`', function test
var v;
var i;

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

for ( i = -1; i > -100; i-- ) {
v = bernoulli( 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 `1`, the function returns `0.5`', function test( t ) {
var v = bernoulli( 1 );
t.strictEqual( v, 0.5, 'returns expected value when provided 1' );
t.end();
});

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

tape( 'if provided a non-integer, the function returns `NaN`', function test( t ) {
var v = bernoulli( 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 Bernoulli number for odd numbers', function test( t ) {
var v;
var i;
for ( i = 1; i < 500; i += 2 ) {
for ( i = 3; i < 500; i += 2 ) {
v = bernoulli( i );

// Odd Bernoulli numbers are equal to zero:
Expand All @@ -93,9 +99,9 @@ tape( 'the function returns +/- infinity for large integers', function test( t )
for ( i = 260; i < 1000; i += 2 ) {
v = bernoulli( i );
if ( i % 4 === 0 ) {
t.strictEqual( v, NINF, 'returns negative infinity' );
t.strictEqual( v, NINF, 'returns expected value' );
} else {
t.strictEqual( v, PINF, 'returns positive infinity' );
t.strictEqual( v, PINF, 'returns expected value' );
}
}
t.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,25 @@ tape( 'if provided a negative number, the function returns `NaN`', opts, functio
var v;
var i;

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

for ( i = -1; i > -100; i-- ) {
v = bernoulli( 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 `1`, the function returns `0.5`', function test( t ) {
var v = bernoulli( 1 );
t.strictEqual( v, 0.5, 'returns expected value when provided 1' );
t.end();
});

tape( 'the function returns the nth Bernoulli number for odd numbers', opts, function test( t ) {
var v;
var i;
for ( i = 1; i < 500; i += 2 ) {
for ( i = 3; i < 500; i += 2 ) {
v = bernoulli( i );

// Odd Bernoulli numbers are equal to zero:
Expand All @@ -90,9 +96,9 @@ tape( 'the function returns +/- infinity for large integers', opts, function tes
for ( i = 260; i < 1000; i += 2 ) {
v = bernoulli( i );
if ( i % 4 === 0 ) {
t.strictEqual( v, NINF, 'returns negative infinity' );
t.strictEqual( v, NINF, 'returns expected value' );
} else {
t.strictEqual( v, PINF, 'returns positive infinity' );
t.strictEqual( v, PINF, 'returns expected value' );
}
}
t.end();
Expand Down

0 comments on commit 3e9c7d7

Please sign in to comment.