Python math.isfinite() Method
Musfirah Waseem
Jan 30, 2023
Python math.isfinite()
method is an efficient way of finding whether a number is finite or not. Note that 0.0
is considered a finite number.
Syntax
math.isfinite(x)
Parameters
x |
The required value (negative or positive) to check. |
Returns
This method returns a Boolean value. It returns True
if x
is a finite value or not a NaN
; otherwise, False
.
Example Code: Use of the math.isfinite()
Method
Example Code:
import math
x = 100
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")
x = 0
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")
x = -98.67
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")
x = math.inf
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")
x = -math.inf
value = math.isfinite(x)
print(f"Is {x} a finite number? {value}")
value = math.isfinite((float("nan")))
print(f"Is NaN a finite number? {value}")
Output:
Is 100 a finite number? True
Is 0 a finite number? True
Is -98.67 a finite number? True
Is inf a finite number? False
Is -inf a finite number? False
Is NaN a finite number? False
Note that the above code shows how we can use this method.
Author: Musfirah Waseem
Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.
LinkedIn