AcAN2()在C++ STL中的作用

这个 atan2() 在C++ STL中是一个内置函数,它返回(y/x)的切线逆,其中y是y坐标的比例,x是x坐标的比例。数值介于- pi pi 代表角度 	heta (x,y)点和正x轴的。它是正X轴和点(X,y)之间的逆时针角度,以弧度为单位。 语法:

null
atan2(data_type y, data_type x)

参数: 该函数接受两个强制参数,如下所述:

  • y- 该值指定y坐标。
  • x- 该值指定x坐标。

参数可以是double、float或long double数据类型。

返回值: 该函数返回一个介于- pi pi 代表角度 	heta (x,y)点和正x轴的。它是正X轴和点(X,y)之间的逆时针角度,以弧度为单位。 下面的程序演示了atan2()函数: 项目1:

CPP

// 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 radiansatan2(y/x) = 45 degrees

项目2:

CPP

// 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 radiansatan2(y/x) = 135 degrees

方案3:

CPP

// 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 radiansatan2(y/x) = 90 degrees

方案4:

CPP

// 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 radiansatan2(y/x) = 0 degrees

错误和例外: 函数返回 没有用于调用的匹配函数 字符串或字符作为参数传递时出错。 方案5:

CPP

// 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);

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享