From: "David Allsopp" <dra-news@metastack.com>
To: "'caml-list caml-list'" <caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] Strange behaviour of string_of_float
Date: Mon, 23 Jun 2008 09:45:04 +0100 [thread overview]
Message-ID: <6975022428ED405199F4BC4DE14D970B@countertenor> (raw)
In-Reply-To: <4b5157c30806221350xe608e08v1ed2d9a1e6b9d744@mail.gmail.com>
> > Richard gave you the reason.
Erm, please correct me if I'm wrong but every single possible floating point
value (on the same architecture) has a string representation that will be
reparsed to the same floating point value (on the same architecture). It's
the reverse that isn't true because floating point numbers are only an
approximation.
> > If you can serialize to binary, you can acheive what you want by
> > serializing the 64 bits integers you get with Int64.bits_of_float and
> > applying Int64.float_of_bits to the integers you deserialize.
>
> Just posted a useless message :-)
>
> This is *exactly* what I was searching for, thanks Daniel.
This is of course a better, more reliable and faster way of serialising, but
the real cause for your original spurious result is down to how
string_of_float is defined in pervasives.ml:
# pi;;
- : float = 3.1415926535897931
# string_of_float pi;;
- : string = "3.14159265359"
In other words, (pi |> string_of_float |> float_of_string) is never going to
be equal to your original pi. For some reason, string_of_float is defined
as:
let string_of_float f = valid_float_lexem (format_float "%.12g" f);;
Perhaps Xavier can say why it's only "%.12g" in the format (I imagine
there's a historical reason) but if you increase it to 16 then you'll get
the answer you expected (0.). All that said, the values given by
string_of_float cannot always be fed back to float_of_string anyway (e.g.
float_of_string (string_of_float nan))
David
next prev parent reply other threads:[~2008-06-23 8:45 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
2008-06-22 20:32 ` Daniel Bünzli
2008-06-22 20:50 ` Paolo Donadeo
2008-06-23 8:45 ` David Allsopp [this message]
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=6975022428ED405199F4BC4DE14D970B@countertenor \
--to=dra-news@metastack.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