gpt4 book ai didi

python - 长整数的最大值

转载 作者:IT老高 更新时间:2023-10-28 21:07:31 25 4
gpt4 key购买 nike

如何将长整数的最大值分配给变量,例如,类似于 C++ 的 LONG_MAX

最佳答案

长整数:

没有明确定义的限制。可用地址空间的数量构成了实际限制。
(取自 this 网站)。请参阅 Numeric Types 上的文档您将在其中看到 长整数具有无限精度。在 Python 2 中,整数超出限制时会自动切换为长整数:

>>> import sys
>>> type(sys.maxsize)
<type 'int'>
>>> type(sys.maxsize+1)
<type 'long'>


对于我们有的整数

maxint 和 maxsize:

可以在 Python 2.x 中使用 sys.maxint 找到 int 的最大值。它在 Python 3 中被删除,但通常可以使用 sys.maxsize 代替。来自 the changelog :

The sys.maxint constant was removed, since there is no longer a limit to the value of integers. However, sys.maxsize can be used as an integer larger than any practical list or string index. It conforms to the implementation’s “natural” integer size and is typically the same as sys.maxint in previous releases on the same platform (assuming the same build options).

对于任何对 the difference 感兴趣的人(Python 2.x):

sys.maxint The largest positive integer supported by Python’s regular integer type. This is at least 2**31-1. The largest negative integer is -maxint-1 — the asymmetry results from the use of 2’s complement binary arithmetic.

sys.maxsize The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have.

为了完整起见,这里是 Python 3 version :

sys.maxsize An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2^31 - 1 on a 32-bit platform and 2^63 - 1 on a 64-bit platform.

float :

float("inf")float("-inf")。这些可以与其他数字类型进行比较:

>>> import sys
>>> float("inf") > sys.maxsize
True

关于python - 长整数的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9860588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com