浮点舍入不正确

Sue*_*ode 3 c++ floating-point floating-accuracy floating-point-precision

在gcc上4.7.3,我的fegetround()函数返回FE_TONEAREST.根据c ++参考,这意味着从零开始四舍五入.本质上,它意味着在乘法后调整尾数的精度时保存移出的最后一位(因为它将是它应该的两倍).然后,将保存的位添加到最终的尾数结果中.

例如,浮点乘法给出以下结果:

0x38b7aad5 * 0x38b7aad5 = 0x3203c5af
Run Code Online (Sandbox Code Playgroud)

乘法后的尾数是

  1011 0111 1010 1010 1101 0101
x 1011 0111 1010 1010 1101 0101
-------------------------------
1[000 0011 1100 0101 1010 1110] [1]000 0101 1001 0101 0011 1001
Run Code Online (Sandbox Code Playgroud)

[23'b]组保持有效数字,而该[1'b]组保持最后一位移出.请注意,结果的尾数是

[000 0011 1100 0101 1010 1111]
Run Code Online (Sandbox Code Playgroud)

最后一位切换到,1因为由于舍入模式,该[1'b1]组被添加到拼接的尾数([23'b]集合).

这是一个让我感到难过的例子,因为它让我觉得硬件没有正确地舍入.

0x20922800 * 0x20922800 = 0x1a6e34c (check this on your machine)

  1010 0110 1110 0011 0100 1101
x 1010 0110 1110 0011 0100 1101
-------------------------------
01[01 0011 0111 0001 1010 0110 0][1]00 0000 0000 0000 0000 0000

Final Mantissas:       
Their Result:      01 0011 0111 0001 1010 0110 0
Correct Result(?): 01 0011 0111 0001 1010 0110 1
Run Code Online (Sandbox Code Playgroud)

我整天都在处理二进制文件,所以我可能会错过这里简单的东西.给定的舍入模式,哪个答案是正确的?

Dav*_*rtz 6

当四舍五入到最近时,IEEE指定关系为偶数.0是偶数,1是奇怪的,所以英特尔是正确的.

  • @Aggieboy:它只有1/2到均匀.如果你甚至超过1/2的iota,那么就会像你的第一个例子那样四舍五入.你的第二个例子是舍入_exactly_ 1/2. (5认同)