As suggested, use
double
s instead of
int
. Enjoy:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double fact(unsigned n)
{
double result =1.0;
unsigned int i;
for (i=2; i<=n; ++i )
result *= i;
return result;
}
int main()
{
unsigned terms;
unsigned count=0;
double result = 0.0;
double angle;
printf("enter number of terms ");
scanf("%d",&terms);
printf("enter value of x ");
scanf("%lf",&angle);
while(count <= terms)
{
double sign = count & 1 ? -1.0 : 1.0;
unsigned n = 2*count+1;
result += sign*(pow(angle,(double)n)/fact(n));
count++;
}
double br = sin(angle);
printf("approximation %g, builtin function result %g, difference %5.2f%%", result, br, fabs(result-br)/br*100.0);
}