Repository navigation
Parse float32 fields with float32 precision in MapTo - #449
Merged
Merged
Conversation
Parsing as a float64 and then narrowing to a float32 rounds twice, so the shortest float32 string for some values, such as 7.038531e-26, read back one ULP off.
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.
Describe the pull request
Follow-up to #448. That PR made
ReflectFromwrite float32 fields as the shortest float32 string, butMapTostill parsed them withstrconv.ParseFloat(str, 64)and then narrowed to float32. Rounding twice (text → float64 → float32) isn't always the same as rounding once. Running all 2³² float32 bit patterns through format → parse found one value, and its negative, that came back wrong: bits0x15ae43fdis written as7.038531e-26and read back one ULP off as7.0385313e-26. Before #448 this value round-tripped, because all 17 digits were written.parseValuenow parses withstrconv.ParseFloat(str, t.Bits()), so float32 fields round once, to the nearest float32. With this change, every finite float32 round-trips exactly.TestStructRoundTrip_Float32gets a case for7.038531e-26. It fails without the fix and passes with it.go test -race ./...,go vet ./...andgofmt -l .pass.One side effect: an out-of-range float32 such as
1e39now fails with strconv's "value out of range" error instead of "value … overflows float32". No test checks that message.Link to the issue: #447
Checklist
AI usage
Made with Amp (Claude). It found the double rounding by checking every float32 bit pattern while reviewing #448, then wrote the fix and the test and ran the checks above.