Floating point problems

Neil Mitchell ndmitchell at gmail.com
Wed Aug 30 14:50:29 EDT 2006


Hi,

>         *Main> 0.2 + 0.1
>         0.30000000000000004

Prelude> (0.1 :: Float) + (0.2 :: Float)
0.3
Prelude> (0.1 :: Double) + (0.2 :: Double)
0.30000000000000004
Prelude> (0.1 :: Float) + 0.2 == 0.3
True

If you use Float's instead of doubles, it stores less precision, and
so gets it right.

Also, if what you really want is decimals, why not:

data Decimal = Decimal {dps :: Integer, value :: Integer}

Then define your Num instance on Decimal, and have perfect high
fidelity numbers throughout.

Thanks

Neil


More information about the Glasgow-haskell-users mailing list