It's a little bit weird.
Show older comments
It's a little bit weird... I wrote appDer(0,delta) < 0.01 but
f = @(t) t.^3;
appDer = @(t, dt) (f(t+dt)- f(t))/dt;
for delta = 1 : -0.001 : 0
if appDer(0,delta) < 0.01
break
end
end
disp([delta,appDer(0,delta)])
0.1000 0.0100
But...
f = @(t) t.^3;
appDer = @(t, dt) (f(t+dt)- f(t))/dt;
for delta = 1 : -0.001 : 0
if appDer(0,delta) < 0.0099999
break
end
end
disp([delta,appDer(0,delta)])
0.0990 0.0098
Why does it happen? Frankly, I can assume the reason, but I want to know about it, exactly. If possible, please give me the answer in Korean.
1 Comment
Answers (2)
It is not weird at all. You just need to learn about floating point numbers, that is all. The most important fact to learn is: how numbers are displayed is just a simplified representation of the binary value. Note also that the values you are using do not have a precise representation in double floating point numbers.
Replace the disp call with this:
fprintf('%.30f\n',[delta,appDer(0,delta)])
and you will see the difference. You might like to try James Tursa's FEX submission for even more insight into floating point number values:
And also read these:
6 Comments
Young-hoon Kang
on 28 May 2016
John D'Errico
on 28 May 2016
Edited: John D'Errico
on 28 May 2016
No. It is NOT exactly 0.01. In fact, a double precision number, as stored in Binary form (which it is) cannot represent 0.01 exactly. For example:
sprintf('%0.55f',0.1)
ans =
0.1000000000000000055511151231257827021181583404541015625
Only a few rare decimal numbers are exactly representable. For example
sprintf('%0.55f',0.25)
ans =
0.2500000000000000000000000000000000000000000000000000000
Of course, you would expect that to be true for this case, as the fraction 1/4 should be represented exactly.
Young-hoon Kang
on 28 May 2016
Image Analyst
on 28 May 2016
I can't tell if you ever read my answer about how the two cases have different number of iterations.
Anyway, have you read the FAQ about how to use tolerances to compare floating point numbers? http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
Young-hoon Kang
on 29 May 2016
Image Analyst
on 29 May 2016
And what do you think about each case running a different number of iterations?
Image Analyst
on 28 May 2016
What the others say about representing continuous numbers in a binary system is true, but did you consider that you are not even executing the loop the same number of times in the two cases?
Try putting in a loop counter to prove it:
f = @(t) t.^3;
appDer = @(t, dt) (f(t+dt)- f(t))/dt;
loopCounter = 0;
for delta = 1 : -0.001 : 0
loopCounter = loopCounter + 1;
if appDer(0,delta) < 0.01
break
end
end
fprintf('After %d iterations, Delta = %f and appDer = %f\n', ...
loopCounter, delta, appDer(0,delta));
% But...
loopCounter = 0;
f = @(t) t.^3;
appDer = @(t, dt) (f(t+dt)- f(t))/dt;
for delta = 1 : -0.001 : 0
loopCounter = loopCounter + 1;
if appDer(0,delta) < 0.0099999
break
end
end
fprintf('After %d iterations, Delta = %f and appDer = %f\n', ...
loopCounter, delta, appDer(0,delta));
% But...
loopCounter = 0;
f = @(t) t.^3;
appDer = @(t, dt) (f(t+dt)- f(t))/dt;
for delta = 1 : -0.001 : 0
loopCounter = loopCounter + 1;
if appDer(0,delta) < 0.0099999
break
end
end
fprintf('After %d iterations, Delta = %f and appDer = %f\n', ...
loopCounter, delta, appDer(0,delta));
You'll see
After 901 iterations, Delta = 0.100000 and appDer = 0.010000
After 902 iterations, Delta = 0.099000 and appDer = 0.009801
So in your first case you break after 901 iterations and in the second case you break out of the loop after 902 iterations. What do you think about that? Is that what you expected? Do you think appDer might give different values if you send in different delta?
I 100% agree with Stephen here. Nothing strange going on.
Categories
Find more on Logical 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!