C++ Reference: Standard C++ Library reference: C Library: cmath: atan2

发布于:2022-12-07 ⋅ 阅读:(340) ⋅ 点赞:(0)

C++官网参考链接:https://cplusplus.com/reference/cmath/atan2/

函数
<cmath> <ctgmath>
atan2

C90
double atan2(double y, double x);
C99
double atan2  (double y, double x);      
float atan2f (float y, float x);
long double atan2l (long double y, long double x);
C++98
double atan2 (double y, double x);      
float atan2 (float y, float x);
long double atan2 (long double y, long double x);
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);       // additional overloads

用两个形参计算反正切 
返回以弧度表示y/x的反正切的值。
为了计算值,函数考虑了两个实参的符号,以确定象限。
在C++中,此函数在<valarray>中重载(参见valarray atan2)。
C99
头文件<tgmath.h>提供了该函数的泛型类型的宏版本。 
C++98
此函数在<valarray>中重载(参见valarray atan2)。
C++11
这个头文件(<cmath>)为其他算术类型 (arithmetic types)(Type1和Type2)的组合提供了额外的重载:这些重载在计算之前有效地将其实参转换为double类型,除非至少有一个实参是long double类型(在这种情况下,两个实参都被转换为long double类型)。
此函数在<valarray>中也被重载(参见valarray atan2)。

形参
y
表示y坐标的值。
x
表示x坐标的值。
如果传递的两个实参都为0,则发生定义域错误

返回值
y/x的反正切的值,在[-pi,+pi]弧度范围内。
1弧度等于180/PI度。 
C90(C++98)
如果发生定义域错误,将全局变量errno设置为EDOM
C99(C++11)
如果发生定义域错误: 
math_errhandling具有MATH_ERRNO集合:全局变量errno设置为EDOM
math_errhandling具有MATH_ERREXCEPT集合:将引发FE_INVALID
 
用例
/* atan2 example */
#include <stdio.h>      /* printf */
#include <math.h>       /* atan2 */

#define PI 3.14159265

int main ()
{
  double x, y, result;
  x = -10.0;
  y = 10.0;
  result = atan2 (y,x) * 180 / PI;
  printf ("The arc tangent for (x=%f, y=%f) is %f degrees\n", x, y, result );
  return 0;
}
输出: