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

Conversation

mattheusv
Copy link

Fixed #289

This PR change the bitSize of FormatFloat to string of 32 to 64. Maybe should be useful add a case statement to FormatFloat of float32 to string, but I don't implement because I was in doubt.

@codecov
Copy link

codecov bot commented Aug 24, 2019

Codecov Report

Merging #292 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #292      +/-   ##
==========================================
+ Coverage   92.17%   92.18%   +<.01%     
==========================================
  Files           9        9              
  Lines        1892     1894       +2     
==========================================
+ Hits         1744     1746       +2     
  Misses        103      103              
  Partials       45       45
Impacted Files Coverage Δ
marshal.go 91.36% <100%> (+0.03%) ⬆️
tomltree_write.go 85.81% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 781fbae...dad4949. Read the comment docs.

marshal.go Outdated
@@ -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 :(

@pelletier
Copy link
Owner

Merged #293, which addresses the same problem, but I believe with a more correct approach. Thank you for your help, sorry for the duplicate work!

@pelletier pelletier closed this Aug 27, 2019
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.

Float64 trucating
2 participants