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

边栏推荐
猜你喜欢

Smart city construction based on GIS 3D visualization technology

ORM model -- creation and query of data records

Word自动生成目录的方法

Fiddler break point

The Himalaya web version will pop up after each pause. It is recommended to download the client solution

Several schemes of building hardware communication technology of Internet of things

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

Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT

Memory ==c language 1

【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
随机推荐
Why does the starting service report an error when installing MySQL? (operating system Windows)
Appx code signing Guide
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
【华为机试真题详解】高矮个子排队
ISP、IAP、ICP、JTAG、SWD的编程特点
Postman tutorial - scripting
SQLyog数据库怎么取消自动保存更改
Web3.0 series distributed storage IPFs
STM32产品介绍
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
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
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
ISP、IAP、ICP、JTAG、SWD的编程特点
Programming features of ISP, IAP, ICP, JTAG and SWD
Parameter sniffing (1/2)
【剑指Offer】42. 栈的压入、弹出序列
C logging method
对存储过程进行加密和解密(SQL 2008/SQL 2012)
JMeter loop controller and CSV data file settings are used together