-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add number/float16/base/ulp-difference
#9321
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
feat: add number/float16/base/ulp-difference
#9321
Conversation
Coverage Report
The above coverage report was generated for the changes in this PR. |
lib/node_modules/@stdlib/number/float16/base/ulp-difference/examples/index.js
Show resolved
Hide resolved
lib/node_modules/@stdlib/number/float16/base/ulp-difference/benchmark/benchmark.js
Show resolved
Hide resolved
lib/node_modules/@stdlib/number/float16/base/ulp-difference/lib/main.js
Outdated
Show resolved
Hide resolved
lib/node_modules/@stdlib/number/float16/base/ulp-difference/lib/main.js
Outdated
Show resolved
Hide resolved
lib/node_modules/@stdlib/number/float16/base/ulp-difference/lib/main.js
Outdated
Show resolved
Hide resolved
kgryte
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this looks good. My main question is around monotone key conversion.
|
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. |
lib/node_modules/@stdlib/number/float16/base/ulp-difference/lib/main.js
Outdated
Show resolved
Hide resolved
Signed-off-by: Athan <[email protected]>
kgryte
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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