Thanks Nicolas. I did check against C++ before writing the email and I can see OCaml is consistent with C++. I'm aware about IEEE 754. I'm seeking opinion for maintaining precision using 2 values as shown.

On the other hand I think I should relax this requirement and let my JSON parser accept this case because realistically I don't see anyone would use numbers at +/- E308.

Thanks


On Tue, 1 Aug 2017 at 12:18, Nicolás Ojeda Bär <nicolas.ojeda.bar@lexifi.com> wrote:
Dear Viet,

I am not sure this is an issue with OCaml (as you can verify using your favourite C compiler).  Rather, I think IEEE 754 double-precision binary floating-point numbers can only represent numbers between 10^{-308} and 10^308 with full decimal digits precision. Numbers smaller than that can only be represented with reduced precision.

Best wishes,
Nicolas

On Tue, Aug 1, 2017 at 12:47 PM, Viet Le <vietlq85@gmail.com> wrote:
Hi all,

I'm writing a JSON parser in pure OCaml and have encountered an issue with small float values:

10 zeros in between
# 1.00000000001e-312 ;;
- : float = 1.00000000001e-312

11 zeros in between
# 1.000000000001e-312 ;;
- : float = 1.00000000000341e-312

# 5e-324 ;;
- : float = 4.94065645841e-324


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.

I checked https://github.com/ocaml/Zarith and it supports only big int & quotients, not floating point.

For values smaller than the limit above, should I just treat as 2 values: (normalized: float, exponent: float), so we will have:

5e-324 -> (5, -324)

Comments and suggestions are appreciated. Thanks.

--
Kind regards,
Viet

--
Kind regards,
Viet