caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Viet Le <vietlq85@gmail.com>
To: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Float precision in OCaml
Date: Tue, 1 Aug 2017 13:56:04 +0100	[thread overview]
Message-ID: <CAG_8+G6g-6zJK-POhaFrUYGAJ4kSOTmtpFyRaWtT-QmEHefxVA@mail.gmail.com> (raw)
In-Reply-To: <etPan.59807016.3723f31.123@erratique.ch>

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

Thanks Daniel and François for pointing out the RFC
https://tools.ietf.org/html/rfc7159 and mentioning IEEE 754-2008 binary64.
For my JSON parser I use Int64 to store integers and float to store
decimal/floating point values. The tweak gives nice feature and helps with
round-trip encoding/decoding.

Since you mentioned the RFC and it states:

"A JSON

   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

   available."

I will relax the condition for now. I used the benchmark provided by Milo
Yip:
https://github.com/miloyip/nativejson-benchmark/blob/master/data/roundtrip/roundtrip24.json

Thanks,
Viet


On 1 August 2017 at 13:12, Daniel Bünzli <daniel.buenzli@erratique.ch>
wrote:

> 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
>



-- 
Kind regards,
Viet

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

  reply	other threads:[~2017-08-01 12:56 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
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 [this message]
     [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=CAG_8+G6g-6zJK-POhaFrUYGAJ4kSOTmtpFyRaWtT-QmEHefxVA@mail.gmail.com \
    --to=vietlq85@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=daniel.buenzli@erratique.ch \
    /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).