I'm trying to compute the value of cos x
using the Taylor series formula
infinity
---- 2k
\ k x
cos(x) = / (-1) * -------------
---- (2k)!
k=0
Shown graphically at http://ppt.cc/G,DC
Here is my program.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double sum=0.0,sum1=0.0;
double x;
cin>>x;
for(int i=0 ; i<=10 ; i=i+1 )
{
for(int i=1 ; i<=20 ; i=i+1)
{
sum1=i*sum1+sum1;
}
sum=pow(-1,(double)i)*pow(x,(double)(2*i))/sum1+sum;
}
cout<<"Sum : "<<sum<<endl;
system("pause");
return 0;
}
The output is -1.#IND
Why?
How can I change the order of "sum1" to make it work right?