Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backporting float("inf") to Python 2.4 and 2.5

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?

like image 590
Ram Rachum Avatar asked Aug 06 '26 22:08

Ram Rachum


2 Answers

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.

like image 155
Mark Rushakoff Avatar answered Aug 08 '26 10:08

Mark Rushakoff


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
like image 22
Jack Lloyd Avatar answered Aug 08 '26 12:08

Jack Lloyd