Infinity is an undefined number which can be negative or positive. A number is used as infinity; sometimes, the sum of two numeric values may be a numeric but different pattern; it may be a negative or positive value.
It is used to compare the solution in algorithms for the 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 in Python
In Python, there is no way or method to represent infinity as an integer. This matches the fundamental characteristic of many other popular programming languages. But due to python being dynamically typed language, you can use float(inf) as an integer to represent it as infinity.
Therefore 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.
In Python, positive infinity and negative infinity can be represented by:
- Positive infinity: inf
- Negative infinity: -inf
Python Program to Define Positive and Negative Infinity Number
Example
# Define Positive infinity number
ptive_inf = float('inf')
print('Positive Infinity: ',ptive_inf)
# Define Negative infinity number
ntive_inf = float('-inf')
print('Negative Infinity: ',ntive_inf)
Output
Positive Infinity: inf
Negative Infinity: -inf
Represent Positive and Negative Infinity Using the Math Module
To determine the infinity number, you can use the python math module.
Note: This will work with only python 3.5 or higher version of python.
Syntax:
- Positive infinity: math.inf
- Negative infinity: -math.inf
Example:
# Import math module
import math
# Positive infinity
ptive_inf = math.inf
print('Positive Infinity: ',ptive_inf)
# Negative infinity
ntive_inf = -math.inf
print('Negative Infinity: ',ntive_inf)
Output
Positive Infinity: inf
Negative Infinity: -inf
Positive infinity number is greatest, and the negative infinity number is the smallest of all numbers.
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 the negative number.
(i.e., ∞-.......-2,-1,0, 1,.2,3......... – negative infinity and -integer.)
Example:
# Define Positive infinity number
ptive_inf = float('inf')
if 99999999999999999 > ptive_inf:
print('Numer is greater than Positive infinity')
else:
print('Positive infinity is greater')
# Define Negative infinity number
ntive_inf = float('-inf')
if -99999999999999999 > ptive_inf:
print('Numer is smaller than Negative infinity')
else:
print('Negative infinity is smaller')
Output
Positive infinity is greater
Negative infinity is smaller
Python Program to Check If the Number Is Infinite
Example:
import math
# Define positive infinity number
ptive_inf = float('inf')
print('Variable is Infinity: ',math.isinf(ptive_inf))
# Define negative infinity number
ntive_inf = float('-inf')
print('Variable is Infinity: ',math.isinf(ntive_inf))
Click and drag to move
Output:
Variable is Infinity: True
Variable is Infinity: True
In the above example, we are using the using isinf method of math library to check if the number is infinity or not.
Represent Positive and Negative Infinity Using the Decimal Module
You can define positive and negative Infinity by using the decimal module.
Syntax:
- Positive Infinity: Decimal('Infinity')
- Negative Infinity: Decimal('-Infinity')
Example:
from decimal import Decimal
import math
# Define positive infinity
ptive_inf = Decimal('Infinity')
print('Variable is Infinity: ',math.isinf(ptive_inf))
# Define negative infinity
ntive_inf = Decimal('-Infinity')
print('Variable Infinity: ',math.isinf(ntive_inf))
Click and drag to move
Output:
Variable is Infinity: True
Variable Infinity: True
Represent Positive and Negative Infinity Using Numpy Library
You can define positive and negative Infinity by using the inf module of the NumPy library
Syntax:
- Positive Infinity: np.inf
- Negative Infinity: -np.inf
Example:
import numpy as np
import math
# Define positive Infinity number
ptive_inf = np.inf
# Check is infinity number
print('Positive infinity number: ',ptive_inf)
# Define negative Infinity number
ntive_inf = -np.inf
# Check is infinity number
print('Negative infinity number: ',ntive_inf)
Output:
Positive infinity number: inf
Negative infinity number: -inf
Check If Positive Infinity Number Is Equal to Negative Infinity Number
You can simple check if the positive infinity number is equal to negative infinity number using simple “==” operator, and the output is always false.
Example:
import math
# Define positive infinity number
ptive_inf = float('inf')
# Define negative infinity number
ntive_inf = float('-inf')
print('Positive infinity equal to Negative infinity: ',ptive_inf == ntive_inf)
Output:
Positive infinity equal to Negative infinity: False
Arithmetic operations on infinity number will give an infinite number
If you perform any arithmetic operation with positive or negative infinity numbers, the result will always be an endless number.
Example:
# Define positive infinity number
ptive_inf = float('inf')
# Multiply Positive infinity number by 5
print('Multiplication : ',ptive_inf * 5)
# Addition to Positive infinity Number
print('Addition : ',ptive_inf + 5)
# Subtraction to Positive infinity Number
print('Subtraction : ',ptive_inf - 5)
# Division to Positive infinity Number
print('Division : ',ptive_inf / 5)
# Define Negative infinity number
ntive_inf = float('-inf')
# Multiply Negative infinity number by 5
print('Multiplication : ',ntive_inf * 5)
# Addition to Negative infinity Number
print('Addition : ',ntive_inf + 5)
# Subtraction to Negative infinity Number
print('Subtraction : ',ntive_inf - 5)
# Division to Negative infinity Number
print('Division : ',ntive_inf / 5)
Output:
Multiplication : inf
Addition : inf
Subtraction : inf
Division : inf
Multiplication : -inf
Addition : -inf
Subtraction : -inf
Division : -inf