Skip to content

Commit 95ee9a0

Browse files
committed
feat: add complex/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 de7678a commit 95ee9a0

File tree

10 files changed

+878
-0
lines changed

10 files changed

+878
-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+
# isAlmostSameValue
22+
23+
> Test whether two complex numbers are approximately the same value 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 isAlmostSameValue = require( '@stdlib/complex/base/assert/is-almost-same-value' );
41+
```
42+
43+
#### isAlmostSameValue( z1, z2, maxULP )
44+
45+
Tests whether two complex numbers are approximately the same value 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 = isAlmostSameValue( z1, z2, 0 );
55+
// returns false
56+
57+
out = isAlmostSameValue( z1, z2, 1 );
58+
// returns true
59+
```
60+
61+
In contrast to the strict equality operator `===`, the function distinguishes between `+0` and `-0` and treats `NaNs` as the same value.
62+
63+
```javascript
64+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
65+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
66+
67+
var z1 = new Complex64( NaN, 3.0 );
68+
var z2 = new Complex64( 1.0, 3.0 );
69+
70+
var out = isAlmostSameValue( z1, z2, 1 );
71+
// returns false
72+
73+
out = isAlmostSameValue( z2, z1, 1 );
74+
// returns false
75+
76+
z1 = new Complex64( NaN, NaN );
77+
z2 = new Complex64( NaN, NaN );
78+
79+
out = isAlmostSameValue( z1, z2, 1 );
80+
// returns true
81+
82+
z1 = new Complex128( 0.0, 0.0 );
83+
z2 = new Complex64( -0.0, -0.0 );
84+
85+
out = isAlmostSameValue( z1, z2, 0 );
86+
// returns false
87+
```
88+
89+
</section>
90+
91+
<!-- /.usage -->
92+
93+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
94+
95+
<section class="notes">
96+
97+
## Notes
98+
99+
- The function implements the [SameValue Algorithm][ecma-262-same-value-algorithm] as specified in ECMAScript 5.
100+
101+
</section>
102+
103+
<!-- /.notes -->
104+
105+
<!-- Package usage examples. -->
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
<!-- eslint no-undef: "error" -->
112+
113+
```javascript
114+
var EPS = require( '@stdlib/constants/float64/eps' );
115+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
116+
var isAlmostSameValue = require( '@stdlib/complex/base/assert/is-almost-same-value' );
117+
118+
var z1 = new Complex128( 1.0, 3.0+EPS );
119+
var z2 = new Complex128( 1.0+EPS, 3.0 );
120+
console.log( isAlmostSameValue( z1, z2, 1 ) );
121+
// => true
122+
123+
z1 = new Complex128( 1.0, 3.0+EPS );
124+
z2 = new Complex128( 1.0+EPS+EPS, 3.0 );
125+
console.log( isAlmostSameValue( z1, z2, 1 ) );
126+
// => false
127+
128+
z1 = new Complex128( 0.0, 0.0 );
129+
z2 = new Complex128( -0.0, 0.0 );
130+
console.log( isAlmostSameValue( z1, z2, 0 ) );
131+
// => false
132+
133+
z1 = new Complex128( NaN, 0.0 );
134+
z2 = new Complex128( 1.0, 0.0 );
135+
console.log( isAlmostSameValue( z1, z2, 1 ) );
136+
// => false
137+
```
138+
139+
</section>
140+
141+
<!-- /.examples -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
[ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
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 isAlmostSameValue = 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 = isAlmostSameValue( 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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( z1, z2, maxULP )
3+
Tests whether two complex numbers are approximately the same value within a
4+
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+
z1: ComplexLike
12+
First complex number.
13+
14+
z2: ComplexLike
15+
Second complex number.
16+
17+
maxULP: number
18+
Maximum allowed ULP difference.
19+
20+
Returns
21+
-------
22+
out: boolean
23+
Boolean indicating whether two complex numbers are approximately the
24+
same value within a specified number of ULPs.
25+
26+
Examples
27+
--------
28+
> var re1 = 1.0;
29+
> var im1 = 3.0;
30+
> var re2 = 1.0 + {{alias:@stdlib/constants/float64/eps}};
31+
> var im2 = 3.0;
32+
> var z1 = new {{alias:@stdlib/complex/float64/ctor}}( re1, im1 );
33+
> var z2 = new {{alias:@stdlib/complex/float64/ctor}}( re2, im2 );
34+
> var v = {{alias}}( z1, z2, 0 )
35+
false
36+
> v = {{alias}}( z1, z2, 1 )
37+
true
38+
39+
See Also
40+
--------
41+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 the same value within a specified number of ULPs (units in the last place).
27+
*
28+
* ## Notes
29+
*
30+
* - The function differs from the `===` operator in that the function treats `-0` and `+0` as distinct and `NaNs` as the same.
31+
*
32+
* @param z1 - first complex number
33+
* @param z2 - second complex number
34+
* @param maxULP - maximum allowed ULP difference
35+
* @returns boolean indicating whether two complex numbers are approximately the same value within a specified number of ULPs
36+
*
37+
* @example
38+
* var EPS = require( '@stdlib/constants/float64/eps' );
39+
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
40+
*
41+
* var z1 = new Complex128( 1.0, 3.0 );
42+
* var z2 = new Complex128( 1.0+EPS, 3.0 );
43+
*
44+
* var bool = isAlmostSameValue( z1, z2, 0 );
45+
* // returns false
46+
*
47+
* bool = isAlmostSameValue( z1, z2, 1 );
48+
* // returns true
49+
*/
50+
declare function isAlmostSameValue( z1: ComplexLike, z2: ComplexLike, maxULP: number ): boolean;
51+
52+
53+
// EXPORTS //
54+
55+
export = isAlmostSameValue;

0 commit comments

Comments
 (0)