Roundoff errors.


In [1]:
a = .1

In [2]:
3 * a - .3


Out[2]:
5.551115123125783e-17

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]:
Decimal('0.1')

In [6]:
3 * a - Decimal('.3')


Out[6]:
Decimal('0.0')

Fractions are pure and exact.


In [7]:
from fractions import Fraction

In [8]:
f = Fraction(3, 12)
f


Out[8]:
Fraction(1, 4)

In [9]:
Fraction(1, 4) + Fraction(1, 3)


Out[9]:
Fraction(7, 12)

In [10]:
Fraction(1, 4) + Fraction(1, 3) + Fraction(5, 12)


Out[10]:
Fraction(1, 1)