Python中的无限整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24587994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Infinite integer in Python
提问by Carlo Pires
Python 3 has float('inf')
and Decimal('Infinity')
but no int('inf')
. So, why a number representing the infinite set of integers is missing in the language? Is int('inf')
unreasonable?
Python 3 有float('inf')
,Decimal('Infinity')
但没有int('inf')
。那么,为什么语言中缺少代表无限整数集的数字?是int('inf')
不合理的?
回答by S?ren V. Poulsen
Taken from here: https://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
取自这里:https: //www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
IEEE 754 floating point numbers can represent positive or negative infinity, and NaN (not a number)
IEEE 754 浮点数可以表示正无穷大或负无穷大,以及 NaN(不是数字)
That is, the representation of float
and Decimal
can store these special values. However, there is nothing within the basic type int
that can store the same. As you exceed the limit of 2^32 in an unsigned 32-bit int, you simply roll over to 0 again.
也就是说,代表性float
和Decimal
可存储这些特殊值。但是,基本类型int
中没有任何东西可以存储相同的内容。当您在无符号 32 位 int 中超过 2^32 的限制时,您只需再次返回到 0。
If you want, you could create a class containing an integer which could feature the possibility of infinite values.
如果需要,您可以创建一个包含整数的类,该整数可以具有无限值的可能性。
回答by Jacques Sauvé
For python 2. It is sometimes the case that you need a very large integer. For example, I may want to produce a subarray with x[:n] and, I may wish to sometimes set n to a value such that the whole array will be produced. Since you can't use a float for n (python wants an integer), you need a "large integer". A good way of doing this is to use the largest integer available: sys.maxint. Here is an example:
对于python 2。有时您需要一个非常大的整数。例如,我可能想用 x[:n] 生成一个子数组,有时我可能希望将 n 设置为一个值,以便生成整个数组。由于您不能对 n 使用浮点数(python 需要一个整数),因此您需要一个“大整数”。这样做的一个好方法是使用可用的最大整数:sys.maxint。下面是一个例子:
# MAX_SOURCES = sys.maxint # normal setting
MAX_SOURCES = 5 # while testing
# code to use an array ...
... sources[:MAX_SOURCES]
So, while testing, I could use a smaller sources array but use the full array in production.
因此,在测试时,我可以使用较小的源数组,但在生产中使用完整的数组。
回答by Neil G
You are right that an integer infinity is possible, and that none has been added to the Python standard. This is probably because math.inf
supplants it in almost all cases (as Martijn stated in his comment).
你是对的,整数无穷大是可能的,并且没有被添加到 Python 标准中。这可能是因为math.inf
在几乎所有情况下都取代了它(正如 Martijn 在他的评论中所说)。
In the meantime, I added an implementation of extended integerson PyPI:
同时,我在 PyPI 上添加了扩展整数的实现:
In [0]: from numbers import Integral, Real
In [0]: from extended_int import int_inf, ExtendedIntegral, Infinite
In [0]: i = int_inf
In [4]: float(i)
Out[4]: inf
In [5]: print(i)
inf
In [6]: i ** i
Out[6]: inf
In [7]: i
Out[7]: inf
In [9]: isinstance(i, Real)
Out[9]: True
In [10]: isinstance(i, Integral)
Out[10]: False
In [11]: isinstance(i, Infinite)
Out[11]: True
In [12]: isinstance(i, ExtendedIntegral)
Out[12]: True
In [13]: isinstance(2, ExtendedIntegral)
Out[13]: True
In [14]: isinstance(2, Infinite)
Out[14]: False