Skip to content

Commit 8e3ef75

Browse files
committed
Refactor to support signed zeros and NaNs
1 parent 3e457d3 commit 8e3ef75

File tree

1 file changed

+24
-6
lines changed
  • lib/node_modules/@stdlib/stats/incr/mmax/lib

1 file changed

+24
-6
lines changed

lib/node_modules/@stdlib/stats/incr/mmax/lib/main.js

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

2323
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
2426
var NINF = require( '@stdlib/constants/math/float64-ninf' );
2527

2628

@@ -78,6 +80,7 @@ function incrmmax( W ) {
7880
* // returns 5.0
7981
*/
8082
function accumulator( x ) {
83+
var v;
8184
var k;
8285
if ( arguments.length === 0 ) {
8386
if ( N === 0 ) {
@@ -91,20 +94,35 @@ function incrmmax( W ) {
9194
// Case: update initial window...
9295
if ( N < W ) {
9396
N += 1;
94-
if ( x > max ) {
97+
if (
98+
isnan( x ) ||
99+
x > max ||
100+
( x === max && isPositiveZero( x ) )
101+
) {
95102
max = x;
96103
}
97104
}
98-
// Case: incoming value is greater than current maximum value...
99-
else if ( x > max ) {
105+
// Case: both incoming and outgoing values are NaN...
106+
else if ( isnan( x ) && isnan( buf[ i ] ) ) {
107+
max = x;
108+
}
109+
// Case: incoming value is greater than current maximum value OR incoming value is equal to current maximum value which is zero...
110+
else if ( x > max || ( x === max && isPositiveZero( x ) ) ) {
100111
max = x;
101112
}
102113
// Case: outgoing value is the current maximum and the new value is less than the maximum, and, thus, we need to find a new maximum among the current values...
103-
else if ( buf[ i ] === max && x < max ) {
114+
else if ( ( buf[ i ] === max && x < max ) || isnan( buf[ i ] ) ) {
104115
max = x;
105116
for ( k = 0; k < W; k++ ) {
106-
if ( k !== i && buf[ k ] > max ) {
107-
max = buf[ k ];
117+
if ( k !== i ) {
118+
v = buf[ k ];
119+
if ( isnan( v ) ) {
120+
max = v;
121+
break; // no need to continue searching
122+
}
123+
if ( v > max || ( v === max && isPositiveZero( v ) ) ) {
124+
max = v;
125+
}
108126
}
109127
}
110128
}

0 commit comments

Comments
 (0)