问题:长整数的最大值

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

How can I assign the maximum value for a long integer to a variable, similar, for example, to C++’s LONG_MAX.


回答 0

长整数:

没有明确定义的限制。可用地址空间的数量构成了实际限制。
(摘自网站)。请参阅“ 数字类型 ”文档,在该文档中您会看到Long integers have unlimited precision。在Python 2中,当整数超出限制时,整数将自动切换为long:

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


对于整数,我们有

maxint和maxsize:

int的最大值可以在Python 2.x中使用找到sys.maxint。它已在Python 3中删除,但sys.maxsize通常可以代替使用。从变更日志

sys.maxint常量已删除,因为整数值不再受限制。但是,sys.maxsize可以用作大于任何实际列表或字符串索引的整数。它符合实现的“自然”整数大小,并且通常与同一平台上的先前发行版中的sys.maxint相同(假定具有相同的生成选项)。

并且,对于任何对差异感兴趣的人(Python 2.x):

sys.maxint Python的常规整数类型支持的最大正整数。这至少是2 ** 31-1。最大的负整数是-maxint-1-不对称性是由于使用2的补码二进制算法引起的。

sys.maxsize平台的Py_ssize_t类型支持的最大正整数,因此列表,字符串,字典和许多其他容器可以具有的最大大小。

为了完整起见,这是Python 3版本

sys.maxsize 一个整数,赋予最大值Py_ssize_t类型的变量可以采用的值。在32位平台上通常为2 ^ 31-1,在64位平台上通常为2 ^ 63-1。

浮动:

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

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

Long integers:

There is no explicitly defined limit. The amount of available address space forms a practical limit.
(Taken from this site). See the docs on Numeric Types where you’ll see that Long integers have unlimited precision. In Python 2, Integers will automatically switch to longs when they grow beyond their limit:

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


for integers we have

maxint and maxsize:

The maximum value of an int can be found in Python 2.x with sys.maxint. It was removed in Python 3, but sys.maxsize can often be used instead. From 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).

and, for anyone interested in 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.

and for completeness, here’s the 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.

floats:

There’s float("inf") and float("-inf"). These can be compared to other numeric types:

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

回答 1

Python long可以任意大。如果您需要一个大于任何其他值的值,则可以使用float('inf'),因为Python 可以轻松比较不同类型的数字值。同样,对于小于任何其他值的值,您可以使用float('-inf')

Python long can be arbitrarily large. If you need a value that’s greater than any other value, you can use float('inf'), since Python has no trouble comparing numeric values of different types. Similarly, for a value lesser than any other value, you can use float('-inf').


回答 2

标题问题的直接答案:

整数的大小不受限制,在Python中没有最大值。

回答哪个可以解决基本用例:

根据您对要做的事情的评论,您目前正在考虑以下方面的想法:

minval = MAXINT;
for (i = 1; i < num_elems; i++)
    if a[i] < a[i-1]
        minval = a[i];

那不是在Python中思考的方式。到Python更好的翻译(但仍然不是最好的)是

minval = a[0]  # Just use the first value
for i in range(1, len(a)):
    minval = min(a[i], a[i - 1])

请注意,上面根本没有使用MAXINT。该解决方案的这一部分适用于任何编程语言:您无需仅在集合中找到最小值就知道最大的可能值。

但是无论如何,您真正在Python中所做的只是

minval = min(a)

也就是说,您根本不需要编写循环。内置min()函数获取整个集合的最小值。

Direct answer to title question:

Integers are unlimited in size and have no maximum value in Python.

Answer which addresses stated underlying use case:

According to your comment of what you’re trying to do, you are currently thinking something along the lines of

minval = MAXINT;
for (i = 1; i < num_elems; i++)
    if a[i] < a[i-1]
        minval = a[i];

That’s not how to think in Python. A better translation to Python (but still not the best) would be

minval = a[0]  # Just use the first value
for i in range(1, len(a)):
    minval = min(a[i], a[i - 1])

Note that the above doesn’t use MAXINT at all. That part of the solution applies to any programming language: You don’t need to know the highest possible value just to find the smallest value in a collection.

But anyway, what you really do in Python is just

minval = min(a)

That is, you don’t write a loop at all. The built-in min() function gets the minimum of the whole collection.


回答 3

longPython 2.x中的type使用任意精度算法,没有最大可能值之类的东西。它受可用内存的限制。对于无法由本机整数表示的值,Python 3.x没有特殊的类型-一切都已准备就绪,int并且转换在后台进行。

long type in Python 2.x uses arbitrary precision arithmetic and has no such thing as maximum possible value. It is limited by the available memory. Python 3.x has no special type for values that cannot be represented by the native machine integer — everything is int and conversion is handled behind the scenes.


回答 4

与C / C ++不同,Python中的Long具有无限的精度。 有关更多信息,请参见python中的数字类型部分。要确定整数的最大值,您可以参考sys.maxint。您可以从sys文档中获取更多详细信息。

Unlike C/C++ Long in Python have unlimited precision. Refer the section Numeric Types in python for more information.To determine the max value of integer you can just refer sys.maxint. You can get more details from the documentation of sys.


回答 5

您可以使用:float的最大值为

float('inf')

否定的

float('-inf')

You can use: max value of float is

float('inf')

for negative

float('-inf')

回答 6

在python3中,您可以将浮点值发送到int函数中,以整数表示形式获取该值1.7976931348623157e + 308。

import sys    
int(sys.float_info.max)

In python3, you can send the float value into the int function the get that number 1.7976931348623157e+308 in integer representation.

import sys    
int(sys.float_info.max)

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