Problem 5-7 using the cosine function approximation function (15point (s)). C

This problem required to achieve a function, seeking to cos (x) using the following approximation equation is accurate to less than the absolute value of a last e:

Here Insert Picture Description

Function interface definition:

double funcos( double e, double x );

Wherein the parameters passed to user error limit e and the argument x; funcos function should return it with a given formula, and meet the error requirement of cos (x) approximation. Input and output are in double precision.

Referee test program Example:

#include <stdio.h>
#include <math.h>

double funcos( double e, double x );

int main()
{    
    double e, x;

    scanf("%lf %lf", &e, &x);
    printf("cos(%.2f) = %.6f\n", x, funcos(e, x));

    return 0;
}

/* 你的代码将被嵌在这里 */

Sample input:

0.01 -3.14

Sample output:

cos(-3.14) = -0.999899
//   Date:2020/3/30
//   Author:xiezhg5
#include <stdio.h>
#include <math.h>

double funcos( double e, double x );

int main()
{    
    double e, x;

    scanf("%lf %lf", &e, &x);
    printf("cos(%.2f) = %.6f\n", x, funcos(e, x));

    return 0;
}

/* 你的代码将被嵌在这里 */
double funcos( double e, double x )
{
	double a=1.0,b=1.0,sum=1.0,item=1.0;
	int i,t=-1;
	//值得学习这种做法 
	for(i=2;fabs(item)>=e;i=i+2)
	{
		a=a*(i*(i-1));   //注意这种技巧 
		b=b*(x*x);      //每次递增二次怎么弄 
		item=1.0*t*b/a;
		sum=sum+item;
		t=-t;
	}
	return sum;
}
Published 137 original articles · won praise 99 · views 3242

Guess you like

Origin blog.csdn.net/qq_45645641/article/details/105209454