This is a very accurate, space-efficient cosine approximation function. It is good to about 12 digits (base 10), which is probably overkill; we could remove some parts to save additional space and execution time.
Code:
#define pi2 1.570796326794896
#define p0 0.999999999781
#define p1 -0.499999993585
#define p2 0.041666636258
#define p3 -0.0013888361399
#define p4 0.00002476016134
#define p5 -0.00000026051495
float rcos (float deg) { // input is in degrees for simplicity in processing GPS coordinates
// 9 bytes of local variables
float frac, t;
unsigned char quad;
if (deg < 0)
deg = -deg;
deg /= 90.0;
quad = (unsigned char)deg;
frac = deg - quad;
if (quad == 0 || quad == 2) t = frac * pi2;
if (quad == 1) t = (1-frac) * pi2;
if (quad == 3) t = (frac-1) * pi2;
t = t*t;
frac = p0 + (p1*t) + (p2*t*t) + (p3*t*t*t) + (p4*t*t*t*t) + (p5*t*t*t*t*t);
if (quad == 2 || quad == 1)
frac = -frac;
return frac;
}
This takes up about 580 bytes, or 290 instructions on the PIC18F4550.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment