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

1 view (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 20 Feb 2023
Edited: MathWorks Support Team on 16 Apr 2023
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
.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!