caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Frédéric Bour" <frederic.bour@lakaban.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Float precision in OCaml
Date: Tue, 1 Aug 2017 13:25:37 +0200	[thread overview]
Message-ID: <45f24f0e-cb29-4d07-7e72-bee1610f1057@lakaban.net> (raw)
In-Reply-To: <CADK7aFNq0duKz-_+N3HTUQb4POAYKCdMjW7LB5-d0GwKpe8Vtw@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2162 bytes --]

The Doubledouble[1] module from my grenier library implements 106-bit 
precision floating point. See 
https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format#Double-double_arithmetic 
for more information.

But this is not a serious solution, if you want to preserve json numbers 
then you should represent them more or less literally (up to what you 
consider "worth" preserving), not by encoding them as IEEE 754 double.

[1]: 
https://github.com/let-def/grenier/blob/master/doubledouble/doubledouble.mli


On 01/08/2017 13:18, Nicolás Ojeda Bär 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 
> <mailto: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
>     <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
>
>


[-- Attachment #2: Type: text/html, Size: 5377 bytes --]

  reply	other threads:[~2017-08-01 11:25 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-01 10:47 Viet Le
2017-08-01 11:18 ` Nicolás Ojeda Bär
2017-08-01 11:25   ` Frédéric Bour [this message]
2017-08-01 11:45     ` Viet Le
2017-08-01 11:40   ` François Bobot
2017-08-01 11:42   ` Viet Le
2017-08-01 11:48     ` François Bobot
2017-08-01 12:12 ` Daniel Bünzli
2017-08-01 12:56   ` Viet Le
     [not found]   ` <etPan.59807b8a.db32dee.123@AirmailxGenerated.am>
2017-08-01 13:17     ` Daniel Bünzli
2017-08-01 13:57   ` Soegtrop, Michael
2017-08-01 14:07     ` Soegtrop, Michael
2017-08-01 15:48     ` Peter Thiemann
2017-08-01 17:12       ` Soegtrop, Michael
2017-08-02  7:41         ` Peter Thiemann
     [not found]   ` <etPan.5980c173.1234cb4.123@AirmailxGenerated.am>
2017-08-01 18:05     ` Daniel Bünzli
2017-08-02  5:46       ` Viet Le
2017-08-02  8:22       ` Soegtrop, Michael

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45f24f0e-cb29-4d07-7e72-bee1610f1057@lakaban.net \
    --to=frederic.bour@lakaban.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).