sum of series using float

戏子无情 提交于 2021-02-07 14:18:29

问题


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 ?


回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/12272977/sum-of-series-using-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!