2

I was wondering about the following: When I wanted to know whether 0.3/3 == 0.1 in Haskell, the answer was false. Then I checked what 0.3/3 actually is in Haskell and as I found out it was 0.099999. I have absolutely no idea why this is the case. Could anybody help me with that?

SR23
  • 49
  • 5
  • 3
    See [why 0.1 doesn't exist in floating point](http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/), and [why doesn't my floating point comparison work?](http://www.parashift.com/c++-faq/floating-point-arith.html) – aruisdante Jul 21 '14 at 21:10
  • 2
    If its any consolation, it isn't just Haskell. :) – codenheim Jul 21 '14 at 21:16
  • 4
    This question *might* not be a duplicate, depending on its interpretation. The result the OP is seeing *is* due to floating-point inaccuracy, **however** Haskell literals are overloaded. So the real answer to *this* question is that Haskell has a set of rules to resolve the ambiguity in certain circumstances, and, in the case of `Fractional` literal it chooses the floating points implementations. You can force Haskell to use other types: `Prelude> 0.3/(3 :: Rational) == 0.1 True` In fact you can use `Prelude> default (Integer, Rational, Double)` so that: `Prelude> 0.3/3 == 0.1 True`. – Bakuriu Jul 22 '14 at 19:23

0 Answers0