开发者

Conversion from radians to degrees

开发者 https://www.devze.com 2023-03-11 19:39 出处:网络
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 r

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?


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.


The angle is the input to tan. So you want:

a = 45 * 3.141592653589793 / 180.0;
b = tan(a);
cout << b << endl;


You must pass radians to the tan function. Also degrees to radian is wrong.

 a = tan(45 * 3.14159265 / 180.);


Tan accepts an angle, and returns a quotient. It is not the other way around. You want

a = tan(45*3.14159265/180); // Now a is equal to 1.
0

精彩评论

暂无评论...
验证码 换一张
取 消