Question about internal aithmetics
1 次查看(过去 30 天)
显示 更早的评论
Let and . I use the following code to find out what is the smallest value of i that makes the equality false.
My question is about the connection, if any, between the result, , and the output of the instruction , namely, 16384. Thanks in advance for any help.
N = 1.23*10^20;
M = N;
i = 0;
while M - N == 0
i = i+1;
M = M+i;
end
i
eps(N)
0 个评论
采纳的回答
John D'Errico
2022-10-7
编辑:John D'Errico
2022-10-7
This is a subtle point. Actually a very good question. We don't need to look at 1e20. the number 1 is entirely adequate.
x = 1;
dx = 1;
while x + dx ~= x
dx = dx/2;
end
dx = dx*2
eps(1)
So when I finally get a result that is no longer different from 1, I multiplied by 2. That gives me the smallest power of 2 I can add to 1, and get a different number. We can think of this as the stride between two floating point numbers. There are no double precision numbers representable between 1 and 1+ eps(1).
2^-52
And since double precision arithmetic uses 52 binary bits to store the numbers (plus a sign bit and an exponent) that makes sense. You can see that eps(1) is just 2^-52.
And you can even see this in the represntation of the numbers in a hex form.
num2hex(x)
num2hex(x + eps)
Do you see that x and x+eps are different in only one bit, when we look at the hex form?
That is all well and good. However, suppose we now do this:
x + 2e-16 == x
So while 2e-16 is LESS than eps(1), we see that adding it to 1 yield a number different from 1. In fact,
format long g
eps/2
x + 1.1102230246252 == x
So I can in fact add a number just slightly larger than eps(1)/2 to 1, and still get a number different from 1.
Effectively, 1+1.1102230246252 ROUNDS the result to the nearest double precision number. Here, that is a number not equal to 1.
So as I said, a subtle question. As I said, eps is not really the smallest number you can add to 1 and get a different result, as many seem to say. eps is the smallest power of 2 you can add to 1, and get a different result. The difference is an important one.
The same ideas apply to larger numbers, but everything just scales up. Did you see that eps(N) as you did it, is exactly a power of 2? That is, 2^14 is 16384. But 8183 is 2^13+1. So 8193 was the smallest integer you could add to N, and have it round up to a larger number than N. But that is not exactly the same thing as eps(N).
2 个评论
Paul
2022-10-7
x = 1;
This line seems incorrect
x + 1.1102230246252 == x
Should it not be
x + 1.1102230246252e-16 == x
"Effectively, 1+eps/2 ROUNDS the result to the nearest double precision number. Here, that is a number not equal to 1."
1 + eps/2 is equal to 1.
1 + eps/2 == 1
Maybe 1 + numberlessthaneps ROUNDS the result ....
John D'Errico
2022-10-7
Oops. I forgot the e-16. Yes. My intent was there, just my typing early in the morning. :) Just a typo. So we have:
x = 1;
x + 1.1102230246252e-16 == x
Anything larger than eps/2) should yield a different result.
x + eps/2 == x
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!