C++ 从弧度到度的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6286276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conversion from radians to degrees
提问by Matt
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...
我想用 C++ 做一个简单的三角函数计算。以下是我遇到的问题的示例。据我所知,C++ 以弧度而不是度数工作。因此,从弧度到度的转换应该是乘以 180 并除以 pi 的简单情况。一个简单的测试是 tan(45),它应该等于 1。下面的程序产生了一个 92.8063 的值但是......
#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?
怎么了?
回答by Mark Ransom
You're doing it backwards. Don't apply the formula to the outputof tan
, apply it to the parameter.
你是在倒退。不要公式适用于输出的tan
,它适用于参数。
Also you'll want to multiply by pi and divide by 180, not vice versa.
您还需要乘以 pi 并除以 180,反之亦然。
回答by Oliver Charlesworth
The angle is the inputto tan
. So you want:
的角度是输入到tan
。所以你要:
a = 45 * 3.141592653589793 / 180.0;
b = tan(a);
cout << b << endl;
回答by Richard Schneider
You must pass radians to the tan function. Also degrees to radian is wrong.
您必须将弧度传递给 tan 函数。弧度的度数也是错误的。
a = tan(45 * 3.14159265 / 180.);
回答by Andreas Rejbrand
Tan acceptsan angle, and returns a quotient. It is notthe other way around. You want
Tan接受一个角度,并返回一个商。这不是相反的。你要
a = tan(45*3.14159265/180); // Now a is equal to 1.