当前位置:网站首页>About binary cannot express decimals accurately

About binary cannot express decimals accurately

2022-07-07 07:14:00 Crystal heart spring

In a previous project, I encountered the problem of inaccurate decimal calculation , As shown in the figure below
 Insert picture description here
I reproduced it , Is roughly 1.05+1+1.05// 3.0999999999999996 Such , So I looked at how binary represents decimal .
To put it simply , That is, we can understand binary floating-point numbers by analogy with decimal floating-point numbers

Decimal system 123.589
Way of understanding 1*1022*1013*100.5*(1/101)8*(1/102)9*(1/10%3)
Binary system 111.101
Way of understanding 1*221*211*20.1*(1/21)1*(1/22)1*(1/23)

Therefore, binary can only accurately represent 0.5(1/2)0.25(1/4)0.125(1/8)0.0625(1/16)、… This kind of decimal , Of course, like 1.02.03.0 This kind of decimal part is all 0 Of can also be accurately expressed , Other decimals cannot be expressed accurately , Therefore, the operation result may have a long tail , The reason why some of them will not have long tails , After referring to other materials, we can find that the language itself has been partially optimized , Too close numbers will be directly optimized into seemingly accurate values by language .

Decimal number Binary system
0.10.0001100110011001100110011001100110011001100110011001101
0.20.001100110011001100110011001100110011001100110011001101
0.30.010011001100110011001100110011001100110011001100110011
0.40.01100110011001100110011001100110011001100110011001101
0.50.1
0.60.10011001100110011001100110011001100110011001100110011
0.70.1011001100110011001100110011001100110011001100110011
0.80.1100110011001100110011001100110011001100110011001101
0.90.11100110011001100110011001100110011001100110011001101
0.100.0001100110011001100110011001100110011001100110011001101
0.110.00011100001010001111010111000010100011110101110000101001
0.120.00011110101110000101000111101011100001010001111010111
0.130.00100001010001111010111000010100011110101110000101001
0.140.00100011110101110000101000111101011100001010001111011
0.150.0010011001100110011001100110011001100110011001100110011
0.160.0010100011110101110000101000111101011100001010001111011
0.170.0010101110000101000111101011100001010001111010111000011
0.180.001011100001010001111010111000010100011110101110000101
0.190.001100001010001111010111000010100011110101110000101001
0.200.001100110011001100110011001100110011001100110011001101
0.210.0011010111000010100011110101110000101000111101011100001
0.220.0011100001010001111010111000010100011110101110000101001
0.230.0011101011100001010001111010111000010100011110101110001
0.240.0011110101110000101000111101011100001010001111010111
0.250.01
0.260.0100001010001111010111000010100011110101110000101001
0.270.010001010001111010111000010100011110101110000101001
0.280.0100011110101110000101000111101011100001010001111011
0.290.010010100011110101110000101000111101011100001010001111
0.300.010011001100110011001100110011001100110011001100110011
0.310.010011110101110000101000111101011100001010001111010111
0.320.010100011110101110000101000111101011100001010001111011
0.330.010101000111101011100001010001111010111000010100011111
0.340.010101110000101000111101011100001010001111010111000011
0.350.01011001100110011001100110011001100110011001100110011
0.360.01011100001010001111010111000010100011110101110000101
0.370.01011110101110000101000111101011100001010001111010111
0.380.01100001010001111010111000010100011110101110000101001
0.390.01100011110101110000101000111101011100001010001111011
0.400.01100110011001100110011001100110011001100110011001101
0.410.011010001111010111000010100011110101110000101000111101
0.420.011010111000010100011110101110000101000111101011100001
0.430.011011100001010001111010111000010100011110101110000101
0.440.011100001010001111010111000010100011110101110000101001
0.450.011100110011001100110011001100110011001100110011001101
0.460.011101011100001010001111010111000010100011110101110001
0.470.0111100001010001111010111000010100011110101110000101
0.480.011110101110000101000111101011100001010001111010111
0.490.0111110101110000101000111101011100001010001111010111
0.500.1
0.510.1000001010001111010111000010100011110101110000101001
0.520.100001010001111010111000010100011110101110000101001
0.530.1000011110101110000101000111101011100001010001111011
0.540.10001010001111010111000010100011110101110000101001
0.550.1000110011001100110011001100110011001100110011001101
0.560.100011110101110000101000111101011100001010001111011
0.570.10010001111010111000010100011110101110000101000111101
0.580.10010100011110101110000101000111101011100001010001111
0.590.10010111000010100011110101110000101000111101011100001
0.600.10011001100110011001100110011001100110011001100110011
0.610.10011100001010001111010111000010100011110101110000101
0.620.10011110101110000101000111101011100001010001111010111
0.630.10100001010001111010111000010100011110101110000101001
0.640.10100011110101110000101000111101011100001010001111011
0.650.10100110011001100110011001100110011001100110011001101
0.660.10101000111101011100001010001111010111000010100011111
0.670.10101011100001010001111010111000010100011110101110001
0.680.10101110000101000111101011100001010001111010111000011
0.690.101100001010001111010111000010100011110101110000101
0.700.1011001100110011001100110011001100110011001100110011
0.710.10110101110000101000111101011100001010001111010111
0.720.1011100001010001111010111000010100011110101110000101
0.730.101110101110000101000111101011100001010001111010111
0.740.1011110101110000101000111101011100001010001111010111
0.750.11
0.760.1100001010001111010111000010100011110101110000101001
0.770.110001010001111010111000010100011110101110000101001
0.780.1100011110101110000101000111101011100001010001111011
0.790.11001010001111010111000010100011110101110000101001
0.800.1100110011001100110011001100110011001100110011001101
0.810.110011110101110000101000111101011100001010001111011
0.820.11010001111010111000010100011110101110000101000111101
0.830.11010100011110101110000101000111101011100001010001111
0.840.11010111000010100011110101110000101000111101011100001
0.850.11011001100110011001100110011001100110011001100110011
0.860.11011100001010001111010111000010100011110101110000101
0.870.11011110101110000101000111101011100001010001111010111
0.880.11100001010001111010111000010100011110101110000101001
0.890.11100011110101110000101000111101011100001010001111011
0.900.11100110011001100110011001100110011001100110011001101
0.910.11101000111101011100001010001111010111000010100011111
0.920.11101011100001010001111010111000010100011110101110001
0.930.11101110000101000111101011100001010001111010111000011
0.940.111100001010001111010111000010100011110101110000101
0.950.1111001100110011001100110011001100110011001100110011
0.960.11110101110000101000111101011100001010001111010111
0.970.1111100001010001111010111000010100011110101110000101
0.980.111110101110000101000111101011100001010001111010111
0.990.1111110101110000101000111101011100001010001111010111
原网站

版权声明
本文为[Crystal heart spring]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070322587907.html