Tuesday, October 16, 2007

Why Double.IsNaN

If you have a double value set to NaN you can check that it's NaN only through the Double.IsNaN method. MSDN quote: "Use IsNaN to determine whether a value is not a number. It is not possible to determine whether a value is not a number by comparing it to another value equal to NaN."

In other words, there is no guarantee that (Double.NaN == Double.NaN).

A simple question - WHY ?

1 comment:

Aaron said...

In IEEE 754 arithmetic, NaN comparison always results in false with another number, except with !=, which is always true. IEEE arithmetic is used by most modern floating point implementations.

Its purpose is to provide a singular value outside of the affinely extended real numbers, perhaps to signal conditions such as "There is no number here" or "I don't care what its value is."

It is possible to check for a NaN directly by using the properties above, but IsNaN more clear and less error-prone. It mirrors C99's isnan(), which has additional advantages that do not apply here.