ctrigonometrytaylor-series

Approximating Sine(x) with a Taylor series in C and having a lot of problems


I'm trying to approximate sine(x) in C using a Taylor series and Stirling's approximation for factorials but I'm getting really weird answers for n<5 and -0 for any n=>5. I literally just started learning yesterday so i'd appreciate it if some more experienced programmers could take a look at it and tell me what's wrong

Taylor Series of Sine

Stirling's approximation of factorials

#include <stdio.h>
#include <math.h>

int main(){

float x,n,s,i,e,p,f,r;
f=M_PI;
e=2.7182818;
s=0;

printf("What value of sine do you want to apporximate?");
    scanf("%f", &x);
printf("With what level of precision do you want to calculate it?");
    scanf("%f", &n);
for(i=0;i<=n; ++i);{
    r=((2*i)+1);
    p=(sqrt(2*r*f)*(pow((r/e),r)));
    s=s+(((pow((-1),i))*(pow(x,((2*i)+1))))/p);
}
printf("the value of sine at %f is %f",x,s);
}

Solution

  • This line

        for(i = 0; i <= n; ++i);{
    

    has an extra semicolon. You're executing an empty loop.