Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion from radians to degrees

I am trying to do a simple trigonometric calculation in C++. The following is an example of the problem I am having with this. As far as I know, C++ works in radians, not degrees. So conversion from radians to degrees should be a simple case of multiplying by 180 and dividing by pi. A simple test is tan(45), which should equate 1. The following program produces a value of 92.8063 however...

#include <iostream>
using namespace std;

#include <math.h>

int main(){
    double a,b;
    a = tan(45);
    b = a * 180 / 3.14159265;
    cout << b;
    return 0;
}

What is wrong?

like image 898
Matt Avatar asked Jun 08 '11 22:06

Matt


People also ask

How do you convert from radians to degrees?

How To Convert Radians to Degrees? The conversion of measure of an angle from radians to degrees can be done using the following formula: Angle in Radians × 180°/π = Angle in Degrees. For example, consider an angle π/9 rad. Now, using the radians to degrees formula, we have π/9 rad × 180°/π = (Angle in Degrees).


1 Answers

You're doing it backwards. Don't apply the formula to the output of tan, apply it to the parameter.

Also you'll want to multiply by pi and divide by 180, not vice versa.

like image 72
Mark Ransom Avatar answered Oct 18 '22 11:10

Mark Ransom