GNOME Bugzilla – Bug 672143
Float values are changed when they become interpreted
Last modified: 2012-09-18 08:48:20 UTC
Created attachment 209842 [details] Test case Please consider attached example. It just reads {'a' : 123.231}, passes interpreted structure to the generator and then, serializes it again to JSON. Output is {"a":123.23099999999999} I understand that internal routines for float handling makes this change but this is behavior that is rather unpredictable for the programmer and should not behave like that. E.g. I am making kind of message bus that is based on JSON, and I have to be sure that message that I forward to the client is *exactly* the same as what I received. It is impossible with current approach. valac 0.14 json-glib 0.14.0
it should behave exactly like that; your use case is an unreasonable expectation. floating point values cannot be "exactly" the same as received. we also cannot lose precision because it's an all-or-nothing kind of situation: if we truncated the floating point values it would be fully arbitrary, and it would lead to loss of precision - you set 0.000334 and on the other end of the wire you get 0.000. JSON is a data exchange format: accuracy of representation beats readability or character-by-character exactness.