Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func parseValue(t reflect.Type, str string) (reflect.Value, error) {
}
v.SetUint(u)
case reflect.Float32, reflect.Float64:
f, err := strconv.ParseFloat(str, 64)
f, err := strconv.ParseFloat(str, t.Bits())
if err != nil {
return v, err
}
Expand Down
9 changes: 9 additions & 0 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,15 @@ func TestStructRoundTrip_Float32(t *testing.T) {
assert.Equal(t, fp, *out.FP)
assert.Equal(t, in.M, out.M)
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)
}

// https://github.com/go-ini/ini/issues/274
Expand Down
Loading