Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] float pretty-printing precision, once more.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: jeanmarc.eber@l...
Subject: [Caml-list] float pretty-printing precision, once more.
caml 3.06+1:  
# let f = 1. /. 86400.;;   
val f : float = 1.15740740741e-05   
# let s = string_of_float f;;   
val s : string = "1.15740740741e-05"   
# let f1 = float_of_string s;;   
val f1 : float = 1.15740740741e-05   
# f1 = f;;   
- : bool = false   
# f1 -. f;;   
- : float = 2.59259844496e-17   
This situation may be understandable, but is unfortunate.  
Disclaimer: I'm not a specialist of the IEEE float format.  
Do I have at hand, at least on an architecture supporting the IEEE format, a  
function that pretty-prints any valid float value (by valid I mean that I  
exclude the "special" values like NaN, infinity, etc.) so that  
float_of_string applied to the resulting string returns my initial value,  
or, at least, a value that, if substracted from my initial one, returns  
zero ?  
In fact, my question goes a little bit further, as it concerns indeed the  
parsing of floats in the caml compiler (that uses internally float_of_string  
if I'm correct).  
Suppose you calculate somewhere (with an caml program, say) a float  
constant (such a calculation may last for hours!), and you want after  
obtaining the result to *generate* a caml source using this calculated  
value. You will probably generate something like  
let my_const = <a float text representation>  
But my example shows that you are loosing precision and accuracy if you  
just use string_of_float. 
Of course the goal is to incorporate this value in a caml source, not  
to read it in binary form from a file (that would be easy!).  
Do anybody know a solution to my problem ?  
Jean-Marc Eber  
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: