Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #289: Float64 trucating #292

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return mval.Uint(), nil
case reflect.Float32, reflect.Float64:
return mval.Float(), nil
return strconv.ParseFloat(fmt.Sprintf("%v", mval.Interface()), 64)
Copy link
Author

Choose a reason for hiding this comment

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

This change was necessary because mval.Float() convert a reflect.Value of float32 to float64 with more decimal places of original data. I don't know if this is the best solution, I'm wating for reviews

Example:

[12.3,45.6,78.9]

was converted to

[12.300000190734863,45.599998474121094,78.9000015258789]

Copy link
Owner

Choose a reason for hiding this comment

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

Even though I think this is still technically correct, it is probably counter intuitive.

Your change looks good, but I would replace it to only apply the logic on the float32 case:

case reflect.Float32:
	return strconv.ParseFloat(strconv.FormatFloat(float64(mval.Interface().(float32)), 'f', -1, 32), 64)
case reflect.Float64:
	return mval.Float(), nil

Maybe there is a better way, but this is the limit of my knowledge of floats handling in go :(

case reflect.String:
return mval.String(), nil
case reflect.Struct:
Expand Down
21 changes: 20 additions & 1 deletion marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Ystrlist = ["Howdy","Hey There"]
String2 = "Three"
`)

var testMarshalFloat64 = []byte(`Value = 1.24675324675
`)

func TestBasicMarshal(t *testing.T) {
result, err := Marshal(basicTestData)
if err != nil {
Expand Down Expand Up @@ -101,6 +104,22 @@ func TestBasicMarshalOrderedWithPointer(t *testing.T) {
}
}

func TestMarshalFloat64(t *testing.T) {
foo := struct {
Value float64
}{
Value: 1.24675324675,
}
b, err := Marshal(&foo)
if err != nil {
t.Fatal(err)
}
expected := testMarshalFloat64
if !bytes.Equal(b, expected) {
t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, b)
}
}

func TestBasicUnmarshal(t *testing.T) {
result := basicMarshalTestStruct{}
err := Unmarshal(basicTestToml, &result)
Expand Down Expand Up @@ -1495,7 +1514,7 @@ func TestUnmarshalPreservesUnexportedFields(t *testing.T) {

[[slice1]]
exported1 = "visible3"

[[slice1]]
exported1 = "visible4"

Expand Down
4 changes: 2 additions & 2 deletions tomltree_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen
// Ensure a round float does contain a decimal point. Otherwise feeding
// the output back to the parser would convert to an integer.
if math.Trunc(value) == value {
return strings.ToLower(strconv.FormatFloat(value, 'f', 1, 32)), nil
return strings.ToLower(strconv.FormatFloat(value, 'f', 1, 64)), nil
}
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, 32)), nil
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, 64)), nil
case string:
if tv.multiline {
return "\"\"\"\n" + encodeMultilineTomlString(value) + "\"\"\"", nil
Expand Down