Why do I get incorrect results with floating point arithmetic when using the colon operator?
1 view (last 30 days)
Show older comments
MathWorks Support Team
on 21 Mar 2013
Edited: MathWorks Support Team
on 16 Apr 2023
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
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
.
0 Comments
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!