MATLAB: Help regarding “format long”

faq6.1format longMATLAB

i am trying to solve a=(4:0.0002:4.20) with format long, but i am getting weird values and not accurated ones like 4.196000000000001 4.196200000000000 4.196400000000001 4.196600000000000 4.196800000000001
But it should be like this 4.196000000000000 4.196200000000000 4.196400000000000 4.196600000000000 4.196800000000000
why matlab is not giving me accurate answer and adding 0.000000000000001 at alternative figures.

Best Answer

Welcome to the joy of round-off error. 0.0002 is not exactly representable in binary, hence adding these floating point numbers causes error at the level of machine precision. Enter eps(4) to see what machine precision is for these values.