Skip to content

feat: add assert/is-almost-equal #7649

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 11 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
170 changes: 170 additions & 0 deletions lib/node_modules/@stdlib/assert/is-almost-equal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<!--

@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.

-->

# isAlmostEqual

> Test if two arguments are approximately equal within a specified number of ULPs (units in the last place).

<section class="usage">

## Usage

```javascript
var isAlmostEqual = require( '@stdlib/assert/is-almost-equal' );
```

#### isAlmostEqual( a, b, maxULP )

Tests if two arguments are approximately equal within a specified number of ULPs (units in the last place).

```javascript
var EPS = require( '@stdlib/constants/float64/eps' );

var bool = isAlmostEqual( 1.0, 1.0+EPS, 1 );
// returns true

bool = isAlmostEqual( '', '', 0 );
// returns true

bool = isAlmostEqual( {}, {}, 1 );
// returns false
```

The function returns `false` if either input value is `NaN` or, in the case of [complex numbers][@stdlib/complex], if either the real or imaginary component is `NaN`.

```javascript
var bool = isAlmostEqual( NaN, 1.0, 1 );
// returns false

bool = isAlmostEqual( NaN, NaN, 1 );
// returns false

var Complex128 = require( '@stdlib/complex/float64/ctor' );
var z1 = new Complex128( NaN, 3.0 );
var z2 = new Complex128( 1.0, 3.0 );

bool = isAlmostEqual( z1, z2, 1 );
// returns false

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

bool = isAlmostEqual( z1, z2, 1 );
// returns false
```

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

```javascript
var bool = isAlmostEqual( 0.0, -0.0, 0 );
// returns true

var Complex128 = require( '@stdlib/complex/float64/ctor' );
var z1 = new Complex128( 0.0, 0.0 );
var z2 = new Complex128( -0.0, -0.0 );

bool = isAlmostEqual( z1, z2, 0 );
// returns true
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var EPS = require( '@stdlib/constants/float64/eps' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var isAlmostEqual = require( '@stdlib/assert/is-almost-equal' );

console.log( isAlmostEqual( true, true, 0 ) );
// => true

console.log( isAlmostEqual( true, false, 1 ) );
// => false

console.log( isAlmostEqual( 'beep', 'beep', 1 ) );
// => true

console.log( isAlmostEqual( 1.0, 1.0+EPS, 1 ) );
// => true

console.log( isAlmostEqual( null, null, 0 ) );
// => true

console.log( isAlmostEqual( 0.0, -0.0, 0 ) );
// => true

console.log( isAlmostEqual( NaN, NaN, 1 ) );
// => false

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

console.log( isAlmostEqual( {}, {}, 1 ) );
// => false

console.log( isAlmostEqual( [], [], 1 ) );
// => false

console.log( isAlmostEqual( isAlmostEqual, isAlmostEqual, 0 ) );
// => true
```

</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">

[@stdlib/complex]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @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.
*/

/* eslint-disable no-empty-function */

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var isAlmostEqual = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var bool;
var v;
var i;

values = [
'',
'5',
0,
5,
NaN,
true,
false,
null,
void 0,
[],
{},
new RegExp( '.*' ), // eslint-disable-line prefer-regex-literals
new Date(),
function noop() {}
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = values[ i%values.length ];
bool = isAlmostEqual( v, v, 1 );
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( bool ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});
46 changes: 46 additions & 0 deletions lib/node_modules/@stdlib/assert/is-almost-equal/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

{{alias}}( a, b, maxULP )
Tests if two arguments are approximately equal within a specified number of
ULPs (units in the last place).

The function returns `false` if either input value is `NaN` or in the case
of complex numbers, if either the real or imaginary component is `NaN`.

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

Parameters
----------
a: any
First input value.

b: any
Second input value.

maxULP: number
Maximum allowed ULP difference.

Returns
-------
bool: boolean
Boolean indicating whether two arguments are approximately equal within
a specified number of ULPs.

Examples
--------
> var bool = {{alias}}( true, true, 0 )
true
> bool = {{alias}}( 1+{{alias:@stdlib/constants/float64/eps}}, 1, 1 )
true
> bool = {{alias}}( {}, {}, 1 )
false
> bool = {{alias}}( 0.0, -0.0, 0 )
true
> bool = {{alias}}( NaN, 1.0, 1 )
false
> bool = {{alias}}( NaN, NaN, 0 )
false

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @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

/**
* Tests if two arguments are approximately equal within a specified number of ULPs (units in the last place).
*
* ## Notes
*
* - The function returns `false` if either input value is `NaN` or in the case of complex numbers, if either the real or imaginary component is `NaN`.
* - The function does not distinguish between `-0` and `+0`, treating them as equal.
*
* @param a - first input value
* @param b - second input value
* @param maxULP - maximum allowed ULP difference
* @returns boolean indicating whether two arguments are approximately equal within a specified number of ULPs
*
* @example
* var EPS = require( '@stdlib/constants/float64/eps' );
*
* var bool = isAlmostEqual( 1.0, 1.0+EPS, 0 );
* // returns false
*
* bool = isAlmostEqual( 1.0, 1.0+EPS, 1 );
* // returns true
*
* bool = isAlmostEqual( {}, {}, 0 );
* // returns false
*
* bool = isAlmostEqual( -0.0, 0.0, 0 );
* // returns true
*
* bool = isAlmostEqual( NaN, NaN, 0 );
* // returns false
*
* bool = isAlmostEqual( [], [], 1 );
* // returns false
*/
declare function isAlmostEqual( a: any, b: any, maxULP: number ): boolean;

Check warning on line 55 in lib/node_modules/@stdlib/assert/is-almost-equal/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 55 in lib/node_modules/@stdlib/assert/is-almost-equal/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //

export = isAlmostEqual;
37 changes: 37 additions & 0 deletions lib/node_modules/@stdlib/assert/is-almost-equal/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* @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 isAlmostEqual = require( './index' );


// TESTS //

// The function returns a boolean...
{
isAlmostEqual( 3.14, 3.14, 1 ); // $ExpectType boolean
isAlmostEqual( null, null, 0 ); // $ExpectType boolean
isAlmostEqual( 'beep', 'boop', 2 ); // $ExpectType boolean
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
isAlmostEqual(); // $ExpectError
isAlmostEqual( 3.14 ); // $ExpectError
isAlmostEqual( 'beep', 'beep' ); // $ExpectError
isAlmostEqual( 3.14, 3.14, 1, 2 ); // $ExpectError
}
Loading
Loading