views:

216

answers:

2

I'm backporting my project from Python 2.6 to Python 2.4 and 2.5. In my project I used float("inf"), and now I find it is unavailable on Python 2.5. Is there a backport of it?

+4  A: 

Spelling it either the long way or the short way works fine for me:

$ python2.4 -c "print float('inf')+200"
inf
$ python2.5 -c "print float('inf')+200"
inf
$ python2.5 -c "print float('infinity')+200"
inf
$ python2.4 -c "print float('infinity')+200"
inf

The -c flag means "execute the following arguments as a Python command."

PEP754 (which was rejected) does mention your issue about special IEEE-754 values. It suggests using something like 1e300000 to generate a floating point overflow and create inf, but it does note that this is ugly and not guaranteed to be portable.

Mark Rushakoff
I don't know what that `-c` flag is, but in Python 2.4 on Windows XP there seems to be no `float('inf')`, nor `float('infinity')`. It raises: `ValueError: invalid literal for float(): inf`
cool-RR
Python float uses hardware floats, so behavior might depend on the platform. For changes, see "Many floating-point features were added." http://docs.python.org/whatsnew/2.6.html
kaizer.se
A: 

You should be able to fake it up by giving Python a sufficiently large floating point constant instead. For instance:

>>> 1e100000
inf
>>> float('inf') == 1e1000000
True
>>> float('inf') == 2e1000000
True
Jack Lloyd