NaN, Infinity literals

Hi, Is there a way to use NaN and Infinity as literals, or at least to test if a value is NaN or Infinity? I tried *Main> let nan=0/0 *Main> nan NaN *Main> nan==0/0 False so "storing" the value does not work... Thanks, Tamas

You want the RealFloat class functions: http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t%3AReal... Tamas K Papp wrote:
Hi,
Is there a way to use NaN and Infinity as literals, or at least to test if a value is NaN or Infinity?
I tried
*Main> let nan=0/0 *Main> nan NaN *Main> nan==0/0 False
so "storing" the value does not work...
Thanks,
Tamas _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi, Tamas K Papp wrote:
Is there a way to use NaN and Infinity as literals, or at least to test if a value is NaN or Infinity?
*Main> let nan=0/0 *Main> nan NaN *Main> nan==0/0 False
This is correct according to the IEEE 754 standard, which defines that NaN compares unequal to everything, including itself. You can test the numbers using the isNaN and isInfinite functions. Incidentally, one can define isNaN x = x /= x for IEEE floating point numbers. Comparing with +Infinity and -Infinity works as expected. regards, Bertram.

Bertram Felgenhauer wrote:
This is correct according to the IEEE 754 standard, which defines that NaN compares unequal to everything, including itself.
This is numerically useful, perhaps, but nonetheless disturbing. For it would be helpful to expect that any type that presumes to be an instance of Eq has a == a = True. There really are two different notions of equality here. Perhaps they should be given two different names in the language? -- Ashley Yakeley Seattle, WA

Ashley Yakeley wrote:
Bertram Felgenhauer wrote:
This is correct according to the IEEE 754 standard, which defines that NaN compares unequal to everything, including itself.
This is numerically useful, perhaps, but nonetheless disturbing. For it would be helpful to expect that any type that presumes to be an instance of Eq has a == a = True.
There really are two different notions of equality here. Perhaps they should be given two different names in the language?
I think that the problem is not in equality, but in NaN: equality on numbers makes perfect sense (although rounding/lack of precision can render it useless), but NaN is *not a number*, so you can't properly compare it with numbers. IMO, NaN should not have the same type as real numbers; illegal operations should return NaN in the form of an error in some monad or by raising an exception (I don't know the behaviour of NaN in other aspects, but this strange equality sounds like propagation of errors/exceptions). Greetings, Arie

Tamas K Papp wrote:
Is there a way to use NaN and Infinity as literals, or at least to test if a value is NaN or Infinity?
*Main> let nan=0/0 *Main> nan NaN *Main> nan==0/0 False
so "storing" the value does not work...
Not sure what you mean here. In IEEE floating point, NaN is not equal to anything, especially not to itself. So the above worked, didn't it? And therefore, isNaN :: Double -> Bool isNaN x = not (x == x) but this is wrong (I believe): isNaN' :: Double -> Bool isNaN' x = x /= x Anyway, isNaN is alerady in the Prelude, and so are isInfinite, isDenormalized and isNegativeZero. This is all a bit ill-defined, but you'll have to live with that. If you also want a personal advise: switch on signaling NaNs (there's a C function to do that, simply foreign import it) and have your program bomb out as soon as a NaN is formed. Propagating them through calculations just increases the headache. Udo. -- FORTUNE PROVIDES QUESTIONS FOR THE GREAT ANSWERS: #4 A: Go west, young man, go west! Q: What do wabbits do when they get tiwed of wunning awound?
participants (6)
-
Arie Peterson
-
Ashley Yakeley
-
Bertram Felgenhauer
-
Chris Kuklewicz
-
Tamas K Papp
-
Udo Stenzel