From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 519147F98F for ; Tue, 1 Aug 2017 13:45:32 +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-f169.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.169; 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.169 as permitted sender) identity=mailfrom; client-ip=209.85.161.169; 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-f169.google.com) identity=helo; client-ip=209.85.161.169; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="vietlq85@gmail.com"; x-sender="postmaster@mail-yw0-f169.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AnVGBxhJ0sg6V56Cl0tmcpTZWNBhigK39O0sv0rFi?= =?us-ascii?q?tYgeKPvxwZ3uMQTl6Ol3ixeRBMOAuqIC07KempujcFRI2YyGvnEGfc4EfD4+ou?= =?us-ascii?q?JSoTYdBtWYA1bwNv/gYn9yNs1DUFh44yPzahANS47xaFLIv3K98yMZFAnhOgpp?= =?us-ascii?q?POT1HZPZg9iq2+yo9ZDeZwZFiCChbb9uMR67sRjfus4KjIV4N60/0AHJonxGe+?= =?us-ascii?q?RXwWNnO1eelAvi68mz4ZBu7T1et+ou+MBcX6r6eb84TaFDAzQ9L281/szrugLd?= =?us-ascii?q?QgaJ+3ART38ZkhtMAwjC8RH6QpL8uTb0u+ZhxCWXO9D9QKsqUjq+8ahkVB7oiD?= =?us-ascii?q?8GNzEn9mHXltdwh79frB64uhBz35LYbISTOfFjfK3SYMkaSHJOUcZfVSNPAo2y?= =?us-ascii?q?YYgSAeQfIelVtJPyq0cUoBakGQWgGOHixzlVjXH2x6061OEhHBnJ0wwhAd0OrG?= =?us-ascii?q?nfotPrO6wPVu210qjIwivfb/NSwDfw64jFfwsuofGJR71wcM7RxVMzGAPCi1Wd?= =?us-ascii?q?sIroNC6b2OQKtmiU9etgVeS3hm8pqgFxoTmvxt8yhYbUh4Ia0ErI9SJjwIY6Pd?= =?us-ascii?q?G4R097YcKkEJtNrC6WLZd5QsQnQ2xupS00yaUGtIalcCQWzJkr3R3SZvydf4SV?= =?us-ascii?q?4x/vSfydLSp5iX9hfr+0mgy8/lK6yuLmU8m5yFZKoTRBktnLrn0N0gbc6smDSv?= =?us-ascii?q?dk/kag1yqD2xnd6u1ZIE05kbDXK5Emwr43mZoTtVrMEjXql0Xxia+abkQk+u62?= =?us-ascii?q?5OT7erjquIOQOotuhgz9MqkigNGzDOUlPgQUQmSW+OCx2KXm/ULjQbVKivM2kr?= =?us-ascii?q?PesJDfPckbuK+5DBFJ0oYn8hayDDer3c8XnXkCNl1FeRaHg5L1NFHJJfD0Fe2/?= =?us-ascii?q?jEi0kDd32/DGOaXsDYnXIXjGlLftZLJ9609HyAov1t1f/JJVCrQZIP3pQEPxtd?= =?us-ascii?q?rYDgU4MwOu2ernBs99hcsiXjehGC6VsbnTuhei4f81a72CfoIZtSz8IL4v4OP0?= =?us-ascii?q?pXswmlYXdK/v25YLPjTwFf1jJwCdYGHwqtYHC2YD+AQkH8Lwj1jXbz9eez6WUu?= =?us-ascii?q?po+DA2FIa/XdrrSYWkgbjH1yC+SM4FLltaA0yBRC+7P76PXO0BPWfLepds?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0BkAAAzaYBZhqmhVdFdHgYMGQYMhBNtJ?= =?us-ascii?q?4MygXmYXoFsjWOIKg6CBC6DOoEQTwKEHj8YAQEBAQEBAQEBAQESAQEBCAsLCCg?= =?us-ascii?q?vgjMkAYJAAQEBAQIBDBcEGQEbHgMBCwYDAgsNGQIPAgIhAQERAQUBHAYBEggTi?= =?us-ascii?q?XsBAw0IEJA9kRs/jAqBbBgFARwngmIFg1cKGScNVkeCdAEBAQEBBQEBAQEBAQE?= =?us-ascii?q?BGAIBBRKDFoIChlOCV4FmEFMpB4I2gmEFgS0BAZ4JMggBAYFQCoV2h2eEcZJBj?= =?us-ascii?q?B+IEDOBFR+BQndeGlyEEh+Bc0A2YocOASSCGgEBAQ?= X-IPAS-Result: =?us-ascii?q?A0BkAAAzaYBZhqmhVdFdHgYMGQYMhBNtJ4MygXmYXoFsjWO?= =?us-ascii?q?IKg6CBC6DOoEQTwKEHj8YAQEBAQEBAQEBAQESAQEBCAsLCCgvgjMkAYJAAQEBA?= =?us-ascii?q?QIBDBcEGQEbHgMBCwYDAgsNGQIPAgIhAQERAQUBHAYBEggTiXsBAw0IEJA9kRs?= =?us-ascii?q?/jAqBbBgFARwngmIFg1cKGScNVkeCdAEBAQEBBQEBAQEBAQEBGAIBBRKDFoICh?= =?us-ascii?q?lOCV4FmEFMpB4I2gmEFgS0BAZ4JMggBAYFQCoV2h2eEcZJBjB+IEDOBFR+BQnd?= =?us-ascii?q?eGlyEEh+Bc0A2YocOASSCGgEBAQ?= X-IronPort-AV: E=Sophos;i="5.41,306,1498514400"; d="scan'208,217";a="233397647" Received: from mail-yw0-f169.google.com ([209.85.161.169]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 01 Aug 2017 13:45:30 +0200 Received: by mail-yw0-f169.google.com with SMTP id l82so8065276ywc.2 for ; Tue, 01 Aug 2017 04:45:30 -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; bh=adNZ+BcA63aMv+UI3ynrV6lJWoJ6o+FQo4rI2BylM6M=; b=UiCIsQS9oPFRsrDytE7hZUVx7NCP12IQYk/NAZUVs/Bjkjr1IMFJK5jTBI3G8zdXPr +wjY6hXwR/Hj4s5s3C1TuYf4smIyXxohIIcVcEiZjGzunf885Sq7eGeS48YRqc/UC4h5 RQwzLJnbkqmdMMHQMLL7ZUGWJq7X6iKltZnzwynzTzPvjWdFeSuW4W8nrzOmflaEdSQq yJX8otC66Tn1cEsFQwVzf4gXGuoknTxm3vOTIZkUTgyq/+5v3tipTe1hXvRJQqWeNqD6 +JobK2nOZGvaFU9s4GxVB+nmxX3ukC9ytL/H01LUz10OKZlUfE/eHoHZjHiaBS0IRNsR YqRQ== 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; bh=adNZ+BcA63aMv+UI3ynrV6lJWoJ6o+FQo4rI2BylM6M=; b=gwAlPX+Lvfm+xo2thiMCGhEtnlX0vj57VAkoo2MRhOLqFdSU3jB3ldKri1Uu89nhNt UfdgDbvwPJhS+3tDhRFOmEFFRBKL4L4RuD9uaKgde24GFPb3Z6O0mF6LdwnzRFrazjkH /AhvkQ3HYL6VM+t5jeyVad2RC7dp2KDapeCXyQz+ZBt47qPbu+mAEHjC51RFxk+CV0oy 1T3s640ZpiX8DJhLotF1vnugzQzplETbRmIKWXMfVXTp5nJe59Hs2XwZojTuWZv2zOeX 7/Lk3/SvvG8/ZdZ/1logRDdRrAmemjzp37kCJAIvC5HFMvr6U304wLwJB+1glyVXFI3F vRNw== X-Gm-Message-State: AIVw1111ECrl/gbxNeedjeFJSaV+lTewfLg9xRdQITsMEUOGtZKNgmqd 4GPsUQKlIcMCTOlM71aBKAS7/HGOzGA3 X-Received: by 10.129.172.19 with SMTP id k19mr14157108ywh.264.1501587929541; Tue, 01 Aug 2017 04:45:29 -0700 (PDT) MIME-Version: 1.0 References: <45f24f0e-cb29-4d07-7e72-bee1610f1057@lakaban.net> In-Reply-To: <45f24f0e-cb29-4d07-7e72-bee1610f1057@lakaban.net> From: Viet Le Date: Tue, 01 Aug 2017 11:45:18 +0000 Message-ID: To: =?UTF-8?B?RnLDqWTDqXJpYyBCb3Vy?= , caml-list@inria.fr Content-Type: multipart/alternative; boundary="f403045e481823b6250555afaf3f" Subject: Re: [Caml-list] Float precision in OCaml --f403045e481823b6250555afaf3f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks Frederic. I will relax the condition for now as I don't know practical use for such need yet. On Tue, 1 Aug 2017 at 12:25, Fr=C3=A9d=C3=A9ric Bour wrote: > 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#D= ouble-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=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 --f403045e481823b6250555afaf3f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks Frederic. I will relax the condition for now = as I don't know practical use for such need yet.

On Tue, 1 Aug 2017 at 12:25, Fr=C3=A9d=C3=A9ric Bour <= ;frederic.bour@lakaban.net= > wrote:
=20=20 =20=20=20=20 =20=20

The Doubledouble[1] module from my grenier library implements 106-bit precision floating point. See https://en.wikipedia.org/wiki/Quadruple-prec= ision_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 t= hem as IEEE 754 double.

[1]: https://github.com/let-def/grenier/blob/master/doubledouble/dou= bledouble.mli


On 01/08/2017 13:1= 8, 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).=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 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 =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=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, 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
--f403045e481823b6250555afaf3f--