Infinity is an undefined number which can be negative and positive. A number is used as infinity sometimes, the sum of two numeric value may be numeric but different pattern, it may be negative or positive value.
It is used to compare solution in algorithms for best solution. Generally, a value set at initial may be positive or negative infinity, we have to take care that no input value is bigger or smaller.
Infinity and Nan are two kinds of exceptional floating values in Python.
In comparison, positive infinity is always bigger than every natural number.
(i.e. 0,1,2,.......... +∞. Positive integer and +infinity.)
In comparison, negative infinity is smaller than negative number.
(i.e. ∞-.......-2,-1,0, 1,.2,3......... – negative infinity and -integer.)
In python, we cannot represent infinity or we can say that there is no way to show the infinity as an integer. But we can use float (inf) as an integer.
How to represent positive and negative infinity in Python:
ptive_inf = float(“inf”) // for Positive infinity ntive_inf = float(“-inf”) // for Negative infinity
Infinity in Python 3.5
import math test = math.inf If we compare ptive_inf > 1 ptive_inf > 1000000
It always true.
Python Infinity and NaN (“not a number”)
As we know that inf is infinity and is the undefined biggest number among positive numbers. When we try to subtract any number out of it then we get a “Not a number” (NaN). Even though infinity is the biggest integer but undefined, when something is undefined how can you subtract something from it, therefore, an error is NaN.
NaN can be represented in Python using 3 basic ways:
n= float ('-inf'), for storing a negative infinite number
p= float ('inf'), for storing a positive infinity number
In Python 3 and higher, the NaN number is represented using the math.nan after importing the math module.
For example, nan=math.nan
This number can be evaluated using math.isnan(nan), where the nan is the argument passed to check whether it is NaN or not.
ptive_inf = float (‘inf’) #positive infinity ntive_inf = float(‘-inf’) #negative infinity nann = float(‘nan) #Not a Number (NaN)
math.inf in Python
math.inf is a module in python, when we are talking about the infinity, it is assigned to a float variable with the help of inf as a float argument. This ‘inf’ constant is available with a math module. When we are dealing with the infinity we import math module.