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: Mattias Engdegå rd <mattias@v...>
Subject: Re: [Caml-list] Strange behaviour of string_of_float
>My intent is to extract an ASCII representation of an OCaml float
>value so that it can be used to recreate *exactly* the same value, at
>least on the same architecture.

A somewhat more portable (and readable, maybe) representation of
floating-point numbers is in hex (a la C99). It is independent of the
precision and binary format used. Unfortunately, ocaml's Printf has
already appropriated %a for a different purpose, but it remains a good
option for those willing to do some manual work.

I have used it in the past to good effect in text-based interchange
formats between applications written in C.

Of course the decimal notation can unambiguously represent any
(binary) floating-point number, so that representation is fine if you
have confidence in the output and reading routines. See, for instance,
William Clinger's _How to Read Floating Point Numbers Accurately_
(http://ftp.ccs.neu.edu/pub/people/will/retrospective.pdf).
But decimal handling will always be a little slower.