MATLAB: Bug in rounding functions? Who can explain? :)

ceilfix?MATLABround

>> format long
>> 4.1*808e6
ans =
3.312800000000000e+09
>> fix(4.1*808e6)
ans =
3.312799999000000e+09
>> fix(4*808e6 + .1*808e6)
ans =
3.312800000000000e+09

Best Answer

>> 4.1*808e6 - 3312800000
ans =
-4.76837158203125e-07
That is, due to floating point roundoff, 4.1*808e6 falls just short of an integer. fix() is the same as floor() for positive numbers so fix() removes the fraction after 3312799999 that is not quite 1.0, exactly like how floor(1.99999999) is 1.0 not 2.0