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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 569BE7F98F for ; Tue, 1 Aug 2017 13:42:57 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=vietlq85@gmail.com; spf=Pass smtp.mailfrom=vietlq85@gmail.com; spf=None smtp.helo=postmaster@mail-yw0-f181.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of vietlq85@gmail.com) identity=pra; client-ip=209.85.161.181; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="vietlq85@gmail.com"; x-sender="vietlq85@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of vietlq85@gmail.com designates 209.85.161.181 as permitted sender) identity=mailfrom; client-ip=209.85.161.181; receiver=mail3-smtp-sop.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 (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-yw0-f181.google.com) identity=helo; client-ip=209.85.161.181; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="vietlq85@gmail.com"; x-sender="postmaster@mail-yw0-f181.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AtAuqKB1KqEVVVMcBsmDT+DRfVm0co7zxezQtwd8Z?= =?us-ascii?q?se0eLPad9pjvdHbS+e9qxAeQG96Ku7Qc06L/iOPJYSQ4+5GPsXQPItRndiQuro?= =?us-ascii?q?EopTEmG9OPEkbhLfTnPGQQFcVGU0J5rTngaRAGUMnxaEfPrXKs8DUcBgvwNRZv?= =?us-ascii?q?JuTyB4Xek9m72/q89pDXYAhEniaxba9vJxiqsAvds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us-ascii?q?ox?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0BkAADCZ4BZhrWhVdFdHgYMGQYMgwKBE?= =?us-ascii?q?W0nhSuYXoFsjWOIKg6CBCiFHwKEHj8YAQEBAQEBAQEBAQESAQEBCAsLCCgvgjM?= =?us-ascii?q?kAYJAAQEBAQIBIwQZARsdAQMBCwYFCw0ZAg8CAiEBAREBBQEcBhOKFgEDDQihc?= =?us-ascii?q?j+MCoFsGAUBHIMJBYNXChknDVZHgnQBAQEBAQUBAQEBAQEBGQIBBRKDFoIChlO?= =?us-ascii?q?CV4FmAwEMBgFMKQeCNoJhBYEtAQGeCTIIAQGBUAqFdodnhHGSQYwfiBAzgRUfg?= =?us-ascii?q?TcLd14aXIQSDxAMgWdANgFhhw4BDReCGgEBAQ?= X-IPAS-Result: =?us-ascii?q?A0BkAADCZ4BZhrWhVdFdHgYMGQYMgwKBEW0nhSuYXoFsjWO?= =?us-ascii?q?IKg6CBCiFHwKEHj8YAQEBAQEBAQEBAQESAQEBCAsLCCgvgjMkAYJAAQEBAQIBI?= =?us-ascii?q?wQZARsdAQMBCwYFCw0ZAg8CAiEBAREBBQEcBhOKFgEDDQihcj+MCoFsGAUBHIM?= =?us-ascii?q?JBYNXChknDVZHgnQBAQEBAQUBAQEBAQEBGQIBBRKDFoIChlOCV4FmAwEMBgFMK?= =?us-ascii?q?QeCNoJhBYEtAQGeCTIIAQGBUAqFdodnhHGSQYwfiBAzgRUfgTcLd14aXIQSDxA?= =?us-ascii?q?MgWdANgFhhw4BDReCGgEBAQ?= X-IronPort-AV: E=Sophos;i="5.41,306,1498514400"; d="scan'208,217";a="233397350" Received: from mail-yw0-f181.google.com ([209.85.161.181]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 01 Aug 2017 13:42:56 +0200 Received: by mail-yw0-f181.google.com with SMTP id s143so8082847ywg.1 for ; Tue, 01 Aug 2017 04:42:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=UIR/ZV1izFwPn80pXrMx2VuV7OCAiaUV6s+7wKVr25Q=; b=cCVj6guMfFBGwTA3Cx4O2k+UWQp0w42Kz0DhXVkI6wOPnvdGzZuNdKWhZkD+Ft7wyi Okn4CTKvlPb1wQihoCD+6td89RB7sL45m6aJQ/k77QrkRyKR/J3ekR4S7pu2OH1B1j5z laj4d2g0cnwL+6/gLuDNPk3E+I57a5K3aMNy+xNSjHANiMZMSlgaEXsargnVNKHREbrG uVqvkHtu3yAj1Q0Mx8ddCpgOSbG5BtCVKB1sB/99u9hRhYb/oKt+D+94opkaUgc1EJsI /Ol7RBfmN7LBjZkC/6m9vzxuMhVkVp12qimMEvqVxc70z0fQC/V0j7IjkgijAD4VrQ/P UM4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=UIR/ZV1izFwPn80pXrMx2VuV7OCAiaUV6s+7wKVr25Q=; b=axBb7QoyyJdnBstUBrSjyfj8azt2djZy7n6HXYttaR+V1m7kUOWBTfSL98tId/pUME ls3kXl8CImZ/4SX954uOGK8UI2c6Uh/+88uByywCQKbC+e/NldfekN1AnHgMOjDw/WzD BOYAyddz6ZmmljkXZ4753GyrX3Rh/ImURgXSW8wIeCEcOzV0vvQm2FGq7tTBgdy8Zpq1 FVlNT/KimQI/hDnLzgx7q6EB9MUo07yEgXKxwlnQTQjH8RTD6k+6l9Nqh/WwZIbkNK+6 86OoODbrKrTI8Jz+l0p/PPATUZ04VKze9gl9oauBSkRBfi9STRaRbCq+A+MpHqIqSTCU eMPg== X-Gm-Message-State: AIVw113hIAfY76VKgX79DkhzhJoTZ/N0jRAiqSkNlgTWao+1EhSrKoih VhLF6os5aRYi8AtVXHkq3Irthpq++iMl X-Received: by 10.13.213.72 with SMTP id x69mr16249448ywd.362.1501587774696; Tue, 01 Aug 2017 04:42:54 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Viet Le Date: Tue, 01 Aug 2017 11:42:43 +0000 Message-ID: To: =?UTF-8?Q?Nicol=C3=A1s_Ojeda_B=C3=A4r?= Cc: OCaml Mailing List Content-Type: multipart/alternative; boundary="001a114fd442e8f3010555afa523" Subject: Re: [Caml-list] Float precision in OCaml --001a114fd442e8f3010555afa523 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=C3=A1s Ojeda B=C3=A4r 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 =3D 1.00000000001e-312 >> >> 11 zeros in between >> # 1.000000000001e-312 ;; >> - : float =3D 1.00000000000341e-312 >> >> # 5e-324 ;; >> - : float =3D 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 --001a114fd442e8f3010555afa523 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks Nicolas. I did check against C++ before writing th= e email and I can see OCaml is consistent with C++. I'm aware about IEE= E 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 A= ug 2017 at 12:18, Nicol=C3=A1s Ojeda B=C3=A4r <nicolas.ojeda.bar@lexifi.com> wrote:
Dear Viet,

I am not sure t= his is an issue with OCaml (as you can verify using your favourite C compil= er).=C2=A0 Rather, I think IEEE 754 double-precision binary floating-point = numbers can only represent numbers between 10^{-308} and 10^308 with full d= ecimal 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 smal= l float values:

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

11 zeros in between
# 1.000000000001e-312 ;;
- : float =3D 1.00000000000341e-312<= /div>

# 5e-324= ;;
- : float =3D 4.94= 065645841e-324


I ha= ven'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=C2=A01.00000000001e-312. To use JSON as precise serializer, it would= be necessary to preserve accuracy.

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

For values smaller than the limit above, s= hould 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 reg= ards,
Viet

--
Kind re= gards,
Viet
--001a114fd442e8f3010555afa523--