number such as 1E400 or 3.141592653589793238462643383279 may indicate potential interoperability problems, since it suggests that the software that created it expects receiving software to have greater capabilities for numeric magnitude and precision than is widely
On 1 August 2017 at 12:47:37, Viet Le (vietlq85@gmail.com) wrote:
> I haven't found precise limit, but as a rule of thumb (*not precise*), for
> a positive float value to keep its precision, it *should* not be smaller
> than 1.00000000001e-312. To use JSON as precise serializer, it would be
> necessary to preserve accuracy.
Here are a few tips:
1. The latest RFC recommends [1] to support the range of IEEE 754 double precision numbers (those are OCaml's float) in JSON numbers. However since you are converting your floating point representation from binary to a decimal one you can't guarantee to round trip your floats with a JSON number.
2. If you want to serialize precise integers with good interop you can do so using JSON number if they are in the range [-2^53;2^53] (the range written in the mentioned RFC seems wrong to me). Those are the integers a double precision floating point numbers are able to represent precisely.
3. If you want to serialize precise floats you should do this as a JSON string using a lossless textual notation for your binary floating point number. Before OCaml 4.03 you can use this [2] function after 4.03 you can use float_of_string and the "%h" format string [3].
Best,
Daniel
[1] https://tools.ietf.org/html/rfc7159#section-6
[2] http://erratique.ch/software/gg/doc/Gg.Float.html# VALpp
[3] https://github.com/ocaml/ocaml/pull/268