A+B=B+A? Not quite…

Try to construct two different programs to calculate the value of

\displaystyle \sum_{k=1}^\infty \frac{1}{k^2}

Some will immediately noticed that this is actually \frac{\pi^2}{6} , 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 10^{-7} . This means that by the time the sum reaches k = 10^6 , the value held in ans is too large for the addition of 10^{-12} 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

A+B=B+A? Not quite…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s