//
// source code: arctan.cc 
// purpose:     calculation of arctan via taylor expansion
// author:      T. Hebbeker   
// version:     1.0, 2006-10-25
//

#include <iostream>
using namespace std;

// function arcus tangens  for |x| < 1  (note: in math.h you find atan)

double arctan (double x)
{
  if(x<=-1 || x>=1) 
  { 
    cout << " Warning: argument of arctan invalid: " << x << endl;
    return 0;
  }

  const int n = 100000;    // number of terms: 
                           //    need many, slow convergence !
  double sum = 0.;
  double x2 = x*x;
  double nominator = x;
  double denominator = 1.;
  int sign = 1;

  for (int i=1; i<=n; i++)   
  {
     sum += sign * nominator/denominator;
     nominator *= x2;
     denominator +=2;
     sign = - sign;
  } 

  return sum;
}


int main()
{
  double x, y;

  cout << endl 
       << " program   === arctan ===   version 1.0 " 
       << endl;

  while(!cin.eof())
  {
    cout << endl
         << " enter value of x (-1...1) ";  
    cin >> x;

    y = arctan(x);

    cout << " arctan(" << x << ") = " << y << endl;
  }

  return 0;
}






