Thread: calculating sin, cos, and tangent function

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    calculating sin, cos, and tangent function

    I am trying to calculate sin, cos, and tan. The program works but it gives it to me in radians. I needed the conversion to degrees which is sin(x) * PI / 180. when i do this it gives me the wrong answer, can someone help me out. this is my code:

    #include <iostream>
    using namespace std;
    #include <iomanip>
    #include <cmath>

    const PI = 3.14159265359;


    int main()
    Code:
    {
       // constant variable can be used to specify array size
       const int arraySize = 45;
       int s[ arraySize ]; // array s has 45 elements
       
       cout << fixed << setprecision(6);
    
       for ( int i = 1; i <= arraySize; i++ ) // set the values
          s[i] = i++;
    
       cout <<"Angle\n" << "(Degrees)" << setw( 9 ) <<"Sine"<< setw(15) 
    	    <<"Cosine"<< setw(14) <<"Tangent"<<endl;
    
       // output contents of array s in tabular format
       for ( int j = 1; j <= arraySize; j++ )   
    		cout << setw( 2 ) << j << setw( 20 ) << sin(j) << setw(13) 
    		     << cos(j * PI * 180) << setw(13) << tan(j) << endl;
    
       return 0;
    } // end main

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    cos(j * PI * 180) is not the same thing as: cos( j ) * ( 180 / pi )
    • "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

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Give PI a type; convert to radians for each of sin, cos, and tan -- like this, for example:
    Code:
    sin(j * PI / 180)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    A guess is that you will also have to specify the type of 180
    as in 180.0

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    i tried it but it calculates if it were sin(1) * PI / 180 -calculates as this wrong
    instead of sin ( 1 * PI /180) - what i want it to do right

    i tried putting
    Code:
    const degree = PI / 180.00;
    for ( int j = 1; j <= arraySize; j++ )   
    		cout << setw( 2 ) << j << setw( 20 ) << sin(j * degree) << setw(13) 
    		     << cos(j) << setw(13) << tan(j) << endl;

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You're not specifying a type for your constants, eg:
    Code:
    const double PI = 3.14159;
    I think it defaults to int the way you have it
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    complete code, taking sin (1) as example:
    sin(1) in radians = .841470
    sin(1) in degrees = .017452

    code calculates:
    correct sin(1) in radians
    wrong sin(1) in degrees -- calculates as if sin(1) * PI / 180. Correct conversion is supposed to be sin(1 * PI / 180.0 )

    Code:
    #include <iostream>
    using namespace std;
    #include <iomanip>
    #include <cmath>
    
    const PI = 3.14159265359;
    const degree = PI / 180.0;
    
    int main()
    {
       // constant variable can be used to specify array size
       const int arraySize = 45;
       int s[ arraySize ]; // array s has 45 elements
       
       cout << setprecision(6);
    
       for ( int i = 1; i <= arraySize; i++ ) // set the values
          s[i] = i++;
    
       cout <<"Angle\n" << "(Degrees)" << setw( 9 ) <<"Sine"<< setw(15) 
    	    <<"Cosine"<< setw(14) <<"Tangent"<<endl;
    
       // output contents of array s in tabular format
       for ( int j = 1; j <= arraySize; j++ )   
    		cout << setw( 2 ) << j << setw( 20 ) << sin(j) << setw(13) 
    		     << cos(j) << setw(13) << tan(j) << endl;
    
       return 0;
    } // end main

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Arrays go from 0 to N-1, not 1 to N.

    Stuff to look at:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    const double PI = 3.14159265359;
    
    int main()
    {
       const int arraySize = 45;
       
       cout << fixed << setprecision(6);
       cout <<"Angle\n" << "(Degrees)" << setw( 9 ) <<"Sine"<< setw(15) 
    	    <<"Cosine"<< setw(14) <<"Tangent"<<endl;
    
       for ( int j = 1; j <= arraySize; j++ )   
    		cout << setw(2)  << j
                 << setw(20) << sin(j * PI / 180)
                 << setw(13) << cos(j * PI / 180)
                 << setw(13) << tan(j * PI / 180) << endl;
    
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    thanks, why did i have to add const double pI so i can know for future reference

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    As mentioned previously...
    Quote Originally Posted by Dave_Sinkula
    Give PI a type
    Quote Originally Posted by JaWiB
    You're not specifying a type for your constants, eg:
    Code:
    const double PI = 3.14159;
    I think it defaults to int the way you have it
    ...you need to specify a type.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    thanks, why did i have to add const double pI so i can know for future reference
    The first thing you should have learned in C++ was how to declare a variable. I suggest you revisit those lessons. For every variable you declare, you have to specify a type, and 'const' is not a type.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or just make a conversion function to hide some of the nastiness of repeatedly doing the same thing
    Code:
    const double degToRad ( double deg ) {
      const double PI = 3.1415926;
      return deg * PI / 180.0;
    }
    
    int main ( ) {
      for ( int j = 0; j <= 45; j++ )
       cout << setw(2)  << j
                << setw(20) << sin(degToRad(j))
                << setw(13) << cos(degToRad(j))
                << setw(13) << tan(degToRad(j)) << endl;
    
    }

  13. #13
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I did it like this:

    Code:
    int number1;
    cout << "enter number for tangens calculation" << endl;
    cin >> number1;
    int calc = number1 * (PI-variable / 180);
    antwoord = tan(calc);
    Borland had included its value in the math.h library as M_PI 3.14159265358979323846.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed