Skip to content

Commit a4cc7bc

Browse files
authored
refactor: clamp return value to expected range
Signed-off-by: Athan <kgryte@gmail.com>
1 parent 7bcbc15 commit a4cc7bc

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

  • lib/node_modules/@stdlib/stats/strided/dpcorrwd/lib

lib/node_modules/@stdlib/stats/strided/dpcorrwd/lib/ndarray.js

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

2323
var sqrt = require( '@stdlib/math/base/special/sqrt' );
24+
var clamp = require( '@stdlib/math/base/special/clamp' );
2425

2526

2627
// MAIN //
@@ -162,6 +163,7 @@ function dpcorrwd( N, x, strideX, offsetX, y, strideY, offsetY ) {
162163
var sx;
163164
var sy;
164165
var C;
166+
var r;
165167
var i;
166168

167169
if ( N <= 0 ) {
@@ -194,7 +196,8 @@ function dpcorrwd( N, x, strideX, offsetX, y, strideY, offsetY ) {
194196
}
195197
sx = sqrt( M2x/N );
196198
sy = sqrt( M2y/N );
197-
return ( C/N ) / ( sx*sy ); // Note: why all the dividing by `N`? To avoid overflow.
199+
r = ( C/N ) / ( sx*sy ); // Why all the dividing by `N`? To avoid overflow.
200+
return clamp( r, -1.0, 1.0 ); // Guard against floating-point errors resulting in |r| > 1
198201
}
199202

200203

0 commit comments

Comments
 (0)