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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Monniaux <David.Monniaux@e...>
Subject: Re: [Caml-list] float rounding
Xavier Leroy a écrit :
>> I'm using Ocaml for an interval arithmetic application.  I"m  curious
>> about what the Ocaml parser/compiler does to float constants.
>>
>> - For the native-code compiler ocamlopt, some ports go the
>> float_of_string route, but most output the literal as a string in the
>> generated assembly code, letting the assembler do the conversion.
>> The assembler is likely to use strtod() or atof(), though.
> As Gerd said, the C standards do not guarantee this, so it really
> depends on how well-implemented your C library is.
In C, if you are really concerned about getting your exact constants up 
to the last bit, you should use the hexadecimal float notation 
introduced in C99. You should also use "%a" or "%A" to print out your 
floating-point values in hexadecimal if you intend to reload them on 
another IEEE-754 system in a portable way.

(Unfortunately on OCaml, %a is already used for another use... Xavier, 
what do you think of adding the hexadecimal float conversion to the 
family of printf functions?)

Anyway, "IEEE-754 compliant", as used by common vendors, is a fairly 
vague claim. For instance, contrary to a commonly held belief, the same 
C or Caml program ran on two different major platforms (say, IA32 vs 
AMD64 or IA32 vs PowerPC) can give different results, possibly even 
depending on compiler options, and this has nothing to do with how they 
parse constants... You might wish to read my preprint:
http://www.di.ens.fr/~monniaux/biblio/pitfalls_of_floating_point.pdf

The only widespread language that I know about that has fixed semantics 
for floating-point is Java if the 'strictfp' modifier is used. 
(Originally, Java was supposed to have a fully fixed semantics, but they 
realized that implementing it over the 80387 compatible floating-point 
stack was difficult and that it also precluded some optimizations, so 
they relaxed it by default and introduced 'strictfp'. Goodbye 
predictability.)