Infinity (inf) in Python
In Python, you can use inf
to represent infinity. This article explains how to create, operate, check, and compare inf
(infinity).
inf
(infinity) in float
type
In Python, the float
type includes inf
, which represents infinity.
You can create inf
with float('inf')
. Other methods, such as using the math
module or the NumPy library, are described later.
f_inf = float('inf')
print(f_inf)
# inf
print(type(f_inf))
# <class 'float'>
Negative infinity
Negative infinity can be represented by adding -
to inf
.
f_inf_minus = -float('inf')
print(f_inf_minus)
# -inf
print(type(f_inf_minus))
# <class 'float'>
Convert inf
to other types
While a float
value can typically be converted to int
using int()
, inf
is an exception and cannot be.
f_inf = float('inf')
# print(int(f_inf))
# OverflowError: cannot convert float infinity to integer
inf
can be converted to the string 'inf'
using str()
.
print(str(f_inf))
# inf
print(type(str(f_inf)))
# <class 'str'>
Create inf
There are several ways to create inf
, such as using the float()
function, the math
module, or the NumPy library.
float('inf')
You can create inf
by specifying the string 'inf'
or 'infinity'
to float()
. It is case-insensitive.
print(float('inf'))
# inf
print(float('infinity'))
# inf
print(float('INF'))
# inf
print(float('INFinity'))
# inf
float
that exceeds the maximum value
Floating-point numbers exceeding the maximum value of float
are automatically treated as inf
.
import sys
f_inf_num = sys.float_info.max * 2
print(f_inf_num)
# inf
You can use sys.float_info.max
to get the maximum value of float
.
Math module in the standard library: math.inf
You can create inf
using math.inf
. Note that math.inf
is also a float
, not a special type.
import math
print(math.inf)
# inf
print(type(math.inf))
# <class 'float'>
print(float('inf') == math.inf)
# True
NumPy: np.inf
You can also create inf
with NumPy. np.inf
is also a float
, not a special type.
import numpy as np
print(np.inf)
# inf
print(type(np.inf))
# <class 'float'>
print(float('inf') == np.inf)
# True
Operations with inf
You can perform addition, subtraction, multiplication, division, and exponentiation operations with inf
.
Addition
No matter what you add to inf
, the result is inf
.
print(float('inf') + 100)
# inf
print(float('inf') + float('inf'))
# inf
Subtraction
Subtracting inf
from inf
results in nan
. nan
is also a float
type and stands for Not a Number
.
If you subtract any other value from inf
, the result is inf
.
print(float('inf') - 100)
# inf
print(float('inf') - float('inf'))
# nan
print(type(float('inf') - float('inf')))
# <class 'float'>
Multiplication
Multiplying inf
by 0
results in nan
. Multiplying it by any other number gives inf
.
print(float('inf') * 2)
# inf
print(float('inf') * float('inf'))
# inf
print(float('inf') * 0)
# nan
Division
inf
divided by inf
is nan
. Dividing 0
by inf
is 0
. If you divide inf
by 0
, an error is raised.
Otherwise, the result is inf
.
print(float('inf') / 2)
# inf
print(float('inf') / float('inf'))
# nan
print(0 / float('inf'))
# 0.0
# print(float('inf') / 0)
# ZeroDivisionError: float division by zero
Exponentiation
inf
to the power of 0
is 1
. 1
to the power of inf
is 1
. 0
to the power of inf
is 0
.
Otherwise, the result is inf
.
print(float('inf') ** 2)
# inf
print(float('inf') ** float('inf'))
# inf
print(float('inf') ** 0)
# 1.0
print(2 ** float('inf'))
# inf
print(1 ** float('inf'))
# 1.0
print(0 ** float('inf'))
# 0.0
Check if X is inf
: ==
, math.isinf()
, np.isinf()
In the following examples, both infinite and non-infinite values are used. Note that eXXX
means 10
to the power of XXX
.
print(1e1000)
# inf
print(1e100)
# 1e+100
==
operator
You can check if a value is inf
using the ==
operator.
print(1e1000 == float('inf'))
# True
print(1e100 == float('inf'))
# False
There are several ways to create inf
, but since they are all of the same float
type, it doesn't matter which one you use.
import math
import numpy as np
print(float('inf') == math.inf == np.inf)
# True
print(1e1000 == math.inf)
# True
print(1e100 == math.inf)
# False
Also, as mentioned above, many arithmetic operations involving inf
result in inf
, so the following expression is also True
.
print(float('inf') == float('inf') * 100)
# True
math.isinf()
The math
module provides the math.isinf()
function.
math.isinf()
returns True
for both positive and negative infinity.
import math
print(math.isinf(1e1000))
# True
print(math.isinf(1e100))
# False
print(math.isinf(-1e1000))
# True
np.isinf()
, np.isposinf()
, np.isneginf()
, np.isfinite()
NumPy provides np.isinf()
, np.isposinf()
, np.isneginf()
, and np.isfinite()
.
np.isinf()
- numpy.isinf — NumPy v1.25 Manual
- Returns True for both positive and negative infinity
np.isposinf()
- numpy.isposinf — NumPy v1.25 Manual
- Returns True for positive infinity
np.isneginf()
- numpy.isneginf — NumPy v1.25 Manual
- Returns True for negative infinity
np.isfinite()
- numpy.isfinite — NumPy v1.25 Manual
- Returns True for finite values
Each function accepts an array-like object, such as a NumPy array ndarray
or a list, as an argument and returns an ndarray whose elements are True
and False
.
import numpy as np
a = np.array([1, np.inf, -np.inf])
print(a)
# [ 1. inf -inf]
print(np.isinf(a))
# [False True True]
print(np.isposinf(a))
# [False True False]
print(np.isneginf(a))
# [False False True]
print(np.isfinite(a))
# [ True False False]
For these functions, you can also specify a scalar value as an argument.
print(np.isinf(1e1000))
# True
np.nan_to_num()
is also available, allowing you to replace infinity values with any desired value.
By default, infinity values are replaced with the largest finite values. You can specify the value to replace with the posinf
and neginf
arguments, introduced in NumPy1.17.
print(np.nan_to_num(a))
# [ 1.00000000e+000 1.79769313e+308 -1.79769313e+308]
print(np.nan_to_num(a, posinf=1e100, neginf=-1e100))
# [ 1.e+000 1.e+100 -1.e+100]
By default, these functions create a new ndarray
. However, if the second argument copy
is set to False
, they update the original ndarray
instead.
np.nan_to_num(a, copy=False)
print(a)
# [ 1.00000000e+000 1.79769313e+308 -1.79769313e+308]
As the name suggests, np.nan_to_num()
can also replace nan
. See the following article for details.
Compare values with inf
You can compare inf
and other values with comparison operators (>
, <
, etc.).
inf
is considered larger than any float
or int
value. However, it cannot be compared to nan
: any comparison with nan
yields False
.
Compare with floating point number float
Use the maximum value of float
as an example.
Positive infinity is greater than the maximum value of float
, and negative infinity is less than the minimum value of float
.
import sys
print(sys.float_info.max)
# 1.7976931348623157e+308
print(float('inf') > sys.float_info.max)
# True
print(-float('inf') < -sys.float_info.max)
# True
Compare with nan
nan
is a special value of float
, which can be created with float('nan')
.
print(float('nan'))
# nan
print(type(float('nan')))
# <class 'float'>
Any comparison involving nan
is always False
.
print(float('inf') > float('nan'))
# False
print(float('inf') < float('nan'))
# False
print(float('inf') == float('nan'))
# False
Compare with integer int
inf
can also be compared with int
.
print(float('inf') > 100)
# True
In Python 3, integers (int
) don't have a maximum limit. This means they can represent values greater than the maximum float
value. However, inf
is still considered larger than any such integer.
import sys
large_int = int(sys.float_info.max) * 10
print(large_int)
# 1797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213268894641827684675467035375169860499105765512820762454900903893289440758685084551339423045832369032229481658085593321233482747978262041447231687381771809192998812504040261841248583680
print(type(large_int))
# <class 'int'>
print(large_int > sys.float_info.max)
# True
print(float('inf') > large_int)
# True
While you can convert int
values to float
if they are smaller than the maximum representable float
value, attempting to convert larger int
values will result in an error.
While you can convert int
values smaller than the maximum float
value using float()
, you cannot for those exceeding this limit.
print(float(10**308))
# 1e+308
# print(float(10**309))
# OverflowError: int too large to convert to float