About

Welcome to Panela, Matt Harrison's take on mostly Open Source, Linux, Python, innovation in those areas, other buzzwords and Dick Proenneke. It comes complete with the illustrations as needed. Note the opinions expressed here are merely my opinions and not the opinions of my employer.

about Matt

Calendar

««Nov 2009»»
SMTWTFS
1234567
8910
11
12
1314
151617
18
192021
22232425262728
2930

Mailing List

My RSS Feeds








Silly python is_nan implementation

posted 2007.06.15 Fri

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
>>>

tags:          

links: digg this    del.icio.us    reddit




1. Seo Sanghyeon left...
2007.06.15 Fri 6:34 pm

Well, the standard solution is to compare with itself. def is_nan(num): return num == num


2. Matt left...
2007.06.15 Fri 8:58 pm

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


3. a random John left...
2007.06.15 Fri 9:29 pm

ah, so thats what you were doing today. :)

The not a number thing is messy in a few languages since two NaNs are not equivalent to each other. I am surprised that you didn't find a built in test for it though.


4. Doug Napoleone left...
2007.06.15 Fri 9:32 pm :: http://www.dougma.com/

Unfortunately PEP 757 which included an isNan function was rejected (for C99 support which is still incomplete). There are some good third party options.

The SciPy package comes with full isNan, isInfinate, and other fun calls. If you are feeling adventurous you can create your own 757/C99 extension module. This can come in handy if you want floating point register control (fp rounding, denorm, etc), and special case fpe (floating point exception) control.

I wrote such a module but it was for work and as such I do not own it :-(


5. Seo Sanghyeon left...
2007.06.16 Sat 12:58 am

Of course I meant num != num, which is only true for NaN. Doesn't that work for you?


6. Richard Moore left...
2007.06.16 Sat 7:29 am

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.


7. Bjoern Graf left...
2007.06.16 Sat 8:13 am

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).


8. Fredrik left...
2007.06.16 Sat 12:08 pm

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:

http://programming.reddit.com/info/1ywzk/comments


9. r left...
2008.10.29 Wed 8:14 am

try math.isnan


10. erik left...
2009.03.26 Thu 3:24 pm

"try math.isnan" - that is in 2.6, OP is using 2.4.4