请用C语言编写如下程序:设计二个子函数,利用级数之和分别计算sinX和cosX, 要求精度为10-6。在主函数中求(sin30O+ cos60O)。
时间: 2023-06-26 09:08:10 浏览: 137
使用c语言计算sinx的值
以下是利用级数之和计算sinX和cosX的C语言代码:
```c
#include <stdio.h>
#include <math.h>
double sinX(double x) {
double result = 0, term = x;
int n = 1;
while (fabs(term) >= 1e-6) {
result += term;
term = -term * x * x / ((2 * n) * (2 * n + 1));
n++;
}
return result;
}
double cosX(double x) {
double result = 0, term = 1;
int n = 0;
while (fabs(term) >= 1e-6) {
result += term;
term = -term * x * x / ((2 * n + 2) * (2 * n + 1));
n++;
}
return result;
}
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.499997
cos(60°) = 0.500000
sin(30°) + cos(60°) = 0.999997
```
阅读全文