-
-
Notifications
You must be signed in to change notification settings - Fork 868
feat: add remaining implementation for number/float64/base/ulp-difference
#7446
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b0a9ec0
feat: add remaining implementation for `number/float64/base/ulp-diffe…
anandkaranubc f613e97
docs: fix function import
anandkaranubc 73e0265
Merge branch 'develop' into feat/ulp-diff
anandkaranubc fdd3156
chore: update `directories` field
anandkaranubc 9e261cc
docs: add link for ulp wiki page
anandkaranubc fd662b1
docs: add line break
anandkaranubc ce8f6b8
docs: add return annotations to examples
anandkaranubc e05da8a
docs: add return annotations to README examples
anandkaranubc 9adf772
test: add infinity tests
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
lib/node_modules/@stdlib/number/float64/base/ulp-difference/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [double-precision][double-precision] floating-point values that separate two [double-precision][double-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/float64/base/ulp-difference' ); | ||
``` | ||
|
||
#### ulpdiff( x, y ) | ||
|
||
Computes the number of representable [double-precision][double-precision] floating-point values that separate two [double-precision][double-precision] floating-point numbers along the real number line. | ||
|
||
```javascript | ||
var EPS = require( '@stdlib/constants/float64/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 [double-precision][double-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/float64/eps' ); | ||
var SMALLEST_SUBNORMAL = require( '@stdlib/constants/float64/smallest-subnormal' ); | ||
var ulpdiff = require( '@stdlib/number/float64/base/ulp-difference' ); | ||
|
||
var d = ulpdiff( 1.0, 1.0+EPS ); | ||
console.log( d ); | ||
anandkaranubc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// => 1.0 | ||
|
||
d = ulpdiff( 5.8364e-319, 5.8367e-319 ); | ||
console.log( d ); | ||
// => 6.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"> | ||
|
||
[double-precision]: https://en.wikipedia.org/wiki/Double-precision_floating-point_format | ||
|
||
[ulp]: https://en.wikipedia.org/wiki/Unit_in_the_last_place | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
53 changes: 53 additions & 0 deletions
53
lib/node_modules/@stdlib/number/float64/base/ulp-difference/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @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 x; | ||
var y; | ||
var i; | ||
|
||
x = uniform( 100, -500.0, 500.0 ); | ||
y = uniform( 100, -500.0, 500.0 ); | ||
|
||
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(); | ||
}); |
45 changes: 45 additions & 0 deletions
45
lib/node_modules/@stdlib/number/float64/base/ulp-difference/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
{{alias}}( x, y ) | ||
Computes the number of representable double-precision floating-point values | ||
that separate two double-precision floating-point numbers along the real | ||
number line. | ||
|
||
Adjacent double-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/float64/eps}} ) | ||
1.0 | ||
> v = {{alias}}( 1.0+{{alias:@stdlib/constants/float64/eps}}, 1.0 ) | ||
1.0 | ||
> v = {{alias}}( 1.0, 1.0+{{alias:@stdlib/constants/float64/eps}}*2.0 ) | ||
2.0 | ||
> v = {{alias}}( 1.0, NaN ) | ||
NaN | ||
> v = {{alias}}( NaN, 1.0 ) | ||
NaN | ||
> v = {{alias}}( NaN, NaN ) | ||
NaN | ||
|
||
See Also | ||
-------- | ||
|
59 changes: 59 additions & 0 deletions
59
lib/node_modules/@stdlib/number/float64/base/ulp-difference/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 double-precision floating-point values that separate two double-precision floating-point numbers along the real number line. | ||
* | ||
* ## Notes | ||
* | ||
* - Adjacent double-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/float64/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; |
58 changes: 58 additions & 0 deletions
58
lib/node_modules/@stdlib/number/float64/base/ulp-difference/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.