## rapids79

I am trying to write a function that calculates the sine of a given angle. The code works fine if the angle in degrees is less than 100...once the angle becomes greater than 100..the result is a large value. I don't know what I am doing wrong. Can someone point out the mistake...thanks a ton.

``````/*Calculate sin x */
typedef unsigned char u8;
typedef double u32;

double pow(u32 , int );
int fact(int );

#define PI 3.1415926

#include<stdio.h>

int main()
{
u32 angle_in_radians = 45 * (PI/180);
double sum =0;
int n = 1,i=0;

for(i=0; i<=16; i++)
{
/*calculate sin(x)*/
sum += pow( -1, i ) * pow( angle_in_radians, n )/fact(n);
n+=2;
}
printf("\nSIN is %f\n", sum);
getchar();
return 0;
}

{
int power = 0;
if(n == 0)
return 1;

return power;
}

int fact(int n)
{
if(n == 0)
return 1;

else return n * fact(n-1);
}``````

Why aren't you including math.h?

Looks like an overflow problem, at first glance.

## rapids79

Thanks for the reply..Overflow problem??? I don't exactly understand where exactly that is coming into picture here. could you please elaborate? thanks..

## sree_ec 10

I am trying to write a function that calculates the sine of a given angle. The code works fine if the angle in degrees is less than 100...once the angle becomes greater than 100..the result is a large value. I don't know what I am doing wrong. Can someone point out the mistake...thanks a ton.

``````/*Calculate sin x */
typedef unsigned char u8;
typedef double u32;

double pow(u32 , int );
int fact(int );

#define PI 3.1415926

#include<stdio.h>

int main()
{
u32 angle_in_radians = 45 * (PI/180);
double sum =0;
int n = 1,i=0;

for(i=0; i<=16; i++)
{
/*calculate sin(x)*/
sum += pow( -1, i ) * pow( angle_in_radians, n )/fact(n);
n+=2;
}
printf("\nSIN is %f\n", sum);
getchar();
return 0;
}

{
int power = 0;
if(n == 0)
return 1;

return power;
}

int fact(int n)
{
if(n == 0)
return 1;

else return n * fact(n-1);
}``````

Your factorial function returns an int value.
By the time you reach your i 8 or 9, n would have reached 17 or 19 and then its factorial is going to be a huge value which your signed int wont be able to handle. Which will surely cause your output to go wrong.
Try with modifying the factorial function such that it returns a higher precision value.

Some more things, you have defined pow() by yourself. It is already available in c in math library. so it could have been avoided.

Even in your pow() function the local variable seems to be of no use...

``````#