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
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
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
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
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
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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。