Re: Does the order matter to add a sequence of floating numbers



On Oct 30, 11:10 am, JosephLee <shoupe...@xxxxxxxxx> wrote:
Let's say float arr[100], we want to add them up, does the order
matter? what is the different between arr[0] +..+ arr[99], and
arr[99]+...+arr[0]?

One way around this (if applicable) is to use rationals as
terms instead of floats.

# Python
import gmpy
print 'The Basel problem: sum of square inverses'
print
terms = [gmpy.mpq(1,i**2) for i in xrange(1,101)]
print 'first 100 terms forwards:'
for i in terms:
print i,
print
print
print 'sum of forwards terms'
forward_sum = sum(terms)
print forward_sum
print
print 'first 100 terms backwards:'
for i in terms[::-1]:
print i,
print
print
print 'sum of backwards terms'
backward_sum = sum(terms[::-1])
print backward_sum
print
print 'forwards same as backwards?',
print forward_sum==backward_sum
print
print """Convert to float AFTER summing the rationals to
eliminate accumulating rounding errors."""
nf = gmpy.mpf(gmpy.numer(forward_sum))
df = gmpy.mpf(gmpy.denom(forward_sum))
print
ff = nf/df
print 'forwards float\n',ff
nb = gmpy.mpf(gmpy.numer(backward_sum))
db = gmpy.mpf(gmpy.denom(backward_sum))
print
bf = nb/db
print 'backwards float\n',bf
print
print 'forwards same as backwards?',
print ff==bf
print
print
print 'The Basel problem: the stupid way'
print
terms = [gmpy.mpf(1.0/i**2) for i in xrange(1,101)]
print 'first 100 terms forwards:'
for i in terms:
print i,
print
print
print 'sum of forwards terms'
forward_sum = sum(terms)
print forward_sum
print
print 'first 100 terms backwards:'
for i in terms[::-1]:
print i,
print
print
print 'sum of backwards terms'
backward_sum = sum(terms[::-1])
print backward_sum
print
print 'forwards same as backwards?',
print forward_sum==backward_sum

## The Basel problem: sum of square inverses
##
## first 100 terms forwards:
## 1 1/4 1/9 1/16 1/25 1/36 1/49 1/64 1/81 1/100 1/121
## 1/144 1/169 1/196 1/225 1/256 1/289 1/324 1/361
## 1/400 1/441 1/484 1/529 1/576 1/625 1/676 1/729
## 1/784 1/841 1/900 1/961 1/1024 1/1089 1/1156 1/1225
## 1/1296 1/1369 1/1444 1/1521 1/1600 1/1681 1/1764
## 1/1849 1/1936 1/2025 1/2116 1/2209 1/2304 1/2401
## 1/2500 1/2601 1/2704 1/2809 1/2916 1/3025 1/3136
## 1/3249 1/3364 1/3481 1/3600 1/3721 1/3844 1/3969
## 1/4096 1/4225 1/4356 1/4489 1/4624 1/4761 1/4900
## 1/5041 1/5184 1/5329 1/5476 1/5625 1/5776 1/5929
## 1/6084 1/6241 1/6400 1/6561 1/6724 1/6889 1/7056
## 1/7225 1/7396 1/7569 1/7744 1/7921 1/8100 1/8281
## 1/8464 1/8649 1/8836 1/9025 1/9216 1/9409 1/9604
## 1/9801 1/10000
##
## sum of forwards terms
## 158950869413303787311229792851755385970238349854
## 3709859889432834803818131090369901/9721861444343
## 810305896579766726231441619755839957462417827203
## 54705517986165248000
##
## first 100 terms backwards:
## 1/10000 1/9801 1/9604 1/9409 1/9216 1/9025 1/8836
## 1/8649 1/8464 1/8281 1/8100 1/7921 1/7744 1/7569
## 1/7396 1/7225 1/7056 1/6889 1/6724 1/6561 1/6400
## 1/6241 1/6084 1/5929 1/5776 1/5625 1/5476 1/5329
## 1/5184 1/5041 1/4900 1/4761 1/4624 1/4489 1/4356
## 1/4225 1/4096 1/3969 1/3844 1/3721 1/3600 1/3481
## 1/3364 1/3249 1/3136 1/3025 1/2916 1/2809 1/2704
## 1/2601 1/2500 1/2401 1/2304 1/2209 1/2116 1/2025
## 1/1936 1/1849 1/1764 1/1681 1/1600 1/1521 1/1444
## 1/1369 1/1296 1/1225 1/1156 1/1089 1/1024 1/961
## 1/900 1/841 1/784 1/729 1/676 1/625 1/576 1/529
## 1/484 1/441 1/400 1/361 1/324 1/289 1/256 1/225
## 1/196 1/169 1/144 1/121 1/100 1/81 1/64 1/49 1/36
## 1/25 1/16 1/9 1/4 1
##
## sum of backwards terms
## 158950869413303787311229792851755385970238349854
## 3709859889432834803818131090369901/9721861444343
## 810305896579766726231441619755839957462417827203
## 54705517986165248000
##
## forwards same as backwards? True
##
## Convert to float AFTER summing the rationals to
## eliminate accumulating rounding errors.
##
## forwards float
## 1.6349839001848928650771694981803237666833217000312
## 63813853110980485631588881260641124619
##
## backwards float
## 1.6349839001848928650771694981803237666833217000312
## 63813853110980485631588881260641124619
##
## forwards same as backwards? True
##
##
## The Basel problem: the stupid way
##
## first 100 terms forwards:
## 1.0 0.25 0.111111111111111104943 0.0625
## 0.0400000000000000008327 0.0277777777777777762358
## 0.0204081632653061208205 0.015625 0.012345679012345678327
## 0.0100000000000000002082 0.00826446280991735560129
## 0.00694444444444444405895 0.00591715976331360932888
## 0.00510204081632653020512 0.00444444444444444444059
## 0.00390625 0.00346020761245674750989
## 0.00308641975308641958176 0.00277008310249307479825
## 0.00250000000000000005204 0.00226757369614512477654
## 0.00206611570247933890032 0.00189035916824196602477
## 0.00173611111111111101474 0.00160000000000000007667
## 0.00147928994082840233222 0.00137174211248285312335
## 0.00127551020408163255128 0.00118906064209274662385
## 0.00111111111111111111015 0.00104058272632674307309
## 9.765625e-4 9.18273645546372808447e-4
## 8.65051903114186877472e-4 8.16326530612244928228e-4
## 7.71604938271604895439e-4 7.30460189919649393285e-4
## 6.92520775623268699563e-4 6.57462195923734418063e-4
## 6.2500000000000001301e-4 5.94883997620464038091e-4
## 5.66893424036281194135e-4 5.40832882639264507681e-4
## 5.1652892561983472508e-4 4.93827160493827172112e-4
## 4.72589792060491506193e-4 4.52693526482571295157e-4
## 4.34027777777777753684e-4 4.16493127863390270611e-4
## 4.00000000000000019169e-4 3.84467512495194167765e-4
## 3.69822485207100583055e-4 3.5599857600569599636e-4
## 3.42935528120713280838e-4 3.3057851239669423923e-4
## 3.1887755102040813782e-4 3.07787011388119409981e-4
## 2.97265160523186655964e-4 2.87273771904625124188e-4
## 2.77777777777777777537e-4 2.68744961031980626135e-4
## 2.60145681581685768273e-4 2.5195263290501388213e-4
## 2.44140625e-4 2.36686390532544376408e-4
## 2.29568411386593202112e-4 2.22766763198930731063e-4
## 2.16262975778546719368e-4 2.10039907582440651349e-4
## 2.04081632653061232057e-4 1.98373338623289029083e-4
## 1.9290123456790122386e-4 1.87652467629949342353e-4
## 1.82615047479912348321e-4 1.77777777777777786297e-4
## 1.73130193905817174891e-4 1.68662506324843982459e-4
## 1.64365548980933604516e-4 1.6023073225444639826e-4
## 1.56250000000000003253e-4 1.5241579027587258432e-4
## 1.48720999405116009523e-4 1.45158949049208871361e-4
## 1.41723356009070298534e-4 1.38408304498269907985e-4
## 1.3520822065981612692e-4 1.32117849121416309609e-4
## 1.2913223140495868127e-4 1.26246686024491849626e-4
## 1.23456790123456793028e-4 1.20758362516604271465e-4
## 1.18147448015122876548e-4 1.15620302925193658224e-4
## 1.13173381620642823789e-4 1.10803324099722987051e-4
## 1.08506944444444438421e-4 1.06281220108406850586e-4
## 1.04123281965847567653e-4 1.02030405060708089827e-4
## 1.00000000000000004792e-4
##
## sum of forwards terms
## 1.63498390018489285491
##
## first 100 terms backwards:
## 1.00000000000000004792e-4 1.02030405060708089827e-4
## 1.04123281965847567653e-4 1.06281220108406850586e-4
## 1.08506944444444438421e-4 1.10803324099722987051e-4
## 1.13173381620642823789e-4 1.15620302925193658224e-4
## 1.18147448015122876548e-4 1.20758362516604271465e-4
## 1.23456790123456793028e-4 1.26246686024491849626e-4
## 1.2913223140495868127e-4 1.32117849121416309609e-4
## 1.3520822065981612692e-4 1.38408304498269907985e-4
## 1.41723356009070298534e-4 1.45158949049208871361e-4
## 1.48720999405116009523e-4 1.5241579027587258432e-4
## 1.56250000000000003253e-4 1.6023073225444639826e-4
## 1.64365548980933604516e-4 1.68662506324843982459e-4
## 1.73130193905817174891e-4 1.77777777777777786297e-4
## 1.82615047479912348321e-4 1.87652467629949342353e-4
## 1.9290123456790122386e-4 1.98373338623289029083e-4
## 2.04081632653061232057e-4 2.10039907582440651349e-4
## 2.16262975778546719368e-4 2.22766763198930731063e-4
## 2.29568411386593202112e-4 2.36686390532544376408e-4
## 2.44140625e-4 2.5195263290501388213e-4
## 2.60145681581685768273e-4 2.68744961031980626135e-4
## 2.77777777777777777537e-4 2.87273771904625124188e-4
## 2.97265160523186655964e-4 3.07787011388119409981e-4
## 3.1887755102040813782e-4 3.3057851239669423923e-4
## 3.42935528120713280838e-4 3.5599857600569599636e-4
## 3.69822485207100583055e-4 3.84467512495194167765e-4
## 4.00000000000000019169e-4 4.16493127863390270611e-4
## 4.34027777777777753684e-4 4.52693526482571295157e-4
## 4.72589792060491506193e-4 4.93827160493827172112e-4
## 5.1652892561983472508e-4 5.40832882639264507681e-4
## 5.66893424036281194135e-4 5.94883997620464038091e-4
## 6.2500000000000001301e-4 6.57462195923734418063e-4
## 6.92520775623268699563e-4 7.30460189919649393285e-4
## 7.71604938271604895439e-4 8.16326530612244928228e-4
## 8.65051903114186877472e-4 9.18273645546372808447e-4
## 9.765625e-4 0.00104058272632674307309
## 0.00111111111111111111015 0.00118906064209274662385
## 0.00127551020408163255128 0.00137174211248285312335
## 0.00147928994082840233222 0.00160000000000000007667
## 0.00173611111111111101474 0.00189035916824196602477
## 0.00206611570247933890032 0.00226757369614512477654
## 0.00250000000000000005204 0.00277008310249307479825
## 0.00308641975308641958176 0.00346020761245674750989
## 0.00390625 0.00444444444444444444059
## 0.00510204081632653020512 0.00591715976331360932888
## 0.00694444444444444405895 0.00826446280991735560129
## 0.0100000000000000002082 0.012345679012345678327
## 0.015625 0.0204081632653061208205 0.0277777777777777762358
## 0.0400000000000000008327 0.0625 0.111111111111111104943
## 0.25 1.0
##
## sum of backwards terms
## 1.63498390018489285551
##
## forwards same as backwards? False


.



Relevant Pages

  • Re: problem in my algorithm...
    ... Consider the terms of the sum until the difference between the values ... with addiction of two consecutive terms be inferior to 0,0001. ... static int factorial{ ... float difference = 1.0f; ...
    (comp.lang.java.programmer)
  • Re: Floating point arithmetic model of C...
    ... What's the cast to float in the ... Then the result plus the value in 'Sum' ... truncated to float precision for storing it in 'Sum'. ... All I can see here are small rounding errors, ...
    (comp.lang.c)
  • Re: Numerical Accuracy
    ... > float c, d; ... Try e.g. with paper and pencil to do what your algorithm does with 7 ... gets stored as 23410) then, after the sum has reached ... possible rounding error - but you will still end up with some ...
    (comp.programming)
  • Re: Some sort of weird round off error -- please help!
    ... The RHS is evaluated as a double, and then truncated to a float ... Sum += ) ... re-implementing something like a neural net simulation in C#. ...
    (comp.lang.c)
  • Re: representational error in C
    ... What I tried to represent here is that there are "goal posts" at ... there is a proposal to add a separate decimal float type ... int main{ ... float sum = 0.0; ...
    (comp.lang.c)