From: "Steven Lord" <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: adding order matters for accuracy ?
Date: Thu, 17 Sep 2009 13:23:57 -0400
Organization: The MathWorks, Inc.
Lines: 50
Message-ID: <h8trau$77e$>
References: <h8mqh5$pgb$> <JRCrm.36321$JG1.20628@newsfe24.iad> <h8pflj$qaf$> <h8piqr$gnu$> <03esm.40290$ec2.8916@newsfe13.iad> <h8tkq3$hu8$>
Reply-To: "Steven Lord" <>
X-Trace: 1253208222 7406 (17 Sep 2009 17:23:42 GMT)
NNTP-Posting-Date: Thu, 17 Sep 2009 17:23:42 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: comp.soft-sys.matlab:571162

"James Tursa" <> wrote in message 
> "Nasser Abbasi" <> wrote in message 
> <03esm.40290$ec2.8916@newsfe13.iad>...


> The same is true for the other numbers. So when you subsequently do the 
> sum(x) command, you are summing up the numbers as if they were exactly 
> equal to the original formula, and are not working with the actual 
> floating point values that all of the floating point methods actually 
> start with. So comparing the sym sum to the various floating point sums is 
> not a valid comparison, particularly for an accuracy assessment. What one 
> should do instead is add up the actual floating point numbers using some 
> type of extended precision scheme (e.g. vpa) and compare *that* to the 
> various floating point sums. i.e., make sure everybody starts with exactly 
> the same numbers. E.g., use num2strexact on the floating point array, turn 
> those into vpa numbers, and then add *those* up. e.g.,
> digits 100
> N = 10000;
> x = 1./(1:N);
> xx = num2strexact(x);
> vx(N) = vpa(0);
> for k=1:N
>    vx(k) = vpa(xx{k});
> end
> sumvx = sum(vx)
> 9.787606036044382210194571627970283600461698370054364204406738281250

If you have Symbolic Math Toolbox, you can use:

N = 10000;
x = sym(1./(1:N), 'f');
vpa(sum(x), 100)

sym(z, 'f') writes Z in the form +/- N*2^e with N, e integers -- which 
should look a little familiar from the bottom of the first page of this 
Cleve's Corner article:

Steve Lord
comp.soft-sys.matlab (CSSM) FAQ: