How to convert angle degrees into radians

This is a discussion on How to convert angle degrees into radians within the C++ Programming forums, part of the General Programming Boards category; Guys...I was adding more parts to that equation solver program to make it a calculator of sorts...I need to know ...

  1. #1
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52

    How to convert angle degrees into radians

    Guys...I was adding more parts to that equation solver program to make it a calculator of sorts...I need to know how to change degrees into radians..some help please?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    PI radians == 180 degrees
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you consider google?

    This is the third or so on the list of MANY that come back if you enter "degrees to radians" into google.

    http://www.teacherschoice.com.au/Mat...les/Angles.htm

    --
    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.

  4. #4
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Code:
     cout << "Enter angle in (d)degrees or (r)radians? - ";
                         if (r == 'd')
                         {
                               cout << "Enter angle theta (degrees) -";
                               cin >> degrees;
                               a = (degrees * 180)/pi;
                         }
                         else if (r == 'r')
                         {
                               cout << "Enter angle theta(radians) -";
                               cin >> radians;
                               a == radians;
                         }
    This then? And yeah I just checked google before Salem replied lol sorry...I'll be careful in future to use the forum as a last resort. And the "r" variable is a char variable I already declared in the Class.(as are degrees,radians and pi..)
    Last edited by SVXX; 10-01-2007 at 03:25 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your degrees to radians is wrong - try reading the page I posted for you.

    --
    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.

  6. #6
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Its the same one I used o.O damn I made a mistake

  7. #7
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    It isn't working...Let me post the problem -:

    This is the first part.
    Code:
    class Program
    {
           private:
                   float a,x,b,c,D,d,f; //Teh math vars.
                   int n; //You guessed it....ANOTHER math variable.
                   long double radians, degrees, Sine, Tangent, Cosine, Arctan, Arcsine, Arccos; //Trigonometry.
                   char g[1]; //For the trigo section.
                                                              
           public:
                   void Case1(); //All of
                   void Case2(); //these
                   void Case3(); //are
                   void Case4(); //the 
                   void Case5(); //mathematical
                   void Case6(); //operations.
                   void Trigonometry();// Trigonometrics. 
                   void EXIT();  //The exit function.
                   void RorQ();  //The restart or quit function.
                   int exit;     //The exit value.
                   int exitRorQ; //The restart or quit value.
                   char rq[1];   // "r" or "q"?(char size 1)
                                 
    };
    The Trigonometrics Part
    Code:
    void Program::Trigonometry()
    {
                        double pi = 3.1416; //Again for trigo.
                        if (g == "M")
                         {
                               setcolor(7);
                               cout << "Enter angle theta (degrees) -";
                               cin >> degrees;
                               a = (degrees * pi)/180;
                               Sine = sin (a);
                               Cosine = cos (a);
                               Tangent = tan (a);
                               Arctan = atan (a);
                               Arccos = acos (a);
                               Arcsine = asin (a);
                               cout << "\nThe trigonometric ratiosare as follows -:\n";
                               setcolor(14);
                               cout << "Sin theta = " << Sine << endl;
                               cout << "Cos theta = " << Cosine << endl;
                               cout << "Tan theta = " << Tangent << endl;
                               cout << "Inverse Sin(Arcsine) theta = " << Arcsine << endl;
                               cout << "Inverse Cos(Arccos) theta = " << Arccos << endl;
                               cout << "Inverse Tan(Arctan) theta = " << Arctan << endl;
                         }
                         else if (g == "N")
                         { 
                               setcolor(7);
                               cout << "Enter angle theta(radians) -";
                               cin >> radians;
                               a == radians;
                               Sine = sin (a);
                               Cosine = cos (a);
                               Tangent = tan (a);
                               Arctan = atan (a);
                               Arccos = acos (a);
                               Arcsine = asin (a);
                               cout << "\nThe trigonometric ratiosare as follows -:\n";
                               setcolor(14);
                               cout << "Sin theta = " << Sine << endl;
                               cout << "Cos theta = " << Cosine << endl;
                               cout << "Tan theta = " << Tangent << endl;
                               cout << "Inverse Sin(Arcsine) theta = " << Arcsine << endl;
                               cout << "Inverse Cos(Arccos) theta = " << Arccos << endl;
                               cout << "Inverse Tan(Arctan) theta = " << Arctan << endl;
                         }
    }
    And the Case6 part
    Code:
    void Program::Case6()
    {
                         cout << "Enter angle in (M)degrees or (N)radians? - ";
                         cin >> g;
                         Trigonometry();
                         setcolor(7);
                         Sleep(3000);
    }
    The output after cout << "Enter angle"..... isn't coming..it just skips the if else statements and goes straight to the getch(); in main..

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You could declare the multiplier as a constant:
    Code:
    const DEG_TO_RAD 0.0174532925;
    Then use call it in you program like:
    Code:
    radians = degrees * DEG_TO_RAD;
    It saves some extra calculations.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by SVXX View Post
    It isn't working...Let me post the problem -:
    So, I suspect you need a "cin.ignore()" after each cin >> somedouble, because cin >> somechar will otherwise get your "newline" that is left in the buffer after you entered the double number.

    --
    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.

  10. #10
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Code:
    setcolor(7);
                               cout << "Enter angle theta (degrees) -";
                               cin >> degrees;
                               cin.ignore();
    Code:
    setcolor(7);
                               cout << "Enter angle theta(radians) -";
                               cin >> radians;
                               cin.ignore();
    These?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, in all places where you read in numbers.

    Edit: Of course, if you do two reads in the same function, you can put just one ignore after the second "cin >> number;", because the newline left in the buffer will be "eaten" by the second "cin >> number;" - but there will be a newline left in the buffer.

    You can also use the "flush the input buffer" method(s) described in the FAQ section (top left of this page).

    --
    Mats
    Last edited by matsp; 10-01-2007 at 06:18 AM.
    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.

  12. #12
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    All the other functions are working...EXCEPT Trigonometry..Trigonometry(Case6) wasn't working even before I entered the cin.ignore()s...and the others were working anyway.

  13. #13
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    where exactly must I implement that code? In the main()?
    Code:
    int main() //Muhuhahahhahaha
    {
         Program OMGDOIT; 
         int y;
         C.exit = 1;
         int ch;          //Clearing input buffers.
         char buf[BUFSIZ];//Same.
         while(C.exit == 1)
         {
        
            cout << "\nWelcome to the Equation Solver!\n";
            cout << "\nChoose one of the following -:\n";         
            cout << "\n1. Find a quadratic equation's nature of roots\n";
            cout << "\n2. Find a quadratic equation's roots\n";
            cout << "\n3. Establish the Vičte relation of the roots and coefficients\n";
            cout << "\n4. Find a term of an arithmetic progression(AP)\n";
            cout << "\n5. Find the sum of the terms of an AP\n";
            cout << "\n6. Find the trigonometric values of an angle\n";
            cout << "\n7. Quit,noobs >_>\n";
            cin >> y;
            cin.ignore();
            
            switch(y)
            {         
                      case 1:
                          OMGDOIT.Case1();
                          getch();
                          OMGDOIT.RorQ();
                          break;
                      case 2:
                          OMGDOIT.Case2();
                          getch();
                          OMGDOIT.RorQ();
                          break;
                      case 3:
                          OMGDOIT.Case3();
                          getch();
                          OMGDOIT.RorQ();
                          break;
                      case 4: 
                          OMGDOIT.Case4();
                          getch();
                          OMGDOIT.RorQ();
                          break;
                      case 5:
                          OMGDOIT.Case5(); 
                          getch();
                          OMGDOIT.RorQ();
                          break;  
                      case 6:
                          cout.flush(); 
                          OMGDOIT.Case6();
                          getch();
                          OMGDOIT.RorQ();
                          break; 
                      case 7:
                           C.exit = 0;  
                           break;  
                      default: 
                          cout << "Enter a number in the list\n";
                          break;
            }
            clrscr(); //Clears screen maybe?
            }
                  
            return 0;
    }

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, perhaps the problem is something else:
    Code:
    g == "M"
    This compares the address of g with the address of the literal "M". You probably don't want that (as it's always false with the current piece of code).

    Code:
    g[0] == 'M';
    would compare the first letter in g with the letter 'M', which is more likely to be what you actually want to compare.

    Also:
    Code:
                               Sine = sin (a);
                               Cosine = cos (a);
                               Tangent = tan (a);
                               Arctan = atan (a);
                               Arccos = acos (a);
                               Arcsine = asin (a);
                               cout << "\nThe trigonometric ratiosare as follows -:\n";
                               setcolor(14);
                               cout << "Sin theta = " << Sine << endl;
                               cout << "Cos theta = " << Cosine << endl;
                               cout << "Tan theta = " << Tangent << endl;
                               cout << "Inverse Sin(Arcsine) theta = " << Arcsine << endl;
                               cout << "Inverse Cos(Arccos) theta = " << Arccos << endl;
                               cout << "Inverse Tan(Arctan) theta = " << Arctan << endl;
    is duplicated. You only need it once.

    However, Isn't atan etc supposed to be multiplied by the conversion back to degrees as well?

    You still only need one piece of code to display things, and to do most of the caclulations.

    Duplicating code is a sure way to "miss" changing something on one side of the else that needs to be changed on both sides. It is a good habit to get into only writing as little as possible inside if/else statements, and keeping any code that is common outside the if/else.

    You could even do the whole calculation of angle from radians to degrees or keep it radians by having a variable "conv_factor" that is set to either 1.0 or the conversion factor, and always multiply by this factor when producing the result. Multiplying by 1.0 is of course a "no change".

    --
    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.

  15. #15
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Hmm I see...let me try this...

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert DegMinSec to Decimal Degrees
    By JacquesLeJock in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 10:59 PM
  2. Temperature conversion...
    By Onslaught in forum C Programming
    Replies: 3
    Last Post: 10-21-2005, 01:15 PM
  3. sin() and cos() that use degrees
    By dwks in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 04:29 PM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 11:53 AM
  5. Is this right
    By Granger9 in forum C Programming
    Replies: 6
    Last Post: 08-14-2002, 02:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21