Skip to content

Commit cb49481

Browse files
committed
feat: add number/float64/base/assert/is-almost-same-value
--- 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 01e0c7f commit cb49481

10 files changed

Lines changed: 718 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
# isAlmostSameValue
22+
23+
> Test if two double-precision floating-point numbers are approximately the 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 isAlmostSameValue = require( '@stdlib/number/float64/base/assert/is-almost-same-value' );
31+
```
32+
33+
#### isAlmostSameValue( a, b, maxULP )
34+
35+
Tests if two double-precision floating-point numbers are approximately the 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 bool = isAlmostSameValue( 1.0, 1.0+EPS, 1 );
41+
// returns true
42+
43+
bool = isAlmostSameValue( 1.0, 1.0+EPS, 0 );
44+
// returns false
45+
```
46+
47+
In contrast to the strict equality operator `===`, the function distinguishes between `+0` and `-0` and treats `NaNs` as the same value.
48+
49+
```javascript
50+
var bool = isAlmostSameValue( NaN, 1.0, 1 );
51+
// returns false
52+
53+
bool = isAlmostSameValue( 1.0, NaN, 1 );
54+
// returns false
55+
56+
bool = isAlmostSameValue( NaN, NaN, 1 );
57+
// returns true
58+
59+
bool = isAlmostSameValue( 0.0, -0.0, 0 );
60+
// returns false
61+
```
62+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<section class="notes">
68+
69+
## Notes
70+
71+
- The function implements the [SameValue Algorithm][ecma-262-same-value-algorithm] as specified in ECMAScript 5.
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 isAlmostSameValue = require( '@stdlib/number/float64/base/assert/is-almost-same-value' );
86+
87+
var bool = isAlmostSameValue( 1.0, 1.0+EPS, 1 );
88+
console.log( bool );
89+
// => true
90+
91+
bool = isAlmostSameValue( 1.0+EPS, 1.0, 1 );
92+
console.log( bool );
93+
// => true
94+
95+
bool = isAlmostSameValue( 1.0, 1.0+EPS+EPS, 1 );
96+
console.log( bool );
97+
// => false
98+
99+
bool = isAlmostSameValue( 1.0, 1.0+EPS, 0 );
100+
console.log( bool );
101+
// => false
102+
103+
bool = isAlmostSameValue( -0.0, 0.0, 0 );
104+
console.log( bool );
105+
// => false
106+
107+
bool = isAlmostSameValue( 1.0, NaN, 1 );
108+
console.log( bool );
109+
// => false
110+
111+
bool = isAlmostSameValue( NaN, NaN, 1 );
112+
console.log( bool );
113+
// => true
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+
[ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
133+
134+
</section>
135+
136+
<!-- /.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 isAlmostSameValue = 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 = isAlmostSameValue( 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( a, b, maxULP )
3+
Tests if two double-precision floating-point numbers are approximately the
4+
same value within a specified number of ULPs (units in the last place).
5+
6+
The function differs from the `===` operator in that the function treats
7+
`-0` and `+0` as distinct and `NaNs` as the same.
8+
9+
Parameters
10+
----------
11+
a: number
12+
First input value.
13+
14+
b: number
15+
Second input value.
16+
17+
maxULP: number
18+
Maximum allowed ULP difference.
19+
20+
Returns
21+
-------
22+
bool: boolean
23+
Boolean indicating whether two double-precision floating-point numbers
24+
are approximately the same value within a specified number of ULPs.
25+
26+
Examples
27+
--------
28+
> var bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float64/eps}}, 1 )
29+
true
30+
> bool = {{alias}}( 1+{{alias:@stdlib/constants/float64/eps}}, 1, 1 )
31+
true
32+
> bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float64/eps}}*2, 1 )
33+
false
34+
> bool = {{alias}}( 0.0, -0.0, 0 )
35+
false
36+
> bool = {{alias}}( NaN, 1.0, 1 )
37+
false
38+
> bool = {{alias}}( NaN, NaN, 0 )
39+
true
40+
41+
See Also
42+
--------
43+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 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 a - first input value
29+
* @param b - second input value
30+
* @param maxULP - maximum allowed ULP difference
31+
* @returns boolean indicating whether two double-precision floating-point numbers are approximately the same value within a specified number of ULPs
32+
*
33+
* @example
34+
* var EPS = require( '@stdlib/constants/float64/eps' );
35+
*
36+
* var bool = isAlmostSameValue( 1.0, 1.0+EPS, 1 );
37+
* // returns true
38+
*
39+
* bool = isAlmostSameValue( 1.0+EPS, 1.0, 1 );
40+
* // returns true
41+
*
42+
* bool = isAlmostSameValue( 1.0, 1.0+EPS+EPS, 1 );
43+
* // returns false
44+
*
45+
* bool = isAlmostSameValue( 1.0, 1.0+EPS, 0 );
46+
* // returns false
47+
*
48+
* bool = isAlmostSameValue( 0.0, -0.0, 0 );
49+
* // returns false
50+
*
51+
* bool = isAlmostSameValue( 1.0, NaN, 1 );
52+
* // returns false
53+
*
54+
* bool = isAlmostSameValue( NaN, NaN, 1 );
55+
* // returns true
56+
*/
57+
declare function isAlmostSameValue( a: number, b: number, maxULP: number ): boolean;
58+
59+
60+
// EXPORTS //
61+
62+
export = isAlmostSameValue;
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 isAlmostSameValue = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isAlmostSameValue( 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+
isAlmostSameValue( '5', 3.14, 1 ); // $ExpectError
32+
isAlmostSameValue( true, 3.14, 1 ); // $ExpectError
33+
isAlmostSameValue( false, 3.14, 1 ); // $ExpectError
34+
isAlmostSameValue( null, 3.14, 1 ); // $ExpectError
35+
isAlmostSameValue( void 0, 3.14, 1 ); // $ExpectError
36+
isAlmostSameValue( [], 3.14, 1 ); // $ExpectError
37+
isAlmostSameValue( {}, 3.14, 1 ); // $ExpectError
38+
isAlmostSameValue( ( 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+
isAlmostSameValue( 3.14, '5', 1 ); // $ExpectError
44+
isAlmostSameValue( 3.14, true, 1 ); // $ExpectError
45+
isAlmostSameValue( 3.14, false, 1 ); // $ExpectError
46+
isAlmostSameValue( 3.14, null, 1 ); // $ExpectError
47+
isAlmostSameValue( 3.14, void 0, 1 ); // $ExpectError
48+
isAlmostSameValue( 3.14, [], 1 ); // $ExpectError
49+
isAlmostSameValue( 3.14, {}, 1 ); // $ExpectError
50+
isAlmostSameValue( 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+
isAlmostSameValue( 3.14, 3.14, '5' ); // $ExpectError
56+
isAlmostSameValue( 3.14, 3.14, true ); // $ExpectError
57+
isAlmostSameValue( 3.14, 3.14, false ); // $ExpectError
58+
isAlmostSameValue( 3.14, 3.14, null ); // $ExpectError
59+
isAlmostSameValue( 3.14, 3.14, void 0 ); // $ExpectError
60+
isAlmostSameValue( 3.14, 3.14, [] ); // $ExpectError
61+
isAlmostSameValue( 3.14, 3.14, {} ); // $ExpectError
62+
isAlmostSameValue( 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+
isAlmostSameValue(); // $ExpectError
68+
isAlmostSameValue( 3.14 ); // $ExpectError
69+
isAlmostSameValue( 3.14, 3.14 ); // $ExpectError
70+
isAlmostSameValue( 3.14, 3.14, 1, 2 ); // $ExpectError
71+
}

0 commit comments

Comments
 (0)