Python cmath.isnan() Function
The Python cmath.isnan() function verifies whether a value is NaN(Not a Number) or not. This function returns the Boolean value, i.e., True if the value is NaN, otherwise False.
A number "x" is said to be NaN if it doesn't represent a real number and can't be expressed as a finite value, i.e., positive or negative infinity. NaN often arises a result of undefined operations, such as taking the square root of a negative number.
Syntax
Following is the basic syntax of the Python cmath.isnan() function −
cmath.isnan(x)
Parameters
This function accepts a numeric value as a parameter and redirects the value to be NaN.
Return Value
This function returns a boolean value which is True is the given number is Nan else it returns False.
Example 1
In the below example, we are verifying if the floating point number "20.5" is "NaN" using cmath.isnan() function −
import cmath x = cmath.isnan(20.5) print(x)
Output
The output obtained is as follows −
False
Example 2
Here, we are rectifying if positive infinity is NaN using cmath.isnan() function −
import cmath
result = cmath.isnan(float('inf'))
print("The result is:",result)
Output
Following is the result for the above code −
The result is: False
Example 3
Now, when we use variable "x" to store NaN. Then this cmath.isnan() function gives positive output.
import cmath
x = float('nan')
y = cmath.isnan(x)
print(y)
Output
We will get the output as follows −
True
Example 4
In the following example, if we are passing string as the input, then this cmath.isnan() function gives TypeError.
import cmath
res = cmath.isnan("Welcome to Tutorialspoint")
print(res)
Output
The result produced is as follows −
Traceback (most recent call last):
File "/home/cg/root/86486/main.py", line 2, in
res = cmath.isnan("Welcome to Tutorialspoint")
TypeError: must be real number, not str
python_modules.htm