sin/cosin/tangent stuff?

Hey folks, I recently decided to go back to school in an effort to learn c++, I am currently 4 weeks into my first class and finally got my first homework assignment. The assignment was to write a program that would calculate the hypotenuse of a right triangle after the user input 2 sides. I have already done that, yay, so I set myself an extra challenge to try and also make the program calculate the remaining angles of the triangle. This was just for my own practice because, let's face it, that's how we learn. Anyway, I pretty immediatly ran into two problems:
1)this is way ahead of what we're learning in class
and 2)apparently I was one of those kids who went "pffft, we're never gonna use this!" in math class low those many years ago. So my first challenge was to teach myself how to do the equations. Math was a while ago.

Anyways, I thought the best way to start would be to write an absurdly simple program that started with two known variables for the sides, and attempt to calculate one angle. This is what I came up with:

1
2
3
4
5
6
7
8
9
10
 #include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double a = 3, b = 4, angle;
	angle = atan(a / b);
		cout << "The angle is: " << angle << endl;
	return 0;
}


Now, I know from doing the math myself that if a right triangle has two sides 3 and 4, the hypotenus is 5 and one of the angles is 36.87.

When ran, that program outputs 0.643501. I'm fairly inept at math but I'm pretty sure that's not 36.87.

Now, I could probably find sample code online that would do the trick, but the whole point of this excercise is to try and learn how to do this on my own. If I just jam in some sample code I don't understand, I haven't learned anything, and that's not very good. So, I am pretty sure my code is very wrong, what do I do to make it right?

Thank you in advance, yes I know I kind of suck at this. Thanks!
0.643501 is the angle value measured in radian. You have to convert it to degree.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#define _USE_MATH_DEFINES  //enable M_PI
#include <cmath>
using namespace std;
int main()
{
    double a = 3, b = 4, angle;
    angle = atan(a / b) * 180.0 / M_PI;  //convert to degree by multiplying (180/pi)
    cout << "The angle is: " << angle << endl;
    return 0;
}


fixed: #define _USE_MATH_DEFINES (with the underscore prefix) is the correct way to tell the compiler to define M_FOO: http://www.cplusplus.com/forum/general/102410/
Last edited on
Ahhhhh! Ok that actually makes perfect sense! Thank you so much!

Ok so I tested it by simply inputting a value of 3.14 in place of pi:

 
	angle = atan(a / b) * 180 / 3.14;


and got a close enough answer. In your example you included #define USE_MATH_DEFINES and used M_PI in the code itself. Could you indulge me for a moment and explain that? I have a pretty good guess at what it means but I would just like to be sure.

Thank you again!
The M_FOO constants are #defined in <cmath>, but only if you are not compiling strict C++ code. The USE_MATH_DEFINES macro tells the compiler to define the M_FOO constants anyway.
http://www.cplusplus.com/forum/general/102410/

fixed: #define _USE_MATH_DEFINES (with the underscore prefix) is the correct way to tell the compiler to define M_FOO. I don't remember it correctly :v
Last edited on
Topic archived. No new replies allowed.