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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
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:
So, while testing, I could use a smaller sources array but use the full array in production.
Taken from here: https://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
That is, the representation of
float
andDecimal
can store these special values. However, there is nothing within the basic typeint
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: