From: Bruno Luong on
"Christopher " <camejia(a)remove.this.alum.mit.edu> wrote in message <hpd3lb$8mc$1(a)fred.mathworks.com>...
> Hi,
>
> I've run into an interesting problem with the following code:
>
> x1 = (0 : 95999) / 40e6;
> x2 = x1 - mean(x1);
> sum(x2)
> sum(x2)
>
> As I repeat the sum(x2) command and print out the result, I get slightly different answers, not all of which are zero. I can understand that the result doesn't come out to be exactly zero due to precision issues, but I would like to get the same answer every time. Any ideas what is going on here?
>
> I thought may it was due to the use of threading, if different numbers of threads would interchange the order of operations and hence the final result. But setting maxNumCompThreads to 1 still gives an answer that varies.
>
> Here's some output from the 'ver' command:
> MATLAB Version 7.8.0.347 (R2009a)
> Operating System: Microsoft Windows XP x64 Version 5.2 (Build 3790: Service Pack 2)
>

I believe you point to an almost death horse. Take a look of this thread:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/260828
http://www.mathworks.com/matlabcentral/newsreader/view_thread/261541

There is a serious issue with 2009A.

If you want predictable sum result, take a look at Jan's XSum
http://www.mathworks.com/matlabcentral/fileexchange/26800-xsum (I have my own FEX for this very reason).

Bruno