[Haskell-cafe] Mystery of an Eq instance

Scott Lawrence bytbox at gmail.com
Fri Sep 20 13:52:16 CEST 2013


On ghc 7.6.3:

Prelude> 3.16227766016837956
3.1622776601683795

So if you specify a number with greater-than-available precision, it will be 
truncated. This isn't an issue with (==), but with the necessary precision 
limitations of Double.

On Fri, 20 Sep 2013, damodar kulkarni wrote:

> Hello,
> There were some recent discussions on the floating point support in Haskell
> and some not-so-pleasant "surprises" people encountered.
>
> There is an Eq instance defined for these types!
>
> So I tried this:
> *Main> sqrt (10.0) ==3.1622776601683795
> True
> *Main> sqrt (10.0) ==3.16227766016837956
> True
> *Main> sqrt (10.0) ==3.1622776601683795643
> True
> *Main> sqrt (10.0) ==3.16227766016837956435443343
> True
>
> It seems strange.
>
> So my doubts are:
> 1. I wonder how the Eq instance is defined in case of floating point types
> in Haskell?
> 2. Can the Eq instance for floating point types be considered "meaningful"?
> If yes, how?
> In general, programmers are **advised** not to base conditional branching
> on tests for **equality** of two floating point values.
> 3. Is this particular behaviour GHC specific? (I am using GHC 6.12.1)
>
> If there are references on this please share.
>
> Thanks and regards,
> -Damodar Kulkarni
>

-- 
Scott Lawrence



More information about the Haskell-Cafe mailing list