2

I wrote quickcheck tests for a Haskell program that optimizes and evaluates a function.

The problem is quickcheck generates expressions resulting in NaN like:

> acos(2)
NaN

Haskell evaluates the following statement as false:

> acos(2)==acos(2)
False

So my quickcheck tests fail with this comparison. Is there any way to compare NaN values?

recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
jvermeulen
  • 505
  • 1
  • 6
  • 14

1 Answers1

7

No, as is defined by IEEE 754 comparing 2 NaNs always return false. To chceck if your value is NaN in Haskell you can use isNaN method or write it by yourself

isNaN' :: a -> Bool
isNaN' a = a /= a
Hauleth
  • 22,873
  • 4
  • 61
  • 112
  • IMO NaN should compare equal to NaN. I know that two NaN's are not necessarily the same value, but returning false breaks all kinds of laws, as well as things like putting floats in a Set or a Map. Also things like `compare nan nan = GT` but `nan > nan = False`. Particularly since `inf == inf` returns True. When that kind of thing is just as undecidable as comparing NaN values. (`nan = 0.0/0` and `inf = 1.0/0` in my examples) – semicolon Mar 29 '16 at 23:37
  • 2
    There is rationale of that on [SO](http://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values/1573715#1573715) by one of the IEEE committee. I won't argue with you but read what he has written. Also [this](http://stackoverflow.com/a/23666623/1017941) is good rationale for that design choice. – Hauleth Mar 29 '16 at 23:42