infinity

All-in-one infinity value for Python. Can be compared to any object.


License
BSD-3-Clause
Install
pip install infinity==1.5

Documentation

infinity

Build Status Version Status Downloads

All-in-one infinity value for Python. Can be compared to any object.

Why?

  • Python has 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.
  • Writing 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.
  • When would you use Infinity?

Installation

Simply grab the package from pypi:

pip install infinity

Supported python versions:

  • Python 2.6
  • Python 2.7
  • Python 3.3
  • Python 3.4
  • Python 3.5
  • PyPy

Object comparison

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

Arithmetic operators

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'

Type coercion

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