So [apparently equality for NaN doesn't make sense](http://mail.python.org/pipermail/python-list/2005-July/330042.html_. (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
>>> 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
>>>


Update math.isnan is available