The math.isnan() method is used to check if a value is a NaN (Not a Number).
Syntax
import math
math.isnan(x)
Parameters
x(required): The value to be checked.
Return Value
Returns boolean value:
- True: If the given value is NaN(not a number)
- False: If the given value is a number
Visual Representation
Example 1: How to Use math.isnan() Method
import math
print(math.isnan(15)) #positive number
print(math.isnan(-15)) #negative number
print(math.isnan(5.55)) #float number
print(math.isnan(float('nan')))
Output
False
False
False
True
Example 2: Passing math.pi and math.e
import math
print(math.isnan(math.pi))
print(math.isnan(math.e))
Output
False
False
Example 3: Passing positive and negative infinity
import math
print(math.isnan(float('inf'))) # Check if positive infinity is NaN
print(math.isnan(float('-inf'))) # Check if negative infinity is NaN
Output
False
False
Related posts
Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog. He is also expert in JavaScript and Python development.