[Haskell-cafe] Bug with [Double]

Serguey Zefirov sergueyz at gmail.com
Wed May 19 05:57:58 EDT 2010


2010/5/19 Erik de Castro Lopo <mle+hs at mega-nerd.com>:
> Dmitry Olshansky wrote:
>
>> It seems that I saw something like this in Cafe recevtly. But I am not sure...
>> In GHC 6.12.1 (Platform 2010 on Windows Vista) I have
>
> <snip>
>
>
>> Any comments?
>
> The problem you point out is not a problem with Haskell, but a problem
> with the whole concept of floating point arithmetic as implemented on
> all modern CPUs. See:
>
>    http://docs.sun.com/source/806-3568/ncg_goldberg.html
>
> You would have got similar problems with just about any language running
> on the same hardware.

This is what used for Double list generation (Haskell Platform 2010):
--------------------------------------------------
numericEnumFromThenTo   :: (Ord a, Fractional a) => a -> a -> a -> [a]
numericEnumFromThenTo e1 e2 e3
    = takeWhile predicate (numericEnumFromThen e1 e2)
                                where
                                 mid = (e2 - e1) / 2
                                 predicate | e2 >= e1  = (<= e3 + mid)
                                           | otherwise = (>= e3 + mid)
--------------------------------------------------
So normal C loop like for {double i = 1; i <= 10; i += 1+2/3) {
insert_list(i); } won't generate the same list, as Haskell does in
[1,1+2/3..10].

PS
Rationals:
Prelude> [1,1+2/3..10] :: [Rational]
[1 % 1,5 % 3,7 % 3,3 % 1,11 % 3,13 % 3,5 % 1,17 % 3,19 % 3,7 % 1,23 %
3,25 % 3,9 % 1,29 % 3,31 % 3]

Same result.


More information about the Haskell-Cafe mailing list