MATLAB: Colon operator numerical error

colonnumerical errror

When I create a vector using the colon operator I noticed for certain vectors there is a small numerical error in the values returned. For example: >> a = 0:.1:1
a =
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
>> a(4)
ans =
0.3
>> a(4) == .3
ans =
0
>> a(4) – .3
ans =
5.5511e-17
I find this very strange. Do other people have this same problem?

Best Answer

Yes, everyone has this problem. Please read this FAQ