faqts : Computers : Programming : Languages : Python : Common Problems : Maths

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

16 of 16 people (100%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

Does Python support any sort of floating point "not a number" (e.g. IEEE infinity)?

Jul 19th, 2000 03:02
unknown unknown, Huaiyu Zhu, Warren Focke


This seems to work and is used in MatPy:
Inf = inf = 1e300**2
NaN = nan = inf - inf
-----------
Python floats inherit the behavior of your C platform's doubles, so on
an IEEE-754 system, they will act fairly IEEE-like, as Huaiyu
demonstrates:
>Inf = inf = 1e300**2
>NaN = nan = inf - inf
However, according to a recent post, the OpenVMS port of python uses
VAX-format math[1], which has less exponent range than IEEE.  I think
that that expression for Inf will not parse there.  I don't even know
if VAXen have Inf or NaN.
-----------
According to discussions on comp.lang.python, there is no universal way 
to treat NaN, but the following codes work on Windows (NT and 98), 
Solaris (2.7 with gcc) and Linux (RH6.1 with egcs).
It core dumps on Tru64 Unix on Compaq Alpha [Solution: When using DEC
(Compaq) compiler to build Python try using -ieee switch].
Supposedly it should work on any machine using IEEE arithmetics.
An alternative that sometimes works is
NaN = math.exp(1000) / math.exp(1000)