Skip to content

Instantly share code, notes, and snippets.

@SohanChy
Created July 26, 2015 18:29
Show Gist options
  • Save SohanChy/b95697193854f5972830 to your computer and use it in GitHub Desktop.
Save SohanChy/b95697193854f5972830 to your computer and use it in GitHub Desktop.
Evaluate value of sine(x) in C
#include <stdio.h>
#include <math.h>
float sine(int deg);
int fact(int n);
int main()
{
int x = 90;
printf(" %f ",sine(x));
return 0;
}
float sine(int deg)
{
float x = (float) deg * (3.1416/180);
int n, lim = 35;
float possum = 0;
for(n = 1; n<lim; n= n+4)
{
possum = possum + pow(x,n)/fact(n);
}
float negsum = 0;
for(n = 3; n<lim; n= n+4)
{
negsum = negsum - pow(x,n)/fact(n);
}
float result = possum+ negsum;
return result;
}
int fact(int n)
{
int x,fact=1;
for (x=1; x<=n; x++)
{
fact= fact * x;
}
return fact;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment