当前位置:网站首页>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
边栏推荐
- How to cancel automatic saving of changes in sqlyog database
- Vs code specifies the extension installation location
- The request object parses the request body and request header parameters
- Finally, there is no need to change a line of code! Shardingsphere native driver comes out
- Guid主键
- ORM -- grouping query, aggregation query, query set queryset object properties
- 大整数类实现阶乘
- Advanced function learning in ES6
- 高数_第1章空间解析几何与向量代数_向量的数量积
- ORM模型--关联字段,抽象模型类
猜你喜欢
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
The request object parses the request body and request header parameters
Appx code signing Guide
High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
PDF文档签名指南
VS Code指定扩展安装位置
【acwing】789. 数的范围(二分基础)
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
Postman interface test II
随机推荐
China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
The method of word automatically generating directory
STM32 ADC和DMA
Appx代码签名指南
Guide de signature du Code Appx
字符串格式化
Fiddler break point
【acwing】786. Number k
Win10 installation vs2015
Kotlin实现微信界面切换(Fragment练习)
JMeter installation
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
嵌入式工程师如何提高工作效率
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
Smart city construction based on GIS 3D visualization technology
[second on] [jeecgboot] modify paging parameters
2022.7.4DAY596