Skip to content

Commit 11565b7

Browse files
feat: add assert/is-almost-equal-array
PR-URL: #7690 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 27435b3 commit 11565b7

File tree

10 files changed

+699
-0
lines changed

10 files changed

+699
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
# isAlmostEqualArray
22+
23+
> Test if two arguments are both generic arrays and contain respective elements which are [approximately equal][@stdlib/assert/is-almost-equal] within a specified number of ULPs (units in the last place).
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isAlmostEqualArray = require( '@stdlib/assert/is-almost-equal-array' );
31+
```
32+
33+
#### isAlmostEqualArray( v1, v2, maxULP )
34+
35+
Tests if two arguments are both generic arrays and contain respective elements which are [approximately equal][@stdlib/assert/is-almost-equal] 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 = isAlmostEqualArray( x, y, 0 );
44+
// returns false
45+
46+
bool = isAlmostEqualArray( x, y, 1 );
47+
// returns true
48+
49+
bool = isAlmostEqualArray( x, [ -1.0, 2.0 ], 1 );
50+
// returns false
51+
```
52+
53+
</section>
54+
55+
<!-- /.usage -->
56+
57+
<section class="notes">
58+
59+
## Notes
60+
61+
- The function returns `false` if either input value is a generic array containing `NaN`.
62+
- The function does not distinguish between `-0` and `+0`, treating them as equal.
63+
64+
</section>
65+
66+
<!-- /.notes -->
67+
68+
<section class="examples">
69+
70+
## Examples
71+
72+
<!-- eslint no-undef: "error" -->
73+
74+
```javascript
75+
var isAlmostEqualArray = require( '@stdlib/assert/is-almost-equal-array' );
76+
77+
var x = [ 1.0, 2.0, 3.0 ];
78+
var y = [ 1.0, 2.0, 3.0 ];
79+
var out = isAlmostEqualArray( x, y, 0 );
80+
// returns true
81+
82+
x = [ -0.0, 0.0, -0.0 ];
83+
y = [ 0.0, -0.0, 0.0 ];
84+
out = isAlmostEqualArray( x, y, 1 );
85+
// returns true
86+
87+
x = [ NaN, NaN, NaN ];
88+
y = [ NaN, NaN, NaN ];
89+
out = isAlmostEqualArray( x, y, 0 );
90+
// returns false
91+
```
92+
93+
</section>
94+
95+
<!-- /.examples -->
96+
97+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
98+
99+
<section class="related">
100+
101+
</section>
102+
103+
<!-- /.related -->
104+
105+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
106+
107+
<section class="links">
108+
109+
[@stdlib/assert/is-almost-equal]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-almost-equal
110+
111+
</section>
112+
113+
<!-- /.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 isAlmostEqualArray = 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 = isAlmostEqualArray( 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( v1, v2, maxULP )
3+
Tests if two arguments are both generic arrays and contain respective
4+
elements which are approximately equal within a specified number of ULPs
5+
(units in the last place).
6+
7+
The function returns `false` if either input value is a generic array
8+
containing `NaN`.
9+
10+
The function does not distinguish between `-0` and `+0`, treating them as
11+
equal.
12+
13+
Parameters
14+
----------
15+
v1: any
16+
First input value.
17+
18+
v2: any
19+
Second input value.
20+
21+
maxULP: integer
22+
Maximum allowed ULP difference.
23+
24+
Returns
25+
-------
26+
bool: boolean
27+
Boolean indicating whether two arguments are approximately equal.
28+
29+
Examples
30+
--------
31+
> var x = [ 1.0, 2.0, 3.0 ];
32+
> var y = [ 1.0, 2.0, 3.0 ];
33+
> var bool = {{alias}}( x, y, 0 )
34+
true
35+
36+
> x = [ NaN, NaN, NaN ];
37+
> y = [ NaN, NaN, NaN ];
38+
> bool = {{alias}}( x, y, 1 )
39+
false
40+
41+
See Also
42+
--------
43+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 equal within a specified number of ULPs (units in the last place).
23+
*
24+
* ## Notes
25+
*
26+
* - The function returns `false` if either input value is a generic array containing `NaN`.
27+
* - The function does not distinguish between `-0` and `+0`, treating them as equal.
28+
*
29+
* @param v1 - first input value
30+
* @param v2 - second input value
31+
* @param maxULP - maximum allowed ULP difference
32+
* @returns boolean indicating whether two arguments are approximately equal
33+
*
34+
* @example
35+
* var x = [ 1.0, 2.0, 3.0 ];
36+
* var y = [ 1.0, 2.0, 3.0 ];
37+
*
38+
* var out = isAlmostEqualArray( x, y, 0 );
39+
* // returns true
40+
*
41+
* @example
42+
* var x = [ 1.0, 2.0, 3.0 ];
43+
* var y = [ 1.0, 2.0, 4.0 ];
44+
*
45+
* var out = isAlmostEqualArray( x, y, 1 );
46+
* // returns false
47+
*/
48+
declare function isAlmostEqualArray( v1: any, v2: any, maxULP: number ): boolean;
49+
50+
51+
// EXPORTS //
52+
53+
export = isAlmostEqualArray;
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 isAlmostEqualArray = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isAlmostEqualArray( 3.14, 3.14, 1 ); // $ExpectType boolean
27+
isAlmostEqualArray( null, null, 1 ); // $ExpectType boolean
28+
isAlmostEqualArray( '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+
isAlmostEqualArray( 3.14, 3.14, '1' ); // $ExpectError
34+
isAlmostEqualArray( 3.14, 3.14, true ); // $ExpectError
35+
isAlmostEqualArray( 3.14, 3.14, false ); // $ExpectError
36+
isAlmostEqualArray( 3.14, 3.14, null ); // $ExpectError
37+
isAlmostEqualArray( 3.14, 3.14, undefined ); // $ExpectError
38+
isAlmostEqualArray( 3.14, 3.14, [] ); // $ExpectError
39+
isAlmostEqualArray( 3.14, 3.14, {} ); // $ExpectError
40+
isAlmostEqualArray( 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+
isAlmostEqualArray(); // $ExpectError
46+
isAlmostEqualArray( 3.14 ); // $ExpectError
47+
isAlmostEqualArray( 3.14, 3.14 ); // $ExpectError
48+
isAlmostEqualArray( '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 isAlmostEqualArray = require( './../lib' );
22+
23+
var x = [ 1.0, 2.0, 3.0 ];
24+
var y = [ 1.0, 2.0, 3.0 ];
25+
var out = isAlmostEqualArray( 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 = isAlmostEqualArray( x, y, 1 );
32+
console.log( out );
33+
// => true
34+
35+
x = [ NaN, NaN, NaN ];
36+
y = [ NaN, NaN, NaN ];
37+
out = isAlmostEqualArray( x, y, 0 );
38+
console.log( out );
39+
// => false

0 commit comments

Comments
 (0)