当前位置:网站首页>01 use function to approximate cosine function (15 points)
01 use function to approximate cosine function (15 points)
2022-07-07 10:21:00 【qq_ forty-two million one hundred and twenty thousand eight hun】
Zhejiang University Edition 《C Language programming experiment and exercise guidance ( The first 3 edition )》 Topic collection
Function interface definition
double funcos( double e, double x );
The parameter passed in by the user is the upper error limit e
And independent variables x
; function funcos
Should return calculated with the given formula 、 And meet the error requirements cos(x) Approximate value . The input and output are within the double precision range
Example of Referee Procedure
#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;
}
/* Your code will be embedded here */
sample input :
0.01 -3.14
sample output :
cos(-3.14) = -0.999899
My code
double fact(int n)
{
if(n == 0)
{
return 1;
}
else
return n*fact(n-1);
}
double funcos(double e,double x)
{
double m = 0;
int n = 0; // Power and order multipliers
double res = 0;
int flag = 1; // Control sign
m = pow(x,n)/fact(n);
while(m >= e)
{
res += flag * m;
n += 2;
flag = -flag; // Change sign
m = pow(x,n)/fact(n); // Here is the absolute value of each term
}
// The last one is that the accuracy is less than e Of
res += m;
return res;
}
Submit results
The reason for the error
The accuracy of the last term is less than e When the terms of are added to the sum Without symbol
Without symbol
Modified code
double fact(int n)
{
if(n == 0)
{
return 1;
}
else
return n*fact(n-1);
}
double funcos(double e,double x)
{
double m = 0, res = 0;
int n = 0; // Power and order multipliers
int flag = 1; // Control sign
m = pow(x,n)/fact(n);
while(m >= e)
{
res += flag * m;
n += 2;
flag = -flag; // Change sign
m = pow(x,n)/fact(n); // Here is the absolute value of each term
}
// The last one is that the accuracy is less than e Of
res += flag*m; // There's a leak here *flag Cause the second checkpoint error
return res;
}
Submit results
边栏推荐
猜你喜欢
fiddler-AutoResponder
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
XML配置文件解析与建模
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
[sword finger offer] 42 Stack push in and pop-up sequence
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Guide de signature du Code Appx
Postman interface test V
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
mysql插入数据创建触发器填充uuid字段值
随机推荐
STM32基础知识—内存映射
电表远程抄表拉合闸操作命令指令
The landing practice of ByteDance kitex in SEMA e-commerce scene
Some test points about coupon test
Guid primary key
ES类和对象、原型
虚数j的物理意义
ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
Fiddler break point
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Introduction to uboot
ORM模型--关联字段,抽象模型类
Appx code signing Guide
【acwing】786. Number k
In addition to the objective reasons for overtime, what else is worth thinking about?
STM32 ADC and DMA
JMeter loop controller and CSV data file settings are used together
Postman interface test II
Advanced function learning in ES6
The Himalaya web version will pop up after each pause. It is recommended to download the client solution