Repository navigation
Conversation
FormatFloat added 0.5 * 10^-precision and truncated, but the sum is itself rounded to a float, so the carry was often lost: 8894.75 with "#,###.#" gave 8,894.7 and 71.575 (just above the tie) gave 71.57, while 0.125 rounded up and 945.125 rounded down. Let strconv round the binary value, and round exact ties up as the old rounder meant to.
This branch has not been deployed
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.
FormatFloatadds0.5 * 10^-precisionand truncates, but that sum is itself rounded to a float, so the carry is often lost:FormatFloat("#,###.#", 8894.75)8,894.78,894.8FormatFloat("#,###.##", 71.575)(stored just above the tie)71.5771.58FormatFloat("#,###.##", 945.125)(exact tie)945.12945.13, as0.125already gives0.13This lets
strconv.FormatFloatround the binary value, and rounds exact ties up as the old rounder intended. On 150,000 random values and precisions 0-9, the output now matches Python'sDecimal(x).quantize(..., ROUND_HALF_UP)in every case; master differs in 7,952. The new rows inTestFormatFloatfail on master. Plainstrconv(ties to even) and treating any trailing 5 as a tie each fail at least one of them.It costs about 200 ns per call instead of 110 on values without a tie (3 allocations instead of 1). A format with more than 9 decimals used to panic with an index out of range; it now formats. #167 moves the same lines, so whichever lands second will need a small rebase.
gofmt,go vetandgo test ./...pass on Go 1.26; I did not run the 1.21 or 1.27 jobs locally.Written with AI assistance (Claude); I have reviewed the change.