7. Numerical Precision

7.1. Problem

0.1 + 0.2
# 0.30000000000000004
0.1 + 0.2 == 0.3
# False

7.2. float anatomy

7.2.1. What is float

../_images/float-anatomy.png

Fig. 7.1. What is float

7.2.2. Points chart

../_images/float-expression.png

Fig. 7.2. Points chart

7.2.3. How computer store float?

../_images/float-mantissa-1.png

Fig. 7.3. How computer store float?

7.2.4. How to read/write float from/to memory?

../_images/float-mantissa-2.png

Fig. 7.4. How to read/write float from/to memory?

7.2.5. Normalized Line

../_images/float-normalized.png

Fig. 7.5. Normalized Line

7.3. decimal

from decimal import Decimal


a = Decimal('0.1')
b = Decimal('0.2')

a + b
# Decimal('0.3')
from decimal import Decimal


a = Decimal('0.3')

float(a)
# 0.3