“This is the 13th day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021”.
preface
Before the article begins, watch the following “weird” scene.
a, b = 0.1.0.2
print(a + b == 0.3)
print(a + b)
out:
False
0.30000000000000004
Copy the code
0.1 + 0.2 == 0.3 I don’t know how you feel when you see this scene for the first time. Anyway, I have a little doubt about life. Why would such a result be produced?
Floating-point limits
Floating-point numbers are represented in computer hardware as a decimal number with base 2 (binary). Let’s first see how 0.125(10) is represented in decimal and binary.
Decimal 0.125(10) is equal to
1\times10^{-1} + 2\times10^{-2} + 5\times10^{-3} = \cfrac{1}{8}
As can be seen from the above results, the binary of 0.1 is:
0.0001100110011001100110011001100110011001100110011.Copy the code
This is a binary infinite repeating decimal, but computer memory is limited, we can’t store all the decimal places. So what’s the solution?
The answer is to cut it off somewhere at the end and take an approximation, so floating-point numbers can only be approximated as binary decimals in most current programming languages that support processor floating-point arithmetic.
Many people using Python are unaware of this difference because Python only prints decimal approximations of the binary values stored on the computer. But keep in mind that even though the output appears to be the exact value of 0.1, the actual stored value is only the binary value closest to 0.1 that the computer can represent.
The solution
1. The Decimal module performs decimal math, converting floating point numbers into strings for operation.
from decimal import Decimal
a, b = Decimal('0.1'), Decimal('0.2')
a + b == Decimal('0.3')
out:True
Copy the code
Numpy. Float32 Saves data in floating-point type 32 in numpy module.
import numpy as np
temp = np.array([0.1.0.2.0.3], dtype=np.float32)
temp[0] + temp[1] == temp[2]
Copy the code
Of course, performance can be compromised with high precision, and in practice small deviations from these approximations may not matter. Just keep an eye on it if it happens!
The result is that floating-point numbers lose their accuracy when converted to binary and then converted back to decimal. I don’t know if you got it?
This is what I want to share today. Search Python New Horizons on wechat, bringing you more useful knowledge every day. More organized nearly a thousand sets of resume templates, hundreds of e-books waiting for you to get oh!