While loop iterating further than expected?

7 ビュー (過去 30 日間)
Guillermo
Guillermo 2018 年 8 月 29 日
コメント済み: OCDER 2018 年 8 月 29 日
Hi. I am experiencing a strange behavior in this simple script:
% CASE 1 - COMPUTES CORRECTLY
m1=1;
while m1<4.80
m1=m1+0.1;
end
disp(m1);
% CASE 2 - DOES NOT COMPUTE CORRECTLY
m2=1;
while m2<4.90
m2=m2+0.1;
end
disp(m2);
I obtain m1 = 4.8, as expected. However, I obtain m2 = 5.0, which implies the loop in CASE 2 has gone on for one iteration too many. Any idea why this might be happening? Is it a precision-related issue? I'm afraid I must be missing something very silly. Thank you for your help.
  2 件のコメント
Stephen23
Stephen23 2018 年 8 月 29 日
"I'm afraid I must be missing something very silly."
The value that you are adding cannot be exactly represented using binary floating point numbers. What you see printed in the command window is the closest representation to 5 or 16 significant digits, depending on your current format setting. To see the "real" value download James Tursa's FEX submission:
disp is useless for this. Use James Tursa's num2strexact and you will see that none of those values really have the exact value 0.1, or their multiples. All you are looking at is a representation of those floating point numbers displayed in the command window, to the precision defined by your format setting. Just because you see 0.1 is displayed tells you nothing about the "real" floating point number's value.
Note that you can change how the value is displayed by changing the format.
You need to learn about the limits of floating point numbers. Start by reading these:
This is worth reading as well:
Guillermo
Guillermo 2018 年 8 月 29 日
Thank you, Stephen. Very instructive. I will look at your references. I was somewhat aware that something like this might be happening but I frankly did not expect it to have such an impact with such simple numbers in two adjacent and very similar instances. Thanks again.

サインインしてコメントする。

採用された回答

OCDER
OCDER 2018 年 8 月 29 日
編集済み: OCDER 2018 年 8 月 29 日
Rounding error. Try something like this:
m1=1;
while m1 < 4.80
m1=round(m1+0.1, 2); %To prevent m1 from drifting too far due to rounding error
end
disp(m1);
% CASE 2
m2=1;
while m2 < 4.90
m2=round(m2+0.1, 2);
end
disp(m2);
  3 件のコメント
Guillermo
Guillermo 2018 年 8 月 29 日
Thank you, OCDER. This did it playing with the tolerances you suggested. It seems a bit of an overkill to have to add a tolerance for such a simple set of parameters but problem solved. Thanks!!!
OCDER
OCDER 2018 年 8 月 29 日
See new answer. Use round(x, n) to round x to the nearest nth decimal place.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by