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.
How stupid is the idea of having infinity included in integer type? More over, how to design a integer/floating point system that makes more sense mathematically?
How to represent an infinite number in Python? - Stack Overflow
Represent infinity as an integer in Python 2.7 - Stack Overflow
float('inf') is bad practice
Videos
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.
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.
In Python, you can do:
test = float("inf")
In Python 3.5, you can do:
import math
test = math.inf
And then:
test > 1
test > 10000
test > x
Will always be true. Unless of course, as pointed out, x is also infinity or "nan" ("not a number").
Additionally (Python 2.x ONLY), in a comparison to Ellipsis, float(inf) is lesser, e.g:
float('inf') < Ellipsis
would return true.
Since Python 3.5 you can use math.inf:
>>> import math
>>> math.inf
inf
To summarise what was said in the comments
There is no way to represent infinity as an integer in Python. This matches the behaviour of many other languages. However, due to Python's dynamic typing system, you can use float('inf') in place of an integer, and in most situations it will behave as you would expect.
As far as creating a 'double' for infinity, in Python there is just one floating point type, called float, unlike other languages such as Java which uses the term float and double for floating point numbers with different precision. In Python, floating point numbers usually use double-precision, so they act the same as doubles in Java.
Something very big that is a integer (made in 3.x, not tested in 2.x):
0x40000 #will most likely crash the shell
so if you wanted a infinite variable you would do:
Infinity = 0x40000
You can do any Integer to Hexedecimal here:
https://www.binaryhexconverter.com/decimal-to-hex-converter
(make sure to add the 0x before the value it returns for python)