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 EC0BB7F98F for ; Tue, 1 Aug 2017 13:18:38 +0200 (CEST) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=nicolas.ojeda.bar@lexifi.com; spf=SoftFail smtp.mailfrom=nicolas.ojeda.bar@lexifi.com; spf=None smtp.helo=postmaster@vrout10.yaziba.net Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of nicolas.ojeda.bar@lexifi.com) identity=pra; client-ip=185.56.204.35; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="nicolas.ojeda.bar@lexifi.com"; x-conformance=sidf_compatible Received-SPF: SoftFail (mail2-smtp-roc.national.inria.fr: domain of nicolas.ojeda.bar@lexifi.com is inclined to not designate 185.56.204.35 as permitted sender) identity=mailfrom; client-ip=185.56.204.35; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="nicolas.ojeda.bar@lexifi.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@vrout10.yaziba.net) identity=helo; client-ip=185.56.204.35; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="nicolas.ojeda.bar@lexifi.com"; x-sender="postmaster@vrout10.yaziba.net"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3At9weKBwRw7FyXEPXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?2uoQIJqq85mqBkHD//Il1AaPBtSLraocw8Pt8InYEVQa5piAtH1QOLdtbDQizf?= =?us-ascii?q?ssogo7HcSeAlf6JvO5JwYzHcBFSUM3tyrjaRs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us-ascii?q?jXIigl8cfayp3YMNZ3yiH/RmJV+ZYXv2jdsbH2cKpBIyTOntiFKfTzFef22yX6?= =?us-ascii?q?cg5jEjD4KmEJvDFciRh+m/3SqnVrlbLjRYC12QFW64Kq2LXv4NbGSZJco3wRIe?= =?us-ascii?q?Ur30bY4q0hUnrzjdybdrJ93+djdQ4ZTi399x7vaVkhM/+TUyBc2H+2WAX2B6ny?= =?us-ascii?q?UDQDpgj/M3mlB01lrWifswuPdfD9EGoqoRCgo=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0DeAAA/Y4BZfSPMOLldHRgHDRgHhAZtJ?= =?us-ascii?q?weFJIlWkHQRiCKIK4UvDoIEKINAgV8ChBdGGAEBAQEBAQEBAQEBEgEBCRYIV4I?= =?us-ascii?q?zJAGCQQECAgEjBFIFCwsEByYCDwICIQESAQUBHAYTihcDDQgEAaF4P4wKgWw6h?= =?us-ascii?q?y8NhBEBAQEBAQUBAQEBAQEdBYMoiFWCV4FmEHwHgjaCYQEEnzg8h1CHZ4RxkkG?= =?us-ascii?q?MH4gQFB+BEwIfgUJ3XhoGhD6CPnRihw4BJIIaAQEB?= X-IPAS-Result: =?us-ascii?q?A0DeAAA/Y4BZfSPMOLldHRgHDRgHhAZtJweFJIlWkHQRiCK?= =?us-ascii?q?IK4UvDoIEKINAgV8ChBdGGAEBAQEBAQEBAQEBEgEBCRYIV4IzJAGCQQECAgEjB?= =?us-ascii?q?FIFCwsEByYCDwICIQESAQUBHAYTihcDDQgEAaF4P4wKgWw6hy8NhBEBAQEBAQU?= =?us-ascii?q?BAQEBAQEdBYMoiFWCV4FmEHwHgjaCYQEEnzg8h1CHZ4RxkkGMH4gQFB+BEwIfg?= =?us-ascii?q?UJ3XhoGhD6CPnRihw4BJIIaAQEB?= X-IronPort-AV: E=Sophos;i="5.41,306,1498514400"; d="scan'208,217";a="285701141" Received: from vrout30-bl.yaziba.net (HELO vrout10.yaziba.net) ([185.56.204.35]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 01 Aug 2017 13:18:38 +0200 Received: from mtaout10.int.yaziba.net (mtaout10.int.yaziba.net [10.4.20.36]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by vrout10.yaziba.net (mx10.yaziba.net) with ESMTPS id 97DD652184 for ; Tue, 1 Aug 2017 13:18:37 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mtaout10.int.yaziba.net (Postfix) with ESMTP id 98E9B160152 for ; Tue, 1 Aug 2017 13:18:37 +0200 (CEST) X-Virus-Scanned: amavisd-new at Received: from mtaout10.int.yaziba.net ([127.0.0.1]) by localhost (mtaout10.int.yaziba.net [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id huXVrtgeJZ4l for ; Tue, 1 Aug 2017 13:18:37 +0200 (CEST) Received: from mail-oi0-f46.google.com (mail-oi0-f46.google.com [209.85.218.46]) by mtaout10.int.yaziba.net (Postfix) with ESMTPSA id 4DF111600FF for ; Tue, 1 Aug 2017 13:18:37 +0200 (CEST) Received: by mail-oi0-f46.google.com with SMTP id x3so12098957oia.1 for ; Tue, 01 Aug 2017 04:18:37 -0700 (PDT) X-Gm-Message-State: AIVw110abHjWpfhjnhwjNoi4A2ZvPhpY15/kESAa3jwfviV33rsSpwdE PtluDXq928F6rBC8pBZXHgGsV2U7og== X-Received: by 10.202.235.140 with SMTP id j134mr15503308oih.281.1501586316049; Tue, 01 Aug 2017 04:18:36 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.142.20 with HTTP; Tue, 1 Aug 2017 04:18:15 -0700 (PDT) In-Reply-To: References: From: =?UTF-8?Q?Nicol=C3=A1s_Ojeda_B=C3=A4r?= Date: Tue, 1 Aug 2017 13:18:15 +0200 X-Gmail-Original-Message-ID: Message-ID: To: Viet Le Cc: OCaml Mailing List Content-Type: multipart/alternative; boundary="001a113ce37ef7c64c0555af4ec8" X-DRWEB-SCAN: ok X-VRSPAM-SCORE: -100 X-VRSPAM-STATE: legit X-VRSPAM-CAUSE: gggruggvucftvghtrhhoucdtuddrfeelkedrieelgdefgecutefuodetggdotefrucfrrhhofhhilhgvmecuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepjghfhfffkffuvfgtsegrtderredttdejnecuhfhrohhmpefpihgtohhljohspgfqjhgvuggrpgeumohruceonhhitgholhgrshdrohhjvggurgdrsggrrheslhgvgihifhhirdgtohhmqeenucffohhmrghinhepghhithhhuhgsrdgtohhmnecukfhppedvtdelrdekhedrvddukedrgeeinecurfgrrhgrmhepmhhouggvpehsmhhtphhouhht X-VRSPAM-EXTCAUSE: mhhouggvpehsmhhtphhouhht Subject: Re: [Caml-list] Float precision in OCaml --001a113ce37ef7c64c0555af4ec8 Content-Type: text/plain; charset="UTF-8" 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 > --001a113ce37ef7c64c0555af4ec8 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Dear Viet,

I am not sure this is an issue with= OCaml (as you can verify using your favourite C compiler).=C2=A0 Rather, I= think IEEE 754 double-precision binary floating-point numbers can only rep= resent numbers between 10^{-308} and 10^308 with full decimal digits precis= ion. Numbers smaller than that can only be represented with reduced precisi= on.

Best wishes,
Nicolas

On Tue, Aug 1, 2017 at 12:4= 7 PM, Viet Le <vietlq85@gmail.com> wrote:
Hi all,

I&#= 39;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-31= 2

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

# 5= e-324 ;;
- : float =3D= 4.94065645841e-324


I haven't found precise limit, but as a rule of thumb (not preci= se), for a positive float value to keep its precision, it sho= uld not be smaller than=C2=A01.00000000001e-312. To use JSON as precise serializer, it = would be necessary to preserve accuracy.

I checked=C2=A0<= a href=3D"https://github.com/ocaml/Zarith" target=3D"_blank">https://github= .com/ocaml/Zarith and it supports only big int & quotients, no= t floating point.

For values smaller than the limi= t above, should I just treat as 2 values: (normalized: float, exponent: flo= at), so we will have:

5e-324 -> (5, -324)

Com= ments and suggestions are appreciated. Thanks.
=

--
Kind regards,
Vi= et

--001a113ce37ef7c64c0555af4ec8--