Problem

MATLAB

Consider the following code named epsError.m,

eps = 1.0;
while 1.0 ~= 1.0 + eps
    disp(num2str(eps));
    eps = eps / 2.0;
end
disp(['final eps:', num2str(eps)]);

Explain what the code is doing. Run the code and observe the output. How could 1.0 ~= 1.0 + eps be false?!

Python

Consider the following code named epsError.py,

eps = 1.0
while 1.0 != 1.0 + eps:
    print ('...............', eps)
    eps /= 2.0
print ('final eps:', eps)

Explain what the code is doing. Run the code and observe the output. How could 1.0 != 1.0 + eps be False?

Here is the output of the code,

Comments