Repository navigation
fix: convert to the nearest f64 - #45
Open
Nick-Lafky wants to merge 1 commit into
Open
Nick-Lafky wants to merge 1 commit into
Nick-Lafky wants to merge 1 commit into
Conversation
Amp-Thread-ID: https://ampcode.com/threads/T-01a11c3a-df69-74b3-b72c-b2ce36b3b318 Co-authored-by: Amp <amp@ampcode.com>
Nick-Lafky
force-pushed
the
f64-nearest
branch
from
October 12, 2026 16:36
2a1616f to
4c81eef
Compare
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.
f64::from(fp)currently splits the number into integral and fractional parts, converts each tof64, and adds them. That's three roundings, and the result is often one ULP away from the nearest double:For 4-decimal values in
[1, 2)about 17% come out wrong this way.This change makes the conversion return the nearest
f64:i16/i32layout and nearly all practicali64ones.<mantissa>e<exponent>into a stack buffer and parsed withstr::parse::<f64>. The standard library's parser is the only stable entry point to a correctly rounded decimal-to-binary conversion (itsdec2fltinternals aren't public), and it's the same approachjava.math.BigDecimal#doubleValueused until JDK 21.Why it's strictly better: for every input the new result is the nearest double, and the old result was either the same or farther away. There's no input where the old code was closer.
Where it's worse: the fallback is slower than the division (it formats and parses a short string) and runs for
i128values with mantissas above 2^53 or any precision above 22. The fast path is unchanged in cost. And since results can move by one ULP for existing users, this is a behaviour change even though it's a fix. You may want to call it out in the release notes beyond the changelog line.The fallback doesn't go through
Display, becauseStringifyoverflows forMINat precision 0 ((inner / COEF).abs()) and for negative 39-digiti128values (fractional + COEF).Tests: values the old sum gets wrong on both the
i64andi128layouts, both sides of the 2^53 mantissa bound, a precision above 22, andMIN/MAXfor each layout.