Python math.inf Constant
The Python math.inf constant represents positive infinity. It is a predefined value available in Python's math module and is used to represent a number that is greater than any finite number.
In general mathematics, infinity (∞) is used to describe situations where a value grows without bound or where a mathematical expression does not have a finite result. Infinity is not a number in the conventional sense; rather, it is a concept that indicates unboundedness or limitless growth.
Syntax
Following is the basic syntax of the Python math.inf constant −
math.inf
Return Value
The constant returns the value of a floating-point positive infinity.
Example 1
In the following example, we are initializing a variable "max_value" with positive infinity (math.inf). This can be useful in situations where you want to ensure a variable holds the maximum possible value and you are comparing it against other values −
import math
max_value = math.inf
print("The maximum value is:", max_value)
Output
The output obtained is as follows −
The maximum value is: inf
Example 2
Here, we are using the math.inf constant to find the minimum value in a list of numbers. We initialize the "minimum_value" variable with positive infinity (math.inf) and then iterate through the list. If a number in the list is smaller than the current "minimum_value", it updates the "minimum_value" accordingly −
import math
numbers = [10, 5, 8, 12, 15, 20]
minimum_value = math.inf
for num in numbers:
if num < minimum_value:
minimum_value = num
print("The minimum value in the list is:", minimum_value)
Output
Following is the output of the above code −
The minimum value in the list is: 5
Example 3
In this example, we perform an arithmetic operation where positive infinity (math.inf) is added to another number −
import math
result = math.inf + 100
print("The result of infinity plus 100 is:", result)
Output
Since adding any finite number to positive infinity results in positive infinity, the result will be positive infinity as shown below −
The result of infinity plus 100 is: inf
Example 4
In mathematics, dividing a finite number by infinity results in zero. Now, we perform division by positive infinity −
import math
x = 10
result = x / math.inf
print("The result of dividing", x, "by infinity is:", result)
Output
We get the output as shown below −
The result of dividing 10 by infinity is: 0.0
python_maths.htm