View Single Post
  #2   Spotlight this post!  
Unread 25-11-2006, 16:35
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Trig. Functions in EasyC

You can approximate sin very well between -PI/2 and PI/2 with the function f(theta) = theta - theta3/6 + theta5/120.

Once you've got that approximation, you can basically compute any sin value by using the periodic properties of sin.

sin(x) = sin(PI - x) between PI/2 and 3PI/2 (this recursively uses the bit we can accurately approximate)
sin(x) = sin(x - 2PI) between 3PI/2 and 2PI.

Here's the code for sin:
Code:
#define PI 3.14159

float ApproxSin(float theta)
{
    while(theta > (3*PI)/2)
    {
        theta -= 2*PI;
    }
    while(theta < -PI/2)
    {
        theta += 2*PI;
    }
    // theta is now between -PI/2 and 3*PI/2

    if(theta > -PI/2 && theta <= PI/2) // basic approximation
    {
        return theta - theta*theta*theta/6 + theta*theta*theta*theta*theta/120;
    }
    else if(theta > PI/2 && theta < 3*PI/2)
    {
        return ApproxSin(PI - theta);
    }
}
An approximation for cos is f(x) = 1 - x2/2 + x4/24, you can do the same thing with it, more or less.
Reply With Quote