While loop causing an infinite loop in MATLAB

16 views (last 30 days)
Hi everyone,
I'm new to computer science and coding in general, so I apologize if this seems like a silly question.
I have a piece of code that involves a while loop which enters an infinite loop. I suspect it has something to do on line five, as when I change the value 0.1 to 1, the code is properly executed. I've attempted to rewrite the code using other loops, but to no avail.
Can I get an explanation as to why the code is entering this infinite loop? Thank you!
s=0
while s ~=5
s = s + 0.1;
end

Accepted Answer

Stephen23
Stephen23 on 8 Oct 2018
Edited: Stephen23 on 8 Oct 2018
"Can I get an explanation as to why the code is entering this infinite loop?"
Of course, it is very simple: you are using a computer which stores values as binary floating point numbers. In exactly the same way that you cannot write 1/3 exactly using a finite decimal fraction, it is impossible to store 0.1 exactly using a finite binary floating point number. So although you might think that you have 0.1, in fact the real values stored in computer memory is slightly different from this. And so when you add them, it collects this floating point error, so that the final total is never exactly equal to 5.
The simplest solution for your code is to use while s<5.
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:
Use James Tursa's num2strexact and you will see that that number does not really have the exact value 0.1. 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 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:
  1 Comment
Trevor Brown
Trevor Brown on 10 Oct 2018
Thank you for your detailed explanation. I'll be sure to read up on the resources you provided.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Oct 2018
Another problem with your code is that you didn't use a failsafe - some way to bail out of the code automatically if you get into an infinite loop. While loops should ALWAYS use a failsafe. The failsafe will kick you out of the loop if you iterate more than some predetermined number of iterations - a number way more than you ever expect to iterate. For example:
s=0;
maxIterations = 100000; % Whatever is more than you expect to need.
loopCounter = 0;
while s ~=5 && loopCounter < maxIterations
s = s + 0.1;
loopCounter = loopCounter + 1;
end
if loopCounter >= maxIterations
warningMessage = sprintf('Loop exited after hitting max allowed iterations (%d).\n', maxIterations);
fprintf('%s\n', warningMessage);
uiwait(warndlg(warningMessage));
end
Also, in your specific situation you should really use s<=5 rather than s~=5 because of what you will learn after reading the FAQ - that 5 will never be hit exactly because 0.1 is not a power of 2.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!