Python Infinity
Although, unexpected as it might appear infinity may be characterized as an unclear number that can either be a positive or negative worth. All math tasks performed on an infinite worth generally lead to an infinite number, say it be aggregate, deduction, increase, or some other activity.
In the realm of software engineering, infinity is for the most part used to gauge execution and advance calculations that perform calculations for an enormous scope application.
Addressing infinity as an Integer in python
The idea of addressing infinity as an integer disregards the meaning of infinity itself. Be that as it may, in python, as it is a unique language, float values can be utilized to address an infinite integer. One can utilize float('inf') as an integer to address it as infinity. The following is the rundown of ways one can address infinity in Python.
1. float('inf') and float('- inf'):
As infinity can be both positive and negative they can be addressed as a float('inf') and float('- inf') individually. The beneath code shows the execution of the above-talked about content:
positive_infinity = float('inf')
print('Positive Infinity: ', positive_infinity)
negative_infinity = float('- inf')
print('Negative Infinity: ', negative_infinity)
Output:
Positive Infinity: inf
Negative Infinity: - inf
2. utilizing Python's math module:
Python's mathematical module can likewise be utilized for addressing infinite integers. The beneath code shows how it is finished:
import math
pstive_infinity = math.inf
print('Positive Infinity: ', pstive_infinity)
ngtive_infinity = - math.inf
print('Negative Infinity: ', ngtive_infinity)
Output:
Positive Infinity: inf
Negative Infinity: - inf
3. utilizing Python's decimal module:
Python's decimal module can likewise be utilized for addressing infinite float values.
It is utilized as Decimal('Infinity') for positive and Decimal('- Infinity') for negative infinite worth.
The beneath code shows its execution:
from decimal import Decimal
import math
positive_infinity = Decimal('Infinity')
print('Positive Infinity: ', positive_infinity)
negative_infinity = Decimal('- Infinity')
print('Negative Infinity: ', negative_infinity)
Output:
Positive Infinity: Infinity
Negative Infinity: - Infinity
4. utilizing Python's Numpy library:
Python's Numpy module can likewise be utilized for addressing infinite qualities. It is utilized as np.inf for positive and - np.inf for negative infinite worth. The utilization of Numpy library for addressing an infinite worth is displayed in the code underneath:
import numpy as np
positive_infinity = np.inf
print('Positive Infinity: ', positive_infinity)
negative_infinity = - np.inf
print('Negative Infinity: ', negative_infinity)
Output:
Positive Infinity: inf
Negative Infinity: - inf
Comparing finite and infinite values
To beware of the remote possibility that a given number is infinite or not, one can use isinf() technique for the numerical library which returns a boolean worth. The beneath code shows the utilization of isinf() technique:
import numpy as np
import math
a = np.inf
b = - np.inf
c = 300
print(math.isinf(a))
print(math.isinf(b))
print(math.isinf(c))
Output:
True
True
False
Contrasting infinite values with finite values in python
The idea of contrasting an infinite worth with limited qualities is pretty much basic. As certain infinity is greater all of the time than each regular number and negative infinity is more modest 100% of the time than negative numbers. For better agreement investigate the code underneath:
import numpy as np
a = np.inf
b = - np.inf
c = 300
d = - 300
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
Checking if positive and negative infinite values are equal
import math
ptive_inf = float('inf')
ntive_inf = float('-inf')
print('Positive infinity equal to Negative infinity: ',ptive_inf == ntive_inf)
Output:
Positive infinity equal to Negative infinity: False