Issue calculating an angle (C)

Discussion in 'Mac Programming' started by mac2x, Sep 10, 2010.

  1. mac2x macrumors 65816

    Sep 19, 2009
    I'll be up front and tell you guys that this is part of an assignment, but I'm only asking about this one thing that has been giving me fits for the past hour.

    Basically, in the context of a larger problem, I need to calculate an angle; specifically by taking the arcsine of 2/3:

    theta = asin (2/3);
    That is what I am attempting to use, but it is evidently incorrect in some way because the output claims theta is 0.000000 (theta is of type double). FWIW, I have initialized the variable theta to 0 on the suggestion of my instructor.

    [edit] I checked the syntax and it seems right, but surely I am missing something. Let me know if you want the entire program.

    Thanks for helping a novice programmer who is feeling like a noob! :D
  2. mac2x thread starter macrumors 65816

    Sep 19, 2009
    Figured it out!

    Boy do I ever feel dumb. :D

    I did this problem on paper before translating it into C...and I got a little too gung-ho on copying. That 2/3 is SUPPOSED to be the quotient of two variables of type double that have been previously calculated by the program.

    I have a feeling this will work better once I correct that issue. :)
  3. SidBala macrumors 6502a

    Jun 27, 2010
  4. timbos macrumors member

    Jan 2, 2009
    Teddy Bear Debugging

    One of my colleagues has a teddy bear on his desk. Whenever he gets stuck at something that seems simple (and correct) to him, he explains it to the bear rather than bug one of us. 9 times out of 10, having to explain the problem out loud makes it clear what the solution is. :)
  5. robvas macrumors 68030

    Mar 29, 2009
    Also, don't forget that the C math functions take arguments in radians, not degrees

    radians = degrees * (180/pi)
  6. LPZ macrumors 65816

    Jul 11, 2006
    Maybe that should be (pi/180)? :)
  7. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    Probably best to use the builtin constants for these things; M_PI for π, M_1_PI for 1/π, and so on.
  8. mac2x thread starter macrumors 65816

    Sep 19, 2009
    Haha, I don't have a teddy bear, but I can sometimes find someone on skype to bug in a similar fashion. :D

    Yes, the conversion is degrees * (π/180). ;) And yes, I am aware that it needs radians. :)
  9. mac2x thread starter macrumors 65816

    Sep 19, 2009
    That sounds interesting, but we haven't covered that yet in class so I'd be nervous about using it. Could you explain further? Thanks. :)
  10. robvas macrumors 68030

    Mar 29, 2009

    The math.h library defines many (often neglected) constants. It is always advisable to use these definitions:

    HUGE -- The maximum value of a single-precision floating-point number.
    M_E -- The base of natural logarithms (e).

    M_LOG2E -- The base-2 logarithm of e.

    M_LOG10E - The base-10 logarithm of e.

    M_LN2 -- The natural logarithm of 2.

    M_LN10 -- The natural logarithm of 10.

    M_PI -- $\pi$.

    M_PI_2 -- $\pi$/2.

    M_PI_4 -- $\pi$/4.
    M_1_PI -- 1/$\pi$.

    M_2_PI -- 2/$\pi$.

    M_2_SQRTPI -- 2/$\sqrt{\pi}$.

    M_SQRT2 -- The positive square root of 2.

    M_SQRT1_2 -- The positive square root of 1/2.

    MAXFLOAT -- The maximum value of a non-infinite single- precision floating point number.

    HUGE_VAL -- positive infinity.
  11. chrono1081 macrumors 604


    Jan 26, 2008
    Isla Nublar
    This works! I've told problems to co-workers who hear me yelling at the computer (and they ask why) and by explaining something (that I know they don't understand) just by saying it outloud I solve it myself.

    There is also the famous "Solved it in a dream" that happens to almost every programmer, where you think of a problem so long you end up dreaming about it and solve it.
  12. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Or the shower, or in the car, or playing a game, etc. Sometimes letting something simmer under the surface instead of pounding your head against it is what your brain needs.

  13. mac2x thread starter macrumors 65816

    Sep 19, 2009

Share This Page