• 关于二进制无法精确表示小数


    在之前做过的一个项目中遇到了小数计算不精确的问题,如下图所示
    在这里插入图片描述
    我重现了一下,大致是1.05+1+1.05// 3.0999999999999996这种的,于是看了下二进制是如何表示小数的。
    简单点讲的话,就是可以类比十进制的浮点数来理解二进制的浮点数

    十进制123.589
    理解方式1*1022*1013*100.5*(1/101)8*(1/102)9*(1/10%3)
    二进制111.101
    理解方式1*221*211*20.1*(1/21)1*(1/22)1*(1/23)

    因此二进制只能精确表示0.5(1/2)0.25(1/4)0.125(1/8)0.0625(1/16)、…这种的小数,当然了像1.02.03.0这种的小数部分全为0的也是可以精确表示的,其他的小数均无法精确表示,所以运算结果也就可能会出现长尾,之所以有一部分不会出现长尾,参考了其他资料后可以发现是语言本身做了部分优化,过分接近的数字会被语言直接优化成看似精确的值。

    十进制小数二进制
    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
  • 相关阅读:
    golang数据结构与算法——十大排序算法
    文心一言 VS 讯飞星火 VS chatgpt (139)-- 算法导论11.4 3题
    【launch启动文件播放数据包】
    鸿蒙Harmony应用开发—ArkTS声明式开发(模态转场设置:半模态转场)
    做接口测试如何上次文件
    90后MIT博士开源创业再获5千万美元融资,进军3D数字内容创作者工具
    Flutter.源码分析.flutter/packages/flutter/lib/src/widgets/scroll_view.dart/ListView
    了解这几点,让你轻松掌握滑台模组的选型方法!
    面试面经|Java面试Spring/Spring MVC面试题
    MyBatis注解开发
  • 原文地址:https://blog.csdn.net/zidieq/article/details/125513241