Python isnan()函数示例| Python数学方法

Python isnan()是一个内置的数学函数,用于检查给定输入是否为有效数字。 isnan()函数位于数学库中,因此要使用此函数,我们首先必须导入isnan()函数。

Python isnan()

内容概述

  • 1个Python isnan()
  • 2结论
  • 3另请参见

isnan()函数用于确定给定参数是否为有效数字。如果给定的数字x作为参数是有效的Python数字(正数或负数),isnan()函数将返回False。

句法

math.isnan(x)

math.isnan()函数仅采用一个参数x,该参数x是python中的任何有效数据类型。

返回值

isnan()函数返回两种类型的值:

True:如果给定的参数不是数字

False:如果给定参数是数字

编程范例

请参阅以下代码。

# app.py

# Importing math library
import math

# Checking working of isnan()

# When the number is integer
print(math.isnan(15))
# When the number is float
print(math.isnan(14.55))
# When the number is negative
print(math.isnan(-34))
# When the number is not finite
print(math.isnan(float('nan')))

输出量

False
False
False
True

在此程序中,我们导入了数学库,然后使用各种输入检查了isnan()函数的输出。

我们分别检查了输出的整数,浮点数和负数。对于所有这些情况,输出为False。然后,我们检查了nan的输出,此处的输出为True,因为nan不是数字。

我们来看另一个例子。

# app.py

import math

# checking isnan() values
print(math.isnan(math.pi))
print(math.isnan(math.e))

输出量

python3 app.py
False
False

结论

如果要在Python中检查NaN值,请使用数学库提供的Python isnan()函数。

最后,Python isnan()函数示例已结束。

也可以看看

Python isinf()

Python isfinite()

Python fsum()

Python fmod()

Python acos()

资讯来源:由0x资讯编译自APPDIVIDEND,版权归作者Ankit Lathiya所有,未经许可,不得转载
你可能还喜欢