In MATLAB, if I do realmax - 1000000 == realmax
I get a logical 1 (true) as an answer. Why is that?
You get a true result because the value of 1000000
(i.e. 1e6
) is much smaller than the floating point relative accuracy of a double precision variable for values at or near the maximum limits. For example:
>> realmax-1e6==realmax % Subtract 1 million
ans =
logical
1 % Still equal; not big enough to register a change
>> realmax-eps(realmax)==realmax % Subtract the distance to the next largest value
ans =
logical
0 % Unequal; yeah, that's big enough to matter
In short, the distance between representable numbers at the maximum limit (i.e. eps(realmax)
) is on the order of 10^292
. Subtracting a much smaller value of 1e6
gives a result that just gets rounded back to what it was before.
You can find more comprehensive explanations of dealing with floating point numbers here and here.