Thread: Calculating Sin/Cosine

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Question Calculating Sin/Cosine

    Hi,
    I'd like help getting started writing my own routines to calculate sin and cos. But I really don't know where to begin..

    This is what I know:

    sin(x) = x (for very small x)
    cos(x) = 1 (for very small x)
    sin(x) = 2*sin(x/2)*cos(x/2)
    cos(x) = (cos(x/2))^2 - (sin(x/2))^2 (where "^" means "power")

    This is what I've done so far:

    while (x >= 0.0001){ //first checks to see if x is really small
    mysinx == 2*(x div 2) * mycosx(x/2);
    }
    while(x = 1){
    mycos(x) == ((x/2))^2 - (mysinx(x/2))^2;
    }

    Thanks, Josh

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This looks like a recursive exercise

    There are two cases
    1. the limiting case - sin(x) = x (for very small x)
    2. the reducing case - sin(x) = 2*sin(x/2)*cos(x/2)

    Code:
    double sin ( double x ) {
        if ( x < 0.000001 ) {
            return x;  // case 1
        } else {
             return 2 * sin( x/2 ) * cos ( x/2 ); // case 2
        }
    }
    Shrug....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What about power series?

    That way, you could write one without relying on the other, and vice versa.

    Toward the bottom of the page..

    http://forum.swarthmore.edu/dr.math/....10.12.00.html
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-08-2006, 11:08 PM
  2. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM
  5. Calculating window sizes
    By Mox in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2001, 09:17 PM