MATLAB: Is the code dont stoping when ‘x’ is equal to 10

floating point

x=0;
while x ~= 10
x = x + 0.1
end

Best Answer

You need to know that it‘s because of floating-point error because .1 is not exactly .1 to check
sprintf('%.32f',.1) % you will see numbers after the decimal Point
Search with the tag floating-point in this forum you will find lot of discussions. You need to add tolerance to your while loop:
while abs(x-10)>1e-4
Note: You don‘t need a loop to do this task, it can be vectorised.
x = .1:.1:10
Related Question