Skip to content

Fix SI-prefix rounding carry below 1e-24 - #157

Open
maximilliangrand wants to merge 1 commit into
d3:mainfrom
maximilliangrand:fix/si-prefix-carry-below-yocto
Open

Fix SI-prefix rounding carry below 1e-24#157
maximilliangrand wants to merge 1 commit into
d3:mainfrom
maximilliangrand:fix/si-prefix-carry-below-yocto

Conversation

@maximilliangrand

Copy link
Copy Markdown

Repro (d3-format@3.1.2):

d3.format(".3s")(9.99e-25); // "0.10y"  — should be "1.0y"
d3.format(".3s")(9.99e-26); // "0.01y"  — should be "0.1y"

"0.10y" is 1e-25, an order of magnitude smaller than the input (~1e-24).

Root causesrc/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 leading 0., 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.

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").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant