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

fmt: Printf() on float64 is not accurate #11660

Closed
jvehent opened this issue Jul 10, 2015 · 3 comments
Closed

fmt: Printf() on float64 is not accurate #11660

jvehent opened this issue Jul 10, 2015 · 3 comments

Comments

@jvehent
Copy link

@jvehent jvehent commented Jul 10, 2015

The following code returns bogus results

package main

import (
    "fmt"
)

var nums []float64 = []float64{
    18446744073709551616,
    18446744073709551615,
    18446744073709551613,
    18446744073709551600,
    99999999999999999,
}

func main() {
    for _, num := range nums {
        fmt.Printf("%%.0f:\t%.0f\n", num)
        fmt.Printf("%%v:\t%v\n---\n", num)
    }
}

output:

%.0f:   18446744073709551616
%v:     1.8446744073709552e+19

---
%.0f:   18446744073709551616
%v:     1.8446744073709552e+19

---
%.0f:   18446744073709551616
%v:     1.8446744073709552e+19

---
%.0f:   18446744073709551616
%v:     1.8446744073709552e+19

---
%.0f:   100000000000000000
%v:     1e+17

I have not identified the source of the issue yet, but these conversion errors are causing bugs in my code. Am I using fmt wrong or is this an issue with either the fmt or strconv packages?

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Jul 10, 2015

18446744073709551616, 18446744073709551615, 18446744073709551613, and 18446744073709551600 are all equal after conversion to float64 due to limited precision.

@mdempsky mdempsky closed this Jul 10, 2015
@jvehent
Copy link
Author

@jvehent jvehent commented Jul 10, 2015

So the float64 has a precision of 53 bits only?

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Jul 10, 2015

Put simply, yes. It's the IEEE-754 64-bit-wide "double-precision" format: https://en.wikipedia.org/wiki/IEEE_754-1985#Double_precision

@mikioh mikioh changed the title Fmt.Printf() on float64 is not accurate fmt: Printf() on float64 is not accurate Jul 11, 2015
@golang golang locked and limited conversation to collaborators Jul 11, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.