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 <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Gunj Joshi <[email protected]> Signed-off-by: Athan Reines <[email protected]>
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"

0 commit comments

Comments
 (0)