atan2()是C++ STL中的內置函數,它返回(y /x)的正切逆,其中y是y坐標的比例,x是x坐標的比例。數值介於–和代表角度(x,y)點和正x軸的角度。它是X軸正方向與點(x,y)之間的逆時針角度(以弧度為單位)。
用法:
atan2(data_type y, data_type x)
參數:該函數接受下麵描述的兩個強製性參數:
- y –此值指定y坐標。
- X -此值指定x坐標。
參數可以是double,float或long double數據類型。
返回值:該函數返回介於–和代表角度(x,y)點和正x軸的角度。它是X軸正方向與點(x,y)之間的逆時針角度(以弧度為單位)。
以下示例程序旨在說明atan2()函數:
示例1:
// CPP program to demonstrate the atan2()
// function when both parameters are of
// same type
#include<bits/stdc++.h>
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:
// CPP program to demonstrate the atan2()
// function when both parameters are of
// different types
#include <bits/stdc++.h>
using namespace std;
int main()
{
double result;
float x = -10.0;
int y = 10;
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) = 2.35619 radians atan2(y/x) = 135 degrees
示例3:
// CPP program to demonstrate the atan2()
// function when y/x is undefined
#include<bits/stdc++.h>
using namespace std;
int main()
{
double x = 0.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) = 1.5708 radians atan2(y/x) = 90 degrees
示例4:
// CPP program to demonstrate the atan2()
// function when both parameters are zero
#include<bits/stdc++.h>
using namespace std;
int main()
{
double x = 0.0, y = 0.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 radians atan2(y/x) = 0 degrees
錯誤和異常:當字符串或字符作為參數傳遞時,該函數不返回任何匹配函數來調用錯誤。
示例5:
// CPP program to demonstrate the atan2()
// errors and exceptions
#include<bits/stdc++.h>
using namespace std;
int main()
{
double x = 0.0, y = 10.0, result;
result = atan2("1", x);
cout << "atan2(y/x) = " << result << " radians" << endl;
cout << "atan2(y/x) = " << result * 180 / 3.141592
<< " degrees" << endl;
return 0;
}
輸出:
prog.cpp:9:26: error: no matching function for call to 'atan2(const char [2], double&)' result = atan2("1", x);
相關用法
- C++ valarray atan2()用法及代碼示例
- C++ div()用法及代碼示例
- C++ log()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
- C++ towlower()用法及代碼示例
- C++ iswxdigit()用法及代碼示例
- C++ iswspace()用法及代碼示例
- C++ iswctype()用法及代碼示例
- C++ unordered_map end( )用法及代碼示例
注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 atan2() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。