Error in simple calculations
2 views (last 30 days)
Show older comments
Hi everyone I'm really having a very weird and silly problem with Matlab. A very simple calculation which is z=(x-y). That's all, but when x= 0.327999967200000 and y=0.319999968000000 then z=0.00799999920000000 which is correct. But when x=0.311999968800000 and y=0.303999969600000 then z=0.00799999920000005 which is wrong as the answer should stay 0.00799999920000000. It is a simple silly stupid operation nevertheless the Matlab is unable to solve it correctly. I used everything, the format the cast the eps() non of them worked and the error is still persistent. What shall I do so the Matlab would perform such calculation without errors. Is it a bug or something? I'm using Matlab 2014b.
Thanx in advanced
0 Comments
Answers (1)
Sebastian Castro
on 17 Apr 2015
Definitely not a bug -- just floating-point error: http://www.mathworks.com/matlabcentral/answers/57444-faq-why-is-0-3-0-2-0-1-not-equal-to-zero
If you care about that level of precision, it might be feasible to work with fixed-point data. This way, you can choose a good fixed-point implementation that can exactly represent a particular set of numbers.
- Sebastian
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!