Infinite integer in Python

Python 3 has float('inf') and Decimal('Infinity') , but not int('inf') . So why is a number representing an infinite number of integers missing in the language? Is int('inf') unreasonable?

+51
python math infinity
Jul 05 '14 at 15:39
source share
3 answers

Taken from here: https://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html

An IEEE 754 floating point number can be positive or negative infinity, and NaN (not a number)

That is, the representation of float and Decimal can store these special values. However, in the int base type, there is nothing that could preserve the same. When you exceed the 2 ^ 32 limit in an unsigned 32-bit int, you just flip to 0 again.

If you want, you can create a class containing an integer that can contain the possibility of infinite values.

+11
Jul 05 '14 at 15:44
source share

You are correct that integer infinity is possible, and that not one of them has been added to the Python standard. This is probably due to the fact that math.inf supplants it in almost all cases (as Martain stated in his comment).

In the meantime, I have added an extension of extended integers to 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 
+7
Mar 01 '16 at 2:05
source share

For python 2. Sometimes it happens that you need a very large integer. For example, I can create a subarray with x [: n] and maybe sometimes I need to set n to a value so that the entire array is created. Since you cannot use float for n (python wants an integer), you need a "large integer". A good way to do this is to use the largest integer: sys.maxint. Here is an example:

 # MAX_SOURCES = sys.maxint # normal setting MAX_SOURCES = 5 # while testing # code to use an array ... ... sources[:MAX_SOURCES] 

So, during testing, I could use a smaller array of sources, but use the full array in the production process.

+2
Mar 05 '15 at 13:14
source share



All Articles