关于python:int的最大值和最小值

Maximum and Minimum values for ints

我在python中寻找整数的最小值和最大值。 例如,在Java中,我们有Integer.MIN_VALUEInteger.MAX_VALUE。 在python中有这样的东西吗?


Python 3

在Python 3中,这个问题不适用。普通的int类型是无界的。

但是,您实际上可能正在寻找机器的字大小。这仍然在Python 3中作为sys.maxsize提供。

Python 2

在Python 2中,plain int值的最大值可用作sys.maxint

1
2
>>> sys.maxint
9223372036854775807

您可以使用-sys.maxint - 1计算最小值,如下所示。

一旦超过此值,Python就会从普通整数切换为长整数。所以大多数时候,你不需要知道它。


sys.maxint常量已从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.
  • PEP 238: An expression like 1/2 returns a float. Use 1//2 to get the truncating behavior. (The latter syntax has existed for years, at
    least since Python 2.2.)
  • 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 repr() of a long integer doesn’t include the trailing L anymore, so code that unconditionally strips that character will chop off the
    last digit instead. (Use str() instead.)
  • Octal literals are no longer of the form 0720; use 0o720 instead.

请参阅:https://docs.python.org/3/whatsnew/3.0.html


如果你只需要一个比其他数字更大的数字,你可以使用

1
float('inf')

以类似的方式,比其他所有人都小:

1
float('-inf')

这适用于python 2和3。


在Python中,一旦传递值sys.maxint,整数将自动从固定大小的int表示切换为可变宽度long表示,取决于您的平台,该值为231 - 1或263 - 1。注意这里附加的L

1
2
3
4
>>> 9223372036854775807
9223372036854775807
>>> 9223372036854775808
9223372036854775808L

从Python手册:

Numbers are created by numeric literals or as the result of built-in functions and operators. Unadorned integer literals (including binary, hex, and octal numbers) yield plain integers unless the value they denote is too large to be represented as a plain integer, in which case they yield a long integer. Integer literals with an 'L' or 'l' suffix yield long integers ('L' is preferred because 1l looks too much like eleven!).

Python非常努力地假装它的整数是数学整数并且是无界的。例如,它可以轻松地计算一个googol:

1
2
>>> 10**100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L


对于Python 3,它是

1
2
3
import sys
max = sys.maxsize
min = -sys.maxsize -1


使用sys模块(python 3)

1
2
3
4
5
6
7
8
import sys

INT_MAX = sys.maxsize  

INT_MIN = -sys.maxsize-1


print(INT_MAX,INT_MIN)


如果你想要数组或列表索引的最大值(相当于C / C ++中的size_t),你可以使用numpy:

1
np.iinfo(np.intp).max

这与sys.maxsize相同,但是优点是您不需要为此导入sys。

如果你想在机器上使用max for native int:

1
np.iinfo(np.intc).max

您可以在doc中查看其他可用类型。

对于浮动,您也可以使用sys.float_info.max


我非常依赖这样的命令。

1
python -c 'import sys; print(sys.maxsize)'

Max int返回:9223372036854775807

有关'sys'的更多参考,您应该访问

https://docs.python.org/3/library/sys.html

https://docs.python.org/3/library/sys.html#sys.maxsize


您可以像这样使用'inf':

import math
bool_true = 0

参考:数学 - 数学函数