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, 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
Answer from Neil G on Stack OverflowYou 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, 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
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.
algorithm - How to implement negative infinity in Python? - Stack Overflow
float('inf') is bad practice
A value doesn't print()
How to represent infinity in a programming language?
Videos
So in my imagined language, efficiency is not an issue. I decide to use arbitrary precision integers(i.e. big ints). I realize that sometimes you need infinity as a boundary, so I'm curious, how bad is the idea of having positive/negative infinity in integer type?
I know the fact that you have more UBs, like 0*inf doesn't make sense, but it's fundamentally the same as div by 0 problems. And it should be solved the same as div by 0s.
And for floating numbers, we're all plagued by precision problems, so I think it should make sense for any floating number to be encoded by x = (a, b), where it means that: a - b < x < a + b, and as you do floating point arithemetic, b grows and you lose the precision.
In general, is there any efforts on designing a number system for both integer/floating nums that make more sense mathematically, when you don't care about performance?
EDIT: just realized that haskell have both NAN and INF included in the language.
Python has special values float('inf') and float('-inf').
As it happens, in Python 2, None is less than any integer, so you can use None. In Python 3 you have (at least) four choices:
- Use min(A) - 1.
- Use
None, and whenever you compare two values, explicitly test for them beingNone. - Define a new data type that consists of either an integer or -∞, and handles comparisons correctly.
- Modify the algorithm so that line 2 is eliminated. You will have to patch
Heap-Increase-Keysomehow.
I don't know why float('inf') was chosen as the way the language accesses infinity since this is using a magic string. Why couldn't it just be float.inf? That way magic string is avoided. Feels too basic of a best practice to be simply passed up. Anyone know the reason behind this?