Skip to content

Commit 0eb11b4

Browse files
committed
Refactor to correctly handle signed zeros
1 parent 537feb0 commit 0eb11b4

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

lib/node_modules/@stdlib/stats/incr/max/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ max = accumulator();
6666

6767
## Notes
6868

69-
- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
69+
- Input values are **not** type checked. If provided `NaN`, the accumulated value is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
7070

7171
</section>
7272

lib/node_modules/@stdlib/stats/incr/max/docs/repl.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
value. If not provided a value, the accumulator function returns the current
88
maximum value.
99

10+
If provided `NaN`, the maximum value is `NaN` for all future invocations.
11+
1012
Returns
1113
-------
1214
acc: Function

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -29,7 +35,7 @@
2935
* var accumulator = incrmax();
3036
*/
3137
function incrmax() {
32-
var max = null;
38+
var max;
3339
return accumulator;
3440

3541
/**
@@ -57,9 +63,14 @@ function incrmax() {
5763
*/
5864
function accumulator( x ) {
5965
if ( arguments.length === 0 ) {
60-
return max;
66+
return ( max === void 0 ) ? null : max;
6167
}
62-
if ( x > max || max === null ) {
68+
if (
69+
max === void 0 ||
70+
x > max ||
71+
isnan( x ) ||
72+
( x === max && isPositiveZero( x ) )
73+
) {
6374
max = x;
6475
}
6576
return max;

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

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

2323
var tape = require( 'tape' );
24+
var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
25+
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2427
var incrmax = require( './../lib' );
2528

2629

@@ -87,3 +90,35 @@ tape( 'if not provided an input value, the accumulator function returns the curr
8790
t.equal( acc(), 3.0, 'returns the current accumulated maximum value' );
8891
t.end();
8992
});
93+
94+
tape( 'the accumulator function correctly handles signed zeros', function test( t ) {
95+
var acc;
96+
var v;
97+
98+
acc = incrmax();
99+
100+
v = acc( -0.0 );
101+
t.equal( isNegativeZero( v ), true, 'returns expected value' );
102+
103+
v = acc( 0.0 );
104+
t.equal( isPositiveZero( v ), true, 'returns expected value' );
105+
106+
v = acc( -0.0 );
107+
t.equal( isPositiveZero( v ), true, 'returns expected value' );
108+
109+
t.end();
110+
});
111+
112+
tape( 'if provided a `NaN`, the accumulator function returns `NaN` for all future invocations', function test( t ) {
113+
var data;
114+
var acc;
115+
var i;
116+
117+
data = [ 2.0, NaN, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ];
118+
acc = incrmax();
119+
for ( i = 0; i < data.length; i++ ) {
120+
acc( data[ i ] );
121+
}
122+
t.equal( isnan( acc() ), true, 'returns expected value' );
123+
t.end();
124+
});

0 commit comments

Comments
 (0)