Skip to content

Format float32 fields with float32 precision in ReflectFrom - #448

Open
bardrop wants to merge 1 commit into
go-ini:mainfrom
bardrop-oss:fix-reflect-float32-precision
Open

bardrop wants to merge 1 commit into
go-ini:mainfrom
bardrop-oss:fix-reflect-float32-precision

Conversation

@bardrop

@bardrop bardrop commented Nov 18, 2026

Copy link
Copy Markdown

Describe the pull request

ReflectFrom wrote a float32 field with float64 digits: 0.1 became 0.10000000149011612, 1.1 became 1.100000023841858. formatValue printed v.Float(), which is the float32 widened to float64.

Now the reflect.Float32 kind is printed as fmt.Sprint(float32(v.Float())). That is the shortest string that parses back to the same float32, so MapTo still reads the exact value. float64 is unchanged. Covers plain fields, *float32 and named float32 types.

New TestStructRoundTrip_Float32 writes F = 0.1, FP = 1.1, M = 2.2 and 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 ./... and gofmt -l . pass locally (Go 1.26.5, macOS arm64). I did not run golangci-lint locally.

Link to the issue: #447

Checklist

  • I agree to follow the Code of Conduct by submitting this pull request.
  • I have read and acknowledge the Contributing guide.
  • I have added test cases to cover the new code.

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.

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
@bardrop
bardrop requested a review from unknwon as a code owner November 18, 2026 06:51

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant