From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 1B7377F98F for ; Tue, 1 Aug 2017 14:56:08 +0200 (CEST) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=vietlq85@gmail.com; spf=Pass smtp.mailfrom=vietlq85@gmail.com; spf=None smtp.helo=postmaster@mail-yw0-f174.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of vietlq85@gmail.com) identity=pra; client-ip=209.85.161.174; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="vietlq85@gmail.com"; x-sender="vietlq85@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of vietlq85@gmail.com designates 209.85.161.174 as permitted sender) identity=mailfrom; client-ip=209.85.161.174; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="vietlq85@gmail.com"; x-sender="vietlq85@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-yw0-f174.google.com) identity=helo; client-ip=209.85.161.174; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="vietlq85@gmail.com"; x-sender="postmaster@mail-yw0-f174.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AHsnqCBdotCRXs1gLi39Kool3lGMj4u6mDksu8pMi?= =?us-ascii?q?zoh2WeGdxcS4Zx7h7PlgxGXEQZ/co6odzbGH4+a4ASQp2tWoiDg6aptCVhsI24?= =?us-ascii?q?09vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6nK94iQPFRrhKAF7?= =?us-ascii?q?Ovr6GpLIj8Swyuu+54Dfbx9GiTe5Yr5+Ng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6e6?= =?us-ascii?q?90si86BZ6iFNeebo+oib2Fmiy8G8sFNSh9FlmQHCKwJM2/UPAWZXfXe5c5nw?= =?us-ascii?q?=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0AGAQBteYBZhq6hVdFdGwEBAQMBAQEJA?= =?us-ascii?q?QEBFgEBAQMBAQEJAQEBgkSBT20nB4UkiGOPfIFsgm+KdIgqghIugV6DOwKEGAc?= =?us-ascii?q?/GAEBAQEBAQEBAQEBEgEBAQgLCwgoL4IzJAGCQAEBAQECASMdARsdAQMBCwYFC?= =?us-ascii?q?w0qAgIhAQERAQUBHAYTihYBAw0IEKIdP4wKggQFARyDCQWDWQoZJw1WR4J0AQE?= =?us-ascii?q?BAQEFAQEBAQEBGgIGEoMWL4FThlOCV4F2U4JmgmEFgS0BAYhGlUYyCAEBgVAKh?= =?us-ascii?q?XaHaIRxgmSPXYwhiBAUH4EVH4FCd14aXINoKh+BdD82AYdvgj8BAQE?= X-IPAS-Result: =?us-ascii?q?A0AGAQBteYBZhq6hVdFdGwEBAQMBAQEJAQEBFgEBAQMBAQE?= =?us-ascii?q?JAQEBgkSBT20nB4UkiGOPfIFsgm+KdIgqghIugV6DOwKEGAc/GAEBAQEBAQEBA?= =?us-ascii?q?QEBEgEBAQgLCwgoL4IzJAGCQAEBAQECASMdARsdAQMBCwYFCw0qAgIhAQERAQU?= =?us-ascii?q?BHAYTihYBAw0IEKIdP4wKggQFARyDCQWDWQoZJw1WR4J0AQEBAQEFAQEBAQEBG?= =?us-ascii?q?gIGEoMWL4FThlOCV4F2U4JmgmEFgS0BAYhGlUYyCAEBgVAKhXaHaIRxgmSPXYw?= =?us-ascii?q?hiBAUH4EVH4FCd14aXINoKh+BdD82AYdvgj8BAQE?= X-IronPort-AV: E=Sophos;i="5.41,306,1498514400"; d="scan'208,217";a="285716162" Received: from mail-yw0-f174.google.com ([209.85.161.174]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 01 Aug 2017 14:56:07 +0200 Received: by mail-yw0-f174.google.com with SMTP id l82so9163080ywc.2 for ; Tue, 01 Aug 2017 05:56:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=AIzIFHkwPbCPXvP/KWz2W3+XORgNjsKrqjWhVi4JnuU=; b=ctKZmr5VwQg+HQTi5WOga9aqC7B5+1olMy9r9kxw/kJKbKtIsluLdbC/xAyJNwjL9R DI4o/31xfrzgPbJfdd+EZJMGopVavrXR0/zefnSZB6miGrvBww9bJpEqF/JXhb/wdGsS hNln7WTJtU6uUwa0FF/wGL0fKFaBERML/k/JIu13he882pIGvUGLzhJBGitAHLe3Bn3B hOVrrEEPnBhuMhN5SbDFRZSYI08wi4MN3cngKBZLV6zumLuu5bvDCM4bGBMV7ZL14Jgc 5kGC9uqlSI4aGaeUzsKm7jeDk+J82pdol/HwwfnRkTXtRUKF4YB1hNFuAzqYCCW5lT92 BoRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=AIzIFHkwPbCPXvP/KWz2W3+XORgNjsKrqjWhVi4JnuU=; b=j1u5UA1rFda1F52fBgMRp0uPkYRChyvQMzY3O1bNjmcriTEH+8SEveEBO2tfxDJ2v7 mmv0GSchbSL9iP1/rRJRw1ctzI7ycVq1Db5ZxW+6KxT5ryAtyGoDUCfOBHcbWU/v9pCV yNejgWwhfUX7VPPpc+ZXTuyyCp2hZIUXZQ2XDjJYDgWpHYi+Qzena1vftFj7QJ9S0xyj s8G895/TBHSkqpjkflZ+XWVvCGTbKF9fwiG1XUa5FTPH59CXjC0Py3MVfIyioHSxiFub Jvu8GO1Y9bIPM5Fmxu9M0zwNqok7XaHcSj0/MIPMvA0/Cb7MGLqTxIrE5/ZnaovCtwZ5 ca2w== X-Gm-Message-State: AIVw110NE7DpoARHaQPOHdOvhQvNlOTDfJt4xhpS5HTNun4kX0+juNEO WGRgAXwFuDP5qpoIUaRYfGdVhLvX3xbBgV8= X-Received: by 10.129.55.196 with SMTP id e187mr11215934ywa.469.1501592165626; Tue, 01 Aug 2017 05:56:05 -0700 (PDT) MIME-Version: 1.0 Received: by 10.129.164.76 with HTTP; Tue, 1 Aug 2017 05:56:04 -0700 (PDT) In-Reply-To: References: From: Viet Le Date: Tue, 1 Aug 2017 13:56:04 +0100 Message-ID: To: =?UTF-8?Q?Daniel_B=C3=BCnzli?= Cc: OCaml Mailing List Content-Type: multipart/alternative; boundary="001a11440268a13eab0555b0ab97" Subject: Re: [Caml-list] Float precision in OCaml --001a11440268a13eab0555b0ab97 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks Daniel and Fran=C3=A7ois 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=C3=BCnzli 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 > --=20 Kind regards, Viet --001a11440268a13eab0555b0ab97 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks Daniel and=C2=A0Fran=C3=A7ois=C2=A0for pointing out the RFC=C2=A0https://tools.ietf.org/html/r= fc7159 and mentioning IEEE 754-2008 binary64. For my JSON parser I use Int64 to store integer= s and float to store decimal/floating point values. The tweak gives nice fe= ature and helps with round-trip encoding/decoding.

Since you mentioned the RFC and i= t 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=C2=A0
=C2=A0 =C2=A0a= vailable."

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

Thanks,
Viet


On 1 August 2017 at 13:12, Daniel B=C3=BCnzli = <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 precis= e*), for
> a positive float value to keep its precision, it *should* not be small= er
> than 1.00000000001e-312. To use JSON as precise seria= lizer, it would be
> necessary to preserve accuracy.

Here are a few tips:=C2=A0

1. The latest RFC recommends [1] to support the range of=C2=A0IEEE 754 doub= le 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 JSO= N number.

2. If you want to serialize precise integers with good interop you can do s= o using JSON number if they are in the range=C2=A0[-2^53;2^53] (the range w= ritten in the mentioned RFC seems wrong to me). Those are the integers a do= uble precision floating point numbers are able to represent precisely.

3. If you want to serialize precise floats you should do this as a JSON str= ing 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 fl= oat_of_string and the "%h" format string [3].

Best,

Daniel

[1]=C2=A0https://tools.ietf.org/html/rfc7159#secti= on-6
[2]=C2=A0http://erratique.ch/software/gg= /doc/Gg.Float.html#VALpp
[3]=C2=A0https://github.com/ocaml/ocaml/pull/268



--
Kind regards,
Viet
--001a11440268a13eab0555b0ab97--