fix: avoid native float overflow when scaling large values in toFixed - #777
Open
spokodev wants to merge 1 commit into
Open
fix: avoid native float overflow when scaling large values in toFixed#777spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
`toFixed` scaled, rounded and divided the value in native float
(`roundingFunction(+`${value}e+${precision}`) / Math.pow(10, precision)`).
For integral values above 2^53 this loses precision, so the integer part
of the output changed depending only on the requested mantissa length.
Example with 9.999999e20:
format({mantissa: 0}) -> 999999900000000000000 (correct)
format({mantissa: 2}) -> 999999900000000000000.00 (correct)
format({mantissa: 3}) -> 999999899999999900000.000 (corrupted)
format({mantissa: 4}) -> 999999899999999900000.0000 (corrupted)
An integral value's integer part must be invariant to the requested
mantissa length. bignumber.js is already imported in this module, so the
scale and divide now run through BigNumber and large values keep their
digits. Adds a regression test asserting the integer part stays constant
for mantissa 0..6.
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.
Problem
The integer part of a large integral value changes depending only on the requested mantissa length:
An integral value's integer part must be invariant to the requested mantissa length, so the
999999899999999900000results are wrong.Root cause
toFixedinsrc/formatting.jsscales, rounds and divides in native float:For an integral value above
2^53, scaling by10^precisionand dividing back in native float loses precision. The result flips between correct and corrupted purely as a function of the requested decimal count.Fix
bignumber.jsis already imported in this module. The scale and divide now run throughBigNumber, so large values keep their digits. The user-suppliedroundingFunctionis still applied, so rounding behavior is unchanged.Test
Added a regression test in the
toFixedblock asserting the integer part of9.999999e20stays999999900000000000000for mantissa0..6. It fails on the current code (mantissa 3, 4, 6) and passes with the fix. Full unit suite stays green (463 specs, 0 failures); integration suite green (2 specs, 0 failures).