Skip to content

Commit 4fe7fa2

Browse files
Om-A-osckgryte
andauthored
refactor: reuse variance in stats/base/dists/binomial/stdev
PR-URL: #11369 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 8b35b4c commit 4fe7fa2

File tree

3 files changed

+10
-30
lines changed

3 files changed

+10
-30
lines changed

lib/node_modules/@stdlib/stats/base/dists/binomial/stdev/lib/main.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020

2121
// MODULES //
2222

23-
var isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' );
24-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2523
var sqrt = require( '@stdlib/math/base/special/sqrt' );
26-
var PINF = require( '@stdlib/constants/float64/pinf' );
24+
var variance = require( '@stdlib/stats/base/dists/binomial/variance' );
2725

2826

2927
// MAIN //
@@ -56,17 +54,7 @@ var PINF = require( '@stdlib/constants/float64/pinf' );
5654
* // returns NaN
5755
*/
5856
function stdev( n, p ) {
59-
if (
60-
isnan( n ) ||
61-
isnan( p ) ||
62-
p < 0.0 ||
63-
p > 1.0 ||
64-
!isNonNegativeInteger( n ) ||
65-
n === PINF
66-
) {
67-
return NaN;
68-
}
69-
return sqrt( n * p * ( 1.0 - p ) );
57+
return sqrt( variance( n, p ) );
7058
}
7159

7260

lib/node_modules/@stdlib/stats/base/dists/binomial/stdev/manifest.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
"libpath": [],
4040
"dependencies": [
4141
"@stdlib/math/base/napi/binary",
42-
"@stdlib/math/base/assert/is-nan",
43-
"@stdlib/math/base/special/sqrt"
42+
"@stdlib/math/base/special/sqrt",
43+
"@stdlib/stats/base/dists/binomial/variance"
4444
]
4545
},
4646
{
@@ -55,9 +55,9 @@
5555
"libraries": [],
5656
"libpath": [],
5757
"dependencies": [
58-
"@stdlib/math/base/assert/is-nan",
5958
"@stdlib/math/base/special/sqrt",
60-
"@stdlib/math/base/special/ceil"
59+
"@stdlib/math/base/special/ceil",
60+
"@stdlib/stats/base/dists/binomial/variance"
6161
]
6262
},
6363
{
@@ -72,9 +72,9 @@
7272
"libraries": [],
7373
"libpath": [],
7474
"dependencies": [
75-
"@stdlib/math/base/assert/is-nan",
7675
"@stdlib/math/base/special/sqrt",
77-
"@stdlib/math/base/special/ceil"
76+
"@stdlib/math/base/special/ceil",
77+
"@stdlib/stats/base/dists/binomial/variance"
7878
]
7979
}
8080
]

lib/node_modules/@stdlib/stats/base/dists/binomial/stdev/src/main.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*/
1818

1919
#include "stdlib/stats/base/dists/binomial/stdev.h"
20-
#include "stdlib/math/base/assert/is_nan.h"
2120
#include "stdlib/math/base/special/sqrt.h"
21+
#include "stdlib/stats/base/dists/binomial/variance.h"
2222
#include <stdint.h>
2323

2424
/**
@@ -33,13 +33,5 @@
3333
* // returns 3.0
3434
*/
3535
double stdlib_base_dists_binomial_stdev( const int32_t n, const double p ) {
36-
if (
37-
stdlib_base_is_nan( p ) ||
38-
n < 0 ||
39-
p < 0.0 ||
40-
p > 1.0
41-
) {
42-
return 0.0 / 0.0;
43-
}
44-
return stdlib_base_sqrt( n * p * (1.0 - p) );
36+
return stdlib_base_sqrt( stdlib_base_dists_binomial_variance( n, p ) );
4537
}

0 commit comments

Comments
 (0)