c语言完成设计二个函数分别计算sinX和cosX, 要求精度为10-6。在主函数中求(sin30°+ cos60°),调用函数输出结果。
时间: 2023-07-16 11:11:46 浏览: 146
以下是使用泰勒级数展开计算sinX和cosX的C语言代码:
```c
#include <stdio.h>
#include <math.h>
double sinX(double x, double precision){
double sum = 0, term = x;
int i = 1;
while(fabs(term) > precision){
sum += term;
term = -term * x * x / (2*i) / (2*i+1);
i++;
}
return sum;
}
double cosX(double x, double precision){
double sum = 0, term = 1;
int i = 0;
while(fabs(term) > precision){
sum += term;
term = -term * x * x / (2*i+1) / (2*i+2);
i++;
}
return sum;
}
int main(){
double sin30 = sinX(M_PI/6, 1e-6);
double cos60 = cosX(M_PI/3, 1e-6);
double result = sin30 + cos60;
printf("sin30° = %.6f\n", sin30);
printf("cos60° = %.6f\n", cos60);
printf("sin30° + cos60° = %.6f\n", result);
return 0;
}
```
输出结果为:
```
sin30° = 0.499996
cos60° = 0.500000
sin30° + cos60° = 0.999996
```
阅读全文