I've been following the guide my prof gave us, but I just can't find where I went wrong. I've also been going through some other questions about implementing the Taylor Series in C.
Just assume that RaiseTo(raise a number to the power of x) is there.
double factorial (int n)
int fact = 1,
for (flag = 1; flag <= n; flag++)
fact *= flag;
double sine (double rad)
plusOrMinus2 = 0; //1 for plus, 0 for minus
val2 = rad,
terms = NUMBER_OF_TERMS; //10 terms
for (flag_2 = 1; flag_2 <= 2 * terms; flag_2 += 2)
radRaisedToX2 = RaiseTo(rad, flag_2);
if (plusOrMinus2 == 0)
val2 -= radRaisedToX2/factorial(flag_2);
plusOrMinus2++; //Add the next number
val2 += radRaisedToX2/factorial(flag_2);
plusOrMinus2--; //Subtract the next number
sin = val2;
double rad, cosx, sinx;
rad = degree * PI / 180.00;
//cosx = cosine (rad);
sinx = sine (rad);
printf("%lf \n%lf", rad, sinx);
12! is larger than can fit into a 32-bit
int, so such values will overflow and therefore won't return what you expect.
Instead of computing the full factorial each time, take a look at each term in the sequence relative to the previous one. For any given term, the next one is
-((x*x)/(flag_2*(flag_2-1)) times the previous one. So start with a term of
x, then multiply by that factor for each successive term.
There's also a trick to calculating the result to the precision of a
double without knowing how many terms you need. I'll leave that as an exercise to the reader.