Skip to content

Commit 0146356

Browse files
committed
feat: add assert/is-almost-same-value-array
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent f507f85 commit 0146356

File tree

10 files changed

+697
-0
lines changed

10 files changed

+697
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isAlmostSameValueArray
22+
23+
> Test if two arguments are both generic arrays and contain respective elements which are [approximately the same value][@stdlib/assert/is-almost-same-value] within a specified number of ULPs (units in the last place).
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isAlmostSameValueArray = require( '@stdlib/assert/is-almost-same-value-array' );
31+
```
32+
33+
#### isAlmostSameValueArray( v1, v2, maxULP )
34+
35+
Tests if two arguments are both generic arrays and contain respective elements which are [approximately the same value][@stdlib/assert/is-almost-same-value] within a specified number of ULPs (units in the last place).
36+
37+
```javascript
38+
var EPS = require( '@stdlib/constants/float64/eps' );
39+
40+
var x = [ 1.0, 2.0 ];
41+
var y = [ 1.0+EPS, 2.0 ];
42+
43+
var bool = isAlmostSameValueArray( x, y, 0 );
44+
// returns false
45+
46+
bool = isAlmostSameValueArray( x, y, 1 );
47+
// returns true
48+
49+
bool = isAlmostSameValueArray( x, [ -1.0, 2.0 ], 1 );
50+
// returns false
51+
```
52+
53+
</section>
54+
55+
<!-- /.usage -->
56+
57+
<section class="notes">
58+
59+
</section>
60+
61+
<!-- /.notes -->
62+
63+
<section class="examples">
64+
65+
## Examples
66+
67+
<!-- eslint no-undef: "error" -->
68+
69+
```javascript
70+
var isAlmostSameValueArray = require( '@stdlib/assert/is-almost-same-value-array' );
71+
72+
var x = [ 1.0, 2.0, 3.0 ];
73+
var y = [ 1.0, 2.0, 3.0 ];
74+
var out = isAlmostSameValueArray( x, y, 0 );
75+
// returns true
76+
77+
x = [ -0.0, 0.0, -0.0 ];
78+
y = [ 0.0, -0.0, 0.0 ];
79+
out = isAlmostSameValueArray( x, y, 0 );
80+
// returns false
81+
82+
x = [ NaN, NaN, NaN ];
83+
y = [ NaN, NaN, NaN ];
84+
out = isAlmostSameValueArray( x, y, 0 );
85+
// returns true
86+
```
87+
88+
</section>
89+
90+
<!-- /.examples -->
91+
92+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
93+
94+
<section class="related">
95+
96+
</section>
97+
98+
<!-- /.related -->
99+
100+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
101+
102+
<section class="links">
103+
104+
[@stdlib/assert/is-almost-same-value]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-almost-same-value
105+
106+
</section>
107+
108+
<!-- /.links -->
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var pkg = require( './../package.json' ).name;
28+
var isAlmostSameValueArray = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = zeroTo( len );
42+
var y = zeroTo( len );
43+
return benchmark;
44+
45+
/**
46+
* Benchmark function.
47+
*
48+
* @private
49+
* @param {Benchmark} b - benchmark instance
50+
*/
51+
function benchmark( b ) {
52+
var bool;
53+
var i;
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
bool = isAlmostSameValueArray( x, y, 1 );
58+
if ( typeof bool !== 'boolean' ) {
59+
b.fail( 'should return a boolean' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isBoolean( bool ) ) {
64+
b.fail( 'should return a boolean' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
}
69+
}
70+
71+
72+
// MAIN //
73+
74+
/**
75+
* Main execution sequence.
76+
*
77+
* @private
78+
*/
79+
function main() {
80+
var len;
81+
var min;
82+
var max;
83+
var f;
84+
var i;
85+
86+
min = 1; // 10^min
87+
max = 6; // 10^max
88+
89+
for ( i = min; i <= max; i++ ) {
90+
len = pow( 10, i );
91+
f = createBenchmark( len );
92+
bench( pkg+':len='+len, f );
93+
}
94+
}
95+
96+
main();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( v1, v2, maxULP )
3+
Tests if two arguments are both generic arrays and contain respective
4+
elements which are approximately the same value within a specified number of
5+
ULPs (units in the last place).
6+
7+
The function differs from the `===` operator in that the function treats
8+
`-0` and `+0` as distinct and `NaNs` as the same.
9+
10+
Parameters
11+
----------
12+
v1: any
13+
First input value.
14+
15+
v2: any
16+
Second input value.
17+
18+
maxULP: integer
19+
Maximum allowed ULP difference.
20+
21+
Returns
22+
-------
23+
bool: boolean
24+
Boolean indicating whether two arguments are approximately the same
25+
value.
26+
27+
Examples
28+
--------
29+
> var x = [ 1.0, 2.0, 3.0 ];
30+
> var y = [ 1.0, 2.0, 3.0 ];
31+
> var bool = {{alias}}( x, y, 0 )
32+
true
33+
34+
> x = [ NaN, NaN, NaN ];
35+
> y = [ NaN, NaN, NaN ];
36+
> bool = {{alias}}( x, y, 1 )
37+
true
38+
39+
See Also
40+
--------
41+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Tests if two arguments are both generic arrays and contain respective elements which are approximately the same value within a specified number of ULPs (units in the last place).
23+
*
24+
* ## Notes
25+
*
26+
* - The function differs from the `===` operator in that the function treats `-0` and `+0` as distinct and `NaNs` as the same.
27+
*
28+
* @param v1 - first input value
29+
* @param v2 - second input value
30+
* @param maxULP - maximum allowed ULP difference
31+
* @returns boolean indicating whether two arguments are approximately the same value
32+
*
33+
* @example
34+
* var x = [ 1.0, 2.0, 3.0 ];
35+
* var y = [ 1.0, 2.0, 3.0 ];
36+
*
37+
* var out = isAlmostSameValueArray( x, y, 0 );
38+
* // returns true
39+
*
40+
* @example
41+
* var x = [ 1.0, 2.0, 3.0 ];
42+
* var y = [ 1.0, 2.0, 4.0 ];
43+
*
44+
* var out = isAlmostSameValueArray( x, y, 1 );
45+
* // returns false
46+
*/
47+
declare function isAlmostSameValueArray( v1: any, v2: any, maxULP: number ): boolean;
48+
49+
50+
// EXPORTS //
51+
52+
export = isAlmostSameValueArray;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import isAlmostSameValueArray = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isAlmostSameValueArray( 3.14, 3.14, 1 ); // $ExpectType boolean
27+
isAlmostSameValueArray( null, null, 1 ); // $ExpectType boolean
28+
isAlmostSameValueArray( 'beep', 'boop', 1 ); // $ExpectType boolean
29+
}
30+
31+
// The compiler throws an error if the function is provided a third argument which is not a number...
32+
{
33+
isAlmostSameValueArray( 3.14, 3.14, '1' ); // $ExpectError
34+
isAlmostSameValueArray( 3.14, 3.14, true ); // $ExpectError
35+
isAlmostSameValueArray( 3.14, 3.14, false ); // $ExpectError
36+
isAlmostSameValueArray( 3.14, 3.14, null ); // $ExpectError
37+
isAlmostSameValueArray( 3.14, 3.14, undefined ); // $ExpectError
38+
isAlmostSameValueArray( 3.14, 3.14, [] ); // $ExpectError
39+
isAlmostSameValueArray( 3.14, 3.14, {} ); // $ExpectError
40+
isAlmostSameValueArray( 3.14, 3.14, ( x: number ): number => x ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided an unsupported number of arguments...
44+
{
45+
isAlmostSameValueArray(); // $ExpectError
46+
isAlmostSameValueArray( 3.14 ); // $ExpectError
47+
isAlmostSameValueArray( 3.14, 3.14 ); // $ExpectError
48+
isAlmostSameValueArray( 'beep', 'beep', 2, {} ); // $ExpectError
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var isAlmostSameValueArray = require( './../lib' );
22+
23+
var x = [ 1.0, 2.0, 3.0 ];
24+
var y = [ 1.0, 2.0, 3.0 ];
25+
var out = isAlmostSameValueArray( x, y, 0 );
26+
console.log( out );
27+
// => true
28+
29+
x = [ -0.0, 0.0, -0.0 ];
30+
y = [ 0.0, -0.0, 0.0 ];
31+
out = isAlmostSameValueArray( x, y, 0 );
32+
console.log( out );
33+
// => false
34+
35+
x = [ NaN, NaN, NaN ];
36+
y = [ NaN, NaN, NaN ];
37+
out = isAlmostSameValueArray( x, y, 0 );
38+
console.log( out );
39+
// => true

0 commit comments

Comments
 (0)