English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Uso e esempio della funzione atan2() in C++

C++ 库函數 <cmath>

La funzione atan2() in C++ restituisce l'arco tangente dei coordinate in radianti.

Questa funzione<cmath>Definito nei file di intestazione.

tan-1(y/x) = atan2(y, x)

原型 di atan2() [dalla versione 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); // Utilizzato per combinazioni di tipi aritmetici.

Parametri di atan2()

La funzione atan2() ha due parametri: le coordinate x e y.

  • x -Questo valore rappresenta la proporzionalità dell'asse x.

  • y -Questo valore rappresenta la proporzionalità dell'asse y.

Valore di ritorno di atan2()

La funzione atan2() restituisce[-π, π]Valori nell'intervallo. Se x e y sono entrambi zero, la funzione atan2() restituirà 0.

Esempio 1: Come atan2() lavora con x e y dello stesso tipo?

#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 << " gradi" << endl;
  
  return 0;
}

運行該程序時,輸出為:

atan2(y/x) = -0.785398 radiani
atan2(y/x) = -45 gradi

Esempio 2: Come utilizzare atan2() con diversi tipi di x e y?

#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 radianti
atan2(y/x) = 174.577 gradi

  C++ 库函數 <cmath>