Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python define the value of "NaN > 0"?

Does Python defined the value of "NaN > 0"? In my Python interpreter, I get:

>>> float('nan') > 0
False

but is this guaranteed?

If I understand correctly, the IEEE 754 standard assigns False to any comparison involving NaN. However, I can't find anything in the Python documentation that indicates that this standard is followed. I even understand that 1/0 should give infinity, under IEEE 754, but Python raises an exception (ZeroDivisionError), so Python does not fully follow IEEE 754.

So, is the result of float('nan') > 0 fully defined in Python? I need to know whether it is the same on all platforms, and with all versions of Python (including old ones).

like image 742
Eric O Lebigot Avatar asked Dec 07 '13 08:12

Eric O Lebigot


1 Answers

All Python implementations that I've used on various platforms use IEEE-754 and would behave as you describe.

However, this does not appear to be formally mandated by the language. So much so that the tutorial has the following to say (emphasis mine):

Almost all machines today (July 2010) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”.

Here is a relevant thread from 2008: Python on non IEEE-754 platforms: plea for information.

It is very easy to check whether your interpreter/platform behave the way you expect. If you're critically dependent on this, you could easily detect non-compliance when your program starts up.

like image 108
NPE Avatar answered Oct 17 '22 13:10

NPE