MATLAB: When I add two floating point numbers the result is not correct above 262144

MATLABsingle

When I execute the following I always get back 262144, it is like it is treating it as an integer
a=single(262144.000);
for i=1:1234,
a= a+single(0.01);
end;
display(a-floor(a));
sprintf('%10.6f',a)
single
0
ans =
'262144.000000'
or if I just do a = single(262144.000)+single(0.01) result is 262144

Best Answer

The amount you are adding, 0.01, is less than the eps of the number you are using. E.g.,
>> a = single(262144.000)
a =
262144
>> eps(a)
ans =
0.0313
>> a + 0.01
ans =
262144
So the result of the addition does not change the value of "a" because there is not enough precision in "a". I.e., the closest number to 262144.01 in IEEE single precision is in fact 262144. The next highest number is 262144.03125.