我如何在Python中表示一个无限数?

2024-04-26 22:29:54 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

自Python 3.5以来,您可以使用math.inf

>>> import math
>>> math.inf
inf

在Python中,您可以执行以下操作:

test = float("inf")

在Python3.5中,您可以执行以下操作:

import math
test = math.inf

然后:

test > 1
test > 10000
test > x

永远都是真的。当然,除非如前所述,x也是无穷大或“nan”(“不是数字”)。

另外(仅限Python 2.x),与Ellipsis相比,float(inf)更小,例如:

float('inf') < Ellipsis

会回到现实。

似乎没有人明确提到过负无穷大,所以我想我应该加上它。

对于正无穷大(只是为了完整性):

math.inf

对于负无穷大:

-math.inf

相关问题 更多 >