All arithmetic operations such as sum, subtraction, multiplication, and division perform an infinite value that always leads to (infinite number).
Infinity is primarily used to optimize algorithms and measure performance, which performs computations in a large-scale application.
What is infinity in Python?
In Python, infinity is described as something that has no end. So it means that it is not represented by an integer.
We all know that arithmetic operations produce a value that is (infinite). This is represented by a float value.
Furthermore, there is no possible method or way for an integer to be represented as an infinity.
This is because this is a fundamental characteristic of many popular programming languages, such as Java and PHP.
But being a dynamically typed language, Python can use float(inf) as an integer to represent (infinity).
Now here are different ways on how infinity in Python works:
1. Using float(‘inf’) and float(‘-inf’):
Infinity can both be a positive or a negative value, which can be represented as a float(‘inf’) or a float(‘-inf’) properly.
# Defining a positive infinite integer
positive_infinity = float('inf')
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = float('-inf')
print('Negative Infinity: ', negative_infinity)
Output:
Positive Infinity: inf
Negative Infinity: -inf
2. Using Python math module:
The Python math module is a standard module that is always available.
To use these mathematical functions, you have to import the import math module so that we can use it for the representation of infinite integers.
import math
# Defining a positive infinite integer
positive_infinity = math.inf
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = -math.inf
print('Negative Infinity: ', negative_infinity)
Output:
Positive Infinity: inf
Negative Infinity: -inf
3. Using Python decimal module:
The module name Python decimal is used to do related tasks in some decimal floating point numbers.
With the help of this module, it can provide a correctly rounded arithmetic floating point.
Also, using this module, we can also use it to represent infinite float values.
To use this, we have to declare it as Decimal(-infinity) for the negative and Decimal(infinity) for the positive infinite value.
from decimal import Decimal
# Defining a positive infinite integer
positive_infinity = Decimal('Infinity')
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = Decimal('-Infinity')
print('Negative Infinity: ', negative_infinity)
Output:
Positive Infinity: Infinity
Negative Infinity: -Infinity
4. Using Python NumPy library:
The Python NumPy module can also be used to represent infinite values.
You just need to use (np.inf) for positive and (-np) for negative infinite values.
The code below uses the NumPy library to represent infinite values.
import numpy as np
# Defining a positive infinite integer
positive_infinity = np.inf
print('Positive Infinity: ', positive_infinity)
# Defining a negative infinite integer
negative_infinity = -np.inf
print('Negative Infinity: ', negative_infinity)
Output:
Positive Infinity: inf
Negative Infinity: -inf
5. Check if a number is infinite in Python:
With the help of the isinf() method of the math library, which returns a value that is a boolean, we can easily check if that given number is infinite or not.
The code below uses the isinf() method to represent an infinite value.
import numpy as np
import math
# Defining a positive infinite integer
a = np.inf
# Defining a negative infinite integer
b = -np.inf
# Define a finite integer
c = 260
# check if a in infinite
print(math.isinf(a))
# check if b in infinite
print(math.isinf(b))
# check if c in infinite
print(math.isinf(c))
Output:
True
True
False
6. Compare infinite values to finite values in Python:
Comparing an infinite value to finite values is a very simple concept.
Once positive infinity is bigger than a natural number and negative infinity is smaller than negative numbers, we can easily compare them.
Let’s look at the sample code below for a better understanding:
import numpy as np
# Defining a positive infinite integer
a = np.inf
# Defining a negative infinite integer
b = -np.inf
# Define a finite + ve integer
c = 260
# Define a finite -ve integer
d = -260
# helper function to make comparisons
def compare(x, y):
if x>y:
print("True")
else:
print("False")
compare(a, b)
compare(a, c)
compare(a, d)
compare(b, c)
compare(b, d)
Output:
True
True
True
False
False
Check if a positive infinity number is equal to a negative infinity number in Python:
To check if the positive infinity number is equal to the negative infinity number, just use the (==) operator and the output should always be false.
The code below is a simple way to check if a positive infinity number is equal to a negative infinity number.
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 numbers that will output an infinite number
When you perform an arithmetic operation, whether positive or negative infinity numbers, the output will always be an infinite number and return true.
# Define positive infinity number
ptive_inf = float('inf')
# Multiply Positive infinity number by 26
print('Multiplication : ',ptive_inf * 26)
# Addition to Positive infinity Number
print('Addition : ',ptive_inf + 26)
# Subtraction to Positive infinity Number
print('Subtraction : ',ptive_inf - 26)
# Division to Positive infinity Number
print('Division : ',ptive_inf / 26)
# Define Negative infinity number
ntive_inf = float('-inf')
# Multiply Negative infinity number by 26
print('Multiplication : ',ntive_inf * 26)
# Addition to Negative infinity Number
print('Addition : ',ntive_inf + 26)
# Subtraction to Negative infinity Number
print('Subtraction : ',ntive_inf - 26)
# Division to Negative infinity Number
print('Division : ',ntive_inf / 26)
Output:
Multiplication : inf
Addition : inf
Subtraction : inf
Division : inf
Multiplication : -inf
Addition : -inf
Subtraction : -inf
Division : -inf
Arithmetic operations on an infinite number:
Since infinity is always a float value, this can be used in many different ways.
The code below is a program for arithmetic operations on an infinite number.
# Define positive infinity value
a = float('inf')
# Define Negative infinity value
b = float('-inf')
# Operations on positive infinity value
print('For Positive Infinity Value:')
print('Addition value : ',a + 26)
print('Subtraction value : ',a - 26)
print('Multiplication value: ',a * 26)
print('Division value: ',a / 26)
print("-----------------------------------")
# Operations on negative infinity value
print('For Negative Infinity Value:')
print('Addition value: ',b + 27)
print('Subtraction value: ',b - 27)
print('Multiplication value: ',b * 27)
print('Division value: ',b / 27)
Output:
For Positive Infinity Value:
Addition value : inf
Subtraction value : inf
Multiplication value: inf
Division value: inf
-----------------------------------
For Negative Infinity Value:
Addition value: -inf
Subtraction value: -inf
Multiplication value: -inf
Division value: -inf
Conclusion
In conclusion, infinity is something that has no end and is represented by a float value.
I hope this lesson has helped you learn a lot.
Check out our Python bisect Module with Different Examples for more life-changing tutorials that could help you a lot.