Tip 47 Discover an Infinity
Pythonic Programming — by Dmitry Zinoviev (57 / 116)
👈 Rational Numbers Exist | TOC | Carve It in Stone 👉
★★★2.7, 3.4+ In Python, an infinity exists. Honestly, it exists in most other programming languages as well, but this is a Python book. So, in Python, an infinity exists. You can access it in at least three ways: apply float to the string ’inf’, find it in math.inf, find it in numpy.inf. All three representations are equal, which makes sense:
float('inf') == math.inf == numpy.inf=> True
Python supports some arithmetic operations on an infinity:
- You can get an infinity by dividing one by a very small number: 1 / 3e-324.
- You can divide by it. The result is a 0 or a negative 0, depending on the sign of the numerator. A negative zero exists, too.
- You can multiply by it. The result is infinity unless you multiply by 0.
- You can add to infinity and subtract from infinity. The result is infinity. If you subtract infinity from any finite number, you get a negative infinity.
Some arithmetic operations are less obvious:
- Multiplying infinity by zero
- Dividing infinity by infinity
- Subtracting infinity from infinity
These operations produce another uncommon result known as a nan (“not-a-number”). A…