当前位置:网站首页>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 story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
- 【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
- Enterprise practice | construction of banking operation and maintenance index system under complex business relations
- Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
- Guid主键
- Google Colab装载Google Drive(Google Colab中使用Google Drive)
- The Himalaya web version will pop up after each pause. It is recommended to download the client solution
- ArcGIS operation: batch modify attribute table
- Pdf document signature Guide
- 搭建物联网硬件通信技术几种方案
猜你喜欢

ORM模型--关联字段,抽象模型类

A wave of open source notebooks is coming

嵌入式背景知识-芯片

ES6中的原型对象

LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件

Programming features of ISP, IAP, ICP, JTAG and SWD

Es classes and objects, prototypes
![[sword finger offer] 42 Stack push in and pop-up sequence](/img/f4/eb69981163683c5b36f17992a87b3e.png)
[sword finger offer] 42 Stack push in and pop-up sequence

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

串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
随机推荐
Guid主键
VS Code指定扩展安装位置
Word自动生成目录的方法
Fiddler break point
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
ISP、IAP、ICP、JTAG、SWD的编程特点
IPv4 socket address structure
ORM -- query type, association query
mysql插入数据创建触发器填充uuid字段值
In addition to the objective reasons for overtime, what else is worth thinking about?
Several schemes of building hardware communication technology of Internet of things
Some test points about coupon test
Download Text, pictures and ab packages used by unitywebrequest Foundation
Es classes and objects, prototypes
C logging method
ORM model -- associated fields, abstract model classes
ORM模型--数据记录的创建操作,查询操作
Leetcode exercise - 113 Path sum II
Appx code signing Guide
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例