Skip to content

Commit be12cf9

Browse files
feat: add assert/is-almost-equal-float32array
PR-URL: #7682 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 48efc05 commit be12cf9

File tree

10 files changed

+715
-0
lines changed

10 files changed

+715
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isAlmostEqualFloat32Array
22+
23+
> Test if two arguments are both [Float32Arrays][@stdlib/array/float32] and contain respective elements which are [approximately equal][@stdlib/assert/is-almost-equal] within a specified number of ULPs (units in the last place).
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isAlmostEqualFloat32Array = require( '@stdlib/assert/is-almost-equal-float32array' );
31+
```
32+
33+
#### isAlmostEqualFloat32Array( v1, v2, maxULP )
34+
35+
Tests if two arguments are both [Float32Arrays][@stdlib/array/float32] and contain respective elements which are [approximately equal][@stdlib/assert/is-almost-equal] within a specified number of ULPs (units in the last place).
36+
37+
```javascript
38+
var EPS = require( '@stdlib/constants/float32/eps' );
39+
var Float32Array = require( '@stdlib/array/float32' );
40+
41+
var x = new Float32Array( [ 1.0, 2.0 ] );
42+
var y = new Float32Array( [ 1.0+EPS, 2.0 ] );
43+
44+
var bool = isAlmostEqualFloat32Array( x, y, 0 );
45+
// returns false
46+
47+
bool = isAlmostEqualFloat32Array( x, y, 1 );
48+
// returns true
49+
50+
bool = isAlmostEqualFloat32Array( x, [ 1.0, 2.0 ], 1 );
51+
// returns false
52+
```
53+
54+
</section>
55+
56+
<!-- /.usage -->
57+
58+
<section class="notes">
59+
60+
## Notes
61+
62+
- The function returns `false` if either input value is a `Float32Array` containing `NaN`.
63+
- The function does not distinguish between `-0` and `+0`, treating them as equal.
64+
65+
</section>
66+
67+
<!-- /.notes -->
68+
69+
<section class="examples">
70+
71+
## Examples
72+
73+
<!-- eslint no-undef: "error" -->
74+
75+
```javascript
76+
var Float32Array = require( '@stdlib/array/float32' );
77+
var isAlmostEqualFloat32Array = require( '@stdlib/assert/is-almost-equal-float32array' );
78+
79+
var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
80+
var y = new Float32Array( [ 1.0, 2.0, 3.0 ] );
81+
var out = isAlmostEqualFloat32Array( x, y, 0 );
82+
// returns true
83+
84+
x = new Float32Array( [ -0.0, 0.0, -0.0 ] );
85+
y = new Float32Array( [ 0.0, -0.0, 0.0 ] );
86+
out = isAlmostEqualFloat32Array( x, y, 1 );
87+
// returns true
88+
89+
x = new Float32Array( [ NaN, NaN, NaN ] );
90+
y = new Float32Array( [ NaN, NaN, NaN ] );
91+
out = isAlmostEqualFloat32Array( x, y, 0 );
92+
// returns false
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
100+
101+
<section class="related">
102+
103+
</section>
104+
105+
<!-- /.related -->
106+
107+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="links">
110+
111+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
112+
113+
[@stdlib/assert/is-almost-equal]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-almost-equal
114+
115+
</section>
116+
117+
<!-- /.links -->
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var Float32Array = require( '@stdlib/array/float32' );
28+
var pkg = require( './../package.json' ).name;
29+
var isAlmostEqualFloat32Array = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x = new Float32Array( zeroTo( len ) );
43+
var y = new Float32Array( zeroTo( len ) );
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var bool;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
bool = isAlmostEqualFloat32Array( x, y, 1 );
59+
if ( typeof bool !== 'boolean' ) {
60+
b.fail( 'should return a boolean' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isBoolean( bool ) ) {
65+
b.fail( 'should return a boolean' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( pkg+':len='+len, f );
94+
}
95+
}
96+
97+
main();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( v1, v2, maxULP )
3+
Tests if two arguments are both Float32Arrays and contain respective
4+
elements which are approximately equal within a specified number of ULPs
5+
(units in the last place).
6+
7+
The function returns `false` if either input value is a `Float32Array`
8+
containing `NaN`.
9+
10+
The function does not distinguish between `-0` and `+0`, treating them as
11+
equal.
12+
13+
Parameters
14+
----------
15+
v1: any
16+
First input value.
17+
18+
v2: any
19+
Second input value.
20+
21+
maxULP: integer
22+
Maximum allowed ULP difference.
23+
24+
Returns
25+
-------
26+
bool: boolean
27+
Boolean indicating whether two arguments are approximately equal.
28+
29+
Examples
30+
--------
31+
> var x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0 ] );
32+
> var y = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0 ] );
33+
> var bool = {{alias}}( x, y, 1 )
34+
true
35+
36+
> x = new {{alias:@stdlib/array/float32}}( [ NaN, NaN, NaN ] );
37+
> y = new {{alias:@stdlib/array/float32}}( [ NaN, NaN, NaN ] );
38+
> bool = {{alias}}( x, y, 1 )
39+
false
40+
41+
See Also
42+
--------
43+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Tests if two arguments are both Float32Arrays and contain respective elements which are approximately equal within a specified number of ULPs (units in the last place).
23+
*
24+
* ## Notes
25+
*
26+
* - The function returns `false` if either input value is a `Float32Array` containing `NaN`.
27+
* - The function does not distinguish between `-0` and `+0`, treating them as equal.
28+
*
29+
* @param v1 - first input value
30+
* @param v2 - second input value
31+
* @param maxULP - maximum allowed ULP difference
32+
* @returns boolean indicating whether two arguments are approximately equal
33+
*
34+
* @example
35+
* var Float32Array = require( '@stdlib/array/float32' );
36+
*
37+
* var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
38+
* var y = new Float32Array( [ 1.0, 2.0, 3.0 ] );
39+
*
40+
* var out = isAlmostEqualFloat32Array( x, y, 0 );
41+
* // returns true
42+
*
43+
* @example
44+
* var Float32Array = require( '@stdlib/array/float32' );
45+
*
46+
* var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
47+
* var y = new Float32Array( [ 1.0, 2.0, 4.0 ] );
48+
*
49+
* var out = isAlmostEqualFloat32Array( x, y, 1 );
50+
* // returns false
51+
*/
52+
declare function isAlmostEqualFloat32Array( v1: any, v2: any, maxULP: number ): boolean;
53+
54+
55+
// EXPORTS //
56+
57+
export = isAlmostEqualFloat32Array;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import isAlmostEqualFloat32Array = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isAlmostEqualFloat32Array( 3.14, 3.14, 1 ); // $ExpectType boolean
27+
isAlmostEqualFloat32Array( null, null, 1 ); // $ExpectType boolean
28+
isAlmostEqualFloat32Array( 'beep', 'boop', 1 ); // $ExpectType boolean
29+
}
30+
31+
// The compiler throws an error if the function is provided a third argument which is not a number...
32+
{
33+
isAlmostEqualFloat32Array( 3.14, 3.14, '1' ); // $ExpectError
34+
isAlmostEqualFloat32Array( 3.14, 3.14, true ); // $ExpectError
35+
isAlmostEqualFloat32Array( 3.14, 3.14, false ); // $ExpectError
36+
isAlmostEqualFloat32Array( 3.14, 3.14, null ); // $ExpectError
37+
isAlmostEqualFloat32Array( 3.14, 3.14, undefined ); // $ExpectError
38+
isAlmostEqualFloat32Array( 3.14, 3.14, [] ); // $ExpectError
39+
isAlmostEqualFloat32Array( 3.14, 3.14, {} ); // $ExpectError
40+
isAlmostEqualFloat32Array( 3.14, 3.14, ( x: number ): number => x ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided an unsupported number of arguments...
44+
{
45+
isAlmostEqualFloat32Array(); // $ExpectError
46+
isAlmostEqualFloat32Array( 3.14 ); // $ExpectError
47+
isAlmostEqualFloat32Array( 3.14, 3.14 ); // $ExpectError
48+
isAlmostEqualFloat32Array( 'beep', 'beep', 2, {} ); // $ExpectError
49+
}

0 commit comments

Comments
 (0)