當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Math atan2()用法及代碼示例

該函數求坐標的反正切。

假設坐標為 (x,y):

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

用法

假設坐標是 (x,y)。語法是:

float atan2(float y, float x);
double atan2(double y, double x);
long double atan2(long double y, long double x);
Promoted atan2(Arithmetic1 y, Arithmetic x );

參數

y:它代表y坐標值。

x:它代表x坐標值。

返回值

它返回範圍 [-?, ?] 中的值,如果 x 和 y 的值都為零,則返回零值。

  • 如果任何參數是整數類型,則將其強製轉換為 double。
  • 如果任何參數是 long double 類型,則將其強製轉換為 long double。

例子1

讓我們看一個 x 和 y 都為零時的簡單示例。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    int x=0;
    int y=0;
    cout<<"Value of tan(y/x) is:"<<tan(y/x)<<'\n';
    std::cout << "Value of tan-1(y/x) is:" <<atan2(y,x)<< std::endl;
    return 0;
}

輸出:

Value of tan(y/x) is:0
Value of tan-1(y/x) is:0

在本例中,當 'x' 和 'y' 都為零時,atan2() 計算反正切。

例子2

讓我們看一個簡單的例子,當 'x' 和 'y' 是不同的類型時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    int x=6;
    float y=7.8;
    cout<<"Value of tan(y/x) is:"<<tan(y/x)<<'\n';
    std::cout << "Value of tan-1(y/x) is:" <<atan2(y,x)<< std::endl;
    return 0;
}

輸出:

Value of tan(y/x) is:3.6021
Value of tan1(y/x) is:0.915101

在此示例中,當 x 為整數類型且 y 為浮點類型時,atan2() 函數查找切線的倒數。






相關用法


注:本文由純淨天空篩選整理自 C++ Math atan2()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。