Search code examples
matlabfloating-accuracyfloor

Why is floor(5 - 2*eps) < 5 false?


It seems absurd to me that Matlab would evaluate floor(5-eps) < 5 as false. In fact, even floor(5-2*eps) < 5 is false.

This is what I would consider a bug, as eps is only about 1e-16 -- far from the minimum double value. Is there a specific reason why Matlab returns false? What's the idea there?


Solution

  • You want to use eps(x), not just eps

    eps(X) is the positive distance from abs(X) to the next larger in magnitude floating point number of the same precision as X.

    In fact, if you compare the HEX representation of x and x+eps(x), normally they should differ by one bit in the mantissa:

    >> format hex
    >> x = 5
    x =
       4014000000000000
    >> x + eps(x)
    ans =
       4014000000000001