当前位置:网站首页>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

边栏推荐
猜你喜欢

一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系

Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT

ORM模型--数据记录的创建操作,查询操作

高数_第1章空间解析几何与向量代数_向量的数量积

每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?

Methods of adding centerlines and centerlines in SolidWorks drawings

【acwing】786. Number k

Embedded background - chip

【acwing】789. 数的范围(二分基础)

IO模型复习
随机推荐
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
Several schemes of building hardware communication technology of Internet of things
Google Colab装载Google Drive(Google Colab中使用Google Drive)
STM32基础知识—内存映射
Parameter sniffing (1/2)
Mongodb creates an implicit database as an exercise
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
2022.7.5DAY597
Phpcms realizes PC website access to wechat native payment
SQLyog数据库怎么取消自动保存更改
Programming features of ISP, IAP, ICP, JTAG and SWD
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
ES6中的函數進階學習
How to cancel automatic saving of changes in sqlyog database
Smart city construction based on GIS 3D visualization technology
ORM--分组查询,聚合查询,查询集QuerySet对象特性
学习记录——高精度加法和乘法
STM32中AHB总线_APB2总线_APB1总线这些是什么
Finally, there is no need to change a line of code! Shardingsphere native driver comes out