caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Strange behaviour of string_of_float
@ 2008-06-22 16:56 Paolo Donadeo
  2008-06-22 19:58 ` [Caml-list] " Richard Jones
  2008-06-22 20:32 ` Daniel Bünzli
  0 siblings, 2 replies; 15+ messages in thread
From: Paolo Donadeo @ 2008-06-22 16:56 UTC (permalink / raw)
  To: caml-list caml-list

Today I noticed this strange behaviour of string_of_float:

Let's start with:

# let pi = 4.0 *. atan 1.0;;
val pi : float = 3.14159265358979312
# let (|>) x f = f x;;
val ( |> ) : 'a -> ('a -> 'b) -> 'b = <fun>

Ok, I want to serialize pi:

# (pi |> string_of_float |> float_of_string) -. pi;;
- : float = 2.06945571790129179e-13

string_of_float is not the inverse of float_of_string, at least in this example.

Is this correct? It's not a problem at all, I used this workaround:

# let my_string_of_float = Printf.sprintf "%.1000g";;
val my_string_of_float : float -> string = <fun>

# (pi |> my_string_of_float |> float_of_string) -. pi;;
- : float = 0.


-- 
Paolo
~
~
:wq


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2008-06-23 12:06 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-22 16:56 Strange behaviour of string_of_float 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
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

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