当前位置:网站首页>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
边栏推荐
- Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
- AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
- ES类和对象、原型
- 关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
- mysql插入数据创建触发器填充uuid字段值
- STM32 ADC和DMA
- IPv4套接字地址结构
- 嵌入式背景知识-芯片
- Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
- ISP、IAP、ICP、JTAG、SWD的编程特点
猜你喜欢
Postman interface test VII
基于gis三维可视化技术的智慧城市建设
Postman interface test II
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)
Prototype object in ES6
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
JMeter loop controller and CSV data file settings are used together
Several schemes of building hardware communication technology of Internet of things
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
随机推荐
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
Fiddler simulates the interface test
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
2022.7.3DAY595
Vs code specifies the extension installation location
字符串格式化
Inno Setup 打包及签名指南
Before joining the chain home, I made a competitive product analysis for myself
[sword finger offer] 42 Stack push in and pop-up sequence
Introduction to uboot
C logging method
Pdf document signature Guide
Postman interface test VI
STM32 product introduction
Introduction to energy Router: Architecture and functions for energy Internet
ArcGIS operation: batch modify attribute table
How to cancel automatic saving of changes in sqlyog database
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)