Skip to content

Commit 55915e5

Browse files
committed
Remove redundant tests and fix signed zero handling when window is sliding
1 parent 328f81f commit 55915e5

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ function incrmmax( W ) {
106106
else if ( isnan( x ) && isnan( buf[ i ] ) ) {
107107
max = x;
108108
}
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 ) ) ) {
109+
// Case: incoming value is greater than current maximum value...
110+
else if ( x > max ) {
111111
max = x;
112112
}
113113
// 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...
@@ -126,6 +126,22 @@ function incrmmax( W ) {
126126
}
127127
}
128128
}
129+
// Case: outgoing value is the current maximum, which is zero, and the new value is also zero, and, thus, we need to correctly handle signed zeros...
130+
else if ( buf[ i ] === max && x === max && x === 0.0 ) {
131+
if ( isPositiveZero( x ) ) {
132+
max = x;
133+
} else if ( isPositiveZero( buf[ i ] ) ) {
134+
// Because the outgoing and incoming are different signs (+,-), we need to search the buffer to see if it contains a positive zero. If so, it becomes the new maximum value; otherwise, the maximum value is incoming value...
135+
max = x;
136+
for ( k = 0; k < W; k++ ) {
137+
if ( k !== i && isPositiveZero( buf[ k ] ) ) {
138+
max = buf[ k ];
139+
break;
140+
}
141+
}
142+
}
143+
// Case: both the outgoing and incoming are both negative zero, so nothing changes
144+
}
129145
// Case: updating existing window; however, the maximum value does not change so nothing to do but update our buffer...
130146

131147
buf[ i ] = x;

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,25 @@ tape( 'if data has yet to be provided, the accumulator function returns `null`',
114114
t.end();
115115
});
116116

117-
tape( 'the accumulator function correctly handles signed zeros (initial window)', function test( t ) {
117+
tape( 'the accumulator function correctly handles signed zeros', function test( t ) {
118+
var expected;
119+
var data;
118120
var acc;
119121
var v;
122+
var i;
120123

121124
acc = incrmmax( 3 );
122125

123-
v = acc( -0.0 );
124-
t.equal( isNegativeZero( v ), true, 'returns expected value' );
125-
126-
v = acc( 0.0 );
127-
t.equal( isPositiveZero( v ), true, 'returns expected value' );
128-
129-
v = acc( -0.0 );
130-
t.equal( isPositiveZero( v ), true, 'returns expected value' );
131-
126+
data = [ -0.0, 0.0, -0.0, -0.0, -0.0, 0.0, -0.0, -0.0, -0.0, 0.0 ];
127+
expected = [ -0.0, 0.0, 0.0, 0.0, -0.0, 0.0, 0.0, 0.0, -0.0, 0.0 ];
128+
for ( i = 0; i < data.length; i++ ) {
129+
v = acc( data[ i ] );
130+
if ( isPositiveZero( expected[ i ] ) ) {
131+
t.equal( isPositiveZero( v ), true, 'returns expected value' );
132+
} else {
133+
t.equal( isNegativeZero( v ), true, 'returns expected value' );
134+
}
135+
}
132136
t.end();
133137
});
134138

0 commit comments

Comments
 (0)