Skip to content

Commit 42bc50f

Browse files
feat: add complex/float64/base/assert/is-almost-equal
PR-URL: #7620 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent db50901 commit 42bc50f

File tree

10 files changed

+744
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)