我计算了该系列的前 20 个元素——
在 2 种方式,第 1 前进,第 2 后退。为此我做了-
#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;
}
我得到了 -
sum 20 first elements - forward: 1.5000001192092896
sum 20 first elements - back: 1.5
有人可以解释为什么这两种方式之间有区别吗?