Version française
Home     About     Download     Resources     Contact us    
Browse thread
float printing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Robert Roessler <roessler@r...>
Subject: Re: [Caml-list] float printing
Christophe Raffalli wrote:

> I have a small problem to read and write float in a file. I use the
> format library and there is no way to write/read a float in hexadecimal
> to be sure you read exactly the same float back from the file (a n bits
> mantisse float may need n decimals to have an exact representation.
> Just compute 1/2^n and count decimals).
> 
> The only way is to convert the float to string via Printf and then print
> the string with Format.print_string.
> 
> Don't you think Pervasives and format should have a write/print_hexfloat
> and Pervasives should have an input_hexfloat function ?

How about the Int64 module?

val bits_of_float : float -> int64

This gives the exact "bits" for the IEEE double-precision float.

val float_of_bits : int64 -> float

This "reconstitutes" the float from the supplied 64-bit "integer".

There are also Int32-based versions of these, but with obviously lower 
precision - and the rounding method used to obtain Int32.bits_of_float 
is not specified.

Robert Roessler
roessler@rftp.com
http://www.rftp.com