C ++中的atan2()函数以弧度返回坐标的反正切。
此函数在<cmath>头文件中定义。
tan-1(y/x) = atan2(y, x)
double atan2(double y, double x); float atan2(float y, float x); long double atan2(long double y, long double x); double atan2(Type1 y, Type2 x); //用于算术类型的组合。
函数atan2()具有两个参数:x坐标和y坐标。
x -此值表示x坐标的比例。
y -此值表示y坐标的比例。
atan2()函数返回[-π,π]范围内的值。如果x和y均为零,则atan2()函数将返回0。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 10.0, y = -10.0, result;
result = atan2(y, x);
cout << "atan2(y/x) = " << result << " radians" << endl;
cout << "atan2(y/x) = " << result*180/3.141592 << " degrees" << endl;
return 0;
}运行该程序时,输出为:
atan2(y/x) = -0.785398 radians atan2(y/x) = -45 degrees
#include <iostream>
#include <cmath>
#define PI 3.141592654
using namespace std;
int main()
{
double result;
float x = -31.6;
int y = 3;
result = atan2(y, x);
cout << "atan2(y/x) = " << result << " radians" << endl;
//以度数表示的显示结果
cout << "atan2(y/x) = " << result*180/PI << " degrees";
return 0;
}运行该程序时,输出为:
atan2(y/x) = 3.04694 radians atan2(y/x) = 174.577 degrees