MATLAB: Unexpected result for arithmetic operation

vectors

Dear all,
I am having some difficulty with what appears to be a bug. Consider the following simple code:
clear;clc;clf;
td=[0.01 0.20:0.20:1.0];
tbinw=0.05;
ibt=ceil(td./tbinw);
tdc=ibt.*tbinw-(tbinw/2.0);
When this is executed, the result for the vector tdc is
tdc =
0.0250 0.1750 0.3750 0.6250 0.7750 0.9750
However, when I test whether the 2nd element of tdc is 0.175, the result is false. Apparently, tdc(2)-0.175 = 2.7756e-17. However, this precision problem does not arise for any of the other elements in tdc.I am wondering why this is the case. This behaviour leads to an error in a subsequent part of my code. I am using a student version of Matlab R2013a.

Best Answer