本文目录一览:
c语言编程sinx
while(fabs(y) = 1e-6) // 去掉分号
printf("sinx的近似值为%lf,sinx的真实值为%lf", sum, sin(x)); // 是lf
C语言编程计算sinx的近似值
#include "stdio.h"
int main(int argc, char *argv[]) {
double x, s, t, eps;
int i;
printf("Please enter x eps(R:0<eps<1)...\n");
if (scanf("%lf%lf", x, eps) != 2 || eps <= 0 || eps >= 1) {
printf("Input error, exit...\n");
return 0;
}
printf("sin(%g)≈", x);
for (s = t = x, x *= x, i = 1; t >= eps; i++) {
(t *= x) /= ((i * i * 2) + i + i);
s += i % 2 ? -t : t;
}
printf("%f\n", s);
return 0;
}
运行样例:
C语言求sinx
修改了一下。 用 前后项的递推: c = c * x * x / (float)i / (float)(i - 1);
#include stdio.h
#include math.h
int main() {
double x, a, b = 1, c = 1, sum;
int i, count = 1;
scanf("%lf", x);
sum = x;
for (i = 3; fabs(c) > 1e-05; i = i + 2) {
c = c * x * x / (float)i / (float)(i - 1);
b = -b;
sum = sum + c * b;
count++;
}
printf("%.3lf %d\n", sum, count);
return 0;
}