English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2006-10-01 (16:36)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] float rounding
> I'm using Ocaml for an interval arithmetic application.  I"m  curious
> about what the Ocaml parser/compiler does to float constants.

The lexer, parser and most of the compilers actually keep them as
strings, just like they were given in the source code.  Conversion to
IEEE binary representation occurs:

- For the bytecode compiler ocamlc, when the bytecode is generated.
The conversion is performed by the float_of_string function, which is
a thin wrapper around the C library strtod() function, as Gerd
Stolpmann said.

- 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.

> May I assume
> that for any constant I enter, eg. 3.1415... (for N digits of pi), that
> the compiler will give me a closest machine representable number?
> i.e., if I bound a constant by the previous and next floating point
> value to that given me by the compiler, will it always be the case
> that my original (mathematical) constant lies in that interval?

As Gerd said, the C standards do not guarantee this, so it really
depends on how well-implemented your C library is.

- Xavier Leroy