c语言设计二个子函数,利用级数之和分别计算sinX和cosX(X为弧度), 要求精度为10-6。(即小于10-6项不累加)在主函数中求(sin30O+ cos60O)。
时间: 2023-07-14 21:13:41 浏览: 114
下面是 C 语言代码实现:
```c
#include <stdio.h>
#include <math.h>
double sinX(double x)
{
double term = x, sum = x;
int n = 1;
while (fabs(term) > 1e-6) {
term *= -x * x / ((2 * n) * (2 * n + 1));
sum += term;
n++;
}
return sum;
}
double cosX(double x)
{
double term = 1, sum = 1;
int n = 1;
while (fabs(term) > 1e-6) {
term *= -x * x / ((2 * n - 1) * (2 * n));
sum += term;
n++;
}
return sum;
}
int main()
{
double sin30 = sinX(30 * M_PI / 180);
double cos60 = cosX(60 * M_PI / 180);
double result = sin30 + cos60;
printf("sin(30°) = %lf\n", sin30);
printf("cos(60°) = %lf\n", cos60);
printf("sin(30°) + cos(60°) = %lf\n", result);
return 0;
}
```
输出结果为:
```
sin(30°) = 0.499999
cos(60°) = 0.500000
sin(30°) + cos(60°) = 1.000000
```
阅读全文