C++ 参考手册

位置:首页 > C++ 参考手册 >数值库 >常用数学函数 > std::atan2, std::atan2f, std::atan2l

定义于头文件 <cmath>
(1)
float       atan2 ( float y, float x );
float       atan2f( float y, float x );
(C++11 起)
double      atan2 ( double y, double x );
(2)
(3)
long double atan2 ( long double y, long double x );
long double atan2l( long double y, long double x );
(C++11 起)
Promoted    atan2 ( Arithmetic1 y, Arithmetic2 x );
(4) (C++11 起)
1-3) 计算 y/x 的弧(反)正切,以参数符号确定正确的象限。
4) 所有 1-3) 所不覆盖的算术类型的重载集或函数模板。若任何参数拥有整数类型,则将它转型为 double 。若任何参数为 long double ,则返回类型 Promoted 亦为 long double ,否则返回类型始终为 double

参数

x, y - 浮点或整数类型

返回值

若不出现错误,则返回 y/x[-π ; +π] 弧度范围中的弧(反)正切( arctan(
y
x
)
)。
Y 参数
返回值
X 参数

若出现定义域错误,则返回实现定义值。

若出现下溢所致的值域错误,则返回(舍入后的)正确结果。

错误处理

报告 math_errhandling 中指定的错误。

xy 均为零则可能出现定义域错误。

若实现支持 IEEE 浮点算术( IEC 60559 ),则

  • xy 均为零,则定义域错误不出现
  • xy 均为零,则也不出现值域错误
  • y 为零,则不出现极点错误
  • y±0x 为负或 -0 ,则返回 ±π
  • y±0x 为正或 +0 ,则返回 ±0
  • y±∞x 有限,则返回 ±π/2
  • y±∞x-∞ ,则返回 ±3π/4
  • y±∞x+∞ ,则返回 ±π/4
  • x±0y 为负,则返回 -π/2
  • x±0y 为正,则返回 +π/2
  • x-∞y 为正有限,则返回
  • x-∞y 为负有限,则返回
  • x+∞y 为正有限,则返回 +0
  • x+∞y 为负有限,则返回 -0
  • x 为 NaN 或 y 为 NaN ,则返回 NaN

注意

std::atan2(y, x) 等价于 std::arg(std::complex<double>(x,y))

POSIX 指定在下溢情况下,返回不修改的 arg ,而若不支持如此,则返回不大于 DBL_MIN 、 FLT_MIN 和 LDBL_MIN 的实现定义值。

示例

#include <iostream>
#include <cmath>
 
int main()
{
    // 正常用法:二个参数的符号确定象限
    std::cout << "(+1,+1) cartesian is (" << hypot(1,1)
              << ',' << atan2(1,1) << ") polar\n"  // atan2(1,1) = +pi/4 ,第 I 象限
              << "(+1,-1) cartesian is (" << hypot(1,-1)
              << ',' << atan2(1,-1) << ") polar\n" // atan2(1, -1) = +3pi/4 ,第 II 象限
              << "(-1,-1) cartesian is (" << hypot(-1,-1)
              << ',' << atan2(-1,-1) << ") polar\n" // atan2(-1,-1) = -3pi/4 ,第 III 象限
              << "(-1,+1) cartesian is (" << hypot(-1,1)
              << ',' << atan2(-1,1) << ") polar\n"; // atan2(-1,-1) = -pi/4 ,第 IV 象限
    // 特殊值
    std::cout << "atan2(0, 0) = " << atan2(0,0)
              << " atan2(0,-0) = " << atan2(0,-0.0) << '\n'
              << "atan2(7, 0) = " << atan2(7,0)
              << " atan2(7,-0) = " << atan2(7,-0.0) << '\n';
}

输出:

(+1,+1) cartesian is (1.41421,0.785398) polar
(+1,-1) cartesian is (1.41421,2.35619) polar
(-1,-1) cartesian is (1.41421,-2.35619) polar
(-1,+1) cartesian is (1.41421,-0.785398) polar
atan2(0, 0) = 0 atan2(0,-0) = 3.14159
atan2(7, 0) = 1.5708 atan2(7,-0) = 1.5708

参阅

(C++11)(C++11)
计算反正弦( arcsin(x)
(函数)
(C++11)(C++11)
计算反余弦( arccos(x)
(函数)
(C++11)(C++11)
计算反正切( arctan(x)
(函数)
返回辐角
(函数模板)
应用函数 std::atan2 到一个 valarray 和一个值
(函数模板)