本题为郑莉的C++语言程序设计例3-3
arctanx(x)=x-1/3*x^3+1/5*x^5-1/7*x^7+1/9*x^9...
当级数中某一项小于10^-15时停止。
代码如下:
#include <iostream>
using namespace std;
//first, calc arctanx,
// arctanx(x)=x - 1 / 3 * x ^ 3 + 1 / 5 * x ^ 5 - 1 / 7 * x ^ 7 + 1 / 9 * x ^ 9
//double arctan1 ( double x )
//{
// double val = 0;
// double a = x;
// double sqr = x * x;
// int i = 1;
//
// if ( x < 0 )
// a *= -1;
//
// for ( int j = 1; a / j > 1e-15; j += 2, i *= -1 )
// {
// val += i * a / j;
// a *= sqr;
//
//to find what happend
cout << " a/j = " << a / j << endl;
cout << " val = " << val << ", a = " << a << endl;
cout << " i = " << i << ", j = " << j << endl;