Skip to content

fix: correctly round subnormals in number/float16/base/to-word - #16511

Open
harsh-thakkar7 wants to merge 1 commit into
stdlib-js:developfrom
harsh-thakkar7:fix/16510-float16-subnormal
Open

harsh-thakkar7 wants to merge 1 commit into
stdlib-js:developfrom
harsh-thakkar7:fix/16510-float16-subnormal

Conversation

@harsh-thakkar7

Copy link
Copy Markdown
Contributor

Resolves #16510

Description

What is the purpose of this pull request?

Fixes subnormal rounding in @stdlib/number/float16/base/to-word at the 11-bit shift boundary. When shift === 11, the code previously took the wrong rounding branch and returned zero for values that should round to the smallest subnormal:

  • toWord( 2.98038125038147e-8 ) returned 0x0000; now returns 0x0001.
  • toWord( -2.98038125038147e-8 ) returned 0x8000; now returns 0x8001.

The fix changes the branch condition from shift < 11 to shift <= 11, so the round bit is taken from the correct bit of the 11-bit mantissa. Values requiring shift > 11 already return zero earlier via the f16Exponent < -10 check.

Regression tests cover the positive and negative rounding cases, the exact 2^-25 tie, and values just below 2^-24.

Related Issues

Does this pull request have any related issues?

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request?

The C/native path is unaffected: it returns the bits of an already encoded stdlib_float16_t. The JS test suite passes (5,013 tests), and the result was cross-checked against native Float16Array: 0 mismatches over a 50,000-value sweep around 2^-25 (previously ~25,007 mismatches).

Checklist

Please ensure that the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

@harsh-thakkar7
harsh-thakkar7 requested a review from a team November 5, 2026 19:03
@stdlib-bot stdlib-bot added Needs Review A pull request which needs code review. labels Nov 5, 2026
@stdlib-bot

stdlib-bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
number/float16/base/to-word $\\color{red}264/282$
$\\color{green}+0.00\\%$
$\\color{red}28/33$
$\\color{green}+0.00\\%$
$\\color{green}2/2$
$\\color{green}+0.00\\%$
$\\color{red}264/282$
$\\color{green}+0.00\\%$

The above coverage report was generated for the changes in this PR.

@harsh-thakkar7
harsh-thakkar7 force-pushed the fix/16510-float16-subnormal branch from f7f5974 to 3d3c4a8 Compare November 5, 2026 19:22

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: number/float16/base/to-word mis-rounds subnormals at the 11-bit shift boundary

2 participants