Well, actually Iain makes a misstatement of sorts. Matlab does not do "sums" to 15 significant digits. It uses floating point arithmetic, doubles to be exact, which effectively use a 52 bit binary mantissa. That is where the 15 significant digit number came from that was referred to by Iain.
log10(2^53-1)
ans =
15.955
So we actually get a little more than 15 significant decimal digits that we can "trust".
If I add two floating point numbers together, MATLAB (or ANY engine that uses floating point arithmetic, not just MATLAB) will lose the bits (information content) that fall below that limit. Since a double cannot carry more precision than that held in a double, this MUST happen. And remember that computers store your numbers internally in a binary form, NOT decimal.
Unfortunately it is true that the order you add a sequence of floating point numbers together affects the result, because of those low order bits being serially lost to the bit bucket. Sorry, but this is simply a fact of life when using ANY computational tool that works with floating point numbers. For example, suppose we were working in 3 significant decimal digits? What would you expect as a result for the operation:
Remember, you can only store 3 significant decimal digits in the result! Would you expect 0.135, or 0.1353? There is no magic here. Computers have limits, unless you are writing a TV show, where computers are all-knowing.
Welcome to the sometimes wacky world of computer arithmetic and floating point numbers. Do some reading, starting here . It will help you to understand such simple things as: (.1 + .2) == .3
ans =
0
(.1 + .2) - .3
ans =
5.5511e-17
Sadly, there is often some divergence between mathematics and what you can perform using computer arithmetic. If your research depends on exact results, then sorry, but you need to either learn enough about numerical analysis to be able to deal with these issues, or learn to work in a higher precision. And sadly, working in a sufficiently high precision will be computationally expensive, as it will be seriously slower.
Personally, I'd suggest learning the numerical methods one needs to avoid problems and to learn to what extent one can to trust those doubles to be yield the results you need. To a large extent, this is simply learning the concepts of significant digits and how to use a tolerance in your tests. (I thought people learned about significant digits at an early age in school? Maybe no more.) An in-depth understanding of numerical methods can help in many ways too. Often one learns better ways to perform a computation, ways that are numerically stable, as opposed to the more direct and brute force solution.
5 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_241928
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_241928
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_241946
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_241946
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_241953
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_241953
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_244502
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_244502
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_244504
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/157905-problemas-with-sum-function#comment_244504
Sign in to comment.