We could not use math.h
I get wrong answers in cases with inputs greater than 54 in my sine function and inputs greater than 37 in my exp functions; I guess it overflows, what should I do? I want to write sine function and exp function on my own and I use Taylor expansion. And I just need 6 digits in the fraction;
//Start of exp function
float exp(float x)
{
double expreturn=0;
long long int fctrl=1;
for(int n=0;n<=12;n++){
fctrl=1;
for(int i=2;i<=n;i++)
fctrl *= i;
expreturn += (pow(x,n)/fctrl);
}
return (float)expreturn;
}//End of exp function
//Start of sin function
float sin(float x)
{
double sinus=0;
long long int fctrl=1;
while(!(0<=x&&x<6.3))
{
if(x<0)
x += PI*2;
else
x -= PI*2;
}
for(int n=0;n<=8;n++)
{
fctrl=1;
for(int i=2;i<=(2*n+1);i++)
fctrl *= i;
sinus += ((pow(-1,n)*pow(x,2*n+1))/fctrl);
}
return (float)sinus;
}//End of sin function
//Start of pow function
float pow(float x,int y)
{
double pow = 1;
for(int i=0;i<y;i++)
pow *= x;
return (float)pow;
}//End of pow Function
Here are some examples:
Input
sin(200)
Desired output
-0.873297
My function output
-0.872985
But it works properly with small values
I use this:
What Could I do now?