Thread: radian to degrees

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    29

    radian to degrees

    hey,
    as you might know calculator can be in radian or degree mode. This is needed when trying to use tan and sin.

    I have noticed that c++ calculates tan in radian mode. I know that you can convert it by multiplying the answer by PI and deviding by 180

    Is there any way to tell c++ to switch to degree mode so that I don't have to keep doing the long way with radian? thanks

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    and if there isn't, how can I use PI in c++?
    (what's the word so that it recognizes it)

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    For PI in C++, I usually declare a constant to hold it, such as:

    #define pi 3.14159265

    It works well enough for me.

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I would also just perform the radian to degree conversion yourself.. just keep in mind that there are precision limitations.

    Here is a recent post that addresses pi precision.
    Last edited by The Brain; 02-17-2005 at 05:21 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Mr.Pink
    Is there any way to tell c++ to switch to degree mode so that I don't have to keep doing the long way with radian? thanks
    No, but you could define your own function:
    Code:
    double tand(double degrees)
    {
      static const double twoPiBy360 = (2 * 3.141592f) / 360.0f;
      return tan( twoPiBy360 * degrees );
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In the cmath header there is a constant called M_PI that is the value of pi.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    For PI in C++, I usually declare a constant to hold it, such as:

    #define pi 3.14159265

    It works well enough for me.
    Much better to use a const in this case - what if you use the pi combination in a string?

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    String literals aren't parsed for macro replacements.

    Code:
    #include <iostream>
    using namespace std;
    
    #define Hello 5
    
    int main()
    {
            cout<<"Hello World"<<endl;
    }
    Output
    Hello World
    Still its better to just use the predefined M_PI

  9. #9
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Thumbs up

    Why, it's simple mathematics sir. First, you should have a const double for pi; something to the tune of:

    Code:
    const double pi = atan(1)*4; //Or.....if you wanted to just go ahead and write it out:
    const double pi = 3.14159265358979323; //A lot of that'll be truncated. ;)
    Radians to degrees is simply 180/pi, sir...which is something to the tune of 57.295, I believe. You can just multiply your radian value by a variable equal to that. You'd probably name the variable something spiffy like "RTD"(Radians To Degrees

    And same goes for "DTR", some const variable to the tune of .01745 (which is the opposite: pi/180).

    Not sure if I was any help whatsoever, but hey.

    I remembered all those digits off the top of my head. That's downright special.
    Last edited by Krak; 02-17-2005 at 11:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 09-27-2008, 07:32 PM
  2. Convert DegMinSec to Decimal Degrees
    By JacquesLeJock in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 11:59 PM
  3. Temperature conversion...
    By Onslaught in forum C Programming
    Replies: 3
    Last Post: 10-21-2005, 01:15 PM
  4. sin() and cos() that use degrees
    By dwks in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 04:29 PM
  5. Is this right
    By Granger9 in forum C Programming
    Replies: 6
    Last Post: 08-14-2002, 02:21 AM