C ++ - 计算sum系列程序中的舍入误差

时间:2016-02-17 13:22:52

标签: c++

#include <iostream>

using namespace std;

// on input n returns the value a_n as a double
double term(int n) { 
  double val = 1.0 / (n+1.0) / (n+1.0);
  if (n%2 != 0) val = -val;
  return val;
}


/* computes the sum for i from k to k+n-1 of term(i) by the
 * direct upwards method */ 
double direct_up(int k,int n) {
  double sum = 0.0;
  for (int i=0; i<n; i++) {
    sum += term(k+i);
  }
  return sum;
}

int main() {
  cout.precision(16);
  int nterms = 0;
  int ft = 0;
  cout << "Enter first term, number of terms" << endl;
  cin >> ft >> nterms;
  cout << "The sum of the " << nterms << " terms starting at " << ft << endl;
  cout << "direct_up:     " << direct_up(ft, nterms) << endl;

  return 0;
}

我创建了一个程序,该程序采用公式并从第k个术语到第(n-1)个术语逐个添加术语。但是我无法弄清楚如何计算每个术语后的舍入误差?

请问你能帮帮我吗?

0 个答案:

没有答案