Fix SI-prefix rounding carry below 1e-24 - #157
Open
maximilliangrand wants to merge 1 commit into
Open
Conversation
When a value smaller than the smallest prefix (yocto, 1e-24) is re-rounded to
the reduced precision that fits after the leading "0.", the rounding can carry
into a higher power of ten (e.g. 9.99 -> 10). The digit position was taken from
the original exponent, ignoring that carry, so the coefficient was placed one
decimal too far right and the result was off by a factor of ten
(format(".3s")(9.99e-25) returned "0.10y" instead of "1.0y").
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.
Repro (d3-format@3.1.2):
"0.10y" is 1e-25, an order of magnitude smaller than the input (~1e-24).
Root cause —
src/formatPrefixAuto.js. For values below the smallest prefix (yocto), the "less than 1y" branch re-rounds the coefficient to the reduced precision that fits after the leading0., but the leading-zero count is taken from the original exponent. When that re-rounding carries into a higher power of ten (9.99 → 10), the placement isn't adjusted, so digits land one decimal too far right.Fix — derive the digit position from the re-rounded decomposition's own exponent, then reuse the existing placement logic, so a carry shifts the result consistently.
Evidence — added a regression test; full suite 169/169 green, lint clean. A differential sweep over sub-yocto inputs shows every changed output is strictly closer to the true value, with zero regressions and all existing sub-yocto tests unchanged.