Skip to content

Commit 649c05a

Browse files
feat: add number/float32/base/ulp-difference
PR-URL: #7451 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 98f079c commit 649c05a

File tree

10 files changed

+748
-0
lines changed

10 files changed

+748
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
# ulpdiff
22+
23+
> 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.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var ulpdiff = require( '@stdlib/number/float32/base/ulp-difference' );
41+
```
42+
43+
#### ulpdiff( x, y )
44+
45+
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.
46+
47+
```javascript
48+
var EPS = require( '@stdlib/constants/float32/eps' );
49+
50+
var d = ulpdiff( 1.0, 1.0+EPS );
51+
// returns 1.0
52+
53+
d = ulpdiff( 1.0+EPS, 1.0 );
54+
// returns 1.0
55+
56+
d = ulpdiff( 1.0, 1.0+EPS+EPS );
57+
// returns 2.0
58+
59+
d = ulpdiff( 1.0, NaN );
60+
// returns NaN
61+
62+
d = ulpdiff( NaN, 1.0 );
63+
// returns NaN
64+
65+
d = ulpdiff( NaN, NaN );
66+
// returns NaN
67+
```
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
74+
75+
<section class="notes">
76+
77+
## Notes
78+
79+
- Adjacent [single-precision][single-precision] floating-point numbers differ by `1` [ulp][ulp] (unit in the last place).
80+
- Signed zeros differ only in the sign bit but are considered numerically equal, and thus their ULP difference is `0`.
81+
82+
</section>
83+
84+
<!-- /.notes -->
85+
86+
<!-- Package usage examples. -->
87+
88+
<section class="examples">
89+
90+
## Examples
91+
92+
<!-- eslint no-undef: "error" -->
93+
94+
```javascript
95+
var EPS = require( '@stdlib/constants/float32/eps' );
96+
var SMALLEST_SUBNORMAL = require( '@stdlib/constants/float32/smallest-subnormal' );
97+
var ulpdiff = require( '@stdlib/number/float32/base/ulp-difference' );
98+
99+
var d = ulpdiff( 1.0, 1.0+EPS );
100+
console.log( d );
101+
// => 1.0
102+
103+
d = ulpdiff( 5.8364e-31, 5.8367e-31 );
104+
console.log( d );
105+
// => 638.0
106+
107+
d = ulpdiff( 0.0, SMALLEST_SUBNORMAL );
108+
console.log( d );
109+
// => 1.0
110+
111+
d = ulpdiff( 0.0, -0.0 );
112+
console.log( d );
113+
// => 0.0
114+
115+
d = ulpdiff( SMALLEST_SUBNORMAL, -SMALLEST_SUBNORMAL );
116+
console.log( d );
117+
// => 2.0
118+
```
119+
120+
</section>
121+
122+
<!-- /.examples -->
123+
124+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
125+
126+
<section class="related">
127+
128+
</section>
129+
130+
<!-- /.related -->
131+
132+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
133+
134+
<section class="links">
135+
136+
[single-precision]: https://en.wikipedia.org/wiki/Single-precision_floating-point_format
137+
138+
[ulp]: https://en.wikipedia.org/wiki/Unit_in_the_last_place
139+
140+
<!-- <related-links> -->
141+
142+
<!-- </related-links> -->
143+
144+
</section>
145+
146+
<!-- /.links -->
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var ulpdiff = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var opts;
34+
var x;
35+
var y;
36+
var i;
37+
38+
opts = {
39+
'dtype': 'float32'
40+
};
41+
x = uniform( 100, -500.0, 500.0, opts );
42+
y = uniform( 100, -500.0, 500.0, opts );
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
y = ulpdiff( x[ i%x.length ], y[ i%y.length ] );
47+
if ( isnan( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
}
51+
b.toc();
52+
if ( isnan( y ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
{{alias}}( x, y )
3+
Computes the number of representable single-precision floating-point values
4+
that separate two single-precision floating-point numbers along the real
5+
number line.
6+
7+
Adjacent single-precision floating-point numbers differ by 1 ulp (unit in
8+
the last place).
9+
10+
Signed zeros differ only in the sign bit but are considered numerically
11+
equal, and thus their ULP difference is 0.
12+
13+
If provided `NaN` as any argument, the function returns `NaN`.
14+
15+
Parameters
16+
----------
17+
x: number
18+
First input value.
19+
20+
y: number
21+
Second input value.
22+
23+
Returns
24+
-------
25+
z: number
26+
Result.
27+
28+
Examples
29+
--------
30+
> var v = {{alias}}( 1.0, 1.0+{{alias:@stdlib/constants/float32/eps}} )
31+
1.0
32+
> v = {{alias}}( 1.0+{{alias:@stdlib/constants/float32/eps}}, 1.0 )
33+
1.0
34+
> v = {{alias}}( 1.0, 1.0+{{alias:@stdlib/constants/float32/eps}}*2.0 )
35+
2.0
36+
> v = {{alias}}( 1.0, NaN )
37+
NaN
38+
> v = {{alias}}( NaN, 1.0 )
39+
NaN
40+
> v = {{alias}}( NaN, NaN )
41+
NaN
42+
43+
See Also
44+
--------
45+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
* Computes the number of representable single-precision floating-point values that separate two single-precision floating-point numbers along the real number line.
23+
*
24+
* ## Notes
25+
*
26+
* - Adjacent single-precision floating-point numbers differ by 1 ulp (unit in the last place).
27+
* - Signed zeros differ only in the sign bit but are considered numerically equal, and thus their ULP difference is 0.
28+
*
29+
* @param x - first input value
30+
* @param y - second input value
31+
* @returns result
32+
*
33+
* @example
34+
* var EPS = require( '@stdlib/constants/float32/eps' );
35+
*
36+
* var d = ulpdiff( 1.0, 1.0+EPS );
37+
* // returns 1.0
38+
*
39+
* d = ulpdiff( 1.0+EPS, 1.0 );
40+
* // returns 1.0
41+
*
42+
* d = ulpdiff( 1.0, 1.0+EPS+EPS );
43+
* // returns 2.0
44+
*
45+
* d = ulpdiff( 1.0, NaN );
46+
* // returns NaN
47+
*
48+
* d = ulpdiff( NaN, 1.0 );
49+
* // returns NaN
50+
*
51+
* d = ulpdiff( NaN, NaN );
52+
* // returns NaN
53+
*/
54+
declare function ulpdiff( x: number, y: number ): number;
55+
56+
57+
// EXPORTS //
58+
59+
export = ulpdiff;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 ulpdiff = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
ulpdiff( 8.0, 8.0 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a first argument which is not a number...
30+
{
31+
ulpdiff( true, 5.0 ); // $ExpectError
32+
ulpdiff( false, 5.0 ); // $ExpectError
33+
ulpdiff( null, 5.0 ); // $ExpectError
34+
ulpdiff( undefined, 5.0 ); // $ExpectError
35+
ulpdiff( '5', 5.0 ); // $ExpectError
36+
ulpdiff( [], 5.0 ); // $ExpectError
37+
ulpdiff( {}, 5.0 ); // $ExpectError
38+
ulpdiff( ( x: number ): number => x, 5.0 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided a second argument which is not a number...
42+
{
43+
ulpdiff( 5.0, true ); // $ExpectError
44+
ulpdiff( 5.0, false ); // $ExpectError
45+
ulpdiff( 5.0, null ); // $ExpectError
46+
ulpdiff( 5.0, undefined ); // $ExpectError
47+
ulpdiff( 5.0, '5' ); // $ExpectError
48+
ulpdiff( 5.0, [] ); // $ExpectError
49+
ulpdiff( 5.0, {} ); // $ExpectError
50+
ulpdiff( 5.0, ( x: number ): number => x ); // $ExpectError
51+
}
52+
53+
// The compiler throws an error if the function is provided an unsupported number of arguments...
54+
{
55+
ulpdiff(); // $ExpectError
56+
ulpdiff( 5.0 ); // $ExpectError
57+
ulpdiff( 5.0, 5.0, 5.0 ); // $ExpectError
58+
}

0 commit comments

Comments
 (0)