Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe13.iad.POSTED!7564ea0f!not-for-mail
From: "Nasser Abbasi" <nma@12000.org>
Newsgroups: comp.soft-sys.matlab
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>
Subject: Re: adding order matters for accuracy ?
Lines: 51
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3598
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
Message-ID: <03esm.40290$ec2.8916@newsfe13.iad>
NNTP-Posting-Host: biclefbbodjieonkdgphainoamommick
X-Complaints-To: abuse@charter.net
X-Trace: jkfjokholhdhgpccadefjppgkgeilljajnfgegcgndkhomfnbiclefbbodjieonkidbicdhfeagapemoebgomaakomaobeehcandkibfnekbjiapgibfjbfjjmegcohgjdfippodjfioglha
NNTP-Posting-Date: Wed, 16 Sep 2009 22:57:00 UTC
Date: Wed, 16 Sep 2009 17:57:10 -0500
Xref: news.mathworks.com comp.soft-sys.matlab:570942


"Matt Fig" <spamanon@yahoo.com> wrote in message 
news:h8piqr$gnu$1@fred.mathworks.com...
> Also, it is easy to check for yourself that the order matters in general:
>
>
> N = 10000;
> x = 1./(1:N);
> s = sum(x);  % Sum from largest to smallest.
> t = sum(x(end:-1:1));  % Sum from smallest to largest.
> u = sum(x(randperm(length(x))));  % Sum in a random order.
> fprintf('%s%16.16f\n','s = ',s)
> fprintf('%s%16.16f\n','t = ',t)
> fprintf('%s%16.16f\n','u = ',u)

Just to add, to see the true sum, add the following:

x=sym(x);
true_sum=sum(x)

This is the output of the whole run

s = 9.7876060360443482
t = 9.7876060360443855
u = 9.7876060360443446

true_sum =

59731303408577589495741906390072043507599027161503501646678297383697896083301675990834893975828034989358435920082998720957994924884571851140401010658432623657075626196791205705114982585681763155357879419066228023462995179001815927755190029539622783747974164452254841118634890644221051242118078136299790687121414379471444514938982431376789461495970818866668619395322343934190599651481359018815262543960797884256526059820889405312462289377738461838086634382461221512147371413813678031551332103248022895431988668434221719412926685845621694138215890947593777607332120227872412112680993525752634465992057598344322743842636769273796539883437021681567009636559221413921809796946293545983221129067333217238694185641994697012288663188077422578333611895921339100126278583970871916569914605612060097954412538855422964220147309394611206634387804268132940387165100954361283005882917382102024566825241082114417785648506931516628284095236149907352445740757080270298970222298991424163619539379506123415305073900947308841264099732110132982632764632068198883523757157097282779105127947415908699046456803088783327339267892397218242149892531634819382214062724683836006845250776286884486715489190409737569114566129511883502649780640931649212129568035914234262923755559747412923111108520287130262209416913462005671669306269807934053781478249657278385565912518307525179105236506956901850157928214964371634050352901438577263415403688744876342581622281256030733222792180829791937510600332883455183961912573678574754990036711533802509672872155996863868388836727975729335797933388962851319194367493845850089824948702213450036620503830646107545091988313722406707058910017833653545414704249599650049775529073029215168015345293265606873008116247627933888624469681929209379851004445950547606319716967098816714100976792571808262515416127074639075705006903023146298225958490602857446499004364230098847223860901543346076376751962314336238774221150289209211751378432174019280769987477093794111640473517064798428096836658747328519765536656676298196590500678131337665197349138788681259861968351936981313054461987131681144483089340991856988043428025656850727976153708483896512667152931176462081303433355765022311774202512649985872100621715856418215625045464787964541932738773191357538604611479225542759007098027870358424560317443242553747238413952882517769076983690840766115761054346470457283501272775705272932015015574729960746787869871830967427872796160497277962209747924610215822414240041919981175183553527109962710692262381015919022945038699046125939158459536515698091554858912900025718007085057575800241172650965898950619696650228180742127488923350197416513983899495613680697847646864527374243212690287528183527042144807812892270503816902486104365285008915018816738494675706590503199629993335386307535817244728933946168295580830933976459037417011710209560438066324927310670987856025972703788030906730702690013353230858511164403003099629811642317147811098214186664826816112190853609874178781272023056290487159141104770242551077022922240000681046956385030670555583042397235810551992602706921203179878941865859375517849067548479812700691094322083291528056575524330694253212066757962691409448601584679990368892614397167682007332647730715826388640206065927409904606472258205780981624977146587435333894957725950134575030950005497539049808956325901221038754649703965036169657056546360742637792305376452012165429157765596363222949864277396100129549456545591305419783907745882377884078105054475788748702301081388038952336417940438634493102268143466861042836686693916770131202546582345064793779312444132692329868545519091171495526819030966607647613196032465924759505098159065109562700803475781336819122118833950986063496350089772134885342508815568074059916785775991250653654294450398794588358184383834078617089708262257087101187548998458958580044184666521405648523984415244952257156609263761417864479017935110784761636467201575450552556321009717575885944618018433778615054989100841212323410929694159577066483119474451653087883705259075470794023256828427805445735028837916654644021388362210940577930190391582636540850980470296616719034963423846654535422064177667222098347718656546479437040239468796487003250200473537404025025079546001003902525774329756640146471253407001427049473235336540768396996735533484560623411926098144095723014746718672904489789302271079738185744351573488375624241287/6102749046968969734217076666139513429511071629620266328234634310701547772196116277582252815944572761157246832065103066159966237375167796437093471691037743544384611658552391515520060941909772711257881920168722142811068438347932841397591751313659650734251625125355588271140981865532100203057929684374909750850294135717934229443018381215782610060696257186042504564900662727276932348143316811904118605684420663730185618156466173264054611402194624112434683350922762738194635045124739880919367348561980305461302784575159407780579594540237725618190720854571117117430409641454313268182386441724515028668319342120420276383396743219377030833563068623456264424366576076172793126144972015537648728844202912312926348642944309403351112490780691541179368116252636682475281595141012919611527536630624161973150852508905926127445413904848691275051791369203221673190687323458376552035455430124274740491512066468647499724417390270872744614706425172339537623801653979857053478213094618266036880598683915572713793895802586927933555089732433229729501480786211238129505770754815644359505863216675549078434075517322500148278177698501939081283332937002588168511793536597307115213293430360388908417395564156118123812419706746222746212519619912187337949353858977181685220241100864701579729776185987384175012736465341234330865258816791284461005988538717555694508542089506647811881934727406889353514106923440930659478427392491089609545898654359100814867781391463518222860447439866858821253988011770888869020790950787657356093141268624387996215126120657410862224334616258244046294083252008205940771730808325410452731072525298209664651667650971081505438998823327186832446448623344019194515907285346068598534738409522838846510777006412277468467685533963089646003224391621756302500028167816167491512333439104535113742164796271851483916412201713088118127605308970914828170372095093000270497899819271548137534369327374377685976999386818294996123274708217062691928244286999772760779210119211639987293670042546537974809372237292405002551943056143177190863305954914700394055397757522862183261870519110458743980507814952846559711287731182880641754507678600478635280385037672945457683481449989687582281362797528906617641032521523432058778719502580259536565711722226835349758782547581844967353715957763034679128377791394385763923576248256221301061237942707200720335738816554515108703456459227905819197946284295666497512051403098749778403559032544425494417530869808687900681529464389565519724651089950345171385897078379565176441437621284246985330237767312361787942957702866248805184065146444480606003227363241663995437164965158496312299539073963395485530129263865193240092940688049389692913434190027546908810881330469682713866817741818066431534264905737825582978642779307791592620860401336663875745544659038115844549650864590895315828292152699817264686815878164124524376060770338064363389602514676349022877972848977194464816173397269811488279861208702194624432312233525662059905917863068667902130460715669652937788779307051858430798065381840790015022294141326847837553724277532526314679651526216457436645252714558027527743004606975615467572142293313274618345604407999272252620467754145634782194603005226520916047839191067968605667419105324525236889457171366718693547352978309807155472914104321016276905182425406941142158471419167129388543776992620021438559699413531353860586941963934687604421868696227348094876292439630412228727150379368080136940567481066376842729563746554701416733924305098201904356234957654085636680528154361656716334983588260102973208260510882089920165577240155341050550475383807013601338435535783718057961847487996656107316204619731986967327198963787802206698922146471926047705303514591363406634361544152775214249697852015373126755545406545398085465570555645844691904262213915328037729341331940896530753657797438471315741036770153186758318883916115820892057395396795618893484129094048050596222795648559408659672397061567665463218095505770015238651624074909119968589623248826725106380303947966060659097097378869302555557178359416560485928816032985126405979389319280284777207457605421181461807138454400159907422045516533913313345710993828044114789191075875866114404717432236391996647027900922653043754944072697190599691082502222803704814458103130520035816265911562259346488897114507167127147224033663292066021873289732909336618094347985544288684637808103183718425600000

EDU>> vpa(true_sum)

ans =

9.7876060360443822641784779048516

You can see that the sum from the lowest to largest (called 't' above) was 
the most accurate. To see this, align the digits

-------------------*
9.7876060360443822641784779048516  --> true
9.7876060360443855 -->t
9.7876060360443482 -->s

See that the digit marked '*',  it went wrong first in the 's' sum, which is 
the sum from large to small (it changed from 8 to 4). While the same digits 
it still correct in the 't' sum, which is the sum from small to large.

--Nasser