Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
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$1@fred.mathworks.com>
References: <h8mqh5$pgb$1@fred.mathworks.com> <JRCrm.36321$JG1.20628@newsfe24.iad> <h8pflj$qaf$1@fred.mathworks.com> <h8piqr$gnu$1@fred.mathworks.com> <03esm.40290$ec2.8916@newsfe13.iad> <h8tkq3$hu8$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1253208222 7406 172.31.44.65 (17 Sep 2009 17:23:42 GMT)
X-Complaints-To: news@mathworks.com
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: news.mathworks.com comp.soft-sys.matlab:571162


"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message 
news:h8tkq3$hu8$1@fred.mathworks.com...
> "Nasser Abbasi" <nma@12000.org> wrote in message 
> <03esm.40290$ec2.8916@newsfe13.iad>...

*snip*

> 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:

http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ