Python - AI 助手

Python cmath.isinf() 函数



Python 的 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(非数字)是否为无穷大:

import cmath
result = cmath.isinf(float('NaN'))
print(result)

输出

结果如下:

False
python_modules.htm
广告
© . All rights reserved.