-
-
Notifications
You must be signed in to change notification settings - Fork 869
feat: add complex/float64/base/assert/is-almost-equal
#7620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kgryte
merged 12 commits into
stdlib-js:develop
from
anandkaranubc:feat/is-almost-equal-c128
Jul 11, 2025
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8dd0b3a
feat: add `complex/float64/base/assert/is-almost-equal-value`
anandkaranubc ff9f557
docs: fix examples
anandkaranubc ee5726f
docs: use conventional verbage
kgryte 6512504
docs: update copy
kgryte c19a0bf
docs: update copy
kgryte 5ca4dde
docs: update note
kgryte 435129a
test: update description
kgryte f3290fa
test: update description
kgryte 0d46457
docs: update examples
kgryte d56924c
fix: rename function
anandkaranubc 36c4892
Merge branch 'develop' into feat/is-almost-equal-c128
anandkaranubc 16c64da
fix: rename function
anandkaranubc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
...ode_modules/@stdlib/complex/float64/base/assert/is-almost-equal-value/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# isAlmostEqualValue | ||
|
||
> Test whether two double-precision complex floating-point numbers are approximately equal within a specified number of ULPs (units in the last place). | ||
|
||
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<!-- Package usage documentation. --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var isAlmostEqualValue = require( '@stdlib/complex/float64/base/assert/is-almost-equal-value' ); | ||
``` | ||
|
||
#### isAlmostEqualValue( z1, z2, maxULP ) | ||
|
||
Tests whether two double-precision complex floating-point numbers are approximately equal within a specified number of ULPs (units in the last place). | ||
|
||
```javascript | ||
var EPS = require( '@stdlib/constants/float64/eps' ); | ||
var Complex128 = require( '@stdlib/complex/float64/ctor' ); | ||
|
||
var z1 = new Complex128( 1.0, 3.0 ); | ||
var z2 = new Complex128( 1.0+EPS, 3.0 ); | ||
|
||
var out = isAlmostEqualValue( z1, z2, 0 ); | ||
// returns false | ||
|
||
out = isAlmostEqualValue( z1, z2, 1 ); | ||
// returns true | ||
``` | ||
|
||
The function returns `false` if either input value has a `NaN` real or imaginary component. | ||
|
||
```javascript | ||
var Complex128 = require( '@stdlib/complex/float64/ctor' ); | ||
|
||
var z1 = new Complex128( NaN, 3.0 ); | ||
var z2 = new Complex128( 1.0, 3.0 ); | ||
|
||
var out = isAlmostEqualValue( z1, z2, 1 ); | ||
// returns false | ||
|
||
out = isAlmostEqualValue( z2, z1, 1 ); | ||
// returns false | ||
|
||
z1 = new Complex128( NaN, NaN ); | ||
z2 = new Complex128( NaN, NaN ); | ||
|
||
out = isAlmostEqualValue( z1, z2, 1 ); | ||
// returns false | ||
``` | ||
|
||
The function does not distinguish between `-0` and `+0`, treating them as equal. | ||
|
||
```javascript | ||
var Complex128 = require( '@stdlib/complex/float64/ctor' ); | ||
|
||
var z1 = new Complex128( 0.0, 0.0 ); | ||
var z2 = new Complex128( -0.0, -0.0 ); | ||
|
||
var out = isAlmostEqualValue( z1, z2, 0 ); | ||
// returns true | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="notes"> | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var EPS = require( '@stdlib/constants/float64/eps' ); | ||
var Complex128 = require( '@stdlib/complex/float64/ctor' ); | ||
var isAlmostEqualValue = require( '@stdlib/complex/float64/base/assert/is-almost-equal-value' ); | ||
|
||
var z1 = new Complex128( 1.0, 3.0+EPS ); | ||
var z2 = new Complex128( 1.0+EPS, 3.0 ); | ||
console.log( isAlmostEqualValue( z1, z2, 1 ) ); | ||
// => true | ||
|
||
z1 = new Complex128( 1.0, 3.0+EPS ); | ||
z2 = new Complex128( 1.0+EPS+EPS, 3.0 ); | ||
console.log( isAlmostEqualValue( z1, z2, 1 ) ); | ||
// => false | ||
|
||
z1 = new Complex128( 0.0, 0.0 ); | ||
z2 = new Complex128( -0.0, 0.0 ); | ||
console.log( isAlmostEqualValue( z1, z2, 0 ) ); | ||
// => true | ||
|
||
z1 = new Complex128( NaN, 0.0 ); | ||
z2 = new Complex128( 1.0, 0.0 ); | ||
console.log( isAlmostEqualValue( z1, z2, 1 ) ); | ||
// => false | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
63 changes: 63 additions & 0 deletions
63
..._modules/@stdlib/complex/float64/base/assert/is-almost-equal-value/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var Complex128 = require( '@stdlib/complex/float64/ctor' ); | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; | ||
var pkg = require( './../package.json' ).name; | ||
var isAlmostEqualValue = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var z1; | ||
var z2; | ||
var v; | ||
var i; | ||
|
||
z1 = [ | ||
new Complex128( randu(), randu() ), | ||
new Complex128( randu(), randu() ) | ||
]; | ||
z2 = [ | ||
new Complex128( randu(), randu() ), | ||
new Complex128( randu(), randu() ), | ||
z1[ 0 ], | ||
z1[ 1 ] | ||
]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = isAlmostEqualValue( z1[ i%z1.length ], z2[ i%z2.length ], 1 ); | ||
if ( typeof v !== 'boolean' ) { | ||
b.fail( 'should return a boolean' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isBoolean( v ) ) { | ||
b.fail( 'should return a boolean' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
41 changes: 41 additions & 0 deletions
41
lib/node_modules/@stdlib/complex/float64/base/assert/is-almost-equal-value/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
{{alias}}( z1, z2, maxULP ) | ||
Tests whether two double-precision complex floating-point numbers are | ||
approximately equal within a specified number of ULPs (units in the last | ||
place). | ||
|
||
The function returns `false` if either input value has a `NaN` real or | ||
imaginary component. | ||
|
||
The function does not distinguish between `-0` and `+0`, treating them as | ||
equal. | ||
|
||
Parameters | ||
---------- | ||
z1: Complex128 | ||
First complex number. | ||
|
||
z2: Complex128 | ||
Second complex number. | ||
|
||
maxULP: number | ||
Maximum allowed ULP difference. | ||
|
||
Returns | ||
------- | ||
out: boolean | ||
Boolean indicating whether two double-precision complex floating-point | ||
numbers are approximately equal within a specified number of ULPs. | ||
|
||
Examples | ||
-------- | ||
> var z1 = new {{alias:@stdlib/complex/float64/ctor}}( 1.0, 3.0 ); | ||
> var z2 = new {{alias:@stdlib/complex/float64/ctor}}( 1.0, 3.0 ); | ||
> var v = {{alias}}( z1, z2, 0 ) | ||
true | ||
> v = {{alias}}( z1, z2, 1 ) | ||
true | ||
|
||
See Also | ||
-------- | ||
|
54 changes: 54 additions & 0 deletions
54
...e_modules/@stdlib/complex/float64/base/assert/is-almost-equal-value/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
import Complex128 = require( '@stdlib/complex/float64/ctor' ); | ||
|
||
/** | ||
* Tests whether two double-precision complex floating-point numbers are approximately equal within a specified number of ULPs (units in the last place). | ||
* | ||
* ## Notes | ||
* | ||
* - The function returns `false` if either input value has a `NaN` real or imaginary component. | ||
* - The function does not distinguish between `-0` and `+0`, treating them as equal. | ||
* | ||
* @param z1 - first complex number | ||
* @param z2 - second complex number | ||
* @param maxULP - maximum allowed ULP difference | ||
* @returns boolean indicating whether two double-precision complex floating-point numbers are approximately equal within a specified number of ULPs | ||
* | ||
* @example | ||
* var EPS = require( '@stdlib/constants/float64/eps' ); | ||
* var Complex128 = require( '@stdlib/complex/float64/ctor' ); | ||
* | ||
* var z1 = new Complex128( 1.0, 3.0 ); | ||
* var z2 = new Complex128( 1.0+EPS, 3.0 ); | ||
* | ||
* var bool = isAlmostEqualValue( z1, z2, 0 ); | ||
* // returns false | ||
* | ||
* bool = isAlmostEqualValue( z1, z2, 1 ); | ||
* // returns true | ||
*/ | ||
declare function isAlmostEqualValue( z1: Complex128, z2: Complex128, maxULP: number ): boolean; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = isAlmostEqualValue; |
68 changes: 68 additions & 0 deletions
68
...node_modules/@stdlib/complex/float64/base/assert/is-almost-equal-value/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Complex128 = require( '@stdlib/complex/float64/ctor' ); | ||
import isAlmostEqualValue = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a boolean... | ||
{ | ||
const z1 = new Complex128( 5.0, 3.0 ); | ||
const z2 = new Complex128( 5.0, 3.0 ); | ||
|
||
isAlmostEqualValue( z1, z2, 1 ); // $ExpectType boolean | ||
} | ||
|
||
// The compiler throws an error if the function is provided a first argument that is not a complex number... | ||
{ | ||
const z2 = new Complex128( 5.0, 3.0 ); | ||
|
||
isAlmostEqualValue( 'abc', z2, 1 ); // $ExpectError | ||
isAlmostEqualValue( 123, z2, 1 ); // $ExpectError | ||
isAlmostEqualValue( true, z2, 1 ); // $ExpectError | ||
isAlmostEqualValue( false, z2, 1 ); // $ExpectError | ||
isAlmostEqualValue( [], z2, 1 ); // $ExpectError | ||
isAlmostEqualValue( {}, z2, 1 ); // $ExpectError | ||
isAlmostEqualValue( ( x: number ): number => x, z2, 1 ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided a second argument that is not a complex number... | ||
{ | ||
const z1 = new Complex128( 5.0, 3.0 ); | ||
|
||
isAlmostEqualValue( z1, 'abc', 1 ); // $ExpectError | ||
isAlmostEqualValue( z1, 123, 1 ); // $ExpectError | ||
isAlmostEqualValue( z1, true, 1 ); // $ExpectError | ||
isAlmostEqualValue( z1, false, 1 ); // $ExpectError | ||
isAlmostEqualValue( z1, [], 1 ); // $ExpectError | ||
isAlmostEqualValue( z1, {}, 1 ); // $ExpectError | ||
isAlmostEqualValue( z1, ( x: number ): number => x, 1 ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided an unsupported number of arguments... | ||
{ | ||
const z1 = new Complex128( 5.0, 3.0 ); | ||
const z2 = new Complex128( 5.0, 3.0 ); | ||
|
||
isAlmostEqualValue(); // $ExpectError | ||
isAlmostEqualValue( z1 ); // $ExpectError | ||
isAlmostEqualValue( z1, z2 ); // $ExpectError | ||
isAlmostEqualValue( z1, z2, 1, 1 ); // $ExpectError | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.