HOWTO · Python
How to Define an Infinite Value in Python
We will learn, with this explanation, what is an infinite value in Python and why we use an infinite value. We also go to learn how we can define an infinite value with the help of different ways in Python.
On this page
Use float("inf") for positive infinity and float("-inf") for negative infinity. These built-in float values are the simplest choice for comparisons and calculations that need an unbounded limit. Python also provides math.inf, Decimal infinity, and numpy.inf for different kinds of work.
Traditional Way to Define an Infinite Value in Python
A large integer is still finite, no matter how many digits it has. Python’s float constructor provides positive and negative infinity directly, without an additional module. The example compares a finite integer with both signs:
large_number = 10**100
positive_infinity = float("inf")
negative_infinity = float("-inf")
print(large_number < positive_infinity)
print(negative_infinity < large_number)
print(positive_infinity)
print(negative_infinity)
Use these values when a calculation or comparison needs to represent an unbounded limit. They are floating-point values, so they are not a way to store an arbitrarily large finite integer.
Define an Infinite Value Using the Math Module in Python
The standard-library math module exposes the same positive infinity as math.inf. Negating it gives negative infinity. This spelling can make mathematical code more readable when the surrounding code already uses math:
import math
positive_infinity = math.inf
negative_infinity = -math.inf
print(positive_infinity)
print(negative_infinity)
Define an Infinite Value Using the Decimal() Function in Python
For decimal arithmetic, construct infinity with Decimal from Python’s decimal module. Pass a string such as "Infinity" or "-Infinity". Decimal values use decimal arithmetic rules, which can be useful for financial or decimal-domain calculations; choosing Decimal does not make every value or operation exact.
from decimal import Decimal
positive_infinity = Decimal("Infinity")
negative_infinity = Decimal("-Infinity")
print(positive_infinity)
print(negative_infinity)
Define an Infinite Value Using the NumPy Module in Python
NumPy provides numpy.inf for array and scientific-computing work. NumPy is an additional dependency; install and import it in the environment where this example will run. Negation gives the corresponding negative value.
import numpy
positive_infinity = numpy.inf
negative_infinity = -numpy.inf
print(positive_infinity)
print(negative_infinity)
To check whether a value is infinite, use the standard-library math.isinf() function. It accepts finite and infinite numeric values and does not require NumPy:
import math
values = [12.5, math.inf, -math.inf]
for value in values:
print(math.isinf(value))
Use float("inf") for a simple built-in value, math.inf when the math module is already part of the code, Decimal with decimal arithmetic, and numpy.inf in NumPy workflows. The related Stack Overflow discussion covers other ways to represent infinity.