isinf()
isinf(number) returns Boolean , True if the input number is positive infinite or negative infinity, False otherwise.
isinf(n)
Example
import math
print(math.isinf(2)) # False
print(math.isinf(-2)) # False
print(math.isinf(0.0)) # False
print(math.isinf(0/1)) # False
print(math.isinf(0.0/1)) # False
Note : 0.0 is considered finite.
import math
print(math.isinf(float('inf'))) # True
print(math.isinf(float('-inf'))) # True
print(math.isinf(float('nan'))) # False
Infinity in Calculations:
Overflowing a large number results in infinity.
print(math.isinf(1e308 * 2)) # Output: True
Infinity from Division:
Division by zero results in positive infinity.
print(math.isinf(1 / 0.0)) # Output: True
Using with Lists:
This filters out infinity values from a list.
numbers = [10, float('inf'), float('-inf'), 42]
inf_values = [n for n in numbers if math.isinf(n)]
print(inf_values) # Output: [inf, -inf]
Difference Between isinf() and isnan()
import math
print(math.isinf(float('inf'))) # True
print(math.isnan(float('nan'))) # True
Use Case: Handling Division by Zero
import math
try:
result = 1.0 / 0.0
except ZeroDivisionError:
result = float('inf')
if math.isinf(result):
print("Result is infinity")
isfinite() »
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com