Why is 10^(log10(x)) not x?
Show older comments
Hello,
When I enter the equation x = 10^(log10(x)), I should get exactly the same value for x again. However, it differs slightly in the decimal places. It is important for my calculation, that it ends up with exactly the same value. Do I have a wrong input in matlab?
a = 14.25;
a2 = 10^(log10(a));
disp(cell2mat(compose('%10.20f',a2)))
14.24999999999999822364
Has anybody an explanation? Thanks a lot!
Accepted Answer
More Answers (1)
a = 14.25;
a2 = 10^(log10(a));
for K = 1 : 100
a3 = 10^(log10(a+K*eps(a)));
if a3 ~= a2; break ;end
end
fprintf('a2 = %10.20f\n', a2);
fprintf('a3 = %10.20f after %d*eps\n', a3, K);
fprintf('a2 difference = %10.20f\n', abs(a2 - a));
fprintf('a3 difference = %10.20f\n', abs(a3 - a));
You can see from this that the value reconstructed into a2 is the closest that the 10^log10 process can get.
The reason that I do the loop over K is that if you try 10^log10(a+eps(a)) you will get back exactly the same as a2. This means that log10(a) is the same as log10(a+eps(a)) in this case, but log10(a+2*eps(a)) is different.
a+eps(a) is the next representable number after a; a+2*eps(a) is the representable number after that.
It is important for my calculation, that it ends up with exactly the same value.
You will need to switch to using the symbolic toolbox.
digits(16)
EPS = 10^(-digits());
a = sym(14.25);
a2 = 10^(log10(a));
for K = 1 : 100
a3 = 10^(log10(a+K*EPS));
if a3 ~= a2; break ;end
end
fprintf('a2 = %10.20f\n', double(a2));
fprintf('a3 = %10.20f after %d*eps\n', double(a3), K);
disp(abs(a2-a))
fprintf('a2 difference = %10.20f\n', double(abs(a2 - a)));
fprintf('a3 difference = %10.20f\n', double(abs(a3 - a)));
3 Comments
Juditha Schmidt
on 9 Mar 2021
Paul
on 9 Mar 2021
a+eps(a) is the next representable number after a; a+2*eps(a) is the representable number after that.
For ever postive value a, Isn't the 2nd representable number after a: a + eps(a) + eps(a + eps(a)) ?
No. eps(a) generally expresses the magnitude of the last bit position in the double precision representation. (I say generally because when you get very close to zero, there are denormalized numbers.)
format long g
a = rand() * 10^randi([-300 300])
num2hex(a)
nexta = typecast(typecast(a, 'uint64') + uint64(1),'double')
num2hex(nexta)
nexta - a
eps(a)
(nexta - a) - eps(a)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!