Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum of series using float

I calculated the first 20 elements of the series -

enter image description here

in 2 ways , 1st - forward , 2nd - backward. For this I did -

#include <iostream>
#include <math.h>
using namespace std;

float sumSeriesForward(int elementCount) {
    float sum = 0;
    for (int i = 0; i < elementCount; ++i) {
        sum += (float) 1 / (pow(3, i));
    }
    return sum;
}

float sumSeriesBack(int elementCount) {
    float sum = 0;
    for (int i = (elementCount - 1); i >= 0; --i) {
        sum += (float) 1 / (pow(3, i));
    }
    return sum;
}

int main() {
    cout.precision(30);
    cout << "sum 20 first elements - forward: " << sumSeriesForward(20) << endl;
    cout << "sum 20 first elements - back: " << sumSeriesBack(20) << endl;
}

And I got -

sum 20 first elements - forward: 1.5000001192092896
sum 20 first elements - back: 1.5

Can someone please explain why is the difference between the 2 ways ?

like image 690
URL87 Avatar asked Sep 05 '12 00:09

URL87


People also ask

Can you use sum on a float?

The sum () function can add complex numbers just like it is used to do a sum of integers, float numbers or a combination of both.

Can you use sum on floats in Python?

Additionally, you can use sum() with any other numeric Python types, such as float , complex , decimal.

How do you find the sum of a float in Python?

Python sum of floats Output: 7.0 If you want to add floating point values with extended precision, you can use math. fsum() function.


2 Answers

In general, floating point numbers cannot represent values exactly. When you operate on the values errors propagate. In your example, when computing backwards you add small values to ever bigger numbers, having a good chance that the sum of the small numbers so far has an effect on the bigger number. On the other hand, when you compute forward you start with the big numbers and the smaller numbers have ever less effect on it. That is, when summing you always want to sum smallest to biggest.

Just consider keep a sum in just a fixed number of digits. For example, keep 4 digits and sum these numbers top to bottom and bottom to top:

values   top to bottom   bottom to top
10.00      10.00            10.01
0.004      10.00            0.010
0.003      10.00            0.006
0.002      10.00            0.003
0.001      10.00            0.001

Floating point numbers work just the same way, using a fixed number of [binary] digits.

like image 140
Dietmar Kühl Avatar answered Oct 04 '22 21:10

Dietmar Kühl


To improve accuracy when summing numbers, consider the Kahan summation algorithm. This significantly reduces the error compared to the obvious approach (including summing numbers from smallest to greatest).

like image 28
Eric Postpischil Avatar answered Oct 04 '22 20:10

Eric Postpischil