
Hi everybody, please have a look at the following lines of code: x :: [Double] x = [2,4,6,8,10] p :: [Double] p = [0.05, 0.2, 0.35, 0.3, 0.1] expectation :: Double expectation = sum $ zipWith (*) x p variance :: Double variance = sum $ zipWith f x p where f i p = (i - expectation)^2 * p when I load this into ghci I get the following: *Main> expectation 6.3999999999999995 *Main> expectation == 6.4 False *Main> variance 4.24 *Main> Why does 'expectation' get evaluated to 6.39999... instead of 6.4?? This is very strange. I have another example which is even more annoying: Have a look at the following code: import qualified Data.Map as M newtype Distribution a = Distribution (M.Map a Double) isDistribution :: Distribution a -> Bool isDistribution (Distribution d) = sum (M.elems d) == 1 uniform :: Ord a => [a] -> Distribution a uniform xs = Distribution (M.fromList (map go xs)) where go i = (i, 1 / fromIntegral (length xs)) When I load this into ghci I get the following: *Probability> isDistribution $ uniform [1..6] False *Probability> (\(Distribution d) -> sum (M.elems d)) $ uniform [1..6] 0.9999999999999999 *Probability> I mean ... hey, what is going on here? Is there any way of getting around this? Cheers, Thomas