Python cmath 模块的 cmath.isinf() 函数检查给定值是正无限还是负无限。
如果指定的数字是无限的,则此函数返回 True,否则返回 False。
例如,如果我们有一个浮点数 “x = float('inf')”,它表示正无穷大,即 “True”。
语法
以下是 Python cmath.isinf() 函数的基本语法 -
cmath.isinf(x)
参数
此函数接受数值作为参数。将浮点数表示为无穷大。
返回值
此方法返回一个布尔值,即 True 或 False。
示例 1
在下面的示例中,我们正在使用 cmath.isinf() 函数 - 校正正无穷大是否为无限 -
import cmath
x = cmath.isinf(float('inf'))
print(x)
输出
获得的输出如下 -
True
示例 2
在这里,我们使用 cmath.isinf() 函数检查负无穷大是否为无穷大 -
import cmath
res = cmath.isinf(float('-inf'))
print(res)
输出
以下是上述代码的输出 -
True
示例 3
现在,我们正在使用 cmath.isinf 函数检查 “100” 是否为无限 -
import cmath
result = cmath.isinf(float(100))
print("The result is:",result)
输出
我们将得到如下所示的输出 -
The result is: False
示例 4
在此示例中,我们使用 cmath.isinf() 函数检查 NaN(Not a Number) 是否为无限大 -
import cmath
result = cmath.isinf(float('NaN'))
print(result)
输出
结果如下 -
False