Skip to content

feat: add number/float32/base/ulp-difference #7451

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
146 changes: 146 additions & 0 deletions lib/node_modules/@stdlib/number/float32/base/ulp-difference/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<!--

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

-->

# ulpdiff

> Compute the number of representable [single-precision][single-precision] floating-point values that separate two [single-precision][single-precision] floating-point numbers along the real number line.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var ulpdiff = require( '@stdlib/number/float32/base/ulp-difference' );
```

#### ulpdiff( x, y )

Computes the number of representable [single-precision][single-precision] floating-point values that separate two [single-precision][single-precision] floating-point numbers along the real number line.

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

var d = ulpdiff( 1.0, 1.0+EPS );
// returns 1.0

d = ulpdiff( 1.0+EPS, 1.0 );
// returns 1.0

d = ulpdiff( 1.0, 1.0+EPS+EPS );
// returns 2.0

d = ulpdiff( 1.0, NaN );
// returns NaN

d = ulpdiff( NaN, 1.0 );
// returns NaN

d = ulpdiff( NaN, NaN );
// returns NaN
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- Adjacent [single-precision][single-precision] floating-point numbers differ by `1` [ulp][ulp] (unit in the last place).
- Signed zeros differ only in the sign bit but are considered numerically equal, and thus their ULP difference is `0`.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

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

```javascript
var EPS = require( '@stdlib/constants/float32/eps' );
var SMALLEST_SUBNORMAL = require( '@stdlib/constants/float32/smallest-subnormal' );
var ulpdiff = require( '@stdlib/number/float32/base/ulp-difference' );

var d = ulpdiff( 1.0, 1.0+EPS );
console.log( d );
// => 1.0

d = ulpdiff( 5.8364e-31, 5.8367e-31 );
console.log( d );
// => 638.0

d = ulpdiff( 0.0, SMALLEST_SUBNORMAL );
console.log( d );
// => 1.0

d = ulpdiff( 0.0, -0.0 );
console.log( d );
// => 0.0

d = ulpdiff( SMALLEST_SUBNORMAL, -SMALLEST_SUBNORMAL );
console.log( d );
// => 2.0
```

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

[single-precision]: https://en.wikipedia.org/wiki/Single-precision_floating-point_format

[ulp]: https://en.wikipedia.org/wiki/Unit_in_the_last_place

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

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

</section>

<!-- /.links -->
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.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var ulpdiff = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var opts;
var x;
var y;
var i;

opts = {
'dtype': 'float32'
};
x = uniform( 100, -500.0, 500.0, opts );
y = uniform( 100, -500.0, 500.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = ulpdiff( x[ i%x.length ], y[ i%y.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

{{alias}}( x, y )
Computes the number of representable single-precision floating-point values
that separate two single-precision floating-point numbers along the real
number line.

Adjacent single-precision floating-point numbers differ by 1 ulp (unit in
the last place).

Signed zeros differ only in the sign bit but are considered numerically
equal, and thus their ULP difference is 0.

If provided `NaN` as any argument, the function returns `NaN`.

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

y: number
Second input value.

Returns
-------
z: number
Result.

Examples
--------
> var v = {{alias}}( 1.0, 1.0+{{alias:@stdlib/constants/float32/eps}} )
1.0
> v = {{alias}}( 1.0+{{alias:@stdlib/constants/float32/eps}}, 1.0 )
1.0
> v = {{alias}}( 1.0, 1.0+{{alias:@stdlib/constants/float32/eps}}*2.0 )
2.0
> v = {{alias}}( 1.0, NaN )
NaN
> v = {{alias}}( NaN, 1.0 )
NaN
> v = {{alias}}( NaN, NaN )
NaN

See Also
--------

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

/**
* Computes the number of representable single-precision floating-point values that separate two single-precision floating-point numbers along the real number line.
*
* ## Notes
*
* - Adjacent single-precision floating-point numbers differ by 1 ulp (unit in the last place).
* - Signed zeros differ only in the sign bit but are considered numerically equal, and thus their ULP difference is 0.
*
* @param x - first input value
* @param y - second input value
* @returns result
*
* @example
* var EPS = require( '@stdlib/constants/float32/eps' );
*
* var d = ulpdiff( 1.0, 1.0+EPS );
* // returns 1.0
*
* d = ulpdiff( 1.0+EPS, 1.0 );
* // returns 1.0
*
* d = ulpdiff( 1.0, 1.0+EPS+EPS );
* // returns 2.0
*
* d = ulpdiff( 1.0, NaN );
* // returns NaN
*
* d = ulpdiff( NaN, 1.0 );
* // returns NaN
*
* d = ulpdiff( NaN, NaN );
* // returns NaN
*/
declare function ulpdiff( x: number, y: number ): number;


// EXPORTS //

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


// TESTS //

// The function returns a number...
{
ulpdiff( 8.0, 8.0 ); // $ExpectType number
}

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

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

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