Skip to content

feat: add number/float64/base/assert/is-almost-equal-value #7473

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

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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,134 @@
<!--

@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 if two double-precision floating-point numbers are approximately equal within a specified number of ULPs (units in the last place).

<section class="usage">

## Usage

```javascript
var isAlmostEqualValue = require( '@stdlib/number/float64/base/assert/is-almost-equal-value' );
```

#### isAlmostEqualValue( a, b )

Test if two double-precision 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 bool = isAlmostEqualValue( 1.0, 1.0+EPS, 1 );
// returns true

bool = isAlmostEqualValue( 1.0, 1.0+EPS, 0 );
// returns false
```

The function returns `false` if either input value is `NaN`.

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

bool = isAlmostEqualValue( 1.0, NaN, 1 );
// returns false

bool = isAlmostEqualValue( NaN, NaN, 1 );
// returns false
```

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

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

</section>

<!-- /.usage -->

<section class="notes">

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var EPS = require( '@stdlib/constants/float64/eps' );
var isAlmostEqualValue = require( '@stdlib/number/float64/base/assert/is-almost-equal-value' );

var bool = isAlmostEqualValue( 1.0, 1.0+EPS, 1 );
console.log( bool );
// => true

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

bool = isAlmostEqualValue( 1.0, 1.0+EPS+EPS, 1 );
console.log( bool );
// => false

bool = isAlmostEqualValue( 1.0, 1.0+EPS, 0 );
console.log( bool );
// => false

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

bool = isAlmostEqualValue( 1.0, NaN, 1 );
console.log( bool );
// => false

bool = isAlmostEqualValue( NaN, NaN, 1 );
console.log( bool );
// => 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,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.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
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 values;
var bool;
var v;
var i;

values = [
5.0,
3.14,
NaN,
-0.0,
0.0,
-1.0
];

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

{{alias}}( a, b, maxULP )
Tests if two double-precision 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 is `NaN`.

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

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

b: number
Second input value.

maxULP: number
Maximum allowed ULP difference.

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

Examples
--------
> var bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float64/eps}}, 1 )
true
> bool = {{alias}}( 1+{{alias:@stdlib/constants/float64/eps}}, 1, 1 )
true
> bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float64/eps}}*2, 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,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.
*/

// TypeScript Version: 4.1

/**
* Tests if two double-precision 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 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 double-precision floating-point numbers are approximately equal within a specified number of ULPs
*
* @example
* var EPS = require( '@stdlib/constants/float64/eps' );
*
* var bool = isAlmostEqualValue( 1.0, 1.0+EPS, 1 );
* // returns true
*
* var bool = isAlmostEqualValue( 1.0+EPS, 1.0, 1 );
* // returns true
*
* bool = isAlmostEqualValue( 1.0, 1.0+EPS+EPS, 1 );
* // returns false
*
* bool = isAlmostEqualValue( 1.0, 1.0+EPS, 0 );
* // returns false
*
* bool = isAlmostEqualValue( 0.0, -0.0, 0 );
* // returns true
*
* bool = isAlmostEqualValue( 1.0, NaN, 1 );
* // returns false
*
* bool = isAlmostEqualValue( NaN, NaN, 1 );
* // returns false
*/
declare function isAlmostEqualValue( a: number, b: number, maxULP: number ): boolean;


// EXPORTS //

export = isAlmostEqualValue;
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* @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 isAlmostEqualValue = require( './index' );


// TESTS //

// The function returns a boolean...
{
isAlmostEqualValue( 3.14, 3.14, 1 ); // $ExpectType boolean
}

// The compiler throws an error if the function is not provided a first argument which is a number...
{
isAlmostEqualValue( '5', 3.14, 1 ); // $ExpectError
isAlmostEqualValue( true, 3.14, 1 ); // $ExpectError
isAlmostEqualValue( false, 3.14, 1 ); // $ExpectError
isAlmostEqualValue( null, 3.14, 1 ); // $ExpectError
isAlmostEqualValue( void 0, 3.14, 1 ); // $ExpectError
isAlmostEqualValue( [], 3.14, 1 ); // $ExpectError
isAlmostEqualValue( {}, 3.14, 1 ); // $ExpectError
isAlmostEqualValue( ( x: number ): number => x, 3.14, 1 ); // $ExpectError
}

// The compiler throws an error if the function is not provided a second argument which is a number...
{
isAlmostEqualValue( 3.14, '5', 1 ); // $ExpectError
isAlmostEqualValue( 3.14, true, 1 ); // $ExpectError
isAlmostEqualValue( 3.14, false, 1 ); // $ExpectError
isAlmostEqualValue( 3.14, null, 1 ); // $ExpectError
isAlmostEqualValue( 3.14, void 0, 1 ); // $ExpectError
isAlmostEqualValue( 3.14, [], 1 ); // $ExpectError
isAlmostEqualValue( 3.14, {}, 1 ); // $ExpectError
isAlmostEqualValue( 3.14, ( x: number ): number => x, 1 ); // $ExpectError
}

// The compiler throws an error if the function is not provided a third argument which is a number...
{
isAlmostEqualValue( 3.14, 3.14, '5' ); // $ExpectError
isAlmostEqualValue( 3.14, 3.14, true ); // $ExpectError
isAlmostEqualValue( 3.14, 3.14, false ); // $ExpectError
isAlmostEqualValue( 3.14, 3.14, null ); // $ExpectError
isAlmostEqualValue( 3.14, 3.14, void 0 ); // $ExpectError
isAlmostEqualValue( 3.14, 3.14, [] ); // $ExpectError
isAlmostEqualValue( 3.14, 3.14, {} ); // $ExpectError
isAlmostEqualValue( 3.14, 3.14, ( x: number ): number => x ); // $ExpectError
}

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