Thread: Help with cos, sin and tan^-1 (arctan);

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Help with cos, sin and tan^-1 (arctan);

    I'm writing a program using cos, sin and arctan. However, Whenever the program computes something it fails at doing cos and sin properly. I have not written it to the arctan stage yet.

    Here is what I'm using:

    angle is inputed by the user. This value is between 0 and 90.

    value is also inputed by the user. This value is any positive value

    Code:
    forcex = value * cos(angle);
    forcey = value * sin(angle);
    When I unput a value of 90 and a angle of 0, I get the correct answer. However, if I input the value of 90 and have an angle of 45 I get: 47 for x and 76 for y.

    I'm using the following .h files:

    math.h
    stdio.h
    stdlib.h

    What exactly am I doing wrong here? Do I need to convert the degrees to radians or is there anyway I can keep it in degrees?

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    sin, cos, etc. use radians. Are you converting?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    That explains it, as I was writing this post I realized it may have to be in radians.... I'm going to have to convert it but honestly I don't remember how to convert degrees to radians. I know that pi/2 is 90, pi is 180, 3pi/2 is 270 and pi is 360... but I really don't know how to convert it a "proper way". Any idea how?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Pi over 180 degress is one degree, and 180 degrees over pi is one radian. Thus

    To convert from degrees to radians multiply degrees by πrad / 180deg
    To convert the other way multiply radians by 180deg / πrad

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Quote Originally Posted by citizen View Post
    Pi over 180 degress is one degree, and 180 degrees over pi is one radian. Thus

    To convert from degrees to radians multiply degrees by πrad / 180deg
    To convert the other way multiply radians by 180deg / πrad
    I'll use this:

    radians = angle * PI/180;

    Thank you for your help guys.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    I'm kind of stuck again guys... How exactly do I use arctan (tan^-1) in C.

    I tried:

    direction = arctan(totaly/totalx);

    However, the compiler does not recognize arctan. What do I use instead to make this work?

  7. #7
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    In C the trigonometric inverses are called asin, acos and atan.
    Take a look inside your math.h

  8. #8
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I hate the fact radians are used. The trig functions using angles always use radians so it might be good to create a macro, function, or something else that converts from degrees to radians and back if you're more comfortable with degrees than radians. The arc functions return an angle in radians and the nonarc ones request an angle in radians.

    #define radians 57.295779513082321 // enough precision for a double
    #define degrees 0.017453292519943296

    Just multiply by these numbers to convert:

    Variable = sin(30*degrees); // 30 degrees should give 0.5
    AnotherVariable = atan(Y/X)*radians // the angle returned is in degrees this way

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The reason why radians are used is for precision,

    @xIcyx perhaps read math.h or look-up whats in it? Rather than guessing...

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    a diffrent way is to use taylor sentence :
    Code:
    f(x) = f(0) + f(0)' / 1! + f(0)^''/2! + f(0)^''' / 3! .. + f(0)^n / {n(x-0)!}
    
    sin a = sum from{n = 0} to {infinity} = {(-1)^n over (2n + 1)! }  x^{2n+1}

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ulillillia View Post
    I hate the fact radians are used.
    People who hate radians are mathematically ignorant. They didn't just make this crap up for no reason you know.

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Agreed. I even wonder why they put degrees in there in the first place. However, I do agree they should put a macro or function in there somewhere for converting (even though it's ridiculously easy).
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > The reason why radians are used is for precision,
    It has nothing to do with precision. Radians are the only "natural" angle measure - for example, derivative formulas for trig functions would have to include a constant factor != 1 if the angle was measured in any other units, and the Taylor series for the trig functions would have to include the same constant factor - for example, using radians, (d/dx) sin(x) == cos(x), and sin(x) == x - x^3/3! + x^5/5! - ... Try working out how to modify these formulas if x is measured in degrees. It's similar to the fact that e is the only "natural" base for logarithms (the same reasons apply).

  14. #14
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I understand what radians are and how the term is defined. Given a circle with a radius of 1 unit, the angle of one radian is where the circumference would equal one unit (or that of the radius), which is about 57.3 degrees. The degree is based on Earth's orbit around the Sun. It was first believed to be 360 days in the year and that's how the 360-degree circle came about. Search Wikipedia for more details on radians and degrees.

  15. #15
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Radians are very useful, and I'm disturbed by the fact that OpenGL uses degrees. Try for example, deriving a trigonometric function using degrees.
    cos(x)' = -sin(x)*0.017453292519943295769236907684886

Popular pages Recent additions subscribe to a feed