Python math.isinf() 方法

Python math 模块 Python math 模块

Python math.isinf() 方法判断 x 是否是无穷大,如果 x 是正或负无穷大,则返回 True ,否则返回 False 。

Python 版本:2.6

语法

math.isinf() 方法语法如下:

math.isinf(x)

参数说明:

  • x -- 必需,数字。如果 x 不是一个数字,返回 TypeError。

返回值

返回一个布尔值,如果 x 是正或负无穷大,则返回 True ,否则返回 False 。

实例

以下实例检查数字是否是无穷大:

实例

# 导入 math 包
import math

# 检查数字是否是无穷大
print(math.isinf(56))
print(math.isinf(-45.34))
print(math.isinf(+45.34))
print(math.isinf(math.inf))
print(math.isinf(float("nan")))
print(math.isinf(float("inf")))
print(math.isinf(float("-inf")))
print(math.isinf(-math.inf))

输出结果:

False
False
False
True
False
True
True
True

Python math 模块 Python math 模块