So apparently equality for NaN doesn't make sense. (NaN or Not a Number is division by zero, and other other operations that return non numbers, which is different than infinity or negative infinity). That makes sense, but sometimes you want to know if a number is really not a number. So how does one do so?
Javascript happens to have a nice isNaN function and today I happened to need one in python. My naive implementation didn't work (is_nan1). But if you coerce to string and compare against "nan" that works
matt@r52 $ python
Python 2.4.4 (#1, May 12 2007, 23:48:56)
[GCC 4.1.1 (Gentoo 4.1.1-r3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = float("NaN")
>>> b = 0
>>> c = float("nan")
>>> d = float("infinity")
>>>
>>> def is_nan1(num):
... return num == float("NaN")
...
>>> print is_nan1(a)
False
>>> print is_nan1(b)
False
>>> print is_nan1(c)
False
>>> print is_nan1(d)
False
>>>
>>>
>>> def is_nan2(num):
... return str(num) == "nan"
...
>>> print is_nan2(a)
True
>>> print is_nan1(b)
False
>>> print is_nan2(c)
True
>>> print is_nan2(d)
False
>>>
Well, the standard solution is to compare with itself.
def is_nan(num): return num == num
Seo Sanghyeon - I'm not sure about your "standard" solution (nor can I find
a reference to it).
float("NaN") != float("NaN")
but for all real numbers your definition returns true
ah, so thats what you were doing today. :)
Unfortunately PEP 757 which included an isNan function was rejected (for
C99 support which is still incomplete). There are some good third party
options.
Of course I meant num != num, which is only true for NaN. Doesn't that work
for you?
You need to be careful in implementing stuff to do with NaN as support on
different platforms is variable. The following fix was just committed to
kjs to address some problems in the win32 runtime
http://lists.kde.org/?l=kde-commits&m=118191209326398&w=2 for example.
There's this little IEEE 754 gem: "The introduction of NaNs can be
confusing, because a NaN is never equal to any other number (including
another NaN), so x = x is no longer always true." (from
http://docs.sun.com/source/806-3568/ncg_goldberg.html#1066).
Seo's solution works for CPython 2.4 and later; your solution works in all
versions, but only on some platforms (the str() representation isn't
standardized). For more on this, including portable solutions and links to
more complete implementations, see this thread: