MATLAB: Bucle doesn’t end

bucle

Hi
Why this bucle doesnt end?
x=0;
while x~=10
x=x+0.1
end

Best Answer

Because the number 0.1 cannot be represented exactly in typical floating point systems. It will be off in approximately the 16th decimal place, and so will not be exactly 10 after 100 times through this loop.
You need to include a tolerance, if you want to check equality of floating point numbers.
You can read more details if you search for these keywords, but here is one good place.