feat: add number/float16/base/ulp-difference#9321
Merged
kgryte merged 3 commits intostdlib-js:developfrom Dec 26, 2025
Merged
Conversation
Contributor
Coverage Report
The above coverage report was generated for the changes in this PR. |
Neerajpathak07
commented
Dec 23, 2025
lib/node_modules/@stdlib/number/float16/base/ulp-difference/examples/index.js
Show resolved
Hide resolved
Neerajpathak07
commented
Dec 23, 2025
lib/node_modules/@stdlib/number/float16/base/ulp-difference/benchmark/benchmark.js
Show resolved
Hide resolved
kgryte
reviewed
Dec 25, 2025
lib/node_modules/@stdlib/number/float16/base/ulp-difference/lib/main.js
Outdated
Show resolved
Hide resolved
kgryte
reviewed
Dec 25, 2025
kgryte
reviewed
Dec 25, 2025
lib/node_modules/@stdlib/number/float16/base/ulp-difference/lib/main.js
Outdated
Show resolved
Hide resolved
kgryte
reviewed
Dec 25, 2025
lib/node_modules/@stdlib/number/float16/base/ulp-difference/lib/main.js
Outdated
Show resolved
Hide resolved
kgryte
requested changes
Dec 25, 2025
Member
kgryte
left a comment
There was a problem hiding this comment.
Overall, this looks good. My main question is around monotone key conversion.
Member
Author
|
Tested the two-complements negation logic with the following edge cases:- d = ulpdiff( -1.1920928955078125e-7, -5.960464477539063e-8 );
console.log( d );
// => 1.0
d = ulpdiff( -5.960464477539063e-8, 5.960464477539063e-8 );
console.log( d );
// => 2.0output log:- [Running] node "c:\Users\HP\Stdlib\stdlib\lib\node_modules\@stdlib\number\float16\base\ulp-difference\test\edgeCase.js"
1
2
what I believe if the logic was broken it would have provided huge ulp diff. |
kgryte
reviewed
Dec 26, 2025
lib/node_modules/@stdlib/number/float16/base/ulp-difference/lib/main.js
Outdated
Show resolved
Hide resolved
Signed-off-by: Athan <kgryte@gmail.com>
kgryte
approved these changes
Dec 26, 2025
Member
kgryte
left a comment
There was a problem hiding this comment.
LGTM. I confirmed that ordering works correctly with the following script:
'use strict';
// MODULES //
var toWord = require( '@stdlib/number/float16/base/to-word' );
var fromWord = require( '@stdlib/number/float16/base/from-word' );
var UINT16_MAX = require( '@stdlib/constants/uint16/max' );
var SIGN_MASK = require( '@stdlib/constants/float16/sign-mask' );
var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
// FUNCTIONS //
/**
* Converts an unsigned 16-bit integer corresponding to the IEEE 754 binary representation of a half-precision floating-point number to a lexicographically ordered integer.
*
* @private
* @param {unsigned16} word - unsigned 16-bit integer
* @returns {integer} lexicographically ordered integer
*/
function monotoneKey( word ) {
if ( word & SIGN_MASK ) { // x < 0
return ( ( ~word + 1 ) &UINT16_MAX ); // two's-complement negation
}
// x >= 0
return ( word | SIGN_MASK ) >>> 0; // push +0 to just above -0
}
/**
* Comparator function.
*
* @private
* @param {number} a - first number
* @param {number} b - second number
* @returns {integer} value indicating sort order
*/
function compare( a, b ) {
if ( a !== a ) {
return 1;
}
if ( b !== b ) {
return -1;
}
if ( a < b ) {
return -1;
}
if ( a > b ) {
return 1;
}
if ( isNegativeZero( a ) ) {
return -1;
}
return 0;
}
// MAIN //
/**
* Main execution sequence.
*
* @private
*/
function main() {
var values;
var i;
values = [];
for ( i = 0; i <= UINT16_MAX; i++ ) {
values.push( fromWord( i ) );
}
values.sort( compare );
for ( i = 0; i < values.length; i++ ) {
console.log( '%d => %d', monotoneKey( toWord( values[ i ] ) ), values[ i ] );
}
}
main();If you run that and pipe to results.txt and inspect the results, you can confirm that lexicographic ordering works as expected.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves none.
Description
This pull request:
number/float16/base/ulp-differenceRelated Issues
This pull request has the following related issues:
Questions
No.
Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
{{TODO: add disclosure if applicable}}
@stdlib-js/reviewers