caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Strange behaviour of string_of_float
Date: Mon, 23 Jun 2008 09:35:40 +0100	[thread overview]
Message-ID: <200806230935.40994.jon@ffconsultancy.com> (raw)
In-Reply-To: <20080622195831.GA22384@annexia.org>

On Sunday 22 June 2008 20:58:31 Richard Jones wrote:
> On Sun, Jun 22, 2008 at 06:56:22PM +0200, Paolo Donadeo wrote:
> > string_of_float is not the inverse of float_of_string, at least in
> > this example.
>
> Yes, you wouldn't expect it to be, because the string is an
> approximate base 10 representation of the float...

That is not true. All finite floats have exact finite decimal representations. 
So it is perfectly reasonable to expect the conversions to recover the 
original number exactly.

As Paolo has shown, OCaml's current string_of_float function is approximate. 
The accuracy of this routine is unspecified but a quick test indicates that 
it is simply printing too few digits to be exact:

# string_of_float pi;;
- : string = "3.14159265359"

Fortunately, you can ask sprintf to generate a sufficiently accurate result:

# open Printf;;
# sprintf "%0.17g" pi;;
- : string = "3.1415926535897931"

The float_of_string function does then recover the number exactly in this 
case:

# float_of_string "3.1415926535897931" -. pi;;
- : float = 0.

Also, you should keep in mind in this context that calculations may be done 
with 80-bit float arithmetic in registers or truncated to 64-bits when stored 
to memory. Moreover, OCaml's bytecode and native code targets can behave 
differently in this context. I do not believe that is a problem with Paolo's 
code here though.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e


  parent reply	other threads:[~2008-06-23  9:06 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-22 16:56 Paolo Donadeo
2008-06-22 19:58 ` [Caml-list] " Richard Jones
2008-06-22 20:45   ` Paolo Donadeo
2008-06-23  1:25     ` Brian Hurt
2008-06-23  7:50       ` Paolo Donadeo
2008-06-23  8:32     ` Mattias Engdegård
2008-06-23  8:50       ` Olivier Andrieu
2008-06-23  8:35   ` Jon Harrop [this message]
2008-06-22 20:32 ` Daniel Bünzli
2008-06-22 20:50   ` Paolo Donadeo
2008-06-23  8:45     ` David Allsopp
2008-06-23  8:55       ` Olivier Andrieu
2008-06-23 12:06         ` David Allsopp
2008-06-23  1:06   ` Brian Hurt
2008-06-23  7:58     ` Xavier Leroy

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=200806230935.40994.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.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).