Python数学库 | isnan() 方法
Python有math
库并且有很多关于它的函数。一个这样的函数是isnan()
。此方法用于检查给定参数是否为有效数字。
Syntax : math.isnan(x)
Parameters :
x [Required] : It is any valid python data type or any number.
Returns: Return type is boolean.
-> Returns False if the given parameter is any number(positive or negative)
-> Returns True if given parameter is NaN (Not a Number).
代码#1:
# Python3 code to demonstrate
# the working of isnan()
import math
# initializing the value
test_int = 4
test_neg_int = -3
test_float = 0.00
# checking isnan() values
# with different numbers
print (math.isnan(test_int))
print (math.isnan(test_neg_int))
print (math.isnan(test_float))
输出:
False
False
False
代码#2:
# Python3 code to demonstrate
# the working of isnan()
import math
# checking isnan() values
# with inbuilt numbers
print (math.isnan(math.pi))
print (math.isnan(math.e))
# checking for NaN value
print (math.isnan(float('nan')))
输出:
False
False
True