|
"Nasser Abbasi" <nma@12000.org> wrote in message <03esm.40290$ec2.8916@newsfe13.iad>...
>
> "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 =
>
>
597313034085775894957419063900720435075990271615035016466782973836978960833016759908348939758280349893584359200829987209579949248845718511404010106584326236570756261967912057051149825856817631553578794190662280234629951790018159277551900295396227837479741644522548411186348906442210512421180781362997906871214143794714445149389824313767894614959708188666686193953223439341905996514813590188152625439607978842565260598208894053124622893777384618380866343824612215121473714138136780315513321032480228954319886684342217194129266858456216941382158909475937776073321202278724121126809935257526344659920575983443227438426367692737965398834370216815670096365592214139218097969462935459832211290673332172386941856419946970122886631880774225783336118959213391001262785839708719165699146056120600979544125388554229642201473093946112066343878042681329403871651009543612830058829173821020245668252410821144177856
485069315166282840952361499073524457407570802702989702222989914241636195393795061234153050739009473088412640997321101329826327646320681988835237571570972827791051279474159086990464568030887833273392678923972182421498925316348193822140627246838360068452507762868844867154891904097375691145661295118835026497806409316492121295680359142342629237555597474129231111085202871302622094169134620056716693062698079340537814782496572783855659125183075251791052365069569018501579282149643716340503529014385772634154036887448763425816222812560307332227921808297919375106003328834551839619125736785747549900367115338025096728721559968638683888367279757293357979333889628513191943674938458500898249487022134500366205038306461075450919883137224067070589100178336535454147042495996500497755290730292151680153452932656068730081162476279338886244696819292093798510044459505476063197169670988167141009767925718082625154
161270746390757050069030231462982259584906028574464990043642300988472238609015433460763767519623143362387742211502892092117513784321740192807699874770937941116404735170647984280968366587473285197655366566762981965905006781313376651973491387886812598619683519369813130544619871316811444830893409918569880434280256568507279761537084838965126671529311764620813034333557650223117742025126499858721006217158564182156250454647879645419327387731913575386046114792255427590070980278703584245603174432425537472384139528825177690769836908407661157610543464704572835012727757052729320150155747299607467878698718309674278727961604972779622097479246102158224142400419199811751835535271099627106922623810159190229450386990461259391584595365156980915548589129000257180070850575758002411726509658989506196966502281807421274889233501974165139838994956136806978476468645273742432126902875281835270421448078128922705038
169024861043652850089150188167384946757065905031996299933353863075358172447289339461682955808309339764590374170117102095604380663249273106709878560259727037880309067307026900133532308585111644030030996298116423171478110982141866648268161121908536098741787812720230562904871591411047702425510770229222400006810469563850306705555830423972358105519926027069212031798789418658593755178490675484798127006910943220832915280565755243306942532120667579626914094486015846799903688926143971676820073326477307158263886402060659274099046064722582057809816249771465874353338949577259501345750309500054975390498089563259012210387546497039650361696570565463607426377923053764520121654291577655963632229498642773961001295494565455913054197839077458823778840781050544757887487023010813880389523364179404386344931022681434668610428366866939167701312025465823450647937793124441326923298685455190911714955268190309666076
47613196032465924759505098159065109562700803475781336819122118833950986063496350089772134885342508815568074059916785775991250653654294450398794588358184383834078617089708262257087101187548998458958580044184666521405648523984415244952257156609263761417864479017935110784761636467201575450552556321009717575885944618018433778615054989100841212323410929694159577066483119474451653087883705259075470794023256828427805445735028837916654644021388362210940577930190391582636540850980470296616719034963423846654535422064177667222098347718656546479437040239468796487003250200473537404025025079546001003902525774329756640146471253407001427049473235336540768396996735533484560623411926098144095723014746718672904489789302271079738185744351573488375624241287/610274904696896973421707666613951342951107162962026632823463431070154777219611627758225281594457276115724683206510306615996623737516779643709347169103774
354438461165855239151552006094190977271125788192016872214281106843834793284139759175131365965073425162512535558827114098186553210020305792968437490975085029413571793422944301838121578261006069625718604250456490066272727693234814331681190411860568442066373018561815646617326405461140219462411243468335092276273819463504512473988091936734856198030546130278457515940778057959454023772561819072085457111711743040964145431326818238644172451502866831934212042027638339674321937703083356306862345626442436657607617279312614497201553764872884420291231292634864294430940335111249078069154117936811625263668247528159514101291961152753663062416197315085250890592612744541390484869127505179136920322167319068732345837655203545543012427474049151206646864749972441739027087274461470642517233953762380165397985705347821309461826603688059868391557271379389580258692793355508973243322972950148078621123812950577075481
564435950586321667554907843407551732250014827817769850193908128333293700258816851179353659730711521329343036038890841739556415611812381241970674622274621251961991218733794935385897718168522024110086470157972977618598738417501273646534123433086525881679128446100598853871755569450854208950664781188193472740688935351410692344093065947842739249108960954589865435910081486778139146351822286044743986685882125398801177088886902079095078765735609314126862438799621512612065741086222433461625824404629408325200820594077173080832541045273107252529820966465166765097108150543899882332718683244644862334401919451590728534606859853473840952283884651077700641227746846768553396308964600322439162175630250002816781616749151233343910453511374216479627185148391641220171308811812760530897091482817037209509300027049789981927154813753436932737437768597699938681829499612327470821706269192824428699977276077921011921
163998729367004254653797480937223729240500255194305614317719086330595491470039405539775752286218326187051911045874398050781495284655971128773118288064175450767860047863528038503767294545768348144998968758228136279752890661764103252152343205877871950258025953656571172222683534975878254758184496735371595776303467912837779139438576392357624825622130106123794270720072033573881655451510870345645922790581919794628429566649751205140309874977840355903254442549441753086980868790068152946438956551972465108995034517138589707837956517644143762128424698533023776731236178794295770286624880518406514644448060600322736324166399543716496515849631229953907396339548553012926386519324009294068804938969291343419002754690881088133046968271386681774181806643153426490573782558297864277930779159262086040133666387574554465903811584454965086459089531582829215269981726468681587816412452437606077033806436338960251467
634902287797284897719446481617339726981148827986120870219462443231223352566205990591786306866790213046071566965293778877930705185843079806538184079001502229414132684783755372427753252631467965152621645743664525271455802752774300460697561546757214229331327461834560440799927225262046775414563478219460300522652091604783919106796860566741910532452523688945717136671869354735297830980715547291410432101627690518242540694114215847141916712938854377699262002143855969941353135386058694196393468760442186869622734809487629243963041222872715037936808013694056748106637684272956374655470141673392430509820190435623495765408563668052815436165671633498358826010297320826051088208992016557724015534105055047538380701360133843553578371805796184748799665610731620461973198696732719896378780220669892214647192604770530351459136340663436154415277521424969785201537312675554540654539808546557055564584469190426221391
5328037729341331940896530753657797438471315741036770153186758318883916115820892057395396795618893484129094048050596222795648559408659672397061567665463218095505770015238651624074909119968589623248826725106380303947966060659097097378869302555557178359416560485928816032985126405979389319280284777207457605421181461807138454400159907422045516533913313345710993828044114789191075875866114404717432236391996647027900922653043754944072697190599691082502222803704814458103130520035816265911562259346488897114507167127147224033663292066021873289732909336618094347985544288684637808103183718425600000
>
> 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
>
You are not really comparing apples to apples here. Start with these two lines:
N = 10000;
x = 1./(1:N);
This creates floating point approximations to the numbers 1/1, 1/2, 1/3, etc. For example, if you download my num2strexact function from the FEX (http://www.mathworks.com/matlabcentral/fileexchange/22239) and use it on x(3) you get the following:
num2strexact(x(3))
ans =
0.333333333333333314829616256247390992939472198486328125
So the actual value is of course only good to about 15-16 decimal digits, as we all know. But the sym command will "fix" that up:
x=sym(x);
x(3)
ans =
1/3
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
This number would be the best number to use for assessing accuracy of the various floating point methods, because it starts with exactly the same values as the other methods.
If one were really concerned about the most accurate strictly floating point method, I suppose one could sort the N array elements, replace the two smallest numbers with their sum, resort the N-1 elements, replace ... etc. But having said that, any program that would need to rely on such a scheme to get the *most* accurate answer is probably doomed from the beginning because of the accuracy of the original data.
James Tursa
|