Skip to content

Commit f5434d9

Browse files
feat: add assert/is-almost-equal
PR-URL: #7649 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 91644d7 commit f5434d9

File tree

10 files changed

+863
-0
lines changed

10 files changed

+863
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
# isAlmostEqual
22+
23+
> Test if two arguments are approximately 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 isAlmostEqual = require( '@stdlib/assert/is-almost-equal' );
31+
```
32+
33+
#### isAlmostEqual( a, b, maxULP )
34+
35+
Tests if two arguments are approximately 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 bool = isAlmostEqual( 1.0, 1.0+EPS, 1 );
41+
// returns true
42+
43+
bool = isAlmostEqual( '', '', 0 );
44+
// returns true
45+
46+
bool = isAlmostEqual( {}, {}, 1 );
47+
// returns false
48+
```
49+
50+
The function returns `false` if either input value is `NaN` or, in the case of [complex numbers][@stdlib/complex], if either the real or imaginary component is `NaN`.
51+
52+
```javascript
53+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
54+
55+
var bool = isAlmostEqual( NaN, 1.0, 1 );
56+
// returns false
57+
58+
bool = isAlmostEqual( NaN, NaN, 1 );
59+
// returns false
60+
61+
var z1 = new Complex128( NaN, 3.0 );
62+
var z2 = new Complex128( 1.0, 3.0 );
63+
64+
bool = isAlmostEqual( z1, z2, 1 );
65+
// returns false
66+
67+
z1 = new Complex128( NaN, NaN );
68+
z2 = new Complex128( NaN, NaN );
69+
70+
bool = isAlmostEqual( z1, z2, 1 );
71+
// returns false
72+
```
73+
74+
The function does not distinguish between `-0` and `+0`, treating them as equal.
75+
76+
```javascript
77+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
78+
79+
var bool = isAlmostEqual( 0.0, -0.0, 0 );
80+
// returns true
81+
82+
var z1 = new Complex128( 0.0, 0.0 );
83+
var z2 = new Complex128( -0.0, -0.0 );
84+
85+
bool = isAlmostEqual( z1, z2, 0 );
86+
// returns true
87+
```
88+
89+
</section>
90+
91+
<!-- /.usage -->
92+
93+
<section class="notes">
94+
95+
## Notes
96+
97+
</section>
98+
99+
<!-- /.notes -->
100+
101+
<section class="examples">
102+
103+
## Examples
104+
105+
<!-- eslint no-undef: "error" -->
106+
107+
```javascript
108+
var EPS = require( '@stdlib/constants/float64/eps' );
109+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
110+
var isAlmostEqual = require( '@stdlib/assert/is-almost-equal' );
111+
112+
console.log( isAlmostEqual( true, true, 0 ) );
113+
// => true
114+
115+
console.log( isAlmostEqual( true, false, 1 ) );
116+
// => false
117+
118+
console.log( isAlmostEqual( 'beep', 'beep', 1 ) );
119+
// => true
120+
121+
console.log( isAlmostEqual( 1.0, 1.0+EPS, 1 ) );
122+
// => true
123+
124+
console.log( isAlmostEqual( null, null, 0 ) );
125+
// => true
126+
127+
console.log( isAlmostEqual( 0.0, -0.0, 0 ) );
128+
// => true
129+
130+
console.log( isAlmostEqual( NaN, NaN, 1 ) );
131+
// => false
132+
133+
var z1 = new Complex128( 1.0, 3.0+EPS );
134+
var z2 = new Complex128( 1.0+EPS, 3.0 );
135+
console.log( isAlmostEqual( z1, z2, 1 ) );
136+
// => true
137+
138+
console.log( isAlmostEqual( {}, {}, 1 ) );
139+
// => false
140+
141+
console.log( isAlmostEqual( [], [], 1 ) );
142+
// => false
143+
144+
console.log( isAlmostEqual( isAlmostEqual, isAlmostEqual, 0 ) );
145+
// => true
146+
```
147+
148+
</section>
149+
150+
<!-- /.examples -->
151+
152+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
153+
154+
<section class="related">
155+
156+
</section>
157+
158+
<!-- /.related -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
[@stdlib/complex]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex
165+
166+
<!-- <related-links> -->
167+
168+
<!-- </related-links> -->
169+
170+
</section>
171+
172+
<!-- /.links -->
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
/* eslint-disable no-empty-function */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var bench = require( '@stdlib/bench' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var pkg = require( './../package.json' ).name;
28+
var isAlmostEqual = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var values;
35+
var bool;
36+
var v;
37+
var i;
38+
39+
values = [
40+
'',
41+
'5',
42+
0,
43+
5,
44+
NaN,
45+
true,
46+
false,
47+
null,
48+
void 0,
49+
[],
50+
{},
51+
new RegExp( '.*' ), // eslint-disable-line prefer-regex-literals
52+
new Date(),
53+
function noop() {}
54+
];
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = values[ i%values.length ];
59+
bool = isAlmostEqual( v, v, 1 );
60+
if ( typeof bool !== 'boolean' ) {
61+
b.fail( 'should return a boolean' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isBoolean( bool ) ) {
66+
b.fail( 'should return a boolean' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( a, b, maxULP )
3+
Tests if two arguments are approximately equal within a specified number of
4+
ULPs (units in the last place).
5+
6+
The function returns `false` if either input value is `NaN` or in the case
7+
of complex numbers, if either the real or imaginary component is `NaN`.
8+
9+
The function does not distinguish between `-0` and `+0`, treating them as
10+
equal.
11+
12+
Parameters
13+
----------
14+
a: any
15+
First input value.
16+
17+
b: any
18+
Second input value.
19+
20+
maxULP: integer
21+
Maximum allowed ULP difference.
22+
23+
Returns
24+
-------
25+
bool: boolean
26+
Boolean indicating whether two arguments are approximately equal within
27+
a specified number of ULPs.
28+
29+
Examples
30+
--------
31+
> var bool = {{alias}}( true, true, 0 )
32+
true
33+
> bool = {{alias}}( 1+{{alias:@stdlib/constants/float64/eps}}, 1, 1 )
34+
true
35+
> bool = {{alias}}( {}, {}, 1 )
36+
false
37+
> bool = {{alias}}( 0.0, -0.0, 0 )
38+
true
39+
> bool = {{alias}}( NaN, 1.0, 1 )
40+
false
41+
> bool = {{alias}}( NaN, NaN, 0 )
42+
false
43+
44+
See Also
45+
--------
46+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 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 `NaN` or in the case of complex numbers, if either the real or imaginary component is `NaN`.
27+
* - The function does not distinguish between `-0` and `+0`, treating them as equal.
28+
*
29+
* @param a - first input value
30+
* @param b - second input value
31+
* @param maxULP - maximum allowed ULP difference
32+
* @returns boolean indicating whether two arguments are approximately equal within a specified number of ULPs
33+
*
34+
* @example
35+
* var EPS = require( '@stdlib/constants/float64/eps' );
36+
*
37+
* var bool = isAlmostEqual( 1.0, 1.0+EPS, 0 );
38+
* // returns false
39+
*
40+
* bool = isAlmostEqual( 1.0, 1.0+EPS, 1 );
41+
* // returns true
42+
*
43+
* bool = isAlmostEqual( {}, {}, 0 );
44+
* // returns false
45+
*
46+
* bool = isAlmostEqual( -0.0, 0.0, 0 );
47+
* // returns true
48+
*
49+
* bool = isAlmostEqual( NaN, NaN, 0 );
50+
* // returns false
51+
*
52+
* bool = isAlmostEqual( [], [], 1 );
53+
* // returns false
54+
*/
55+
declare function isAlmostEqual( a: any, b: any, maxULP: number ): boolean;
56+
57+
58+
// EXPORTS //
59+
60+
export = isAlmostEqual;

0 commit comments

Comments
 (0)