Skip to content

Commit 47cfa90

Browse files
gunjjoshikgryte
andauthored
feat!: update the return value for n=1 in math/base/special/bernoulli
BREAKING CHANGE: update return value for `n=1` In order to migrate and preserve prior behavior, users should special case `n=1` and return `0`. The change in this commit aligns return values with SymPy and R; although, other libraries and envs choose to return `-0.5`. PR-URL: #3108 Ref: #3037 (comment) Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Gunj Joshi <gunjjoshi8372@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 53a3667 commit 47cfa90

File tree

13 files changed

+52
-39
lines changed

13 files changed

+52
-39
lines changed

lib/node_modules/@stdlib/math/base/special/bernoulli/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var v = bernoulli( 0 );
4343
// returns 1.0
4444

4545
v = bernoulli( 1 );
46-
// returns 0.0
46+
// returns 0.5
4747

4848
v = bernoulli( 2 );
4949
// returns ~0.167
@@ -158,7 +158,7 @@ double out = stdlib_base_bernoulli( 0 );
158158
// returns 1.0
159159

160160
out = stdlib_base_bernoulli( 1 );
161-
// returns 0.0
161+
// returns 0.5
162162
```
163163

164164
The function accepts the following arguments:

lib/node_modules/@stdlib/math/base/special/bernoulli/benchmark/benchmark.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
// MODULES //
2222

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

37+
x = randu( 100, 0, 500 );
38+
3839
b.tic();
3940
for ( i = 0; i < b.iterations; i++ ) {
40-
x = floor( randu()*500.0 );
41-
y = bernoulli( x );
41+
y = bernoulli( x[ i % x.length ] );
4242
if ( isnan( y ) ) {
4343
b.fail( 'should not return NaN' );
4444
}

lib/node_modules/@stdlib/math/base/special/bernoulli/benchmark/benchmark.native.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
26-
var floor = require( '@stdlib/math/base/special/floor' );
25+
var randu = require( '@stdlib/random/array/discrete-uniform' );
2726
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var tryRequire = require( '@stdlib/utils/try-require' );
2928
var pkg = require( './../package.json' ).name;
@@ -44,10 +43,11 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4443
var y;
4544
var i;
4645

46+
x = randu( 100, 0, 500 );
47+
4748
b.tic();
4849
for ( i = 0; i < b.iterations; i++ ) {
49-
x = floor( randu() * 500.0 );
50-
y = bernoulli( x );
50+
y = bernoulli( x[ i % x.length ] );
5151
if ( isnan( y ) ) {
5252
b.fail( 'should not return NaN' );
5353
}

lib/node_modules/@stdlib/math/base/special/bernoulli/benchmark/c/native/benchmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,18 @@ static double rand_double( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
double x;
94+
double x[ 100 ];
9595
double y;
9696
double t;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
x[ i ] = ( 500.0 * rand_double() );
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = ( 500.0 * rand_double() );
102-
y = stdlib_base_bernoulli( x );
105+
y = stdlib_base_bernoulli( (int)( x[ i % 100 ] ) );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

lib/node_modules/@stdlib/math/base/special/bernoulli/docs/repl.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
> var y = {{alias}}( 0 )
2222
1.0
2323
> y = {{alias}}( 1 )
24-
0.0
24+
0.5
2525
> y = {{alias}}( 2 )
2626
~0.167
2727
> y = {{alias}}( 3 )

lib/node_modules/@stdlib/math/base/special/bernoulli/docs/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @example
3232
* var y = bernoulli( 1 );
33-
* // returns 0.0
33+
* // returns 0.5
3434
*
3535
* @example
3636
* var y = bernoulli( 2 );

lib/node_modules/@stdlib/math/base/special/bernoulli/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* // returns 1.0
3131
*
3232
* y = bernoulli( 1 );
33-
* // returns 0.0
33+
* // returns 0.5
3434
*
3535
* y = bernoulli( 2 );
3636
* // returns ~0.166

lib/node_modules/@stdlib/math/base/special/bernoulli/lib/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var MAX_BERNOULLI = 258|0; // asm type annotation
4747
*
4848
* @example
4949
* var y = bernoulli( 1 );
50-
* // returns 0.0
50+
* // returns 0.5
5151
*
5252
* @example
5353
* var y = bernoulli( 2 );
@@ -85,6 +85,9 @@ function bernoulli( n ) {
8585
if ( isnan( n ) || !isNonNegativeInteger( n ) ) {
8686
return NaN;
8787
}
88+
if ( n === 1 ) {
89+
return 0.5;
90+
}
8891
if ( isOdd( n ) ) {
8992
return 0.0;
9093
}

lib/node_modules/@stdlib/math/base/special/bernoulli/lib/native.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var addon = require( './../src/addon.node' );
3737
*
3838
* @example
3939
* var y = bernoulli( 1 );
40-
* // returns 0.0
40+
* // returns 0.5
4141
*
4242
* @example
4343
* var y = bernoulli( 2 );

lib/node_modules/@stdlib/math/base/special/bernoulli/manifest.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
"libpath": [],
3838
"dependencies": [
3939
"@stdlib/math/base/napi/unary",
40-
"@stdlib/math/base/assert/is-nonnegative-integer",
41-
"@stdlib/math/base/assert/is-nan",
4240
"@stdlib/math/base/assert/is-odd",
4341
"@stdlib/constants/float64/ninf",
4442
"@stdlib/constants/float64/pinf"
@@ -55,8 +53,6 @@
5553
"libraries": [],
5654
"libpath": [],
5755
"dependencies": [
58-
"@stdlib/math/base/assert/is-nonnegative-integer",
59-
"@stdlib/math/base/assert/is-nan",
6056
"@stdlib/math/base/assert/is-odd",
6157
"@stdlib/constants/float64/ninf",
6258
"@stdlib/constants/float64/pinf"
@@ -73,8 +69,6 @@
7369
"libraries": [],
7470
"libpath": [],
7571
"dependencies": [
76-
"@stdlib/math/base/assert/is-nonnegative-integer",
77-
"@stdlib/math/base/assert/is-nan",
7872
"@stdlib/math/base/assert/is-odd",
7973
"@stdlib/constants/float64/ninf",
8074
"@stdlib/constants/float64/pinf"

lib/node_modules/@stdlib/math/base/special/bernoulli/src/main.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/assert/is_nonnegative_integer.h"
20-
#include "stdlib/math/base/assert/is_nan.h"
2119
#include "stdlib/math/base/assert/is_odd.h"
2220
#include "stdlib/constants/float64/ninf.h"
2321
#include "stdlib/constants/float64/pinf.h"
@@ -170,9 +168,12 @@ int32_t MAX_BERNOULLI = 258;
170168
* // returns 1
171169
*/
172170
double stdlib_base_bernoulli( const int32_t n ) {
173-
if ( stdlib_base_is_nan( n ) || !stdlib_base_is_nonnegative_integer( n ) ) {
171+
if ( n < 0 ) {
174172
return 0.0 / 0.0; // NaN
175173
}
174+
if ( n == 1 ) {
175+
return 0.5;
176+
}
176177
if ( stdlib_base_is_odd( n ) ) {
177178
return 0.0;
178179
}

lib/node_modules/@stdlib/math/base/special/bernoulli/test/test.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,37 @@ tape( 'if provided a negative number, the function returns `NaN`', function test
4444
var v;
4545
var i;
4646

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

4949
for ( i = -1; i > -100; i-- ) {
5050
v = bernoulli( i );
51-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
51+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
5252
}
5353
t.end();
5454
});
5555

56+
tape( 'if provided `1`, the function returns `0.5`', function test( t ) {
57+
var v = bernoulli( 1 );
58+
t.strictEqual( v, 0.5, 'returns expected value' );
59+
t.end();
60+
});
61+
5662
tape( 'if provided `NaN`, the function returns `NaN`', function test( t ) {
5763
var v = bernoulli( NaN );
58-
t.strictEqual( isnan( v ), true, 'returns NaN when provided a NaN' );
64+
t.strictEqual( isnan( v ), true, 'returns expected value' );
5965
t.end();
6066
});
6167

6268
tape( 'if provided a non-integer, the function returns `NaN`', function test( t ) {
6369
var v = bernoulli( 3.14 );
64-
t.strictEqual( isnan( v ), true, 'returns NaN' );
70+
t.strictEqual( isnan( v ), true, 'returns expected value' );
6571
t.end();
6672
});
6773

6874
tape( 'the function returns the nth Bernoulli number for odd numbers', function test( t ) {
6975
var v;
7076
var i;
71-
for ( i = 1; i < 500; i += 2 ) {
77+
for ( i = 3; i < 500; i += 2 ) {
7278
v = bernoulli( i );
7379

7480
// Odd Bernoulli numbers are equal to zero:
@@ -93,9 +99,9 @@ tape( 'the function returns +/- infinity for large integers', function test( t )
9399
for ( i = 260; i < 1000; i += 2 ) {
94100
v = bernoulli( i );
95101
if ( i % 4 === 0 ) {
96-
t.strictEqual( v, NINF, 'returns negative infinity' );
102+
t.strictEqual( v, NINF, 'returns expected value' );
97103
} else {
98-
t.strictEqual( v, PINF, 'returns positive infinity' );
104+
t.strictEqual( v, PINF, 'returns expected value' );
99105
}
100106
}
101107
t.end();

lib/node_modules/@stdlib/math/base/special/bernoulli/test/test.native.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,25 @@ tape( 'if provided a negative number, the function returns `NaN`', opts, functio
5353
var v;
5454
var i;
5555

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

5858
for ( i = -1; i > -100; i-- ) {
5959
v = bernoulli( i );
60-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
60+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
6161
}
6262
t.end();
6363
});
6464

65+
tape( 'if provided `1`, the function returns `0.5`', opts, function test( t ) {
66+
var v = bernoulli( 1 );
67+
t.strictEqual( v, 0.5, 'returns expected value' );
68+
t.end();
69+
});
70+
6571
tape( 'the function returns the nth Bernoulli number for odd numbers', opts, function test( t ) {
6672
var v;
6773
var i;
68-
for ( i = 1; i < 500; i += 2 ) {
74+
for ( i = 3; i < 500; i += 2 ) {
6975
v = bernoulli( i );
7076

7177
// Odd Bernoulli numbers are equal to zero:
@@ -90,9 +96,9 @@ tape( 'the function returns +/- infinity for large integers', opts, function tes
9096
for ( i = 260; i < 1000; i += 2 ) {
9197
v = bernoulli( i );
9298
if ( i % 4 === 0 ) {
93-
t.strictEqual( v, NINF, 'returns negative infinity' );
99+
t.strictEqual( v, NINF, 'returns expected value' );
94100
} else {
95-
t.strictEqual( v, PINF, 'returns positive infinity' );
101+
t.strictEqual( v, PINF, 'returns expected value' );
96102
}
97103
}
98104
t.end();

0 commit comments

Comments
 (0)