Version française
Home     About     Download     Resources     Contact us    
Browse thread
Strange behaviour of string_of_float
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Paolo Donadeo <p.donadeo@g...>
Subject: Strange behaviour of string_of_float
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