Python isnan

Python的isnan函数是用于检查给定值是否为有效数字的数学函数之一。如果它是正数或负数,则返回False,否则返回True。

此编程语言中isnani的语法如下所示。

math.isnan(value)

Python isnan示例

这是一个简单的isnani函数示例,用于检查零是否为有效数字。

import math
 
print(math.isnan(0))
False

在这里,我们将此isnani函数用于不同的正数和负数值。我们还在该方法中使用了math.pi。

import math
 
print(math.isnan(1))
 
print(math.isnan(0.00))
 
print(math.isnan(-2))
 
print(math.isnan(-4.0002))
 
print(math.isnan(math.pi))
False
False
False
False
False

让我来检查一下这个Python方法是否能处理NaN(非数字)。

import math
 
print(math.isnan(float('NaN')))
True

在这个数学函数示例中,我们将使用无穷大、math.inf、非数字值作为方法参数。

import math
 
print(math.isnan(float('NaN')))
 
print(math.isnan(float('inf')))
 
print(math.isnan(float('-inf')))
 
print(math.isnan(math.inf))
 
print(math.isnan(-math.inf))
isnan Function Example