How to Represent "Infinity" in Python?

In Python, you can represent "infinity" (i.e. an infinite number) in the following two ways:

  1. Using math.inf or -math.inf (Python 3.5+);
  2. Using float('inf') or float('-inf').

Please note that both these representations follow the IEEE 754 floating-point standard, are equivalent, and can be used interchangeably.

Using math.inf or -math.inf

Starting with Python 3.5, you can use math.inf and -math.inf to represent positive and negative infinity respectively:

  • Positive Infinity:

    The value math.inf (i.e. positive infinity) is always greater than any other number:

    # Python 3.5+
    import math
    
    print(math.inf > -100) # True
    print(math.inf > 0) # True
    print(math.inf > 100) # True
    
  • Negative Infinity:

    The value -math.inf (i.e. negative infinity) is always less than any other number:

    # Python 3.5+
    import math
    
    print(-math.inf < -100) # True
    print(-math.inf < 0) # True
    print(-math.inf < 100) # True
    

You can use positive and negative infinity as you would any other value in Python. For example:

# Python 3.5+
import math

# assigning infinity to a variable
inf = math.inf

print("Infinity:", inf) # "Infinity: inf"
# Python 3.5+
import math

# function that returns infinity
def get_infinity():
  return math.inf

result = get_infinity()

print("Returned Infinity:", result) # "Returned Infinity: inf"

You can check if a value is infinite by using the math.isinf() function:

# Python 3.5+
import math

# function that takes infinity as an argument
def is_inf(value):
  # check if infinity using `math.isinf()`
  return math.isinf(value)

print(is_inf(math.inf)) # True
print(is_inf(-math.inf)) # True

print(is_inf(float('inf'))) # True
print(is_inf(float('-inf'))) # True

print(is_inf(42)) # False
print(is_inf(-42)) # False

Using float('inf') or float('-inf')

float('inf') and float('-inf') can be used to represent positive and negative infinity respectively:

  • Positive Infinity:

    The value float('inf') (i.e. positive infinity) is always greater than any other number:

    print(float('inf') > -100) # True
    print(float('inf') > 0) # True
    print(float('inf') > 100) # True
    
  • Negative Infinity:

    The value float('-inf') (i.e. negative infinity) is always less than any other number:

    print(float('-inf') < -100) # True
    print(float('-inf') < 0) # True
    print(float('-inf') < 100) # True
    

You can use positive and negative infinity as you would any other value in Python. For example:

# assigning infinity to a variable
inf = float('inf')

print("Infinity:", inf) # "Infinity: inf"
# function that returns infinity
def get_infinity():
  return float('inf')

result = get_infinity()

print("Returned Infinity:", result) # "Returned Infinity: inf"

You can check if a value is infinite by directly comparing the value against float('inf') or float('-inf'):

import math

# function that takes infinity as an argument
def is_inf(value):
  # directly comparing value against infinity
  return value == float('inf') or value == float('-inf')

print(is_inf(math.inf)) # True
print(is_inf(-math.inf)) # True

print(is_inf(float('inf'))) # True
print(is_inf(float('-inf'))) # True

print(is_inf(42)) # False
print(is_inf(-42)) # False

This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.