避免精确损失双倍

时间:2014-03-25 09:06:46

标签: c math

我有以下代码:

func(double val) {
   // I am trying with the following values. both of which are in the range as per  
   // IEEE 754 std.
   // val = 1.847474
   int temp = [some_val = (1 << 23)];
   double temp2 = val * temp;
   printf("the produt111a = %15f\n",temp2);
}
temp2中的

值会导致精度损失。

但是,如果我在进行乘法时直接替换val的值,我得到了正确的结果。

在这种情况下,可以采取哪些措施来避免精确损失?

1 个答案:

答案 0 :(得分:4)

精度是从一个浮点数到下一个浮点数的(相对)差异。

准确度是数值结果与确切结果之间的(相对)差异。

乘法2的乘法会改变浮点数的指数部分,并保持尾数位不变(另请参阅David Hammen先前的评论)。因此,既不应该损失相对精度(仍然是相同的f.p.数字类型),也不应该相对准确。除非您非常接近数字上溢或下溢。

相关问题