Skip to content

Instantly share code, notes, and snippets.

@nsmgr8
Created June 1, 2011 08:07
Show Gist options
  • Save nsmgr8/1001958 to your computer and use it in GitHub Desktop.
Save nsmgr8/1001958 to your computer and use it in GitHub Desktop.
sine taylor series for projanmo forum query
#include <stdio.h>
#include <math.h>
long fact(long p) {
long i, mult = 1;
for(i=1; i<=p; i++)
mult *= i;
return mult;
}
float sinex(float x, int n) {
int m, i, j = 1;
float sum = 0;
for(i=0; i<n; i++) {
m = 2 * i + 1;
sum += j * pow(x, m) / fact(m);
j = -j;
}
return sum;
}
int main() {
int i, n;
float x;
while(1) {
printf("Number of terms (enter 0 to quit): ");
scanf("%d", &n);
if(n == 0)
break;
printf("Enter a value for x: ");
scanf("%f", &x);
printf("\nValue of sin(%f) is %f\n", x, sin(x));
printf("The series gave me %f\n\n", sinex(x, n));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment