caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Kenichi Asai <asai@is.ocha.ac.jp>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] string_of_float (0.1 +. 0.2)
Date: Wed, 15 Jun 2022 16:07:58 +0200	[thread overview]
Message-ID: <CAPFanBFf-ksPwj48=TZX+GzTOe5cM=cL_i_RnK+gjiRWNmyFtA@mail.gmail.com> (raw)
In-Reply-To: <Yqk9CiraXjKDfF51@pllab.is.ocha.ac.jp>

[-- Attachment #1: Type: text/plain, Size: 2049 bytes --]

Nathanaëlle Courant, with help from Julien Lepiller and myself, wrote a
rather complete interpreter/evaluator for OCaml parsetrees (in OCaml) as
part of the Camlboot project

  https://arxiv.org/abs/2202.09231
  https://github.com/Ekdohibs/camlboot
  https://github.com/Ekdohibs/camlboot/tree/master/interpreter

We take as input OCaml parsetrees, but we evaluate into a type of "value"
that we defined ourselves, and stores a "float" for floating-point numbers:

https://github.com/Ekdohibs/camlboot/blob/2692b14a4e685387194556e511fe23057d25c6c3/interpreter/data.ml#L46-L66

type value =
...
| Float of float
...

Kenichi, I don´t understand what your own constraints, but in general I
have the impression that "float" is better than "string" to represent
double values used for computation. "string" was meant to accurately
represent the source value and avoid any serialization/portability issue,
but those constraints are rather for data exchange.

On Wed, Jun 15, 2022 at 4:00 AM Kenichi Asai <asai@is.ocha.ac.jp> wrote:

> On OCaml 4.12.0 on M1 mac, I got:
>
> # 0.1 +. 0.2;;
> - : float = 0.300000000000000044
> # string_of_float (0.1 +. 0.2);;
> - : string = "0.3"
>
> Why don't I obtain "0.300000000000000044" here?
>
> Here is some background.  I am writing an OCaml interpreter that
> mimics most part of the original OCaml interpreter.  In the OCaml
> interpreter, 0.1 and 0.2 are represented as
>
> Pexp_constant (Pconst_float ("0.1", None))
> Pexp_constant (Pconst_float ("0.2", None))
>
> When I add these two numbers, I would have to execute
>
> let a = float_of_string "0.1"
> let b = float_of_string "0.2"
> let c = a +. b
> let d = string_of_float c
>
> and then return
>
> Pexp_constant (Pconst_float (d, None))
>
> At this point, however, since d is "0.3" instead of
> "0.300000000000000044" (even though c is 0.300000000000000044), I
> cannot return 0.300000000000000044 as a result.  How can I mimic the
> OCaml behavior?
>
> Sincerely,
>
> --
> Kenichi Asai
>

[-- Attachment #2: Type: text/html, Size: 3093 bytes --]

  parent reply	other threads:[~2022-06-15 14:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-15  1:59 Kenichi Asai
2022-06-15  6:22 ` Andreas Rossberg
2022-06-15  7:00   ` François Pottier
2022-06-15 14:07 ` Gabriel Scherer [this message]
2022-06-15 14:25   ` Daniel Bünzli
2022-06-16  1:45     ` Kenichi Asai
2022-06-16  6:24       ` Oleg
2022-06-16  9:01         ` Andreas Rossberg
2022-06-16  9:14           ` [Caml-list] unsubscribe Jean-Denis EIDEN JEAN-DENIS

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAPFanBFf-ksPwj48=TZX+GzTOe5cM=cL_i_RnK+gjiRWNmyFtA@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=asai@is.ocha.ac.jp \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).