Thread: radian to degree value problem?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    28

    radian to degree value problem?

    i want to convert the value of

    sin45

    it gives the value of

    0.850903534 of sin45(which is in radian).

    i have to use its degree 's value

    mean to say that
    0.70710678118654752440084436210485 which is its degree value.

    what should i do?

    i am trying this
    0.850903534 * 180 / Pi
    but
    it gives this 48.753180728611482440956052242364 which is in degree but not actual result

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you need to do 45 * pi / 180, and then feed that value into sin. There is no (reasonable) way to convert a sin-value from radians to degrees or the other way around, after you have called sin. You need to convert the degrees to radians before calling sin.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You mean you need to compute the sine of 45 degrees, I assume. Then you need to convert the value of 45 (not sin45) from degrees to radians, and compute the sine of that to store in the variable sin45.

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    A "macro" I frequently use for this very thing (it's not a real macro):

    Code:
    #define degrees 0.0174532925199432958 // to the limit of a double plus a bit extra
    Just multiply by this value to convert degrees to radians. For the inverse, you could divide by this value or use this "macro" (it's not a real one):

    Code:
    #define radians 57.295779513082321 // to the limits of a double plus a bit extra
    Using them is done this way:

    Code:
    TestValue = sin(30.0*degrees); // you should get exactly 0.5
    TestValueInverse = asin(TestValue)*radians; // you should get 30 back
    TestValueInverse2 = asin(TestValue)/degrees; // the alternative, but less clear method
    I do this a lot for calculating slopes in my game. It speeds things up and makes it easy to read as well. Hopefully this will help.

    Edit: 30.0 in example usage, not 30 (the latter would be for an int, not a double).
    Last edited by ulillillia; 04-02-2009 at 04:25 AM. Reason: Usage mistake fixed
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by ulillillia View Post
    Edit: 30.0 in example usage, not 30 (the latter would be for an int, not a double).
    Actually, 30 (integer) multiplied by a double would result in a double.

    Your macro (or better yet: use a constant) won't make things faster. The compiler will (or, should) optimize it to that anyway. But I do agree it makes things more readable.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    28

    Talking

    thx you matsp and
    grumpy

    you are saying it right which i need

    thx you

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I mainly meant that it's faster to type it. What use is there in using a constant instead?

    Code:
    const double degrees = 0.0174532925199432958;
    const double radians = 57.295779513082321;
    ...
    
    TestValue = sin(30.0*degrees);
    ...
    For the int/double thing, the program would (I think) have to convert the int to a double, which is a bit CPU intensive compared to just using an all-out double as is.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ulillillia View Post
    I mainly meant that it's faster to type it. What use is there in using a constant instead?

    Code:
    const double degrees = 0.0174532925199432958;
    const double radians = 57.295779513082321;
    ...
    
    TestValue = sin(30.0*degrees);
    ...
    For the int/double thing, the program would (I think) have to convert the int to a double, which is a bit CPU intensive compared to just using an all-out double as is.
    Yes, but conversion to double will happen at compile-time for constants, so it's no cost at the runtime phase of the code. [At least for decent compilers, which includes the two most popular here: gcc and Microsofts C/C++]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM