Skip to content

feat: add complex/float32/base/assert/is-almost-equal #7622

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
merged 2 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

-->

# isAlmostEqualf

> Test whether two single-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 isAlmostEqualf = require( '@stdlib/complex/float32/base/assert/is-almost-equal' );
```

#### isAlmostEqualf( z1, z2, maxULP )

Tests whether two single-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/float32/eps' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );

var z1 = new Complex64( 1.0, 3.0 );
var z2 = new Complex64( 1.0+EPS, 3.0 );

var out = isAlmostEqualf( z1, z2, 0 );
// returns false

out = isAlmostEqualf( z1, z2, 1 );
// returns true
```

The function returns `false` if either input value has a `NaN` real or imaginary component.

```javascript
var Complex64 = require( '@stdlib/complex/float32/ctor' );

var z1 = new Complex64( NaN, 3.0 );
var z2 = new Complex64( 1.0, 3.0 );

var out = isAlmostEqualf( z1, z2, 1 );
// returns false

out = isAlmostEqualf( z2, z1, 1 );
// returns false

z1 = new Complex64( NaN, NaN );
z2 = new Complex64( NaN, NaN );

out = isAlmostEqualf( z1, z2, 1 );
// returns false
```

The function does not distinguish between `-0` and `+0`, treating them as equal.

```javascript
var Complex64 = require( '@stdlib/complex/float32/ctor' );

var z1 = new Complex64( 0.0, 0.0 );
var z2 = new Complex64( -0.0, -0.0 );

var out = isAlmostEqualf( 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/float32/eps' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var isAlmostEqualf = require( '@stdlib/complex/float32/base/assert/is-almost-equal' );

var z1 = new Complex64( 1.0, 3.0+EPS );
var z2 = new Complex64( 1.0+EPS, 3.0 );
console.log( isAlmostEqualf( z1, z2, 1 ) );
// => true

z1 = new Complex64( 1.0, 3.0+EPS );
z2 = new Complex64( 1.0+EPS+EPS, 3.0 );
console.log( isAlmostEqualf( z1, z2, 1 ) );
// => false

z1 = new Complex64( 0.0, 0.0 );
z2 = new Complex64( -0.0, 0.0 );
console.log( isAlmostEqualf( z1, z2, 0 ) );
// => true

z1 = new Complex64( NaN, 0.0 );
z2 = new Complex64( 1.0, 0.0 );
console.log( isAlmostEqualf( 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 -->
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 Complex64 = require( '@stdlib/complex/float32/ctor' );
var randu = require( '@stdlib/random/base/randu' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var isAlmostEqualf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var z1;
var z2;
var v;
var i;

z1 = [
new Complex64( randu(), randu() ),
new Complex64( randu(), randu() )
];
z2 = [
new Complex64( randu(), randu() ),
new Complex64( randu(), randu() ),
z1[ 0 ],
z1[ 1 ]
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = isAlmostEqualf( 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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

{{alias}}( z1, z2, maxULP )
Tests whether two single-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: Complex64
First complex number.

z2: Complex64
Second complex number.

maxULP: number
Maximum allowed ULP difference.

Returns
-------
out: boolean
Boolean indicating whether two single-precision complex floating-point
numbers are approximately equal within a specified number of ULPs.

Examples
--------
> var re1 = 1.0;
> var im1 = 3.0;
> var re2 = 1.0 + {{alias:@stdlib/constants/float32/eps}};
> var im2 = 3.0;
> var z1 = new {{alias:@stdlib/complex/float32/ctor}}( re1, im1 );
> var z2 = new {{alias:@stdlib/complex/float32/ctor}}( re2, im2 );
> var v = {{alias}}( z1, z2, 0 )
false
> v = {{alias}}( z1, z2, 1 )
true

See Also
--------

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 Complex64 = require( '@stdlib/complex/float32/ctor' );

/**
* Tests whether two single-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 single-precision complex floating-point numbers are approximately equal within a specified number of ULPs
*
* @example
* var EPS = require( '@stdlib/constants/float32/eps' );
* var Complex64 = require( '@stdlib/complex/float32/ctor' );
*
* var z1 = new Complex64( 1.0, 3.0 );
* var z2 = new Complex64( 1.0+EPS, 3.0 );
*
* var bool = isAlmostEqualf( z1, z2, 0 );
* // returns false
*
* bool = isAlmostEqualf( z1, z2, 1 );
* // returns true
*/
declare function isAlmostEqualf( z1: Complex64, z2: Complex64, maxULP: number ): boolean;


// EXPORTS //

export = isAlmostEqualf;
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 Complex64 = require( '@stdlib/complex/float32/ctor' );
import isAlmostEqualf = require( './index' );


// TESTS //

// The function returns a boolean...
{
const z1 = new Complex64( 5.0, 3.0 );
const z2 = new Complex64( 5.0, 3.0 );

isAlmostEqualf( 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 Complex64( 5.0, 3.0 );

isAlmostEqualf( 'abc', z2, 1 ); // $ExpectError
isAlmostEqualf( 123, z2, 1 ); // $ExpectError
isAlmostEqualf( true, z2, 1 ); // $ExpectError
isAlmostEqualf( false, z2, 1 ); // $ExpectError
isAlmostEqualf( [], z2, 1 ); // $ExpectError
isAlmostEqualf( {}, z2, 1 ); // $ExpectError
isAlmostEqualf( ( 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 Complex64( 5.0, 3.0 );

isAlmostEqualf( z1, 'abc', 1 ); // $ExpectError
isAlmostEqualf( z1, 123, 1 ); // $ExpectError
isAlmostEqualf( z1, true, 1 ); // $ExpectError
isAlmostEqualf( z1, false, 1 ); // $ExpectError
isAlmostEqualf( z1, [], 1 ); // $ExpectError
isAlmostEqualf( z1, {}, 1 ); // $ExpectError
isAlmostEqualf( 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 Complex64( 5.0, 3.0 );
const z2 = new Complex64( 5.0, 3.0 );

isAlmostEqualf(); // $ExpectError
isAlmostEqualf( z1 ); // $ExpectError
isAlmostEqualf( z1, z2 ); // $ExpectError
isAlmostEqualf( z1, z2, 1, 1 ); // $ExpectError
}
Loading