长整数的最大值 [英] Maximum value for long integer

查看:103
本文介绍了长整数的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

长整数:

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

<预><代码>>>>导入系统>>>类型(sys.maxsize)<输入'int'>>>>类型(sys.maxsize+1)<输入'长'>


对于我们有的整数

maxint 和 maxsize:

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

<块引用>

sys.maxint 常量被移除,因为不再有限制到整数值.但是, sys.maxsize 可以用作大于任何实际列表或字符串索引的整数.它符合实现的自然"整数大小,通常是相同的作为同一平台上以前版本中的 sys.maxint(假设相同的构建选项).

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

<块引用>

sys.maxint Python 正则支持的最大正整数整数类型.这至少是 2**31-1.最大的负整数is -maxint-1 — 使用 2 的补码导致的不对称性二进制算术.

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

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

<块引用>

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

浮动:

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

<预><代码>>>>导入系统>>>浮动(inf")>sys.maxsize真的

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

解决方案

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

这篇关于长整数的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆