isabelino
Member level 1
Please: I need routines about cosine and sine , on Picbasic or PICC compiler. I need precision , more than 32bit floating point, because I need to calculate seconds. I working on Pic18F452 micro
Thanks .
Thanks .
////////////////////////////// Trig Functions //////////////////////////////
#undef PI_DIV_BY_TWO
#define PI_DIV_BY_TWO 1.570796326794896
#undef TWOBYPI
#define TWOBYPI 0.6366197724
/////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// float cos(float x)
////////////////////////////////////////////////////////////////////////////
// Description : returns the cosine value of the angle x, which is in radian
// Date : 9/20/2001
//
float cos(float x)
{
float y, t, t2 = 1.0;
int quad, i;
float frac;
float p[4] = {
-0.499999993585,
0.041666636258,
-0.0013888361399,
0.00002476016134
};
if (x < 0) x = -x; // absolute value of input
quad = (int)(x / PI_DIV_BY_TWO); // quadrant
frac = (x / PI_DIV_BY_TWO) - quad; // fractional part of input
quad = quad % 4; // quadrant (0 to 3)
if (quad == 0 || quad == 2)
t = frac * PI_DIV_BY_TWO;
else if (quad == 1)
t = (1-frac) * PI_DIV_BY_TWO;
else // should be 3
t = (frac-1) * PI_DIV_BY_TWO;
y = 0.999999999781;
t = t * t;
for (i = 0; i <= 3; i++)
{
t2 = t2 * t;
y = y + p[i] * t2;
}
if (quad == 2 || quad == 1)
y = -y; // correct sign
return (y);
}
////////////////////////////////////////////////////////////////////////////
// float sin(float x)
////////////////////////////////////////////////////////////////////////////
// Description : returns the sine value of the angle x, which is in radian
// Date : 9/20/2001
//
float sin(float x)
{
return cos(x - PI_DIV_BY_TWO);
}