📜  C++ atan2()

📅  最后修改于: 2020-09-25 07:23:49             🧑  作者: Mango

C++中的atan2() 函数以弧度返回坐标的反正切。

此函数在头文件中定义。

[Mathematics] tan-1(y/x) = atan2(y, x) [In C++ Programming]

atan2()原型[从C++ 11标准开始]

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); // For combinations of arithmetic types.

atan2()参数

函数 atan2()带有两个参数:x坐标和y坐标。

atan2()返回值

atan2() 函数返回[-π,π]范围内的值。如果xy均为零,则atan2() 函数将返回0。

示例1:atan2()如何与相同类型的x和y一起工作?

#include 
#include 

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

示例2:atan2()如何与不同类型的x和y一起使用?

#include 
#include 
#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;
  
  // Display result in degrees
  cout << "atan2(y/x) = " << result*180/PI << " degrees";

  return 0;
}

运行该程序时,输出为:

atan2(y/x) = 3.04694 radians
atan2(y/x) = 174.577 degrees