Question about internal aithmetics

4 visualizzazioni (ultimi 30 giorni)
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
i = 8193
eps(N)
ans = 16384

Risposta accettata

John D'Errico
John D'Errico il 7 Ott 2022
Modificato: John D'Errico il 7 Ott 2022
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
dx = 2.2204e-16
eps(1)
ans = 2.2204e-16
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
ans = 2.2204e-16
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)
ans = '3ff0000000000000'
num2hex(x + eps)
ans = '3ff0000000000001'
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
ans = logical
0
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
ans =
1.11022302462516e-16
x + 1.1102230246252 == x
ans = logical
0
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 Commenti
Paul
Paul il 7 Ott 2022
x = 1;
This line seems incorrect
x + 1.1102230246252 == x
ans = logical
0
Should it not be
x + 1.1102230246252e-16 == x
ans = logical
0
"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
ans = logical
1
Maybe 1 + numberlessthaneps ROUNDS the result ....
John D'Errico
John D'Errico il 7 Ott 2022
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
ans = logical
0
Anything larger than eps/2) should yield a different result.
x + eps/2 == x
ans = logical
1

Accedi per commentare.

Più risposte (0)

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by