Skip to content

Commit 50787bf

Browse files
authored
refactor: add floating-point guard
Signed-off-by: Athan <kgryte@gmail.com>
1 parent 8abc1c1 commit 50787bf

File tree

1 file changed

+4
-1
lines changed
  • lib/node_modules/@stdlib/stats/strided/dpcorrwd/src

1 file changed

+4
-1
lines changed

lib/node_modules/@stdlib/stats/strided/dpcorrwd/src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "stdlib/stats/strided/dpcorrwd.h"
2020
#include "stdlib/math/base/special/sqrt.h"
21+
#include "stdlib/math/base/special/clamp.h"
2122
#include "stdlib/blas/base/shared.h"
2223
#include "stdlib/strided/base/stride2offset.h"
2324

@@ -166,6 +167,7 @@ double API_SUFFIX(stdlib_strided_dpcorrwd_ndarray)( const CBLAS_INT N, const dou
166167
double sy;
167168
double sx;
168169
double C;
170+
double r;
169171

170172
if ( N <= 0 ) {
171173
return 0.0 / 0.0; // NaN
@@ -197,5 +199,6 @@ double API_SUFFIX(stdlib_strided_dpcorrwd_ndarray)( const CBLAS_INT N, const dou
197199
}
198200
sx = stdlib_base_sqrt( M2x/N );
199201
sy = stdlib_base_sqrt( M2y/N );
200-
return ( C/N ) / ( sx*sy ); // Note: why all the dividing by `N`? To avoid overflow.
202+
r = ( C/N ) / ( sx*sy ); // Why all the dividing by `N`? To avoid overflow.
203+
return stdlib_base_clamp( r, -1.0, 1.0 ); // Guard against accumulated floating-point errors resulting in |r| > 1
201204
}

0 commit comments

Comments
 (0)