All-in-one infinity value for Python. Can be compared to any object.
float('inf')
and float('-inf')
. However these simply
represent floating point infinity values. I wanted to create a class which can
be compared to any comparable object.float('inf')
is clumsy compared to just inf
pow(1, float('inf'))
returns 1 whereas it should be undefined. In
infinity this operation returns TypeError
.Simply grab the package from pypi:
pip install infinity
Supported python versions:
The Infinity
class supports rich comparison methods:
>>> import sys
>>> from datetime import datetime
>>> from infinity import inf
>>> 3 < inf
True
>>> datetime(2000, 2, 2) < inf
True
>>> -inf < inf
True
>>> inf == inf
True
>>> -inf == -inf
True
It also supports arithmetic operators:
>>> inf + inf
inf
>>> -inf - inf
-inf
>>> inf + 3
inf
>>> inf + datetime(2000, 2, 2)
inf
>>> 5 / inf
0
>>> 3 / -inf
0
>>> pow(inf, 0.5)
inf
The following operations raise TypeError
exceptions:
>>> inf - inf
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for -: 'Infinity' and 'Infinity'
>>> -inf + inf
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'Infinity' and 'Infinity'
>>> inf / inf # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for /: 'Infinity' and 'Infinity'
>>> inf * 0
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for *: 'Infinity' and 'int'
>>> pow(inf, 0) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for ** or pow(): 'Infinity' and 'int'
>>> pow(1, inf) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'Infinity'
Infinity objects can be coerced to various types:
>>> float(inf) == float('inf')
True
>>> float(-inf) == float('-inf')
True
>>> str(inf)
'inf'
>>> str(-inf)
'-inf'
>>> bool(inf)
True
>>> bool(-inf)
True