caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Kenichi Asai <asai@is.ocha.ac.jp>
To: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
Cc: Gabriel Scherer <gabriel.scherer@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] string_of_float (0.1 +. 0.2)
Date: Thu, 16 Jun 2022 10:45:58 +0900	[thread overview]
Message-ID: <YqqLVsxjHWWiA51Y@pllab.is.ocha.ac.jp> (raw)
In-Reply-To: <etPan.62a9ebf6.462d9d5f.12f3c@erratique.ch>

Thank you all for the information.

> 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

Yes, I read the programming paper.  That's an interesting project.

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

I am developing an OCaml stepper that executes an OCaml program step
by step.  From the OCaml parsetree for

let a = 0.1 +. 0.2 +. 0.4

I want an OCaml parsetree for

let a = 0.3 +. 0.4

or

let a = 0.300000000000000044 +. 0.4

if this is what OCaml uses internally.  I want to produce an OCaml
parsetree rather than my own parsetree that maintains float as is,
because I could then reuse pretty printer of OCaml.

From Andreas' e-mail, I understand what's happening.  (Thank you!)  I
thought I would use Printf.sprintf "%.18g" in place of string_of_float,
but Daniel's e-mail made me think it could be insufficient, because
even if I use .18 (or .24), it is still an approximation of the float.
(Am I correct?)  I tried to use `Format.sprintf "%h"` in place of
string_of_float, but the OCaml pretty printer produces

let a = 0x1.3333333333334p-2 +. 0.4

which is not suitable for a stepper used by novice programmers.  For
now, I think I will use Printf.sprintf "%.18g" and see if students see
any difference between OCaml execution and stepper execution.

I agree with Francois that it would be nice if the OCaml manual could
mention a potential loss of information.  I first thought that
replacing "%.12g" with "%.18g" solves the problem, but "the" string
representation of a float turned out to be more complicated than I
thought.

Thank you all for the discussion!

Sincerely,

-- 
Kenichi Asai

  reply	other threads:[~2022-06-16  1:47 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
2022-06-15 14:25   ` Daniel Bünzli
2022-06-16  1:45     ` Kenichi Asai [this message]
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=YqqLVsxjHWWiA51Y@pllab.is.ocha.ac.jp \
    --to=asai@is.ocha.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=daniel.buenzli@erratique.ch \
    --cc=gabriel.scherer@gmail.com \
    /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).