jshon icon indicating copy to clipboard operation
jshon copied to clipboard

Rounding of floats

Open goodevilgenius opened this issue 8 years ago • 4 comments

Floats are rounded very poorly in jshon.

E.g., echo [0.3] | jshon outputs:

[
 0.29999999999999999
]

goodevilgenius avatar Sep 17 '16 20:09 goodevilgenius

That's not rounding, that's 0.3 represented as a float.

$ python -c 'print format(0.3, ".20f")'
0.29999999999999998890

jmetzmeier avatar Sep 18 '16 00:09 jmetzmeier

It's still a bug. 0.29999999999999999 ≠ .3

goodevilgenius avatar Sep 18 '16 01:09 goodevilgenius

No it is not. It is literally how floats work. https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

milhnl avatar Dec 08 '16 23:12 milhnl

Even if that is literally how floats work, let me demonstrate how other JSON processing tools seem not to mind:

$ echo [0.3] | jq
[
  0.3
]
$ python -c "import json; print(json.dumps([0.3]))"
[0.3]

It's not that unreasonable to expect that same result from another JSON tool.

Nindaleth avatar Feb 02 '21 20:02 Nindaleth