diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/README.md b/lib/node_modules/@stdlib/stats/incr/nanmrange/README.md new file mode 100644 index 000000000000..27c6e2977b2b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/README.md @@ -0,0 +1,151 @@ + + +# incrnanmrange + +> Compute a moving [range][range] incrementally, ignoring `NaN` values. + +
+ +The [**range**][range] is defined as the difference between the maximum and minimum values. + +
+ + + +
+ +## Usage + +```javascript +var incrnanmrange = require( '@stdlib/stats/incr/nanmrange' ); +``` + +#### incrnanmrange( window ) + +Returns an accumulator `function` which incrementally computes a moving [range][range], ignoring `NaN` values. The `window` parameter defines the number of values over which to compute the moving [range][range]. + +```javascript +var accumulator = incrnanmrange( 3 ); +``` + +#### accumulator( \[x] ) + +If provided an input value `x`, the accumulator function returns an updated [range][range]. If not provided an input value `x`, the accumulator function returns the current [range][range]. + +```javascript +var accumulator = incrnanmrange( 3 ); + +var r = accumulator(); +// returns null + +// Fill the window... +r = accumulator( 2.0 ); // [2.0] +// returns 0.0 + +r = accumulator( 1.0 ); // [2.0, 1.0] +// returns 1.0 + +r = accumulator( NaN ); // [2.0, 1.0, NaN] +// returns 1.0 + +r = accumulator( 3.0 ); // [1.0, NaN, 3.0] +// returns 2.0 + +// Window begins sliding... +r = accumulator( -7.0 ); // [NaN, 3.0, -7.0] +// returns 10.0 + +r = accumulator( -5.0 ); // [3.0, -7.0, -5.0] +// returns 10.0 + +r = accumulator(); +// returns 10.0 +``` + +
+ + + +
+ +## Notes + +- Input values are **not** type checked. If provided `NaN`, the value is ignored. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. +- As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. + +
+ + + +
+ +## Examples + + + +```javascript +var randu = require( '@stdlib/random/base/randu' ); +var incrnanmrange = require( '@stdlib/stats/incr/nanmrange' ); + +var accumulator; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmrange( 5 ); + +// For each simulated datum, update the moving range... +for ( i = 0; i < 100; i++ ) { + if ( randu() < 0.2 ) { + v = NaN; + } else { + v = randu() * 100.0; + } + accumulator( v ); +} +console.log( accumulator() ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/incr/nanmrange/benchmark/benchmark.js new file mode 100644 index 000000000000..31fb4b52721f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/benchmark/benchmark.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var incrnanmrange = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var f; + var i; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = incrnanmrange( (i%5)+1 ); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::accumulator', function benchmark( b ) { + var acc; + var v; + var i; + + acc = incrnanmrange( 5 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = acc( randu() ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/docs/repl.txt b/lib/node_modules/@stdlib/stats/incr/nanmrange/docs/repl.txt new file mode 100644 index 000000000000..9d634bf5eeef --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/docs/repl.txt @@ -0,0 +1,44 @@ + +{{alias}}( W ) + Returns an accumulator function which incrementally computes a moving range. + + The `W` parameter defines the number of values over which to compute the + moving range. + + If provided a value, the accumulator function returns an updated moving + range. If not provided a value, the accumulator function returns the current + moving range. + + As `W` values are needed to fill the window buffer, the first `W-1` returned + values are calculated from smaller sample sizes. Until the window is full, + each returned value is calculated from all provided values. + + Parameters + ---------- + W: integer + Window size. + + Returns + ------- + acc: Function + Accumulator function. + + Examples + -------- + > var accumulator = {{alias}}( 3 ); + > var r = accumulator() + null + > r = accumulator( 2.0 ) + 0.0 + > r = accumulator( -5.0 ) + 7.0 + > r = accumulator( NaN ) + 7.0 + > r = accumulator( 5.0 ) + 10.0 + > r = accumulator() + 10.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/incr/nanmrange/docs/types/index.d.ts new file mode 100644 index 000000000000..55c4eb8c99d1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/docs/types/index.d.ts @@ -0,0 +1,78 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +/** +* If provided a value, returns an updated range; otherwise, returns the current range. +* +* ## Notes +* +* - If provided `NaN`, the `NaN` value is ignored. +* - If the number of non-NaN values in the window is less than two, the function returns `null`. +* +* @param x - value +* @returns range +*/ +type accumulator = ( x?: number ) => number | null; + +/** +* Returns an accumulator function which incrementally computes a moving range, ignoring `NaN` values. +* +* ## Notes +* +* - The `W` parameter defines the number of values over which to compute the moving range. +* - As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values. +* - `NaN` values are ignored. +* +* @param W - window size +* @throws must provide a positive integer +* @returns accumulator function +* +* @example +* var accumulator = incrnanmrange( 3 ); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0 ); +* // returns 0.0 +* +* r = accumulator( -5.0 ); +* // returns 7.0 +* +* r = accumulator( NaN ); +* // returns 7.0 +* +* r = accumulator( 3.0 ); +* // returns 8.0 +* +* r = accumulator( 5.0 ); +* // returns 10.0 +* +* r = accumulator(); +* // returns 10.0 +*/ +declare function incrnanmrange( W: number ): accumulator; + + +// EXPORTS // + +export = incrnanmrange; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/docs/types/test.ts b/lib/node_modules/@stdlib/stats/incr/nanmrange/docs/types/test.ts new file mode 100644 index 000000000000..64c0c55ade9f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/docs/types/test.ts @@ -0,0 +1,66 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import incrnanmrange = require( './index' ); + + +// TESTS // + +// The function returns an accumulator function... +{ + incrnanmrange( 3 ); // $ExpectType accumulator +} + +// The compiler throws an error if the function is provided an argument which is not a number... +{ + incrnanmrange( '5' ); // $ExpectError + incrnanmrange( true ); // $ExpectError + incrnanmrange( false ); // $ExpectError + incrnanmrange( null ); // $ExpectError + incrnanmrange( undefined ); // $ExpectError + incrnanmrange( [] ); // $ExpectError + incrnanmrange( {} ); // $ExpectError + incrnanmrange( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid number of arguments... +{ + incrnanmrange(); // $ExpectError + incrnanmrange( 2, 3 ); // $ExpectError +} + +// The function returns an accumulator function which returns an accumulated result... +{ + const acc = incrnanmrange( 3 ); + + acc(); // $ExpectType number | null + acc( 3.14 ); // $ExpectType number | null +} + +// The compiler throws an error if the returned accumulator function is provided invalid arguments... +{ + const acc = incrnanmrange( 3 ); + + acc( '5' ); // $ExpectError + acc( true ); // $ExpectError + acc( false ); // $ExpectError + acc( null ); // $ExpectError + acc( [] ); // $ExpectError + acc( {} ); // $ExpectError + acc( ( x: number ): number => x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/examples/index.js b/lib/node_modules/@stdlib/stats/incr/nanmrange/examples/index.js new file mode 100644 index 000000000000..dc979e1e0ddd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var randu = require( '@stdlib/random/base/randu' ); +var incrnanmrange = require( './../lib' ); + +var accumulator; +var r; +var v; +var i; + +// Initialize an accumulator: +accumulator = incrnanmrange( 5 ); + +// For each simulated datum, update the moving range... +console.log( '\nValue\tRange\n' ); +for ( i = 0; i < 100; i++ ) { + v = ( randu() > 0.2 ) ? NaN : randu() * 100.0; + r = accumulator( v ); + console.log( '%d\t%d', v.toFixed( 4 ), (r === null) ? null : r.toFixed( 4 ) ); +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/lib/index.js b/lib/node_modules/@stdlib/stats/incr/nanmrange/lib/index.js new file mode 100644 index 000000000000..a50e275b9719 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/lib/index.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute a moving range incrementally, ignoring NaN values. +* +* @module @stdlib/stats/incr/nanmrange +* +* @example +* var incrnanmrange = require( '@stdlib/stats/incr/nanmrange' ); +* +* var accumulator = incrnanmrange( 3 ); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0 ); +* // returns 0.0 +* +* r = accumulator( -5.0 ); +* // returns 7.0 +* +* r = accumulator( NaN ); +* // returns 7.0 +* +* r = accumulator( 3.0 ); +* // returns 8.0 +* +* r = accumulator( 5.0 ); +* // returns 10.0 +* +* r = accumulator(); +* // returns 10.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/lib/main.js b/lib/node_modules/@stdlib/stats/incr/nanmrange/lib/main.js new file mode 100644 index 000000000000..08f3a8240eb1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/lib/main.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var mrange = require( '@stdlib/stats/incr/mrange' ); + + +// MAIN // + +/** +* Returns an accumulator function which incrementally computes a moving range, ignoring `NaN` values. +* +* @param {PositiveInteger} W - window size +* @throws {TypeError} must provide a positive integer +* @returns {Function} accumulator function +* +* @example +* var accumulator = incrnanmrange( 3 ); +* +* var r = accumulator(); +* // returns null +* +* r = accumulator( 2.0 ); +* // returns 0.0 +* +* r = accumulator( -5.0 ); +* // returns 7.0 +* +* r = accumulator( NaN ); +* // returns 7.0 +* +* r = accumulator( 3.0 ); +* // returns 8.0 +* +* r = accumulator( 5.0 ); +* // returns 10.0 +* +* r = accumulator(); +* // returns 10.0 +*/ +function incrnanmrange( W ) { + var acc = mrange( W ); + return accumulator; + + /** + * If provided a value, the accumulator function returns an updated range. If not provided a value, the accumulator function returns the current range. + * + * @private + * @param {number} [x] - new value + * @returns {(number|null)} range or null + */ + function accumulator( x ) { + if ( arguments.length === 0 || isnan( x ) ) { + return acc(); + } + return acc( x ); + } +} + + +// EXPORTS // + +module.exports = incrnanmrange; diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/package.json b/lib/node_modules/@stdlib/stats/incr/nanmrange/package.json new file mode 100644 index 000000000000..9ab86db1cc3e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/stats/incr/nanmrange", + "version": "0.0.0", + "description": "Compute a moving range incrementally.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "maximum", + "max", + "minimum", + "min", + "range", + "dispersion", + "variance", + "domain", + "extent", + "incremental", + "accumulator", + "sliding window", + "sliding", + "window", + "moving" + ] +} diff --git a/lib/node_modules/@stdlib/stats/incr/nanmrange/test/test.js b/lib/node_modules/@stdlib/stats/incr/nanmrange/test/test.js new file mode 100644 index 000000000000..74b6acc152fc --- /dev/null +++ b/lib/node_modules/@stdlib/stats/incr/nanmrange/test/test.js @@ -0,0 +1,419 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var incrnanmrange = require( '@stdlib/stats/incr/nanmrange/lib' ); +var abs = require( '@stdlib/math/base/special/abs' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof incrnanmrange, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a positive integer', function test( t ) { + var values; + var i; + + values = [ + '5', + -5.0, + 0.0, + 3.14, + true, + false, + null, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + incrnanmrange( value ); + }; + } +}); + +tape( 'the function returns an accumulator function', function test( t ) { + t.equal( typeof incrnanmrange( 3 ), 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the accumulator function computes a moving range incrementally', function test( t ) { + var expected; + var actual; + var data; + var acc; + var N; + var i; + + data = [ 2.0, 3.0, 2.0, 4.0, 3.0, 4.0, 2.0, 2.0, 2.0, 1.0, 0.0, 4.0, -1.0 ]; + N = data.length; + + acc = incrnanmrange( 3 ); + + actual = []; + for ( i = 0; i < N; i++ ) { + actual.push( acc( data[ i ] ) ); + } + expected = [ + 0.0, + 1.0, + 1.0, + 2.0, + 2.0, + 1.0, + 2.0, + 2.0, + 0.0, + 1.0, + 2.0, + 4.0, + 5.0 + ]; + + t.deepEqual( actual, expected, 'returns expected incremental results' ); + t.end(); +}); + +tape( 'if not provided an input value, the accumulator function returns the current range', function test( t ) { + var data; + var acc; + var i; + + data = [ 2.0, 3.0, 5.0, 4.0 ]; + acc = incrnanmrange( 2 ); + for ( i = 0; i < data.length; i++ ) { + acc( data[ i ] ); + } + t.equal( acc(), 1.0, 'returns expected value' ); + t.end(); +}); + +tape( 'if data has yet to be provided, the accumulator function returns `null`', function test( t ) { + var acc = incrnanmrange( 3 ); + t.equal( acc(), null, 'returns null' ); + t.end(); +}); + +tape( 'the accumulator function correctly handles signed zeros', function test( t ) { + var expected; + var data; + var acc; + var v; + var i; + + acc = incrnanmrange( 3 ); + + data = [ + 0.0, // 0 => min: 0.0, max: 0.0 (0) + -0.0, // 0, -0 => min: -0.0, max: 0.0 (1) + 0.0, // 0, -0, 0 => min: -0.0, max: 0.0 (2) + 0.0, // -0, 0, 0 => min: -0.0, max: 0.0 (3) + 0.0, // 0, 0, 0 => min: 0.0, max: 0.0 (4) + -0.0, // 0, 0, -0 => min: -0.0, max: 0.0 (5) + 0.0, // 0, -0, 0 => min: -0.0, max: 0.0 (6) + 0.0, // -0, 0, 0 => min: -0.0, max: 0.0 (7) + 0.0, // 0, 0, 0 => min: 0.0, max: 0.0 (8) + -0.0, // 0, 0, -0 => min: -0.0, max: 0.0 (9) + -0.0, // 0, -0, -0 => min: -0.0, max: 0.0 (10) + -0.0, // -0, -0, -0 => min: -0.0, max: -0.0 (11) + 0.0, // -0, -0, 0 => min: -0.0, max: 0.0 (12) + + -0.0, // -0, 0, -0 => min: -0.0, max: 0.0 (13) + 0.0, // 0, -0, 0 => min: -0.0, max: 0.0 (14) + -0.0, // -0, 0, -0 => min: -0.0, max: 0.0 (15) + -0.0, // 0, -0, -0 => min: -0.0, max: 0.0 (16) + -0.0, // -0, -0, -0 => min: -0.0, max: -0.0 (17) + 0.0, // -0, -0, 0 => min: -0.0, max: 0.0 (18) + -0.0, // -0, 0, -0 => min: -0.0, max: 0.0 (19) + -0.0, // 0, -0, -0 => min: -0.0, max: 0.0 (20) + -0.0, // -0, -0, -0 => min: -0.0, max: -0.0 (21) + 0.0, // -0, -0, 0 => min: -0.0, max: 0.0 (22) + 0.0, // -0, 0, 0 => min: -0.0, max: 0.0 (23) + 0.0, // 0, 0, 0 => min: 0.0, max: 0.0 (24) + -0.0, // 0, 0, -0 => min: -0.0, max: 0.0 (25) + + // Case 1: out: -0, in: +0, cnt: 1 + 3.14, // 0, -0, 3.14 => min: -0.0, max: 3.14 + 3.14, // -0, 3.14, 3.14 => min: -0.0, max: 3.14 + 0.0, // 3.14, 3.14, 0 => min: 0.0, max: 3.14 + + // Case 2: out: +0, in: -0, cnt: 1 + 3.14, // 3.14, 0, 3.14 => min: 0.0, max: 3.14 + 3.14, // 0, 3.14, 3.14 => min: 0.0, max: 3.14 + -0.0, // 3.14, 3.14, -0 => min: -0.0, max: 3.14 + + // Case 3: out: -0, in: -0, cnt: 1 + 3.14, // 3.14, -0, 3.14 => min: -0.0, max: 3.14 + 3.14, // -0, 3.14, 3.14 => min: -0.0, max: 3.14 + -0.0, // 3.14, 3.14, -0 => min: -0.0, max: 3.14 + + // Case 4: out: -0, in: +0, cnt: 2 + 3.14, // 3.14, -0, 3.14 => min: -0.0, max: 3.14 + -0.0, // -0, 3.14, -0 => min: -0.0, max: 3.14 + 0.0, // 3.14, -0, 0 => min: -0.0, max: 3.14 + + // Case 5: out: +0, in: +0, cnt: 1 + 3.14, // -0, 0, 3.14 => min: -0.0, max: 3.14 + 3.14, // 0, 3.14, 3.14 => min: 0.0, max: 3.14 + 0.0, // 3.14, 3.14, 0 => min: 0.0, max: 3.14 + + // Case 6: out: +0, in: -0, cnt: 2 + 3.14, // 3.14, 0, 3.14 => min: 0.0, max: 3.14 + -0.0, // 0, 3.14, -0 => min: -0.0, max: 3.14 + 0.0, // 3.14, -0, 0 => min: -0.0, max: 3.14 + + // Case 7: out: +0, in: +0, cnt: 2 + 3.14, // -0, 0, 3.14 => min: -0.0, max: 3.14 + 0.0, // 0, 3.14, 0 => min: 0.0, max: 3.14 + 0.0, // 3.14, 0, 0 => min: 0.0, max: 3.14 + + // Reset: + -0.0, // 0, 0, -0 => min: -0.0, max: 0.0 + + // Case 8: out: -0, in: +0, cnt: 1 + -3.14, // 0, -0, -3.14 => min: -3.14, max: 0.0 + -3.14, // 0, -3.14, -3.14 => min: -3.14, max: 0.0 + 0.0, // -3.14, -3.14, 0 => min: -3.14, max: 0.0 + + // Case 9: out: +0, in: -0, cnt: 1 + -3.14, // -3.14, 0, 3.14 => min: -3.14, max: 0.0 + -3.14, // 0, -3.14, -3.14 => min: -3.14, max: 0.0 + -0.0, // -3.14, -3.14, -0 => min: -3.14, max: -0.0 + + // Case 10: out: -0, in: -0, cnt: 1 + -3.14, // -3.14, -0, -3.14 => min: -3.14, max: -0.0 + -3.14, // -0, -3.14, -3.14 => min: -3.14, max: -0.0 + -0.0, // -3.14, -3.14, -0 => min: -3.14, max: -0.0 + + // Case 11: out: -0, in: +0, cnt: 2 + -3.14, // -3.14, -0, -3.14 => min: -3.14, max: -0.0 + -0.0, // -0, -3.14, -0 => min: -3.14, max: -0.0 + 0.0, // -3.14, -0, 0 => min: -3.14, max: 0.0 + + // Case 12: out: +0, in: +0, cnt: 1 + -3.14, // -0, 0, -3.14 => min: -3.14, max: 0.0 + -3.14, // 0, -3.14, -3.14 => min: -3.14, max: 0.0 + 0.0, // -3.14, -3.14, 0 => min: -3.14, max: 0.0 + + // Case 13: out: +0, in: -0, cnt: 2 + -3.14, // -3.14, 0, -3.14 => min: -3.14, max: 0.0 + -0.0, // 0, -3.14, -0 => min: -3.14, max: 0.0 + 0.0, // -3.14, -0, 0 => min: -3.14, max: 0.0 + + // Case 14: out: +0, in: +0, cnt: 2 + -3.14, // -0, 0, -3.14 => min: -3.14, max: 0.0 + 0.0, // 0, -3.14, 0 => min: -3.14, max: 0.0 + 0.0, // -3.14, 0, 0 => min: -3.14, max: 0.0 + + // Reset: + 0.0, // 0, 0, 0 => min: 0.0, max: 0.0 + + // Case 15: out: +0, in: -0, cnt: 2 + -3.14, // 0, 0, -3.14 => min: -3.14, max: 0.0 + 0.0, // 0, -3.14, 0 => min: -3.14, max: 0.0 + -0.0 // -3.14, 0, -0 => min: -3.14, max: 0.0 + ]; + expected = [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + + // Case 1: + 3.14, + 3.14, + 3.14, + + // Case 2: + 3.14, + 3.14, + 3.14, + + // Case 3: + 3.14, + 3.14, + 3.14, + + // Case 4: + 3.14, + 3.14, + 3.14, + + // Case 5: + 3.14, + 3.14, + 3.14, + + // Case 6: + 3.14, + 3.14, + 3.14, + + // Case 7: + 3.14, + 3.14, + 3.14, + + // Reset: + 0.0, + + // Case 8: + 3.14, + 3.14, + 3.14, + + // Case 9: + 3.14, + 3.14, + 3.14, + + // Case 10: + 3.14, + 3.14, + 3.14, + + // Case 11: + 3.14, + 3.14, + 3.14, + + // Case 12: + 3.14, + 3.14, + 3.14, + + // Case 13: + 3.14, + 3.14, + 3.14, + + // Case 14: + 3.14, + 3.14, + 3.14, + + // Reset: + 0.0, + + // Case 15: + 3.14, + 3.14, + 3.14 + ]; + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + if ( expected[ i ] === 0.0 ) { + t.equal( isNegativeZero( v ), isNegativeZero( expected[ i ] ), 'returns expected value for window '+i ); + } else { + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + } + } + t.end(); +}); + +tape( 'if provided NaN, the accumulator function ignores it and returns the previous result', function test( t ) { + var expected; + var data; + var acc; + var v; + var i; + + acc = incrnanmrange( 3 ); + + data = [ + 3.14, // [3.14] -> range: 0.0 (single value) + NaN, // [3.14] -> range: 0.0 (NaN ignored) + 5.0, // [3.14, 5.0] -> range: 1.86 + 7.0, // [3.14, 5.0, 7.0] -> range: 3.86 + NaN, // [3.14, 5.0, 7.0] -> range: 3.86 (NaN ignored) + NaN, // [3.14, 5.0, 7.0] -> range: 3.86 (NaN ignored) + 2.0, // [5.0, 7.0, 2.0] -> range: 5.0 + NaN, // [5.0, 7.0, 2.0] -> range: 5.0 (NaN ignored) + 1.0, // [7.0, 2.0, 1.0] -> range: 6.0 + 10.0 // [2.0, 1.0, 10.0] -> range: 9.0 + ]; + expected = [ + 0.0, // single value has range 0 + 0.0, // NaN ignored, previous value returned + 1.86, // range between 3.14 and 5.0 + 3.86, // range between 3.14 and 7.0 + 3.86, // NaN ignored, previous value returned + 3.86, // NaN ignored, previous value returned + 5.0, // range between 2.0 and 7.0 + 5.0, // NaN ignored, previous value returned + 6.0, // range between 1.0 and 7.0 + 9.0 // range between 1.0 and 10.0 + ]; + + for ( i = 0; i < data.length; i++ ) { + v = acc( data[ i ] ); + if ( isnan( expected[ i ] ) ) { + t.equal( isnan( v ), true, 'returns expected value for window '+i ); + } else if ( i === 2 ) { + t.ok( abs(v - expected[i]) < 0.01, 'returns expected value for window '+i ); + } else { + t.equal( v, expected[ i ], 'returns expected value for window '+i ); + } + } + + t.equal( acc(), 9.0, 'returns current range when called without arguments' ); + + t.end(); +});