Infinite integer in Python Infinite integer in Python python python

Infinite integer in Python


Taken from here: 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)

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.

If you want, you could create a class containing an integer which could feature the possibility of infinite values.


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).

In the meantime, I added an implementation of extended integers on PyPI:

In [0]: from numbers import Integral, RealIn [0]: from extended_int import int_inf, ExtendedIntegral, InfiniteIn [0]: i = int_infIn [4]: float(i)Out[4]: infIn [5]: print(i)infIn [6]: i ** iOut[6]: infIn [7]: iOut[7]: infIn [9]: isinstance(i, Real)Out[9]: TrueIn [10]: isinstance(i, Integral)Out[10]: FalseIn [11]: isinstance(i, Infinite)Out[11]: TrueIn [12]: isinstance(i, ExtendedIntegral)Out[12]: TrueIn [13]: isinstance(2, ExtendedIntegral)Out[13]: TrueIn [14]: isinstance(2, Infinite)Out[14]: False


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:

# MAX_SOURCES = sys.maxint # normal settingMAX_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.