MATLAB Answers

Why do I get incorrect results with floating point arithmetic when using the colon operator?

9 views (last 30 days)
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

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Aug 2015
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.


Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!