提问人:m615 提问时间:11/6/2023 最后编辑:Cris Luengom615 更新时间:11/7/2023 访问量:76
为什么计算机科学中的“(eps * 0.5) + 1”不大于“1”?
Why is "(eps * 0.5) + 1" not greater than "1" in computer science?
问:
我正在学习Matlab,我不明白为什么不大于1。(eps * 0.5) + 1
eps
ans =
2.220446049250313e-16
fprintf('%.52f\n', eps);
0.0000000000000002220446049250313080847263336181640625
sign(eps)
ans =
1
% 1 means that eps is >= 0
eps >= 0
ans =
logical
1
eps > 0
ans =
logical
1
eps < 0
ans =
logical
0
% so, now I take half of eps
my_half_eps = eps * 0.5;
my_half_eps
my_half_eps =
1.110223024625157e-16
fprintf('%.52f\n', my_half_eps);
0.0000000000000001110223024625156540423631668090820312
sign(my_half_eps)
ans =
1
% half eps is positive
my_half_eps >= 0
ans =
logical
1
my_half_eps > 0
ans =
logical
1
my_half_eps < 0
ans =
logical
0
fprintf('%.52f\n', (eps + 1));
1.0000000000000002220446049250313080847263336181640625
% correct
fprintf('%.52f\n', (my_half_eps + 1));
1.0000000000000000000000000000000000000000000000000000
% WHAT ???
diary off
我认为这是我可以加到 1 的最小数字。那么,将 1 加到一个数字上比这个问题少吗?eps
eps
答:
当执行加法或几乎所有浮点运算时,正确舍入的结果1 如下所示:
- 我们使用实数算术计算了确切的结果。
- 该结果四舍五入到最接近的可表示数字(使用有效的舍入规则选择)。
最常见的四舍五入规则是四舍五入到最接近的可表示数字,如果出现平局,则四舍五入到偶数低的数字。阿拉伯数字
在通常用于精度的格式中,1 表示为:double
+1.0000000000000000000000000000000000000000000000002•20
(我用粗体标记了最后一个数字,以直观地标记其位置)。下一个可表示的数字是
+1.0000000000000000000000000000000000000000000000012•20
eps
,所谓的“机器epsilon”,是:
+0.0000000000000000000000000000000000000000000000012•20
So the real-number arithmetic result of adding ½ to 1 is:eps
+1.00000000000000000000000000000000000000000000000012•20
Looking at 1, the real-number result of ½ + 1, and the next representable number, we can see ½ + 1 is exactly halfway between the two representable numbers:eps
eps
+1.0000000000000000000000000000000000000000000000002•20 +1.00000000000000000000000000000000000000000000000012•20 +1.0000000000000000000000000000000000000000000000012•20
Therefore, the floating-point operation of adding ½ to 1 will produce the neighbor with the even low digit, which is 1.0000000000000000000000000000000000000000000000002•20 = 1.eps
If you add ⅝ to 1, the result will be the next representable number, 1.0000000000000000000000000000000000000000000000012•20, because the real-number result will pass the halfway point, so the rounding will be to the next number.eps
Footnote
1 Difficult operations such as sine and power (exponentiation) are often implemented with less than correct rounding. And this applies to single operations only; sequences of multiple operations are generally not expected to produce a correctly rounded result, unless specifically designed and documented for that.
2 In a severely limited format, with one-digit precision, it is possible both neighbors end in odd digits, as with 9•100 and 1•101. In this case, the number with greater magnitude is used.
评论
eps
1
eps
1
1+eps
my_half_eps + 1
1 + my_half_eps