4. Numerical Precision

4.1. Problem

0.1 + 0.2
# 0.30000000000000004

4.2. float anatomy

4.2.1. What is float

../_images/float-anatomy.png

Fig. 4.1. What is float

4.2.2. Points chart

../_images/float-expression.png

Fig. 4.2. Points chart

4.2.3. How computer store float?

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

Fig. 4.3. How computer store float?

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

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

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

4.2.5. Normalized Line

../_images/float-normalized.png

Fig. 4.5. Normalized Line

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