MATLAB: Do I get incorrect results with floating point arithmetic when using the colon operator

arithmeticerrorsfloatingMATLABpoint

When a vector of type double is created using the colon operator, arithmetic/relational operations on the vector may produce incorrect results.
myvector = 0:.1:1.1;
find(myvector == .9)
generates the following output:
ans =
Empty matrix: 1-by-0

Best Answer

This behavior is due to precision limitations of floating-point arithmetic. When specifying an interval [a,b] in steps of d using the colon operator (a:d:b), MATLAB generates the first half of the interval using a+xd and the latter half using b-xd, where x is a natural number.
As a result, there is a greater accumulation of rounding errors toward the middle of the generated vector. Any subsequent arithmetic/relational operations on the vector may produce unexpected results because of these rounding errors.
Use best practices when writing code to perform such operations on floating point numbers. Suggestions can be found in this related MATLAB Answers post.