Skip to content

Commit bd517b1

Browse files
committed
feat: add number/float64/base/assert/is-almost-equal-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 bc66be0 commit bd517b1

File tree

10 files changed

+209
-235
lines changed

10 files changed

+209
-235
lines changed

lib/node_modules/@stdlib/number/float64/base/assert/is-almost-equal-value/README.md

Lines changed: 43 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2024 The Stdlib Authors.
5+
Copyright (c) 2025 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -18,48 +18,49 @@ limitations under the License.
1818
1919
-->
2020

21-
# isSameValue
21+
# isAlmostEqualValue
2222

23-
> Test if two double-precision floating-point numbers are the same value.
23+
> Test if two double-precision floating-point numbers are approximately equal within a specified number of ULPs (units in the last place).
2424
2525
<section class="usage">
2626

2727
## Usage
2828

2929
```javascript
30-
var isSameValue = require( '@stdlib/number/float64/base/assert/is-same-value' );
30+
var isAlmostEqualValue = require( '@stdlib/number/float64/base/assert/is-almost-equal-value' );
3131
```
3232

33-
#### isSameValue( a, b )
33+
#### isAlmostEqualValue( a, b )
3434

35-
Tests if two double-precision floating-point numbers `a` and `b` are the same value.
35+
Test if two double-precision floating-point numbers are approximately equal within a specified number of ULPs (units in the last place).
3636

3737
```javascript
38-
var bool = isSameValue( 3.14, 3.14 );
38+
var EPS = require( '@stdlib/constants/float64/eps' );
39+
40+
var bool = isAlmostEqualValue( 1.0, 1.0+EPS, 1 );
3941
// returns true
4042

41-
bool = isSameValue( 5.0, 3.0 );
43+
bool = isAlmostEqualValue( 1.0, 1.0+EPS, 0 );
4244
// returns false
4345
```
4446

45-
In contrast to the strict equality operator `===`, the function distinguishes between `+0` and `-0` and treats `NaNs` as the same value.
46-
47-
<!-- eslint-disable no-compare-neg-zero, use-isnan -->
47+
The function returns `false` if either input value is `NaN`.
4848

4949
```javascript
50-
var bool = ( 0.0 === -0.0 );
51-
// returns true
52-
53-
bool = isSameValue( 0.0, -0.0 );
50+
var bool = isAlmostEqualValue( NaN, 1.0, 1 );
5451
// returns false
5552

56-
bool = isSameValue( -0.0, -0.0 );
57-
// returns true
53+
bool = isAlmostEqualValue( 1.0, NaN, 1 );
54+
// returns false
5855

59-
bool = ( NaN === NaN );
56+
bool = isAlmostEqualValue( NaN, NaN, 1 );
6057
// returns false
58+
```
59+
60+
The function does not distinguish between `-0` and `+0`, treating them as equal.
6161

62-
bool = isSameValue( NaN, NaN );
62+
```javascript
63+
var bool = isAlmostEqualValue( 0.0, -0.0, 0 );
6364
// returns true
6465
```
6566

@@ -69,10 +70,6 @@ bool = isSameValue( NaN, NaN );
6970

7071
<section class="notes">
7172

72-
## Notes
73-
74-
- The function implements the [SameValue Algorithm][ecma-262-same-value-algorithm] as specified in ECMAScript 5.
75-
7673
</section>
7774

7875
<!-- /.notes -->
@@ -84,128 +81,42 @@ bool = isSameValue( NaN, NaN );
8481
<!-- eslint no-undef: "error" -->
8582

8683
```javascript
87-
var isSameValue = require( '@stdlib/number/float64/base/assert/is-same-value' );
88-
89-
var bool = isSameValue( 3.14, 3.14 );
90-
// returns true
91-
92-
bool = isSameValue( 0.0, 0.0 );
93-
// returns true
94-
95-
bool = isSameValue( -0.0, 0.0 );
96-
// returns false
97-
98-
bool = isSameValue( NaN, NaN );
99-
// returns true
100-
```
101-
102-
</section>
103-
104-
<!-- /.examples -->
84+
var EPS = require( '@stdlib/constants/float64/eps' );
85+
var isAlmostEqualValue = require( '@stdlib/number/float64/base/assert/is-almost-equal-value' );
10586

106-
<!-- C interface documentation. -->
87+
var bool = isAlmostEqualValue( 1.0, 1.0+EPS, 1 );
88+
console.log( bool );
89+
// => true
10790

108-
* * *
91+
bool = isAlmostEqualValue( 1.0+EPS, 1.0, 1 );
92+
console.log( bool );
93+
// => true
10994

110-
<section class="c">
95+
bool = isAlmostEqualValue( 1.0, 1.0+EPS+EPS, 1 );
96+
console.log( bool );
97+
// => false
11198

112-
## C APIs
99+
bool = isAlmostEqualValue( 1.0, 1.0+EPS, 0 );
100+
console.log( bool );
101+
// => false
113102

114-
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
103+
bool = isAlmostEqualValue( -0.0, 0.0, 0 );
104+
console.log( bool );
105+
// => true
115106

116-
<section class="intro">
107+
bool = isAlmostEqualValue( 1.0, NaN, 1 );
108+
console.log( bool );
109+
// => false
117110

118-
</section>
119-
120-
<!-- /.intro -->
121-
122-
<!-- C usage documentation. -->
123-
124-
<section class="usage">
125-
126-
### Usage
127-
128-
```c
129-
#include "stdlib/number/float64/base/assert/is_same_value.h"
130-
```
131-
132-
#### stdlib_base_float64_is_same_value( a, b )
133-
134-
Tests if two double-precision floating-point numbers `a` and `b` are the same value.
135-
136-
```c
137-
#include <stdbool.h>
138-
139-
bool v = stdlib_base_float64_is_same_value( 3.14, 3.14 );
140-
// returns true
141-
142-
v = stdlib_base_float64_is_same_value( 0.0, -0.0 );
143-
// returns false
144-
```
145-
146-
The function accepts the following arguments:
147-
148-
- **a**: `[in] double` first input value.
149-
- **b**: `[in] double` second input value.
150-
151-
```c
152-
bool stdlib_base_float64_is_same_value( const double a, const double b );
153-
```
154-
155-
</section>
156-
157-
<!-- /.usage -->
158-
159-
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
160-
161-
<section class="notes">
162-
163-
</section>
164-
165-
<!-- /.notes -->
166-
167-
<!-- C API usage examples. -->
168-
169-
<section class="examples">
170-
171-
### Examples
172-
173-
```c
174-
#include "stdlib/number/float64/base/assert/is_same_value.h"
175-
#include <stdbool.h>
176-
#include <stdio.h>
177-
178-
int main( void ) {
179-
const double a[] = {
180-
5.0,
181-
-2.0,
182-
0.0,
183-
0.0/0.0
184-
};
185-
const double b[] = {
186-
5.0,
187-
2.0,
188-
-0.0,
189-
0.0/0.0
190-
};
191-
192-
bool v;
193-
int i;
194-
for ( i = 0; i < 4; i++ ) {
195-
v = stdlib_base_float64_is_same_value( a[ i ], b[ i ] );
196-
printf( "Same value? %s\n", ( v ) ? "True" : "False" );
197-
}
198-
}
111+
bool = isAlmostEqualValue( NaN, NaN, 1 );
112+
console.log( bool );
113+
// => false
199114
```
200115

201116
</section>
202117

203118
<!-- /.examples -->
204119

205-
</section>
206-
207-
<!-- /.c -->
208-
209120
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
210121

211122
<section class="related">
@@ -218,8 +129,6 @@ int main( void ) {
218129

219130
<section class="links">
220131

221-
[ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12
222-
223132
</section>
224133

225134
<!-- /.links -->

lib/node_modules/@stdlib/number/float64/base/assert/is-almost-equal-value/benchmark/benchmark.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2525
var pkg = require( './../package.json' ).name;
26-
var isSameValue = require( './../lib' );
26+
var isAlmostEqualValue = require( './../lib' );
2727

2828

2929
// MAIN //
@@ -46,7 +46,7 @@ bench( pkg, function benchmark( b ) {
4646
b.tic();
4747
for ( i = 0; i < b.iterations; i++ ) {
4848
v = values[ i%values.length ];
49-
bool = isSameValue( v, v );
49+
bool = isAlmostEqualValue( v, v, 1 );
5050
if ( typeof bool !== 'boolean' ) {
5151
b.fail( 'should return a boolean' );
5252
}

lib/node_modules/@stdlib/number/float64/base/assert/is-almost-equal-value/docs/repl.txt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

2-
{{alias}}( a, b )
3-
Tests if two double-precision floating-point numbers are the same value.
2+
{{alias}}( a, b, maxULP )
3+
Tests if two double-precision floating-point numbers are approximately equal
4+
within a specified number of ULPs (units in the last place).
45

5-
The function differs from the `===` operator in that the function treats
6-
`-0` and `+0` as distinct and `NaNs` as the same.
6+
The function returns `false` if either input value is `NaN`.
7+
8+
The function does not distinguish between `-0` and `+0`, treating them as
9+
equal.
710

811
Parameters
912
----------
@@ -13,20 +16,29 @@
1316
b: number
1417
Second input value.
1518

19+
maxULP: number
20+
Maximum allowed ULP difference.
21+
1622
Returns
1723
-------
1824
bool: boolean
1925
Boolean indicating whether two double-precision floating-point numbers
20-
are the same value.
26+
are approximately equal within a specified number of ULPs.
2127

2228
Examples
2329
--------
24-
> var bool = {{alias}}( -0.0, -0.0 )
30+
> var bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float64/eps}}, 1 )
31+
true
32+
> bool = {{alias}}( 1+{{alias:@stdlib/constants/float64/eps}}, 1, 1 )
2533
true
26-
> bool = {{alias}}( -0.0, 0.0 )
34+
> bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float64/eps}}*2, 1 )
2735
false
28-
> bool = {{alias}}( NaN, NaN )
36+
> bool = {{alias}}( 0.0, -0.0, 0 )
2937
true
38+
> bool = {{alias}}( NaN, 1.0, 1 )
39+
false
40+
> bool = {{alias}}( NaN, NaN, 0 )
41+
false
3042

3143
See Also
3244
--------
Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2024 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -19,35 +19,45 @@
1919
// TypeScript Version: 4.1
2020

2121
/**
22-
* Tests if two double-precision floating-point numbers are the same value.
22+
* Tests if two double-precision floating-point numbers are approximately equal within a specified number of ULPs (units in the last place).
2323
*
2424
* ## Notes
2525
*
26-
* - The function differs from the `===` operator in that the function treats `-0` and `+0` as distinct and `NaNs` as the same.
26+
* - The function returns `false` if either input value is `NaN`.
27+
* - The function does not distinguish between `-0` and `+0`, treating them as equal.
2728
*
2829
* @param a - first input value
2930
* @param b - second input value
30-
* @returns boolean indicating whether two double-precision floating-point numbers are the same value
31+
* @param maxULP - maximum allowed ULP difference
32+
* @returns boolean indicating whether two double-precision floating-point numbers are approximately equal within a specified number of ULPs
3133
*
3234
* @example
33-
* var bool = isSameValue( 3.14, 3.14 );
35+
* var EPS = require( '@stdlib/constants/float64/eps' );
36+
*
37+
* var bool = isAlmostEqualValue( 1.0, 1.0+EPS, 1 );
3438
* // returns true
3539
*
36-
* @example
37-
* var bool = isSameValue( -0.0, -0.0 );
40+
* var bool = isAlmostEqualValue( 1.0+EPS, 1.0, 1 );
3841
* // returns true
3942
*
40-
* @example
41-
* var bool = isSameValue( -0.0, 0.0 );
43+
* bool = isAlmostEqualValue( 1.0, 1.0+EPS+EPS, 1 );
4244
* // returns false
4345
*
44-
* @example
45-
* var bool = isSameValue( NaN, NaN );
46+
* bool = isAlmostEqualValue( 1.0, 1.0+EPS, 0 );
47+
* // returns false
48+
*
49+
* bool = isAlmostEqualValue( 0.0, -0.0, 0 );
4650
* // returns true
51+
*
52+
* bool = isAlmostEqualValue( 1.0, NaN, 1 );
53+
* // returns false
54+
*
55+
* bool = isAlmostEqualValue( NaN, NaN, 1 );
56+
* // returns false
4757
*/
48-
declare function isSameValue( a: number, b: number ): boolean;
58+
declare function isAlmostEqualValue( a: number, b: number, maxULP: number ): boolean;
4959

5060

5161
// EXPORTS //
5262

53-
export = isSameValue;
63+
export = isAlmostEqualValue;

0 commit comments

Comments
 (0)