当前位置:网站首页>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
边栏推荐
- Serial communication relay Modbus communication host computer debugging software tool project development case
- ArcGIS operation: converting DWG data to SHP data
- Agile course training
- Bean operation domain and life cycle
- 单片机(MCU)最强科普(万字总结,值得收藏)
- 电表远程抄表拉合闸操作命令指令
- China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
- IO模型复习
- Postman interface test VI
- Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
猜你喜欢
The method of word automatically generating directory
Es classes and objects, prototypes
A wave of open source notebooks is coming
Guide de signature du Code Appx
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
Several schemes of building hardware communication technology of Internet of things
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
Programming features of ISP, IAP, ICP, JTAG and SWD
STM32 ADC和DMA
The landing practice of ByteDance kitex in SEMA e-commerce scene
随机推荐
Fiddler simulates the interface test
Before joining the chain home, I made a competitive product analysis for myself
mysql插入数据创建触发器填充uuid字段值
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
PDF文档签名指南
反射效率为什么低?
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
UnityWebRequest基础使用之下载文本、图片、AB包
基于gis三维可视化技术的智慧城市建设
Methods of adding centerlines and centerlines in SolidWorks drawings
Memory ==c language 1
Postman interface test IV
Programming features of ISP, IAP, ICP, JTAG and SWD
搭建物联网硬件通信技术几种方案
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
【acwing】786. 第k个数
Serial communication relay Modbus communication host computer debugging software tool project development case