2

随便逛了一下知乎和思否的博文,大都没回答到点上。

I.浮点数的二进制存储

采用 IEEE 754 规范来存储浮点数:
1位【正负符号】+11位【指数】+52位【有效数字】,如下图
clipboard.png

由于0.1.toString(2)=0.0001100110011001100110011001100110011001100110011001101
所以
0.1 = 2^-4 * [1].1001100110011001100110011001100110011001100110011010
0.2 = 2^-3 * [1].1001100110011001100110011001100110011001100110011010

II. 到底怎么相加

铁律是52位有效数字,也就是:

0.1 = 2^-3 *  0.1100110011001100110011001100110011001100110011001101(0)
0.2 = 2^-3 *  1.1001100110011001100110011001100110011001100110011010
sum = 2^-2 * 1.0011001100110011001100110011001100110011001100110011(1)

由于有效数字变成了53位,根据IEEE754 rounding mode 的 Round to Nearest,若x在a和b之间,选择最低有效位为零的值

a = 2^-2 * 1.0011001100110011001100110011001100110011001100110011
x = 2^-2 * 1.0011001100110011001100110011001100110011001100110011(1)
b = 2^-2 * 1.0011001100110011001100110011001100110011001100110100

当比较0.1+0.2 和 0.3时,实际比较的是

0.1 + 0.2 => 0:01111111101:0011001100110011001100110011001100110011001100110[100]
0.3       => 0:01111111101:0011001100110011001100110011001100110011001100110[011]

转成10进制,看上去是:

0.1 + 0.2 => 0.300000000000000044408920985006...
0.3       => 0.299999999999999988897769753748...

结果很明显啦

参考资料:
https://stackoverflow.com/que...


seasonley
607 声望693 粉丝

一切皆数据