I was asked to calculate sine using UDF.. using pow is not allowed... i did the following way...
Code:
#include <stdio.h>
#include <conio.h>
int fact(int num);
float powe(float base, int power);
float sine(float x);
int main(void)
{
float xx,x;
printf("Enter the angle in degree : ");
scanf("%f",&xx);
while(xx>=360)
xx=xx-360;
if (xx>=270) xx=xx-360;
if(xx>=180 && xx<270) xx= xx-180;
if(xx>90 && xx<180) xx=180-xx;
x=3.141592654/180*xx;
printf("Sin %d = %.4f",(int)xx,sine(x));
getch();
return 0;
}
float sine(float x)
{
int i;
float sine=0;
for(i=1;i<=10;i++)
sine=sine+powe(-1,i+1)*(powe(x,2*i-1)/fact(2*i-1));
return sine;
}
int fact(int num)
{
int i,p=1;
for (i=1;i<=num;i++)
{
p=p*i;
}
return p;
}
float powe(float base , int power)
{
int i;
float ans=1;
for(i=0;i<power;i++)
ans=ans*base;
return ans;
}
if i dont use the codes in red, it cant calculate if the value of degree is higher... is there any other simpler way to create this function..?