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()
{
	/*Angle being converted to radians*/
	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;
}

double pow(u32 angle_in_radians, int n)
{
	int power = 0;
	if(n == 0)
		return 1;
	
	else return angle_in_radians * pow(angle_in_radians, n-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.

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

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()
{
	/*Angle being converted to radians*/
	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;
}

double pow(u32 angle_in_radians, int n)
{
	int power = 0;
	if(n == 0)
		return 1;
	
	else return angle_in_radians * pow(angle_in_radians, n-1);

	return power;
}

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

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

As Adak already said , it looks like an overflow.
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...

Please avoid using

#
typedef double u32;

typedef s like this.. it will confuse people... Usually u32 will strike as Unsigned data type with 32 bits [atleast for me].. more often used for 'unsigned int' in 32 bit systems.

Thanks for the clarification..problem solved...Thanks again...I'm marking this thread as solved.

Thanks for the clarification...problem solved...thanks again..will mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.