如何在Python中表示整数的最小值和最大值?在Java中,我们有Integer。MIN_VALUE和Integer.MAX_VALUE。
请参见:Python中的最大浮点数是什么?
如何在Python中表示整数的最小值和最大值?在Java中,我们有Integer。MIN_VALUE和Integer.MAX_VALUE。
请参见:Python中的最大浮点数是什么?
当前回答
你可以这样使用'inf':
import math
bool_true = 0 < math.inf
bool_false = 0 < -math.inf
参考:math -数学函数
其他回答
你可以这样使用'inf':
import math
bool_true = 0 < math.inf
bool_false = 0 < -math.inf
参考:math -数学函数
sys。maxsize常量已从Python 3.0开始移除,取而代之的是使用sys.maxsize。
Integers PEP 237: Essentially, long renamed to int. That is, there is only one built-in integral type, named int; but it behaves mostly like the old long type. ... 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).
如果你只需要一个比其他所有数字都大的数字,你可以使用
float('inf')
小数:以类似的方式比其他数都小的数:
float('-inf')
这在python2和python3中都有效。
我非常依赖这样的命令。
python -c 'import sys; print(sys.maxsize)'
返回的最大int值:9223372036854775807
有关'sys'的更多参考,您可以访问
https://docs.python.org/3/library/sys.html
https://docs.python.org/3/library/sys.html#sys.maxsize
sys。Maxsize并不是实际支持的最大整数值。您可以将maxsize加倍并将其自身相乘,它仍然是一个有效且正确的值。
然而,如果你尝试sys。Maxsize ** sys。Maxsize,它将挂你的机器相当长的时间。正如许多人指出的那样,字节和位大小似乎并不相关,因为它实际上不存在。我猜当python需要更多的内存空间时,它会很高兴地扩展它的整数。所以一般来说没有限制。
现在,如果您谈论的是以一种安全的方式打包或存储整数,以便以后可以完整地检索它们,那么当然这是相关的。我真的不确定打包,但我知道python的pickle模块处理这些事情很好。字符串表示显然没有实际限制。
所以,底线是:你的应用限制是什么?数值数据需要什么?使用这个限制,而不是python中几乎不存在的整数限制。