# 1.2. Precision¶

## 1.2.1. Rationale¶

>>> 0.1
0.1

>>> 0.2
0.2

>>> 0.3
0.3

>>> 0.1 + 0.2 == 0.3
False

>>> 0.1 + 0.2
0.30000000000000004

>>> round(0.1+0.2, 16) == 0.3
True

>>> round(0.1+0.2, 17) == 0.3
False


## 1.2.2. IEEE 754 standard¶

>>> 1.234 == 1234 * 10e-4
True

>>> 1234 * 10e-4
1.234 Figure 1.1. What is float as defined by IEEE 754 standard¶ Figure 1.2. Points chart¶ Figure 1.3. How computer store float? As defined by IEEE 754 standard¶ Figure 1.4. How to read/write float from/to memory?¶ Figure 1.5. Normalized Line¶

## 1.2.3. Solutions¶

• Round values to 4 decimal places (generally acceptable)

• Store values as int, do operation and then divide. For example instead of 1.99 USD, store price as 199 US cents

• Use Decimal type

• Decimal type is much slower

Problem:

candy = 0.10      # price in dollars
cookie = 0.20     # price in dollars

result = candy + cookie
print(result)
# 0.30000000000000004


Round values to 4 decimal places (generally acceptable):

candy = 0.10      # price in dollars
cookie = 0.20     # price in dollars

result = round(candy + cookie, 4)
print(result)
# 0.3


Store values as int, do operation and then divide:

candy = 10        # price in cents
cookie = 20       # price in cents

result = (candy + cookie) / 100   # divide by 100 (number of cents in dollar)
print(result)
# 0.30


Use Decimal type:

from decimal import Decimal

candy = Decimal('0.10')     # price in dollars
cookie = Decimal('0.20')    # price in dollars

result = candy + cookie
print(result)
# 0.30