Skip to content

Commit d0857d0

Browse files
committed
feat: add complex/base/assert/is-almost-equal
--- 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 f83da1e commit d0857d0

File tree

10 files changed

+881
-0
lines changed

10 files changed

+881
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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 complex 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/base/assert/is-almost-equal' );
41+
```
42+
43+
#### isAlmostEqual( z1, z2, maxULP )
44+
45+
Tests whether two complex 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 Complex64 = require( '@stdlib/complex/float32/ctor' );
65+
66+
var z1 = new Complex64( NaN, 3.0 );
67+
var z2 = new Complex64( 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 Complex64( NaN, NaN );
76+
z2 = new Complex64( 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+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
87+
88+
var z1 = new Complex128( 0.0, 0.0 );
89+
var z2 = new Complex64( -0.0, -0.0 );
90+
91+
var out = isAlmostEqual( z1, z2, 0 );
92+
// returns true
93+
```
94+
95+
</section>
96+
97+
<!-- /.usage -->
98+
99+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
100+
101+
<section class="notes">
102+
103+
</section>
104+
105+
<!-- /.notes -->
106+
107+
<!-- Package usage examples. -->
108+
109+
<section class="examples">
110+
111+
## Examples
112+
113+
<!-- eslint no-undef: "error" -->
114+
115+
```javascript
116+
var EPS = require( '@stdlib/constants/float64/eps' );
117+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
118+
var isAlmostEqual = require( '@stdlib/complex/base/assert/is-almost-equal' );
119+
120+
var z1 = new Complex128( 1.0, 3.0+EPS );
121+
var z2 = new Complex128( 1.0+EPS, 3.0 );
122+
console.log( isAlmostEqual( z1, z2, 1 ) );
123+
// => true
124+
125+
z1 = new Complex128( 1.0, 3.0+EPS );
126+
z2 = new Complex128( 1.0+EPS+EPS, 3.0 );
127+
console.log( isAlmostEqual( z1, z2, 1 ) );
128+
// => false
129+
130+
z1 = new Complex128( 0.0, 0.0 );
131+
z2 = new Complex128( -0.0, 0.0 );
132+
console.log( isAlmostEqual( z1, z2, 0 ) );
133+
// => true
134+
135+
z1 = new Complex128( NaN, 0.0 );
136+
z2 = new Complex128( 1.0, 0.0 );
137+
console.log( isAlmostEqual( z1, z2, 1 ) );
138+
// => false
139+
```
140+
141+
</section>
142+
143+
<!-- /.examples -->
144+
145+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
146+
147+
<section class="related">
148+
149+
</section>
150+
151+
<!-- /.related -->
152+
153+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="links">
156+
157+
</section>
158+
159+
<!-- /.links -->
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
25+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
26+
var randu = require( '@stdlib/random/base/randu' );
27+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
28+
var pkg = require( './../package.json' ).name;
29+
var isAlmostEqual = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var z1;
36+
var z2;
37+
var v;
38+
var i;
39+
40+
z1 = [
41+
new Complex128( randu(), randu() ),
42+
new Complex128( randu(), randu() ),
43+
new Complex64( randu(), randu() ),
44+
new Complex64( randu(), randu() )
45+
];
46+
z2 = [
47+
new Complex128( randu(), randu() ),
48+
new Complex128( randu(), randu() ),
49+
new Complex64( randu(), randu() ),
50+
new Complex64( randu(), randu() ),
51+
z1[ 0 ],
52+
z1[ 1 ]
53+
];
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
v = isAlmostEqual( z1[ i%z1.length ], z2[ i%z2.length ], 1 );
58+
if ( typeof v !== 'boolean' ) {
59+
b.fail( 'should return a boolean' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isBoolean( v ) ) {
64+
b.fail( 'should return a boolean' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});
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 complex numbers are approximately equal within a specified
4+
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: ComplexLike
16+
First complex number.
17+
18+
z2: ComplexLike
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 complex numbers are approximately equal
28+
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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
/// <reference types="@stdlib/types"/>
22+
23+
import { ComplexLike } from '@stdlib/types/complex';
24+
25+
/**
26+
* Tests whether two complex numbers are approximately equal within a specified number of ULPs (units in the last place).
27+
*
28+
* ## Notes
29+
*
30+
* - The function returns `false` if either input value has a `NaN` real or imaginary component.
31+
* - The function does not distinguish between `-0` and `+0`, treating them as equal.
32+
*
33+
* @param z1 - first complex number
34+
* @param z2 - second complex number
35+
* @param maxULP - maximum allowed ULP difference
36+
* @returns boolean indicating whether two complex numbers are approximately equal within a specified number of ULPs
37+
*
38+
* @example
39+
* var EPS = require( '@stdlib/constants/float64/eps' );
40+
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
41+
*
42+
* var z1 = new Complex128( 1.0, 3.0 );
43+
* var z2 = new Complex128( 1.0+EPS, 3.0 );
44+
*
45+
* var bool = isAlmostEqual( z1, z2, 0 );
46+
* // returns false
47+
*
48+
* bool = isAlmostEqual( z1, z2, 1 );
49+
* // returns true
50+
*/
51+
declare function isAlmostEqual( z1: ComplexLike, z2: ComplexLike, maxULP: number ): boolean;
52+
53+
54+
// EXPORTS //
55+
56+
export = isAlmostEqual;

0 commit comments

Comments
 (0)