带有Python示例的math.isnan()方法

Python math.isnan() 方法

math.isnan()方法数学模块的库方法,用于检查给定数字是否为“ NaN”(不是数字),它接受一个数字,如果给定数字为“ NaN”,则返回True,否则返回False。

它的语法 math.isnan() 方法:

    math.isnan(n)

Parameter(s): n –必须检查是否为“ NaN”的数字。

返回值: bool –返回布尔值(“ True”或“ False”)。

示例

    Input:
    a = 10
    b = float('inf')
    c = float('nan')

    # 函数调用
    print(math.isnan(a))
    print(math.isnan(b))
    print(math.isnan(c))

    Output:
    False
    False
    True

Python代码演示示例 math.isnan() 方法

# python代码演示示例 
# math.isnan() method

# 导入数学模块
import math

# math.isnan() method test on finite value
print(math.isnan(10))
print(math.isnan(0))
print(math.isnan(10.23))
print(math.isnan(0.0))


# math.isnan() method test on infinite and NaN value
print(math.isnan(float('inf')))
print(math.isnan(float('-inf')))
print(math.isnan(float('nan')))

输出结果

False
False
False
False
False
True