Try to construct two different programs to calculate the value of

Some will immediately noticed that this is actually , but we try doing it by definition.

First, we calculate the sum by usual order

Next, we calculate the sum by reversed order

The answer should be the same, even for computers, right? However, when I tried in C++

First approach:

float zeta_up(long n){
float ans = 0.;
for (long k=1; k<=n; k++){
ans += 1./float(k)/float(k);
}
return ans;
}

Second approach:

float zeta_down(long n){
float ans = 0.;
for (long k=n; k>=1; k--){
ans += 1./float(k)/float(k);
}
return ans;
}

And a constant value for reference:

const float zeta = M_PI*M_PI/6.;

Three values are:

zeta_up = 1.64473
zeta_down = 1.64493
zeta = 1.64493

So, the second approach seems more accurate, why is that?

The discrepancy is due to roundoff issues. A float holds only so many digits and (on my computer) the epsilon value for a float is around . This means that by the time the sum reaches , the value held in ans is too large for the addition of to have any effect. Many terms at the end of the series have little or no effect on the sum; their contribution is lost. However, those tail terms do have a cumulative contribution on the actual sum and are needed to give an accurate result. By summing in the reverse order these small terms do not have their contributions lost.

C++ For Mathematicians by Edward Scheinerman

### Like this:

Like Loading...

*Related*