1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
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`_?
.. _it should be undefined:
http://math.stackexchange.com/questions/319764
/1-to-the-power-of-infinity-why-is-it-indeterminate
.. _When would you use Infinity:
http://stackoverflow.com/questions/382603/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:
.. code-block:: python
>>> 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:
.. code-block:: python
>>> 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:
.. code-block:: python
>>> 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:
.. code-block:: python
>>> float(inf) == float('inf')
True
>>> float(-inf) == float('-inf')
True
>>> str(inf)
'inf'
>>> str(-inf)
'-inf'
>>> bool(inf)
True
>>> bool(-inf)
True
.. |Build Status| image:: https://travis-ci.org/kvesteri/infinity.png?branch=master
:target: https://travis-ci.org/kvesteri/infinity
.. |Version Status| image:: https://img.shields.io/pypi/v/infinity.svg
:target: https://pypi.python.org/pypi/infinity/
.. |Downloads| image:: https://img.shields.io/pypi/dm/infinity.svg
:target: https://pypi.python.org/pypi/infinity/
|