当前位置:网站首页>Why 0.1+0.2=0.3000000000000004

Why 0.1+0.2=0.3000000000000004

2022-06-10 17:40:00 luoganttcc

 Insert picture description here

chart 1

 Insert picture description here

chart 2

0.1 and 0.2 How to express ?
According to the previous explanation , Decimal system 0.1 Convert to binary decimal , Get is 0.0001100… ( repeat 1100) Such a circular binary decimal , Use IEEE754 It is shown in the figure below :

 Insert picture description here

chart 3

For this 0.1 Binary encoding of binary data ,

Then cut the code , How to cut , From left to right , find first 1 Just cut it down , Cut such a long number into two ends
0001100110011001100110011001100110011001100110011001101
Index mantissa
0001100110011001100110011001100110011001100110011001101

You can find that there are four people in front of you , yes 0001 , So the exponent of this floating point number is -4 , Why negative numbers , It should be a decimal , It's counting to the right ,

The second picture above shows , The exponent bits of floating point numbers are 8 A bit , It can express 2 8 = 256 2^8=256 28=256 Count ,float A type must express either a very large number or a very small number of absolute values , 0 and 255 Corresponding to two special cases , code 1-254 Then corresponding in sequence Index range -126-127

Decimal index Floating point number eight digit exponential decimal encoding Floating point eight bit exponential binary encoding
-12610000 0001
-12520000 0010
-41230111 1011
11281000 0000
1272541111 1110
in other words , chart 2 Inside the binary code by 127+(-4)=123 =0111 1011
Index 23 Potential mantissa Rounding item Truncate item
0001100110011001100110011001100110011001100110011001101
first 1 Division The last item in the mantissa is 0, But the rounding term is 0.75, Need to enter 1 1 ∗ ( 0.5 ) − 1 + 1 ∗ ( 0.5 ) − 2 1 ∗ ( 0.5 ) − 3 1 ∗ ( 0.5 ) − 4 = 0.75 1*(0.5)^{-1}+1*(0.5)^{-2}1*(0.5)^{-3}1*(0.5)^{-4}=0.75 1(0.5)1+1(0.5)21(0.5)31(0.5)4=0.75
10011001100110011001101

On the rounding of floating point numbers ,IEEE Floating point format defines 4 There are different ways of rounding , As shown in the following table . among , The default rounding method is to round even numbers , The other three can be used to calculate the upper and lower bounds .

Next, let's consider the mantissa , Digit occupancy 23 A bit , If you are directly in the mantissa Intercept 23 A bit , that Floating point numbers will always be less than the number we need to express , You cut off a small tail of a dragon , The dragon must have lost weight . So be sure to add rounding rules , There is no simple rounding

Same method ,0.2 Expressed as a single precision floating-point number is :0.20000000298023223876953125. therefore ,0.1 + 0.2 The result is :0.300000004470348358154296875

 Insert picture description here

use python Code To achieve


# Convert decimal decimals to binary 
def dec2bin(x):
  x -= int(x)
  bins = []
  while x:
    x *= 2
    bins.append(1 if x>=1. else 0)
    x -= int(x)
  return bins
#print()

#cc=dec2bin(0.1)

0.1 The binary representation of

cc1=dec2bin(0.1)
#cc1=0001100110011001100110011001100110011001100110011001101

0.2 The binary representation of

cc2=dec2bin(0.2)
#cc1=001100110011001100110011001100110011001100110011001101

0.1 Standard floating point count format

cc11=0
for k in range 
原网站

版权声明
本文为[luoganttcc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101638051874.html