Skip to content

feat: add assert/is-almost-equal-float32array #7682

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
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
117 changes: 117 additions & 0 deletions lib/node_modules/@stdlib/assert/is-almost-equal-float32array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<!--

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

-->

# isAlmostEqualFloat32Array

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

<section class="usage">

## Usage

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

#### isAlmostEqualFloat32Array( v1, v2, maxULP )

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

```javascript
var EPS = require( '@stdlib/constants/float32/eps' );
var Float32Array = require( '@stdlib/array/float32' );

var x = new Float32Array( [ 1.0, 2.0 ] );
var y = new Float32Array( [ 1.0+EPS, 2.0 ] );

var bool = isAlmostEqualFloat32Array( x, y, 0 );
// returns false

bool = isAlmostEqualFloat32Array( x, y, 1 );
// returns true

bool = isAlmostEqualFloat32Array( x, [ 1.0, 2.0 ], 1 );
// returns false
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The function returns `false` if either input value is a `Float32Array` containing `NaN`.
- The function does not distinguish between `-0` and `+0`, treating them as equal.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var Float32Array = require( '@stdlib/array/float32' );
var isAlmostEqualFloat32Array = require( '@stdlib/assert/is-almost-equal-float32array' );

var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
var y = new Float32Array( [ 1.0, 2.0, 3.0 ] );
var out = isAlmostEqualFloat32Array( x, y, 0 );
// returns true

x = new Float32Array( [ -0.0, 0.0, -0.0 ] );
y = new Float32Array( [ 0.0, -0.0, 0.0 ] );
out = isAlmostEqualFloat32Array( x, y, 1 );
// returns true

x = new Float32Array( [ NaN, NaN, NaN ] );
y = new Float32Array( [ NaN, NaN, NaN ] );
out = isAlmostEqualFloat32Array( x, y, 0 );
// returns 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">

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

[@stdlib/assert/is-almost-equal]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-almost-equal

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* @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 pow = require( '@stdlib/math/base/special/pow' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var Float32Array = require( '@stdlib/array/float32' );
var pkg = require( './../package.json' ).name;
var isAlmostEqualFloat32Array = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = new Float32Array( zeroTo( len ) );
var y = new Float32Array( zeroTo( len ) );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var bool;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
bool = isAlmostEqualFloat32Array( x, y, 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();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':len='+len, f );
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

{{alias}}( v1, v2, maxULP )
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).

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

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

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

v2: any
Second input value.

maxULP: integer
Maximum allowed ULP difference.

Returns
-------
bool: boolean
Boolean indicating whether two arguments are approximately equal.

Examples
--------
> var x = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0 ] );
> var y = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0 ] );
> var bool = {{alias}}( x, y, 1 )
true

> x = new {{alias:@stdlib/array/float32}}( [ NaN, NaN, NaN ] );
> y = new {{alias:@stdlib/array/float32}}( [ NaN, NaN, NaN ] );
> bool = {{alias}}( x, y, 1 )
false

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* @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 both Float32Arrays and contain respective elements which are approximately equal within a specified number of ULPs (units in the last place).
*
* ## Notes
*
* - The function returns `false` if either input value is a `Float32Array` containing `NaN`.
* - The function does not distinguish between `-0` and `+0`, treating them as equal.
*
* @param v1 - first input value
* @param v2 - second input value
* @param maxULP - maximum allowed ULP difference
* @returns boolean indicating whether two arguments are approximately equal
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
*
* var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
* var y = new Float32Array( [ 1.0, 2.0, 3.0 ] );
*
* var out = isAlmostEqualFloat32Array( x, y, 0 );
* // returns true
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
*
* var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
* var y = new Float32Array( [ 1.0, 2.0, 4.0 ] );
*
* var out = isAlmostEqualFloat32Array( x, y, 1 );
* // returns false
*/
declare function isAlmostEqualFloat32Array( v1: any, v2: any, maxULP: number ): boolean;

Check warning on line 52 in lib/node_modules/@stdlib/assert/is-almost-equal-float32array/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 52 in lib/node_modules/@stdlib/assert/is-almost-equal-float32array/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //

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


// TESTS //

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

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

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