Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- double fact(double d) {
- if (d < 1) {
- return 1;
- } else {
- return d*fact(d-1);
- }
- }
- int main() {
- double x, part, sum = 0, eps, diff;
- int n = 0 ,i = 1;;
- // x = 3.14;
- // eps = 0.0001;
- scanf("%lf %lf", &x, &eps);
- if (eps > 0 && eps < 1) {
- diff = 1 + eps;
- part = x;
- //printf("%f %f %f", sin(x)-sum, eps);
- //printf("%lf", fact(3));
- for (i = 1; diff > eps; i++){
- sum += part;
- //part = pow((-1),i-1) * pow(x, 2 * i - 1) /fact(2 * i - 1);
- part*= (-1) * x * x / (2. * i)/(2. * i + 1.);
- diff = fabs(sin(x)-sum);
- n++;
- }
- printf("%s %lf %s %lf %s %d", "x = ",x," sum = ",sum," n = " ,n);
- // 0 0.001 0
- // 3.14 0.0001 0.00161324
- // 1.57 0.00001 1
- // 1.05 0.0001 0.86602
- // 2.06 0.0001 0.869296 // mine is 0.882776
- } else{printf("%s", "Invalid data");}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement