[Haskell-cafe] Proper Handling of Exceptional IEEE Floating Point Numbers

Nick Bowler nbowler at elliptictech.com
Thu Apr 22 13:25:32 EDT 2010


On 16:34 Thu 22 Apr     , Barak A. Pearlmutter wrote:
> Comparison of exceptional IEEE floating point numbers, like Nan, seems
> to have some bugs in ghci (version 6.12.1).
> 
> These are correct, according to the IEEE floating point standards:
> 
>     Prelude> 0 < (0/0)
>     False
...
> But these are inconsistent with the above, and arguably incorrect:
...
>     Prelude> compare 0 (0/0)
>     GT
...
> I'd suggest that compare involving a NaN should yield
> 
>     error "violation of the law of the excluded middle"

The problem stems from the fact that Float and Double are instances of a
class for totally ordered data types (namely Ord), which they are not.

While it might be worthwhile to make compare error in this case, the
consequences of this instance are much, much worse.  For example, max
is not commutative (as you have observed).  Data.Map.insert with Double
keys can cause elements to disappear from the map (at least as far as
Data.Map.lookup is concerned).  Using "sort" on a list of doubles
exposes the underlying sorting algorithm used.

-- 
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)


More information about the Haskell-Cafe mailing list