Roundoff errors.
In [1]:
a = .1
In [2]:
3 * a - .3
Out[2]:
Decimals also have rounding errors, and we understand them better.
In [3]:
from decimal import Decimal, getcontext
In [4]:
getcontext().prec = 2
In [5]:
a = Decimal('.1')
a
Out[5]:
In [6]:
3 * a - Decimal('.3')
Out[6]:
Fractions are pure and exact.
In [7]:
from fractions import Fraction
In [8]:
f = Fraction(3, 12)
f
Out[8]:
In [9]:
Fraction(1, 4) + Fraction(1, 3)
Out[9]:
In [10]:
Fraction(1, 4) + Fraction(1, 3) + Fraction(5, 12)
Out[10]: