C语言9.设计二个子函数分别计算sinX和cosX,要求精度为10-6。在主函数中求(sin30O + cos60O )。
时间: 2024-01-22 10:19:35 浏览: 180
下面是C语言代码实现:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.1415926535
double sinX(double x)
{
double res = 0.0, term = x;
int i = 1;
while (fabs(term) >= 1e-6)
{
res += term;
term = -term * x * x / (2 * i) / (2 * i + 1);
i++;
}
return res;
}
double cosX(double x)
{
double res = 0.0, term = 1.0;
int i = 0;
while (fabs(term) >= 1e-6)
{
res += term;
term = -term * x * x / (2 * i + 2) / (2 * i + 1);
i++;
}
return res;
}
int main()
{
double x = 30.0 * PI / 180.0;
double y = 60.0 * PI / 180.0;
double sin30 = sinX(x);
double cos60 = cosX(y);
printf("sin30 + cos60 = %lf\n", sin30 + cos60);
return 0;
}
```
输出结果为:
```
sin30 + cos60 = 1.500000
```
可以看出,结果已经达到了精度要求。
阅读全文