问题:整数的最大值和最小值

我正在寻找python中整数的最小值和最大值。例如,在Java中,我们有Integer.MIN_VALUEInteger.MAX_VALUE。python中是否有类似的东西?

I am looking for minimum and maximum values for integers in python. For eg., in Java, we have Integer.MIN_VALUE and Integer.MAX_VALUE. Is there something like this in python?


回答 0

Python 3

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

但是,您实际上可能正在寻找有关当前解释器的字长的信息,在大多数情况下,该信息将与机器的字长相同。该信息仍在Python 3中以形式提供sys.maxsize,这是一个有符号的单词可以表示的最大值。等效地,它是最大可能列表或内存序列的大小

通常,无符号字可表示的最大值为,字中sys.maxsize * 2 + 1的位数为math.log2(sys.maxsize * 2 + 2)。有关更多信息,请参见此答案

Python 2

在Python 2中,纯int值的最大值可作为sys.maxint

>>> sys.maxint
9223372036854775807

你可以计算与最小值-sys.maxint - 1如图所示这里

一旦超过此值,Python就会从纯整数无缝转换为长整数。因此,大多数时候,您不需要了解它。

Python 3

In Python 3, this question doesn’t apply. The plain int type is unbounded.

However, you might actually be looking for information about the current interpreter’s word size, which will be the same as the machine’s word size in most cases. That information is still available in Python 3 as sys.maxsize, which is the maximum value representable by a signed word. Equivalently, it’s the size of the largest possible list or in-memory sequence.

Generally, the maximum value representable by an unsigned word will be sys.maxsize * 2 + 1, and the number of bits in a word will be math.log2(sys.maxsize * 2 + 2). See this answer for more information.

Python 2

In Python 2, the maximum value for plain int values is available as sys.maxint:

>>> sys.maxint
9223372036854775807

You can calculate the minimum value with -sys.maxint - 1 as shown here.

Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won’t need to know it.


回答 1

如果您只需要一个大于所有其他数字的数字,则可以使用

float('inf')

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

float('-inf')

这适用于python 2和3。

If you just need a number that’s bigger than all others, you can use

float('inf')

in similar fashion, a number smaller than all others:

float('-inf')

This works in both python 2 and 3.


回答 2

sys.maxint常数已经在Python 3.0取出以后,改为使用sys.maxsize

整数

  • PEP 237:从本质上讲,long已重命名为int。也就是说,只有一个内置的整数类型,称为int;但它的行为基本上类似于旧的long类型。
  • PEP 238:类似1/2的表达式返回浮点数。使用1 // 2获得截断行为。(至少从Python 2.2开始,后一种语法已经存在多年了。)
  • sys.maxint常量已删除,因为整数值不再受限制。但是,sys.maxsize可以用作大于任何实际列表或字符串索引的整数。它符合实现的“自然”整数大小,并且通常与同一平台上的先前版本中的sys.maxint相同(假定具有相同的生成选项)。
  • 长整数的repr()不再包含结尾的L,因此无条件剥离该字符的代码将砍掉最后一位数字。(改为使用str()。)
  • 八进制文字不再采用0720的形式;请改用0o720。

参考:https : //docs.python.org/3/whatsnew/3.0.html#integers

The sys.maxint constant has been removed from Python 3.0 onward, instead use 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.

Refer : https://docs.python.org/3/whatsnew/3.0.html#integers


回答 3

在Python中,一旦您传递值,整数将自动从固定大小的int表示形式转换为宽度可变的long表示形式,该值取决于平台sys.maxint是2 311-1或2 63-1。请注意L,此处已附加:

>>> 9223372036854775807
9223372036854775807
>>> 9223372036854775808
9223372036854775808L

Python手册

数字是通过数字文字或内置函数和运算符创建的。未经修饰的整数文字(包括二进制,十六进制和八进制数字)将生成纯整数,除非它们表示的值太大而无法表示为纯整数,在这种情况下,它们将生成一个长整数。带'L''l'后缀的整数文字产生长整数('L'首选,因为1l看起来太像11!)。

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

>>> 10**100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L

In Python integers will automatically switch from a fixed-size int representation into a variable width long representation once you pass the value sys.maxint, which is either 231 – 1 or 263 – 1 depending on your platform. Notice the L that gets appended here:

>>> 9223372036854775807
9223372036854775807
>>> 9223372036854775808
9223372036854775808L

From the Python manual:

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 tries very hard to pretend its integers are mathematical integers and are unbounded. It can, for instance, calculate a googol with ease:

>>> 10**100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L

回答 4

对于Python 3,它是

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

For Python 3, it is

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

回答 5

您可以这样使用’inf’:

import math
bool_true = 0 < math.inf
bool_false = 0 < -math.inf

请参阅:数学—数学函数

You may use ‘inf’ like this:

import math
bool_true = 0 < math.inf
bool_false = 0 < -math.inf

Refer: math — Mathematical functions


回答 6

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

np.iinfo(np.intp).max

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

如果要在计算机上将max用于本机int:

np.iinfo(np.intc).max

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

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

If you want the max for array or list indices (equivalent to size_t in C/C++), you can use numpy:

np.iinfo(np.intp).max

This is same as sys.maxsize however advantage is that you don’t need import sys just for this.

If you want max for native int on the machine:

np.iinfo(np.intc).max

You can look at other available types in doc.

For floats you can also use sys.float_info.max.


回答 7

我严重依赖这样的命令。

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

返回的最大整数:9223372036854775807

有关“ sys”的更多参考,请访问

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

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

I rely heavily on commands like this.

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

Max int returned: 9223372036854775807

For more references for ‘sys’ you should access

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

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。