Description
round2(-Number.MIN_VALUE) returns the positive smallest subnormal instead of the negative one. Found this while digging through math/base/special.
The function does the usual if (x < 0) { x = -x; sign = -1; } thing up front so the rest of the body only deals with magnitudes, then multiplies back by sign on every return path; except one. There's an early-return for "this is exactly the smallest subnormal, can't round it any further":
if ( p === MIN_EXP_SUBNORMAL ) {
return x; // <-- x is already the abs value at this point, sign is dropped
}
Since x was already reassigned to its absolute value two lines earlier, this just returns the magnitude, not the original signed input. Every other branch in the function (sign * PINF, sign * HUGE, sign * y1, sign * y2) gets this right; this one spot doesn't.
Existing test suite doesn't catch it; it only checks round2(SMALLEST_SUBNORMAL) (positive), never the negative case, so sign being +1 there masked the bug.
Related Issues
None
Questions
No.
Demo
No response
Reproduction
var round2 = require( '@stdlib/math/base/special/round2' );
var v = round2( -Number.MIN_VALUE ); // -5e-324
// returns 5e-324, should return -5e-324
Expected Results:
-5e-324 (sign preserved, matching every other return path in the function).
Actual Results:
5e-324 — sign silently flipped.
Expected Results
`-5e-324` (sign preserved, matching every other return path in the function).
Actual Results
`5e-324` — sign silently flipped.
Version
0.4.1
Environments
Node.js
Browser Version
No response
Node.js / npm Version
Node v22.22.2, npm 10.9.7
Platform
No response
Checklist
Description
round2(-Number.MIN_VALUE)returns the positive smallest subnormal instead of the negative one. Found this while digging throughmath/base/special.The function does the usual
if (x < 0) { x = -x; sign = -1; }thing up front so the rest of the body only deals with magnitudes, then multiplies back bysignon every return path; except one. There's an early-return for "this is exactly the smallest subnormal, can't round it any further":Since
xwas already reassigned to its absolute value two lines earlier, this just returns the magnitude, not the original signed input. Every other branch in the function (sign * PINF,sign * HUGE,sign * y1,sign * y2) gets this right; this one spot doesn't.Existing test suite doesn't catch it; it only checks
round2(SMALLEST_SUBNORMAL)(positive), never the negative case, sosignbeing+1there masked the bug.Related Issues
None
Questions
No.
Demo
No response
Reproduction
Expected Results:
-5e-324(sign preserved, matching every other return path in the function).Actual Results:
5e-324— sign silently flipped.Expected Results
Actual Results
`5e-324` — sign silently flipped.Version
0.4.1
Environments
Node.js
Browser Version
No response
Node.js / npm Version
Node v22.22.2, npm 10.9.7
Platform
No response
Checklist