5. Numerical Precision

5.1. Problem

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

5.2. float anatomy

5.2.1. What is float

../_images/float-anatomy.png

Figure 5.2. What is float

5.2.2. Points chart

../_images/float-expression.png

Figure 5.3. Points chart

5.2.3. How computer store float?

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

Figure 5.4. How computer store float?

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

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

Figure 5.5. How to read/write float from/to memory?

5.2.5. Normalized Line

../_images/float-normalized.png

Figure 5.6. Normalized Line

5.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