Reputation: 241
I am trying to compute the MacLaurin series for e-x = 1 - x + (x2 / 2!) - (x3 / 3!) +...
My values seem to work up to a certain point and then deviate completely. Is there something wrong with rounding or am I using the wrong type of variable for such a question?
int i;
double sum=0;
double x = 8.3;
for(i=0; i<26; i++)
{
sum = sum+ (((pow(-1,i)) * (pow(x,i)))/factorial(i));
printf("Sum = %.12f\n\n\n",sum);
}
return 0;
I don't understand why, but up to the 12th term, the values are correct but after that, it begins to completely differ.
Upvotes: 0
Views: 1675
Reputation: 215627
Presumably your factorial
function, which you're not showing, is performing integer arithmetic. After 12! you're going to overflow a 32-bit integer. Switch to using double
in the factorial
function too.
Upvotes: 1