How can I represent an infinite number in Python?

PythonInfiniteInfinity

Python Problem Overview


How can I represent an infinite number in python? No matter which number you enter in the program, no number should be greater than this representation of infinity.

Python Solutions


Solution 1 - Python

In Python, you can do:

test = float("inf")

In Python 3.5, you can do:

import math
test = math.inf

And then:

test > 1
test > 10000
test > x

Will always be true. Unless of course, as pointed out, x is also infinity or "nan" ("not a number").

Additionally (Python 2.x ONLY), in a comparison to Ellipsis, float(inf) is lesser, e.g:

float('inf') < Ellipsis

would return true.

Solution 2 - Python

Since Python 3.5 you can use math.inf:

>>> import math
>>> math.inf
inf

Solution 3 - Python

No one seems to have mentioned about the negative infinity explicitly, so I think I should add it.

For negative infinity:

-math.inf

For positive infinity (just for the sake of completeness):

math.inf

Solution 4 - Python

I don't know exactly what you are doing, but float("inf") gives you a float Infinity, which is greater than any other number.

Solution 5 - Python

There is an infinity in the NumPy library: from numpy import inf. To get negative infinity one can simply write -inf.

Solution 6 - Python

Another, less convenient, way to do it is to use Decimal class:

from decimal import Decimal
pos_inf = Decimal('Infinity')
neg_inf = Decimal('-Infinity')

Solution 7 - Python

In python2.x there was a dirty hack that served this purpose (NEVER use it unless absolutely necessary):

None < any integer < any string

Thus the check i < '' holds True for any integer i.

It has been reasonably deprecated in python3. Now such comparisons end up with

TypeError: unorderable types: str() < int()

Solution 8 - Python

Also if you use SymPy you can use sympy.oo

>>> from sympy import oo
>>> oo + 1
oo
>>> oo - oo
nan

etc.

Solution 9 - Python

For Positive Infinity

pos_inf_val = float("infinity")

For Negative Infinity

neg_inf_val = float("-infinity")

Solution 10 - Python

Representing in python

float("inf") or float("INF") or float("Inf") or float("inF") or float("infinity") or float("Infinity") creates a float object holding

You can also represent -∞ in python

float("-inf") or float("-INF") or float("-Inf") or float("-infinity") creates a float object holding -∞

You can perform arithmetic operations:

infinity = float("inf")
ninfinity = float("-inf")
nan = float("nan")

print(infinity*infinity)#inf
print(ninfinity+infinity)#not a number
print(1/-infinity)#is -0.0
print(nan*nan)# is not a number
print(1/infinity) # is 0.0 since 1/∞ is 0

Output:

$ python3 floating.py
inf
nan
-0.0
nan
0.0

Solution 11 - Python

In Summary, there is two kinds definition for Infinity.

For Positive Infinity

posVal1 = math.inf
posVal2 = float("inf")

For Negative Infinity

negVal1 = -math.inf
negVal2 = float("-inf")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionssierralView Question on Stackoverflow
Solution 1 - PythonWilHallView Answer on Stackoverflow
Solution 2 - Pythonuser1804599View Answer on Stackoverflow
Solution 3 - PythonSнаđошƒаӽView Answer on Stackoverflow
Solution 4 - PythonNed BatchelderView Answer on Stackoverflow
Solution 5 - PythonLenar HoytView Answer on Stackoverflow
Solution 6 - PythonDenis MalinovskyView Answer on Stackoverflow
Solution 7 - PythonAntony HatchkinsView Answer on Stackoverflow
Solution 8 - PythonUSERNAME GOES HEREView Answer on Stackoverflow
Solution 9 - PythonOmarView Answer on Stackoverflow
Solution 10 - PythonUdesh RanjanView Answer on Stackoverflow
Solution 11 - PythonZgpeaceView Answer on Stackoverflow