Skip to content

Format float32 fields with float32 precision in ReflectFrom - #448

Merged
unknwon merged 1 commit into
go-ini:mainfrom
bardrop-oss:fix-reflect-float32-precision
Nov 18, 2026
Merged

unknwon merged 1 commit into
go-ini:mainfrom
bardrop-oss:fix-reflect-float32-precision

Conversation

@bardrop

@bardrop bardrop commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

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

@unknwon unknwon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the formatting fix is right, but the read side needs a matching change.

MapTo still parses float32 fields as a float64 and then narrows them, which rounds twice. I ran all 2³² float32 bit patterns through format → ParseFloat(s, 64) → float32. One value (and its negative) comes back wrong: bits 0x15ae43fd is written as 7.038531e-26 and reads back one ULP off as 7.0385313e-26. Main doesn't have this problem because it writes all 17 digits.

Fix in parseValue (struct.go line 214). GitHub won't take a suggestion on that line because it's outside this PR's diff:

 	case reflect.Float32, reflect.Float64:
-		f, err := strconv.ParseFloat(str, 64)
+		f, err := strconv.ParseFloat(str, t.Bits())

The suggestion below adds a regression test for it.

Comment thread struct_test.go
require.NotNil(t, out.FP)
assert.Equal(t, fp, *out.FP)
assert.Equal(t, in.M, out.M)
assert.Equal(t, in.D, out.D)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression case for the double rounding. It fails without the ParseFloat change (expected 7.038531e-26, actual 7.0385313e-26):

Suggested change
assert.Equal(t, in.D, out.D)
assert.Equal(t, in.D, out.D)
// Parsing this one as a float64 and then narrowing it to a float32 rounds
// twice and lands one ULP off.
in = cfg{F: 7.038531e-26, FP: &fp}
data, err = marshal(in)
require.NoError(t, err)
out = cfg{}
require.NoError(t, unmarshal(data, &out))
assert.Equal(t, in.F, out.F)

@unknwon unknwon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@unknwon
unknwon merged commit 010acfd into go-ini:main Nov 18, 2026
4 checks passed
@unknwon

unknwon commented Nov 18, 2026

Copy link
Copy Markdown
Member

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.

2 participants