Skip to content

Commit e38f5ec

Browse files
feat: add number/float64/base/assert/is-almost-equal-value
PR-URL: #7473 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 649c05a commit e38f5ec

File tree

10 files changed

+701
-0
lines changed

10 files changed

+701
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
# isAlmostEqualValue
22+
23+
> Test if two double-precision floating-point numbers 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 isAlmostEqualValue = require( '@stdlib/number/float64/base/assert/is-almost-equal-value' );
31+
```
32+
33+
#### isAlmostEqualValue( a, b, maxULP )
34+
35+
Tests if two double-precision floating-point numbers 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 = isAlmostEqualValue( 1.0, 1.0+EPS, 1 );
41+
// returns true
42+
43+
bool = isAlmostEqualValue( 1.0, 1.0+EPS, 0 );
44+
// returns false
45+
```
46+
47+
The function returns `false` if either input value is `NaN`.
48+
49+
```javascript
50+
var bool = isAlmostEqualValue( NaN, 1.0, 1 );
51+
// returns false
52+
53+
bool = isAlmostEqualValue( 1.0, NaN, 1 );
54+
// returns false
55+
56+
bool = isAlmostEqualValue( NaN, NaN, 1 );
57+
// returns false
58+
```
59+
60+
The function does not distinguish between `-0` and `+0`, treating them as equal.
61+
62+
```javascript
63+
var bool = isAlmostEqualValue( 0.0, -0.0, 0 );
64+
// returns true
65+
```
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<section class="notes">
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var EPS = require( '@stdlib/constants/float64/eps' );
85+
var isAlmostEqualValue = require( '@stdlib/number/float64/base/assert/is-almost-equal-value' );
86+
87+
var bool = isAlmostEqualValue( 1.0, 1.0+EPS, 1 );
88+
console.log( bool );
89+
// => true
90+
91+
bool = isAlmostEqualValue( 1.0+EPS, 1.0, 1 );
92+
console.log( bool );
93+
// => true
94+
95+
bool = isAlmostEqualValue( 1.0, 1.0+EPS+EPS, 1 );
96+
console.log( bool );
97+
// => false
98+
99+
bool = isAlmostEqualValue( 1.0, 1.0+EPS, 0 );
100+
console.log( bool );
101+
// => false
102+
103+
bool = isAlmostEqualValue( -0.0, 0.0, 0 );
104+
console.log( bool );
105+
// => true
106+
107+
bool = isAlmostEqualValue( 1.0, NaN, 1 );
108+
console.log( bool );
109+
// => false
110+
111+
bool = isAlmostEqualValue( NaN, NaN, 1 );
112+
console.log( bool );
113+
// => false
114+
```
115+
116+
</section>
117+
118+
<!-- /.examples -->
119+
120+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
121+
122+
<section class="related">
123+
124+
</section>
125+
126+
<!-- /.related -->
127+
128+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="links">
131+
132+
</section>
133+
134+
<!-- /.links -->
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var isAlmostEqualValue = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var bool;
34+
var v;
35+
var i;
36+
37+
values = [
38+
5.0,
39+
3.14,
40+
NaN,
41+
-0.0,
42+
0.0,
43+
-1.0
44+
];
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
v = values[ i%values.length ];
49+
bool = isAlmostEqualValue( v, v, 1 );
50+
if ( typeof bool !== 'boolean' ) {
51+
b.fail( 'should return a boolean' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isBoolean( bool ) ) {
56+
b.fail( 'should return a boolean' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
{{alias}}( a, b, maxULP )
3+
Tests if two double-precision floating-point numbers are approximately equal
4+
within a specified number of ULPs (units in the last place).
5+
6+
The function returns `false` if either input value is `NaN`.
7+
8+
The function does not distinguish between `-0` and `+0`, treating them as
9+
equal.
10+
11+
Parameters
12+
----------
13+
a: number
14+
First input value.
15+
16+
b: number
17+
Second input value.
18+
19+
maxULP: number
20+
Maximum allowed ULP difference.
21+
22+
Returns
23+
-------
24+
bool: boolean
25+
Boolean indicating whether two double-precision floating-point numbers
26+
are approximately equal within a specified number of ULPs.
27+
28+
Examples
29+
--------
30+
> var bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float64/eps}}, 1 )
31+
true
32+
> bool = {{alias}}( 1+{{alias:@stdlib/constants/float64/eps}}, 1, 1 )
33+
true
34+
> bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float64/eps}}*2, 1 )
35+
false
36+
> bool = {{alias}}( 0.0, -0.0, 0 )
37+
true
38+
> bool = {{alias}}( NaN, 1.0, 1 )
39+
false
40+
> bool = {{alias}}( NaN, NaN, 0 )
41+
false
42+
43+
See Also
44+
--------
45+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 double-precision floating-point numbers 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`.
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 double-precision floating-point numbers are approximately equal within a specified number of ULPs
33+
*
34+
* @example
35+
* var EPS = require( '@stdlib/constants/float64/eps' );
36+
*
37+
* var bool = isAlmostEqualValue( 1.0, 1.0+EPS, 1 );
38+
* // returns true
39+
*
40+
* var bool = isAlmostEqualValue( 1.0+EPS, 1.0, 1 );
41+
* // returns true
42+
*
43+
* bool = isAlmostEqualValue( 1.0, 1.0+EPS+EPS, 1 );
44+
* // returns false
45+
*
46+
* bool = isAlmostEqualValue( 1.0, 1.0+EPS, 0 );
47+
* // returns false
48+
*
49+
* bool = isAlmostEqualValue( 0.0, -0.0, 0 );
50+
* // returns true
51+
*
52+
* bool = isAlmostEqualValue( 1.0, NaN, 1 );
53+
* // returns false
54+
*
55+
* bool = isAlmostEqualValue( NaN, NaN, 1 );
56+
* // returns false
57+
*/
58+
declare function isAlmostEqualValue( a: number, b: number, maxULP: number ): boolean;
59+
60+
61+
// EXPORTS //
62+
63+
export = isAlmostEqualValue;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 isAlmostEqualValue = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isAlmostEqualValue( 3.14, 3.14, 1 ); // $ExpectType boolean
27+
}
28+
29+
// The compiler throws an error if the function is not provided a first argument which is a number...
30+
{
31+
isAlmostEqualValue( '5', 3.14, 1 ); // $ExpectError
32+
isAlmostEqualValue( true, 3.14, 1 ); // $ExpectError
33+
isAlmostEqualValue( false, 3.14, 1 ); // $ExpectError
34+
isAlmostEqualValue( null, 3.14, 1 ); // $ExpectError
35+
isAlmostEqualValue( void 0, 3.14, 1 ); // $ExpectError
36+
isAlmostEqualValue( [], 3.14, 1 ); // $ExpectError
37+
isAlmostEqualValue( {}, 3.14, 1 ); // $ExpectError
38+
isAlmostEqualValue( ( x: number ): number => x, 3.14, 1 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is not provided a second argument which is a number...
42+
{
43+
isAlmostEqualValue( 3.14, '5', 1 ); // $ExpectError
44+
isAlmostEqualValue( 3.14, true, 1 ); // $ExpectError
45+
isAlmostEqualValue( 3.14, false, 1 ); // $ExpectError
46+
isAlmostEqualValue( 3.14, null, 1 ); // $ExpectError
47+
isAlmostEqualValue( 3.14, void 0, 1 ); // $ExpectError
48+
isAlmostEqualValue( 3.14, [], 1 ); // $ExpectError
49+
isAlmostEqualValue( 3.14, {}, 1 ); // $ExpectError
50+
isAlmostEqualValue( 3.14, ( x: number ): number => x, 1 ); // $ExpectError
51+
}
52+
53+
// The compiler throws an error if the function is not provided a third argument which is a number...
54+
{
55+
isAlmostEqualValue( 3.14, 3.14, '5' ); // $ExpectError
56+
isAlmostEqualValue( 3.14, 3.14, true ); // $ExpectError
57+
isAlmostEqualValue( 3.14, 3.14, false ); // $ExpectError
58+
isAlmostEqualValue( 3.14, 3.14, null ); // $ExpectError
59+
isAlmostEqualValue( 3.14, 3.14, void 0 ); // $ExpectError
60+
isAlmostEqualValue( 3.14, 3.14, [] ); // $ExpectError
61+
isAlmostEqualValue( 3.14, 3.14, {} ); // $ExpectError
62+
isAlmostEqualValue( 3.14, 3.14, ( x: number ): number => x ); // $ExpectError
63+
}
64+
65+
// The compiler throws an error if the function is provided an unsupported number of arguments...
66+
{
67+
isAlmostEqualValue(); // $ExpectError
68+
isAlmostEqualValue( 3.14 ); // $ExpectError
69+
isAlmostEqualValue( 3.14, 3.14 ); // $ExpectError
70+
isAlmostEqualValue( 3.14, 3.14, 1, 2 ); // $ExpectError
71+
}

0 commit comments

Comments
 (0)