当 β = 10 且 p = 3 时,为什么 9.0 + 4.53 + 4.53 会产生 9.05?
Why does 9.0 + 4.53 + 4.53 yield 9.05 when β = 10 and p = 3?
我正在阅读每位计算机科学家应了解的浮点运算知识中的这一段:
(6)
(Suppose the triangle is very flat; that is, a ≈ b + c. Then s ≈ a,
and the term (s - a) in formula (6) subtracts two nearby numbers, one
of which may have rounding error. For example, if a = 9.0, b = c =
4.53, the correct value of s is 9.03 and A is 2.342.... Even though the computed value of s (9.05) is in error by only 2 ulps, the
computed value of A is 3.04, an error of 70 ulps.
来自:http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#1403
我想知道为什么 (a+b+c)/2
等于 9.05,其中 a = 9.0
,而 b = c = 4.53
?
我想它可能涉及的硬件将首先计算 a+b
,结果 13.53
,四舍五入为 13.5
。然后 c
将添加到 13.5
,得到 18.03
,最终四舍五入为 18.0
。最后,18.0
除以 2
。这会产生 9.00
,并提供给变量 s
。假设此硬件至少有一个保护位。
所以哪里错了?
如果他们先 b + c = 9.06
,然后添加 a + 9.06 = 18.06
,四舍五入到 18.1
,他们会得到 9.05
。
我想这个细节不是那么重要,关键是只有三位有效数字,
- 您将以
9.00
或 9.05
结尾,但不会以正确的 9.03
结尾(即使可以表示该数字)
- 你做的每一个额外的操作都会引入更多的不准确性,因此最终结果可能会远远超过三位数的限制(
3.04
vs 2.342...
,甚至连右第一位数字)
我正在阅读每位计算机科学家应了解的浮点运算知识中的这一段:
(6)
(Suppose the triangle is very flat; that is, a ≈ b + c. Then s ≈ a, and the term (s - a) in formula (6) subtracts two nearby numbers, one of which may have rounding error. For example, if a = 9.0, b = c = 4.53, the correct value of s is 9.03 and A is 2.342.... Even though the computed value of s (9.05) is in error by only 2 ulps, the computed value of A is 3.04, an error of 70 ulps.
来自:http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#1403
我想知道为什么 (a+b+c)/2
等于 9.05,其中 a = 9.0
,而 b = c = 4.53
?
我想它可能涉及的硬件将首先计算 a+b
,结果 13.53
,四舍五入为 13.5
。然后 c
将添加到 13.5
,得到 18.03
,最终四舍五入为 18.0
。最后,18.0
除以 2
。这会产生 9.00
,并提供给变量 s
。假设此硬件至少有一个保护位。
所以哪里错了?
如果他们先 b + c = 9.06
,然后添加 a + 9.06 = 18.06
,四舍五入到 18.1
,他们会得到 9.05
。
我想这个细节不是那么重要,关键是只有三位有效数字,
- 您将以
9.00
或9.05
结尾,但不会以正确的9.03
结尾(即使可以表示该数字) - 你做的每一个额外的操作都会引入更多的不准确性,因此最终结果可能会远远超过三位数的限制(
3.04
vs2.342...
,甚至连右第一位数字)