/* calculate cos(x) with Taylor-Polynom * Apel Roland * 16.1.97 */ #include #include #define CALCTYPE float #define SQR(x) (x*x) /* stop if effect of next interation is to small */ #define EPSILON 0.000001 #define ABS(x) fabs(x) CALCTYPE kosinus = 0.; CALCTYPE term = 1.; CALCTYPE k2 = 0; CALCTYPE cosinus(CALCTYPE x) { while (ABS(term) > ABS(kosinus)*EPSILON) { kosinus = kosinus + term; k2 = k2 + 2; term = (-term*SQR(x))/ (CALCTYPE) (k2*(k2-1)); } return(kosinus); } int main (int argc, char ** argv) { if (argc != 2) { fprintf(stderr, "usage: %s \n" \ " e.g. %s 3.1415 \n", argv[0], argv[0]); return(1); } printf("result: %f \n",cosinus( atof(argv[1]) ) ); return (0); }