fix: NumDigits underreports for some exact powers of ten#425
Open
c-tonneslan wants to merge 1 commit into
Open
fix: NumDigits underreports for some exact powers of ten#425c-tonneslan wants to merge 1 commit into
c-tonneslan wants to merge 1 commit into
Conversation
math.Log10(1eN) rounds down to N-1 for several N (15, 24, 28, ...) because the exact decimal can't survive a float64 round-trip. The fast path returned int(math.Log10(...)) + 1, so a coefficient like 1000000000000000 (16 digits) came back as 15. Count digits via int64 division for the IsInt64 branch. The slow big.Int path is unchanged. Regression tests cover 1e15 and -1e15 (both via plain literal and scientific-notation input) and 1<<53 (the old fast-path boundary). Fixes shopspring#420 Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
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.
From the reporter in #420:
The fast path returns
int(math.Log10(math.Abs(float64(i64)))) + 1, andmath.Log10of an exact power of ten doesn't always round to the exact integer. On my machine:So 1e15 takes the wrong branch and
int(...) + 1returns 15 instead of 16. Same shape at 1e24, 1e28, and other powers where the float64 representation drifts down.Count digits via plain int64 division instead. No floats, no estimate. The
big.Intslow path is unchanged.Regression cases added to
TestDecimal_NumDigitscover1e15directly, with sign and scientific-notation variants, plus1<<53(the boundary of the old fast path). Fails on master, passes here.go test ./...is clean.Fixes #420.