692

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.

Elias Zamaria
  • 89,268
  • 31
  • 107
  • 141
ssierral
  • 7,637
  • 6
  • 24
  • 44
  • 48
    `math.inf` is useful as an initial value in optimisation problems, because it works correctly with min, eg. `min(5, math.inf) == 5`. For example, in shortest path algorithms, you can set unknown distances to `math.inf` without needing to special case `None` or assume an upper bound `9999999`. Similarly, you can use `-math.inf` as a starting value for maximisation problems. – Colonel Panic Oct 12 '16 at 10:59
  • 1
    In most cases, an alternative to using math.inf in optimization problems is to start with the first value. – Tobias Bergkvist May 30 '20 at 07:26
  • 2
    Can't help but wonder how so many Python question seems to attract a multitude of ways to do the same thing. How is that Pythonian or compatible with Zen of Python.... – nyholku Oct 25 '21 at 14:25
  • 1
    @nyholku agree.. `There should be one-- and preferably only one --obvious way to do it.` – Sнаđошƒаӽ Feb 10 '22 at 01:17

11 Answers11

846

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.

WilHall
  • 10,734
  • 5
  • 29
  • 52
104

Since Python 3.5 you can use math.inf:

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

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
Sнаđошƒаӽ
  • 15,289
  • 12
  • 72
  • 86
33

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

Ned Batchelder
  • 345,440
  • 70
  • 544
  • 649
32

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

Lenar Hoyt
  • 5,722
  • 6
  • 46
  • 55
  • 1
    How do `float("inf")`, `math.inf`, and `np.inf` compare? Which one to use when? – CGFoX Mar 30 '22 at 07:01
  • The first two are native i.e. require no dependency. `np.inf` requires the Numpy package. `float('inf')` is a bit hacky as it involves parsing a string, but on the upside it does not even require an import and the parsing is typically computationally negligible. If you use one of the math packages anyway, though, then just use them. If you happen to use both `math` and `np`, then `np.inf` is the shortest one. – Lenar Hoyt Mar 30 '22 at 16:33
27

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

from decimal import Decimal
pos_inf = Decimal('Infinity')
neg_inf = Decimal('-Infinity')
Denis Malinovsky
  • 5,422
  • 1
  • 20
  • 17
  • 30
    why don't you add _why it is less convenient and why anyone should use it_? – Niccolò Jul 25 '14 at 11:59
  • Main drawback for me is length of this solution, it's not so clear and concise. But someone might actually prefer this. – Denis Malinovsky Jul 25 '14 at 16:56
  • Wouldn't this be an infinity value of a different type? – szablica Oct 31 '14 at 21:26
  • 5
    Let's see: `Decimal('Infinity') == float('inf')` returns `True`, so it's pretty much the same. – Denis Malinovsky Nov 01 '14 at 18:17
  • His question is if wouldn't it be of a different type and the answer is yes. Should've checked `Decimal('Infinity') is float('inf')` and that's a `False` – Afzal S.H. Jun 22 '15 at 07:47
  • 9
    @afzal_SH `float('inf') is float('inf')` returns `False` too – nemesisdesign Jun 27 '15 at 16:24
  • 6
    infinity is different even from itself, so your comment didn't make much sense to me, IMHO – nemesisdesign Jun 29 '15 at 13:20
  • 1
    @nemesisdesign Not true. `Inf == Inf` in standard float arithmetic. Maybe you are thinking of `NaN != NaN`? – becko Mar 17 '17 at 13:45
  • 1
    @becko, in python the `==` operator and the `is` operator mean two different things. Try to run in the python REPL the following code: `float('inf') is float('inf')` and `float('inf') == float('inf')` – nemesisdesign Mar 21 '17 at 14:36
  • 9
    `float('inf') is float('inf')` -> `False`, just holds that they are different objects with different instances, but not that the internal contents are different -- actually as @nemesisdesign pointed `float('int') == float('int')` holds to `True`. This is the same problem like comparing mutable objects like [1,2,3] is [1,2,3] and [1,2,3] == [1,2,3], which are, in order, False and True.. More info see: https://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs – Manoel Vilela Sep 01 '17 at 10:51
16

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()
Antony Hatchkins
  • 28,884
  • 9
  • 105
  • 105
11

Also if you use SymPy you can use sympy.oo

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

etc.

Georgy
  • 9,972
  • 7
  • 57
  • 66
USERNAME GOES HERE
  • 601
  • 12
  • 27
1

For Positive Infinity

pos_inf_val = float("infinity")

For Negative Infinity

neg_inf_val = float("-infinity")
Omar
  • 806
  • 11
  • 11
0

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
Udesh Ranjan
  • 1,661
  • 2
  • 8
  • 21
0

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")
Zgpeace
  • 3,045
  • 23
  • 25