caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oleg <oleg@okmij.org>
To: asai@is.ocha.ac.jp
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] string_of_float (0.1 +. 0.2)
Date: Thu, 16 Jun 2022 15:24:05 +0900	[thread overview]
Message-ID: <20220616062405.GA1555@Melchior.localnet> (raw)
In-Reply-To: <YqqLVsxjHWWiA51Y@pllab.is.ocha.ac.jp>


Actually the similar problem of accurately conveying floats also
occurs in MetaOCaml/Code generation. After all, what you are doing is
a sort of reflection.

First of all, accurate (lossless) printing of floats is a research
area in itself. The latest result is

  http://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf
  https://github.com/google/double-conversion

Perhaps some day it can be incorporated in OCaml, so that
string_of_float truly returns *the* printable representation of a float.

Hexademical printout (which is also supported in C) 
mentioned by Daniel Buenzli is another way -- provided we don't
actually have to look at the printed value, because we probably won't
understand it anyway.

In practice in our recent project, we settled on

let float : float -> float cde = fun x ->
  let str = if Float.is_integer x then string_of_float x else
            Printf.sprintf "%.17g" x

which seems to work well. At least, it solved the problems when the
results of our generated signal processing code differed slightly from
the results of the hand-written reference C code, due to slightly
different printed FP values in FP array initializers (filter
coefficients). We used to use string_of_float back then. With the
above float, the problem is solved.

I have to add that float above does not account for NaN, plus/minus
infinity and -0. So, the fully production code also has to add these
cases.


  reply	other threads:[~2022-06-16  6:24 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
2022-06-16  6:24       ` Oleg [this message]
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=20220616062405.GA1555@Melchior.localnet \
    --to=oleg@okmij.org \
    --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).