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

边栏推荐
- The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
- 【剑指Offer】42. 栈的压入、弹出序列
- STM32 Basics - memory mapping
- Programming features of ISP, IAP, ICP, JTAG and SWD
- Why is the reflection efficiency low?
- 单片机(MCU)最强科普(万字总结,值得收藏)
- 2022.7.4DAY596
- STM32 ADC和DMA
- 网上可以开炒股账户吗安全吗
- ORM -- database addition, deletion, modification and query operation logic
猜你喜欢

虚数j的物理意义

字符串格式化

【二开】【JeecgBoot】修改分页参数

Some thoughts on the testing work in the process of R & D

The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
![[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet](/img/3c/890f2481231482645554f86dd614a9.png)
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet

The request object parses the request body and request header parameters

Google Colab装载Google Drive(Google Colab中使用Google Drive)

Enterprise practice | construction of banking operation and maintenance index system under complex business relations

LeetCode 练习——113. 路径总和 II
随机推荐
Leetcode exercise - 113 Path sum II
JMeter loop controller and CSV data file settings are used together
字符串格式化
Smart city construction based on GIS 3D visualization technology
[ORM framework]
对存储过程进行加密和解密(SQL 2008/SQL 2012)
Mongodb creates an implicit database as an exercise
EasyExcel读取写入简单使用
STM32 product introduction
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
Prototype object in ES6
搭建物联网硬件通信技术几种方案
Postman interface test VII
VS Code指定扩展安装位置
[sword finger offer] 42 Stack push in and pop-up sequence
Serial communication relay Modbus communication host computer debugging software tool project development case
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
Phpcms realizes PC website access to wechat native payment
2022.7.3DAY595