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

边栏推荐
- STM32 product introduction
- [sword finger offer] 42 Stack push in and pop-up sequence
- fiddler-AutoResponder
- Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
- 2022.7.4DAY596
- LeetCode 练习——113. 路径总和 II
- AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
- Postman tutorial - scripting
- MCU is the most popular science (ten thousand words summary, worth collecting)
- 大整数类实现阶乘
猜你喜欢

ORM--数据库增删改查操作逻辑

ORM model -- associated fields, abstract model classes

【acwing】786. Number k

Fiddler break point

Fiddler simulates the interface test

How to cancel automatic saving of changes in sqlyog database

Postman interface test VII

LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件

LeetCode 练习——113. 路径总和 II

Word自动生成目录的方法
随机推荐
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
ORM -- query type, association query
JMeter loop controller and CSV data file settings are used together
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
STM32 Basics - memory mapping
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
虚数j的物理意义
Guid主键
Learning records - high precision addition and multiplication
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
Interface test
Smart city construction based on GIS 3D visualization technology
Parameter sniffing (1/2)
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Word自动生成目录的方法
STM32 ADC和DMA
A wave of open source notebooks is coming
ArcGIS operation: batch modify attribute table
嵌入式工程师如何提高工作效率
Why is the reflection efficiency low?