Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* float printing
@ 2005-03-24 20:20 Christophe Raffalli
  2005-03-24 20:31 ` [Caml-list] " Ken Rose
  2005-03-24 20:39 ` Robert Roessler
  0 siblings, 2 replies; 3+ messages in thread
From: Christophe Raffalli @ 2005-03-24 20:20 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1060 bytes --]


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 ?

--
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature. The public key is
stored on www.keyserver.net
---------------------------------------------

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

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

* Re: [Caml-list] float printing
  2005-03-24 20:20 float printing Christophe Raffalli
@ 2005-03-24 20:31 ` Ken Rose
  2005-03-24 20:39 ` Robert Roessler
  1 sibling, 0 replies; 3+ messages in thread
From: Ken Rose @ 2005-03-24 20:31 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

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 ?

Maybe Int64.bits_of_float and Int64.float_of_bits will do what you need.

  - ken


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

* Re: [Caml-list] float printing
  2005-03-24 20:20 float printing Christophe Raffalli
  2005-03-24 20:31 ` [Caml-list] " Ken Rose
@ 2005-03-24 20:39 ` Robert Roessler
  1 sibling, 0 replies; 3+ messages in thread
From: Robert Roessler @ 2005-03-24 20:39 UTC (permalink / raw)
  To: Christophe Raffalli; +Cc: caml-list

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


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

end of thread, other threads:[~2005-03-24 20:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-24 20:20 float printing Christophe Raffalli
2005-03-24 20:31 ` [Caml-list] " Ken Rose
2005-03-24 20:39 ` Robert Roessler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox