Nan with Python 2.5 on Windows

How do I build Nan with Python 2.5 on Windows?

float('nan')

with a mistake ValueError: invalid literal for float(): nan

Summary of answers: Neither float('inf')

nor float('nan')

does it work with Python 2.5 and Windows. This is a bug that has been fixed in Python 2.6.

If you are using numpy you can use numpy.inf

and numpy.nan

.

If you want a workaround without numpy, you can use an expression that overflows, for example 1e1000

to get inf, and 1e1000 / 1e1000

or 1e1000 - 1e1000

to get nan.

+3


source to share


3 answers


Another way is to divide inf

by yourself:

>>> float('inf') / float('inf')
nan

      



Or in a more obscure way, which may not work across platforms (but works around this particular bug in Python 2.5 on Windows):

>>> 1e31337 / 1e31337
nan
>>> 1e31337 - 1e31337
nan

      

+3


source


There is already an accepted answer to this question, but I think the following should work unless you want to rely on overflow and install numpy ... (not tested since I don't have python2.5 or windows)



>>> import numpy as np
>>> np.nan
nan
>>> np.inf
inf

      

+1


source


Upgrade your Python distribution if possible. The behavior you conduct is considered a mistake. (Note: Cython .)

Canonically, Python should support this definition in a nan

cross-platform way. This behavior appears to have been fixed in Python 2.6 and 3.0.

( Additional reading )


Of course this works in Linux Python versions:

$ python2.4
Python 2.4.3 (#1, Sep 21 2011, 19:55:41) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> float('nan')
nan

$ python2.5
Python 2.5.2 (r252:60911, Jun 26 2008, 10:20:40) 
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> float('nan')
nan

      

0


source







All Articles