# calculating sin, cos, and tangent function

• 02-22-2006
rcastel2
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```
• 02-22-2006
The Brain
cos(j * PI * 180) is not the same thing as: cos( j ) * ( 180 / pi )
• 02-22-2006
Dave_Sinkula
Give PI a type; convert to radians for each of sin, cos, and tan -- like this, for example:
Code:

`sin(j * PI / 180)`
• 02-22-2006
A guess is that you will also have to specify the type of 180
as in 180.0
• 02-22-2006
rcastel2
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;```
• 02-22-2006
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
• 02-22-2006
rcastel2
complete code, taking sin (1) as example:
sin(1) in degrees = .017452

code calculates:
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```
• 02-22-2006
Dave_Sinkula
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; }```
• 02-22-2006
rcastel2
thanks, why did i have to add const double pI so i can know for future reference
• 02-22-2006
Dave_Sinkula
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.
• 02-22-2006
7stud
Quote:

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.
• 02-23-2006
Salem
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; }```
• 02-23-2006
Ideswa
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.