

// www.xbdev.net

// Simple sine function implementation - no lib's req.

// Forward declarations for cosine(..)
double powerd( double x, int y);
double factorial_div( double value, int x);
double cosine( double x);

/********************************************************************************/
/*                                                                              */
/*  cosine( x)                                                                  */
/*                                                                              */
/********************************************************************************/

double cosine( double x)
{
	int i=0;
	int j=1;
	int sign =1;
	double y1 = 0.0;
	double diff = 1000.0;

	if( x < 0) x = -1 * x;

	while ( x > 360.0*3.1415926/180)
	{
		x = x - 360*3.1415926/180;
	}

	if( x > (270 * (3.1415926/180)) )
	{
		x = 360 * (3.1415926/180) - x;

	}

	if( x > (180 * (3.1415926/180)) )
	{
		sign = -1;
		x = x - 180 * (3.1415926/180);

	}

	if( x > (90 * (3.1415926/180)) )
	{
		sign = -1;
		x = 180 * (3.1415926/180) - x;

	}// End if(..)

	while( powerd( diff, 2) > 1.0E-16 ) 
	{
		i++;
		diff = j * factorial_div( powerd( x, (2*i -2)) , (2*i -2));
		y1 = y1 + diff;
		j = -1 * j;

	}// End while(..)
	return (y1*sign);
}// End cosine(..)


/********************************************************************************/
/*                                                                              */
/*  powerd( x, power )                                                          */
/*                                                                              */
/********************************************************************************/

double powerd( double x, int y)
{
	int i=0;
	double ans=1.0;

	if(y==0) return 1.000;
	else 
	{
		while( i < y)
		{
			i++;
			ans = ans * x;

		}// End while(..)
	}// End else
	return ans;
}// End powerd(..)


/********************************************************************************/
/*                                                                              */
/*  factorial( x )                                                              */
/*                                                                              */
/********************************************************************************/

double factorial_div( double value, int x)
{
	if(x == 0) return 1;
	else
	{
		while( x > 1)
		{
		value = value / x--;

		}// End while(..)
	}// End else
	return value;
}// End factorial_div(..)


