// program to compute the cos using the Taylor series expansion
// uses double precision for the computation
// reads in the value of x and computes the Taylor series term by term

// header files that include standard functions
#include <stdio.h>   // for IO
#include <string.h>  // for manipulating strings
#include <math.h>    // alternate math functions

//the main program
int main()
{
//define variables used in the program
long int j;
double x, dum, sum ;

//send a message to the screen
printf("type in the value of x and hit return\n") ;

//read in the variable x
scanf("%lf",&x) ;

//send messages to the screen
printf("you typed in x = %22.16E\n",x) ;
printf("hit return to see each succeeding term\n") ;

//initialize variables used in the Taylor series
dum = double(1) ; j = 1 ; sum = dum ;

//send message to the screen
printf("the %i term = %22.16E\n",(j/2),dum);
printf("appr = %22.16E\n",sum) ;
printf("exac = %22.16E\n",cos(x)) ;
getchar() ;
//compute terms until cos converges
 while(fabs(dum) > 1.e-16)
 {
//compute the next term in the series
 dum *= double(-1)*x*x/double(j*j+j) ;

//update which term you're on
 j += 2 ;

//add the term to all of the previous terms in the series
 sum += dum ;

//send info to the screen about how the series is going
 printf("the %i term = %22.16E\n",(j/2),dum);
 printf("appr = %22.16E\n",sum) ;
 printf("exac = %22.16E\n",cos(x)) ;
 getchar() ;
 }//while(fabs(dum) > 1.e-8)
}
