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 773B27F98F for ; Tue, 1 Aug 2017 13:25:39 +0200 (CEST) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=frederic.bour@lakaban.net; spf=Pass smtp.mailfrom=frederic.bour@lakaban.net; spf=None smtp.helo=postmaster@mail.lakaban.net Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of frederic.bour@lakaban.net) identity=pra; client-ip=213.251.185.180; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="frederic.bour@lakaban.net"; x-sender="frederic.bour@lakaban.net"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of frederic.bour@lakaban.net designates 213.251.185.180 as permitted sender) identity=mailfrom; client-ip=213.251.185.180; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="frederic.bour@lakaban.net"; x-sender="frederic.bour@lakaban.net"; 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.lakaban.net) identity=helo; client-ip=213.251.185.180; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="frederic.bour@lakaban.net"; x-sender="postmaster@mail.lakaban.net"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3A/QbSqxAbp2WhRI6j0ZwhUyQJP3N1i/DPJgcQr6Af?= =?us-ascii?q?oPdwSPT6pcbcNUDSrc9gkEXOFd2CrakV26yO6+jJYi8p2d65qncMcZhBBVcuqP?= =?us-ascii?q?49uEgeOvODElDxN/XwbiY3T4xoXV5h+GynYwAOQJ6tL1LdrWev4jEMBx7xKRR6?= =?us-ascii?q?JvjvGo7Vks+7y/2+94fdbghMhzexe69+IAmrpgjNq8cahpdvJLwswRXTuHtIfO?= =?us-ascii?q?pWxWJsJV2Nmhv3+9m98p1+/SlOovwt78FPX7n0cKQ+VrxYES8pM3sp683xtBnM?= =?us-ascii?q?VhWA630BWWgLiBVIAgzF7BbnXpfttybxq+Rw1DWGMcDwULs5Xymp4aV2Rx/ykC?= =?us-ascii?q?oJKiA38G/XhMJzgqxUrh2uqB5jzIPPeo6ZKOBzc7nBcd4UR2dMWNtaWSxbAoO7?= =?us-ascii?q?aosCF+oPPfxCqIbgvVwOtxq+ChWxD+7yzT9Im2X21rA93uQvCwHJwhEvEMwUsH?= =?us-ascii?q?TVsNr5LrwdUeeozKnS1jjDa+lZ2TTn54fTaB8hp+uAXbRufsrN0EQiER7OgFuX?= =?us-ascii?q?qYzgJTyV1+INvnCV7uphVuKvinYnpB9srTiu3MggkI7Jhpgaylzc+iV5wZo1Jd?= =?us-ascii?q?2lSEFge9KrDJxQtyScOoBrQc0iW3lltDgnxrAIo5K3YigHxIk9yxLCaPGKc5KE?= =?us-ascii?q?7g/gWeqNOTt1h29pdKihixqs7USs0O3xWtOq3FpUoCdIlMTHuGoX2BzJ8MeHT+?= =?us-ascii?q?Nw/ke/1jaL0ADe8uRELlo1larfMZIhxqQ/lpkJvkvYBCD2nl/6jKmIeUU44uSo?= =?us-ascii?q?6uLnbav6ppKEKYN4lw/zPr4wlsG/HOg0KBUCUmub9OimybHu/lH1TK1PjvIsk6?= =?us-ascii?q?nZtJ7aJd4cpq68GwJV3Jwj5AiiADi4ytQYnGUIIEhedxKAkojpIUrDIPTiAve8?= =?us-ascii?q?mVujiy1kx/bcMr3nHJrNMmDPkK39crZl905c1A0zwMhD6J1OD7EBJOv/WkvwtN?= =?us-ascii?q?zDEh80KBe0wubiCNVlzIwSQ2OPAqmDMKPTq1CE/OwvI/PfLLMS7T30Lvxg4//1?= =?us-ascii?q?kVc4n0UcdO+nx8g5cne9S9p8okkYe3vnyv0GDH1C6gQkQeDnklyJFzRUemyaW6?= =?us-ascii?q?s/7zsyDsSgAJuVFdPlu6CIwCruRs4eXWtBEF3ZSXo=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0AbBgA4ZIBZ/7S5+9VdHgYMGQYMhBMDa?= =?us-ascii?q?iePAZB0iDONWg6CBC6ESk8ChF0YAQEBAQEBAQEBAQFqKIIzIoJEAQUjBBkBATg?= =?us-ascii?q?PCxgZAgwDAgIhJRETBgIBAReJfAMZDK1Ga4FsOieCYQEBBYQgDYQRAQoBAQEBA?= =?us-ascii?q?RoDBYMohVqCe4JXgWYQUwkgB4I2gmGfPTyHUIdnkCeHC4wfiVgfOIEKUy9ThF4?= =?us-ascii?q?qghR0YocOASSCGgEBAQ?= X-IPAS-Result: =?us-ascii?q?A0AbBgA4ZIBZ/7S5+9VdHgYMGQYMhBMDaiePAZB0iDONWg6?= =?us-ascii?q?CBC6ESk8ChF0YAQEBAQEBAQEBAQFqKIIzIoJEAQUjBBkBATgPCxgZAgwDAgIhJ?= =?us-ascii?q?RETBgIBAReJfAMZDK1Ga4FsOieCYQEBBYQgDYQRAQoBAQEBARoDBYMohVqCe4J?= =?us-ascii?q?XgWYQUwkgB4I2gmGfPTyHUIdnkCeHC4wfiVgfOIEKUy9ThF4qghR0YocOASSCG?= =?us-ascii?q?gEBAQ?= X-IronPort-AV: E=Sophos;i="5.41,306,1498514400"; d="scan'208,217";a="285702015" Received: from pepper.lakaban.net (HELO mail.lakaban.net) ([213.251.185.180]) by mail2-smtp-roc.national.inria.fr with ESMTP; 01 Aug 2017 13:25:38 +0200 Received: from [192.168.0.14] (ip-223.net-81-220-5.versailles.rev.numericable.fr [81.220.5.223]) (Authenticated sender: defre@ygg-drasil.fr) by mail.lakaban.net (Postfix) with ESMTPSA id 6CFA98A0187 for ; Tue, 1 Aug 2017 11:19:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lakaban.net; s=default; t=1501586345; bh=m1bwggSFsLB4+r27gtgyJaO7yts7YS92MqLj/K2YqU4=; h=Subject:To:References:From:Date:In-Reply-To:From; b=m+FWo27HVbX2/ua/7tvFAVvyA2H47PERffp356d17R57tB36cYPcYufKwA9U2fSAK 8aliaRbcrBnHJ0VoHHbayq3DnSNH8xGFw9yVGnGfrRJz9Evc63VhD6VvZ54S3LTmmJ iTFCNLc3dl/gnxJu5FblMZjFEkqJMj+t8MpMxIuM= To: caml-list@inria.fr References: From: =?UTF-8?B?RnLDqWTDqXJpYyBCb3Vy?= Message-ID: <45f24f0e-cb29-4d07-7e72-bee1610f1057@lakaban.net> Date: Tue, 1 Aug 2017 13:25:37 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------FAEF2A4BB29A87DE1FA2A70E" Content-Language: en-US Subject: Re: [Caml-list] Float precision in OCaml This is a multi-part message in MIME format. --------------FAEF2A4BB29A87DE1FA2A70E Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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 > 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 > > --------------FAEF2A4BB29A87DE1FA2A70E Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

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


--------------FAEF2A4BB29A87DE1FA2A70E--