Skip to content

Commit 95732eb

Browse files
committed
feat: add complex/float32/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 7c681c7 commit 95732eb

File tree

10 files changed

+759
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)