Skip to content

Commit f1fd9f3

Browse files
committed
feat: temp commit
1 parent f9059f4 commit f1fd9f3

File tree

10 files changed

+814
-0
lines changed

10 files changed

+814
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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 bool = isAlmostEqual( NaN, 1.0, 1 );
54+
// returns false
55+
56+
bool = isAlmostEqual( NaN, NaN, 1 );
57+
// returns false
58+
59+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
60+
var z1 = new Complex128( NaN, 3.0 );
61+
var z2 = new Complex128( 1.0, 3.0 );
62+
63+
bool = isAlmostEqual( z1, z2, 1 );
64+
// returns false
65+
66+
z1 = new Complex128( NaN, NaN );
67+
z2 = new Complex128( NaN, NaN );
68+
69+
bool = isAlmostEqual( z1, z2, 1 );
70+
// returns false
71+
```
72+
73+
The function does not distinguish between `-0` and `+0`, treating them as equal.
74+
75+
```javascript
76+
var bool = isAlmostEqual( 0.0, -0.0, 0 );
77+
// returns true
78+
79+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
80+
var z1 = new Complex128( 0.0, 0.0 );
81+
var z2 = new Complex128( -0.0, -0.0 );
82+
83+
bool = isAlmostEqual( z1, z2, 0 );
84+
// returns true
85+
```
86+
87+
</section>
88+
89+
<!-- /.usage -->
90+
91+
<section class="notes">
92+
93+
## Notes
94+
95+
</section>
96+
97+
<!-- /.notes -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var EPS = require( '@stdlib/constants/float64/eps' );
107+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
108+
var isAlmostEqual = require( '@stdlib/assert/is-almost-equal' );
109+
110+
console.log( isAlmostEqual( true, true, 0 ) );
111+
// => true
112+
113+
console.log( isAlmostEqual( true, false, 1 ) );
114+
// => false
115+
116+
console.log( isAlmostEqual( 'beep', 'beep', 1 ) );
117+
// => true
118+
119+
console.log( isAlmostEqual( 1.0, 1.0+EPS, 1 ) );
120+
// => true
121+
122+
console.log( isAlmostEqual( null, null, 0 ) );
123+
// => true
124+
125+
console.log( isAlmostEqual( 0.0, -0.0, 0 ) );
126+
// => true
127+
128+
console.log( isAlmostEqual( NaN, NaN, 1 ) );
129+
// => false
130+
131+
var z1 = new Complex128( 1.0, 3.0+EPS );
132+
var z2 = new Complex128( 1.0+EPS, 3.0 );
133+
console.log( isAlmostEqual( z1, z2, 1 ) );
134+
// => true
135+
136+
console.log( isAlmostEqual( {}, {}, 1 ) );
137+
// => false
138+
139+
console.log( isAlmostEqual( [], [], 1 ) );
140+
// => false
141+
142+
console.log( isAlmostEqual( isAlmostEqual, isAlmostEqual, 0 ) );
143+
// => true
144+
```
145+
146+
</section>
147+
148+
<!-- /.examples -->
149+
150+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
151+
152+
<section class="related">
153+
154+
</section>
155+
156+
<!-- /.related -->
157+
158+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
159+
160+
<section class="links">
161+
162+
[@stdlib/complex]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex
163+
164+
<!-- <related-links> -->
165+
166+
<!-- </related-links> -->
167+
168+
</section>
169+
170+
<!-- /.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: number
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;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 isAlmostEqual = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isAlmostEqual( 3.14, 3.14, 1 ); // $ExpectType boolean
27+
isAlmostEqual( null, null, 0 ); // $ExpectType boolean
28+
isAlmostEqual( 'beep', 'boop', 2 ); // $ExpectType boolean
29+
}
30+
31+
// The compiler throws an error if the function is provided an unsupported number of arguments...
32+
{
33+
isAlmostEqual(); // $ExpectError
34+
isAlmostEqual( 3.14 ); // $ExpectError
35+
isAlmostEqual( 'beep', 'beep' ); // $ExpectError
36+
isAlmostEqual( 3.14, 3.14, 1, 2 ); // $ExpectError
37+
}

0 commit comments

Comments
 (0)