[ Redirected to Haskell list ]
Michael Marte wrote:
> When adding the three double numbers
> 0.7, 0.2, and 0.1, a ghc 4.07-compiled program (compilation flag -O)
> yields 0.9999999999999999. Whatever this string stands for, [...]
The closest thing the addition of those numbers can give you with
IEEE-754 double precision numbers. :-)
0.1 base 10 = 0.19999999999999999... base 16, IIRC.
Ironically, GHC-4.08 with -O yields exactly 1 because of its improved
constant folding, which is done via rationals. GCC sometimes does
similar surprising things when optimization is switched on, see its
man pages, especially the flag -ffloat-store. It is a little bit
debatable if the improved precision/range is a bonus or simply wrong.
The latest GHC from CVS now takes a route similar to the recent
Java language spec (see the sections on the strictfp modifier):
* If the -fstrictfp Option is given, *all* calculations (at
compile time and runtime) are done with the precision/range
of the type in question.
* If the option is not given, you can sometimes have results
which are *more* exact, but never less exact. Intermediate
results can even leave the range of the floating point type
in question without being converted to infinity.
(Is this OK, Lennart? ;-)
Cheers,
Sven