Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
encoding/json: float32 may not convert correctly #34559
What version of Go are you using (
This is not a bug. There's no way to distinguish
This behavior is generalized to float32s for Go's JSON parser: print as few non-zero digits as is needed to precisely identify the right float32. Note that reading the value back as a float32 reproduces the original float32. In float32 math, 2684354600 == 2684354560.
Similarly, in float64 math, 123456789123456789123 == 123456789123456800000.
If you want to distinguish between 2684354600 and 2684354560, the answer is to not use a float32 at all. Use a float64 instead. (In general a float32 has so little precision that it's almost never the right answer.)
I'd be happy to look at examples from other languages but almost all of them are probably operating on float64s, not float32s, either because the language doesn't have float32 at all or because the language does the C-like thing where float32s become float64s in most calls.
That is, try:
That's what most C-like languages would do implicitly.