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

Barak A. Pearlmutter barak at cs.nuim.ie
Thu Apr 22 11:34:49 EDT 2010


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
    Prelude> 0 > (0/0)
    False
    Prelude> 0 == (0/0)
    False

But these are inconsistent with the above, and arguably incorrect:

    Prelude> compare (0/0) (0/0)
    GT
    Prelude> compare (0/0) 0
    GT
    Prelude> compare 0 (0/0)
    GT
    Prelude> compare (0/0) (1/0)
    GT
    Prelude> compare  (1/0) (0/0)
    GT

I'd suggest that compare involving a NaN should yield

    error "violation of the law of the excluded middle"

The min and max functions have strange behaviour with regard to NaN,
especially when mixed with Infinity:

    Prelude> max (0/0) (1/0)
    NaN
    Prelude> max (1/0) (0/0)
    Infinity
    Prelude> min (0/0) (1/0)
    Infinity
    Prelude> max (0/0) 0
    NaN
    Prelude> max 0 (0/0)
    0.0

Hugs (Version: September 2006) has similar issues:

    Hugs> compare (0/0) (0/0)
    EQ
    Hugs> compare (0/0) 1
    EQ
    Hugs> (0/0) == (0/0)
    False
    Hugs> min (0/0) 1
    nan
    Hugs> min 1 (0/0)
    1.0
    Hugs> max (0/0) 1
    1.0

Discuss?
--
Barak A. Pearlmutter <barak at cs.nuim.ie>
 Hamilton Institute & Dept Comp Sci, NUI Maynooth, Co. Kildare, Ireland
 http://www.bcl.hamilton.ie/~barak/


More information about the Haskell-Cafe mailing list