Python math.isfinite() 方法
math.isfinite()方法是数学模块的一种库方法,用于检查给定数字是否为非有限数,它接受数字(整数/浮点数,有限,无限或NaN),如果数字既不是无穷大也不是非零,则返回True NaN(非数字),否则返回False。
它的语法 math.isfinite() 方法:
math.isfinite(n)
Parameter(s): n –一个必须检查的数字,它是否有限。
返回值: bool –返回布尔值(“ True”或“ False”)。
示例
Input:
a = 10
b = float('inf')
# 函数调用
print(math.isfinite(a))
print(math.isfinite(b))
Output:
True
False
Python代码演示示例 math.isfinite() 方法
# python代码演示示例
# math.isfinite() method
# 导入数学模块
import math
# math.isfinite() method test on finite value
print(math.isfinite(10))
print(math.isfinite(0))
print(math.isfinite(10.23))
print(math.isfinite(0.0))
# math.isfinite() method test on infinite value
print(math.isfinite(float('inf')))
print(math.isfinite(float('-inf')))
print(math.isfinite(float('nan')))
输出结果
True
True
True
True
False
False
False
初步格式
点击上传
X