Repository navigation
Conversation
ReflectFrom wrote a float32 0.1 as 0.10000000149011612 because formatValue printed the value widened to float64. Format the float32 kind as a float32, which still parses back to the same value. Fixes go-ini#447
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.
Describe the pull request
ReflectFromwrote afloat32field with float64 digits:0.1became0.10000000149011612,1.1became1.100000023841858.formatValueprintedv.Float(), which is the float32 widened to float64.Now the
reflect.Float32kind is printed asfmt.Sprint(float32(v.Float())). That is the shortest string that parses back to the same float32, soMapTostill reads the exact value.float64is unchanged. Covers plain fields,*float32and named float32 types.New
TestStructRoundTrip_Float32writesF = 0.1,FP = 1.1,M = 2.2and reads them back. It fails on main and passes with the change. I also checked 2M random float32 bit patterns: format then parse gives the same value for all of them.go test -race -count=1 ./...,go vet ./...andgofmt -l .pass locally (Go 1.26.5, macOS arm64). I did not run golangci-lint locally.Link to the issue: #447
Checklist
AI usage
This PR was made with an AI coding agent (Claude): it found the bug while fuzzing the struct round trip, wrote the fix and test, and ran the checks above. No human reviewed it before opening.