Namespaces
Variants

std:: atan2, std:: atan2f, std:: atan2l

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float atan2 ( float y, float x ) ;

double atan2 ( double y, double x ) ;

long double atan2 ( long double y, long double x ) ;
(C++23 前)
/*浮点类型*/

atan2 ( /*浮点类型*/ y,

/*浮点类型*/ x ) ;
(C++23 起)
(C++26 起为 constexpr)
float atan2f ( float y, float x ) ;
(2) (C++11 起)
(C++26 起为 constexpr)
long double atan2l ( long double y, long double x ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template < class V0, class V1 >

constexpr /*数学通用 SIMD 类型*/ < V0, V1 >

atan2 ( const V0 & v_y, const V1 & v_x ) ;
(S) (C++26 起)
附加重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double atan2 ( Integer y, Integer x ) ;
(A) (C++26 起为 constexpr)
1-3) 根据参数的符号计算 y / x 的反正切值,以确定正确的象限。 标准库为所有无 cv 限定的浮点类型提供了 std::atan2 的重载版本作为参数类型。 (C++23 起)
S) SIMD重载对 v_y v_x 执行逐元素的 std::atan2 运算。
(定义参见 math-common-simd-t
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型被视为 double
(since C++11)

目录

参数

y, x - 浮点数或整数值

返回值

If no errors occur, the arc tangent of y / x ( arctan(
y
x
)
) in the range [-π, +π] radians, is returned.
y 参数
返回值
x 参数

如果发生定义域错误,则返回一个由实现定义的值(在支持 NaN 的情况下返回 NaN)。

如果由于下溢发生范围错误,将返回正确结果(舍入后)。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

x y 同时为零时可能出现定义域错误。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 如果 x y 均为零,则 不会 发生定义域错误。
  • 如果 x y 均为零,也不会发生值域错误。
  • 如果 y 为零,则不会发生极点错误。
  • 如果 y 为 ±0 且 x 为负数或 -0,则返回 ±π。
  • 如果 y 为 ±0 且 x 为正数或 +0,则返回 ±0。
  • 如果 y 为 ±∞ 且 x 为有限值,则返回 ±π/2。
  • 如果 y 为 ±∞ 且 x 为 -∞,则返回 ±3π/4。
  • 如果 y 为 ±∞ 且 x 为 +∞,则返回 ±π/4。
  • 如果 x 为 ±0 且 y 为负数,则返回 -π/2。
  • 如果 x 为 ±0 且 y 为正数,则返回 +π/2。
  • 如果 x 为 -∞ 且 y 为有限正值,则返回 +π。
  • 如果 x 为 -∞ 且 y 为有限负值,则返回 -π。
  • 如果 x 为 +∞ 且 y 为有限正值,则返回 +0。
  • 如果 x 为 +∞ 且 y 为有限负值,则返回 -0。
  • 如果 x y 为 NaN,则返回 NaN。

注释

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

POSIX 规范 规定,在下溢情况下,将返回 y / x 的值;若该计算不被支持,则返回一个不大于 DBL_MIN FLT_MIN LDBL_MIN 的实现定义值。

额外的重载并不需要完全按照 (A) 的形式提供。只需确保对于它们的第一个参数 num1 和第二个参数 num2 满足以下条件:

  • num1 num2 具有类型 long double ,则 std :: atan2 ( num1, num2 ) 的效果等同于 std :: atan2 ( static_cast < long double > ( num1 ) ,
    static_cast < long double > ( num2 ) )
  • 否则,若 num1 和/或 num2 具有类型 double 或整数类型,则 std :: atan2 ( num1, num2 ) 的效果等同于 std :: atan2 ( static_cast < double > ( num1 ) ,
    static_cast < double > ( num2 ) )
  • 否则,若 num1 num2 具有类型 float ,则 std :: atan2 ( num1, num2 ) 的效果等同于 std :: atan2 ( static_cast < float > ( num1 ) ,
    static_cast < float > ( num2 ) )
(C++23 前)

num1 num2 具有算术类型,则 std :: atan2 ( num1, num2 ) 的效果等同于 std :: atan2 ( static_cast < /*common-floating-point-type*/ > ( num1 ) ,
static_cast < /*common-floating-point-type*/ > ( num2 ) )
,其中 /*common-floating-point-type*/ num1 num2 类型之间具有最高 浮点转换等级 和最高 浮点转换子等级 的浮点类型,整数类型的实参被认为具有与 double 相同的浮点转换等级。

若不存在具有最高等级和子等级的此类浮点类型,则 重载决议 不会从提供的重载中得到可用候选。

(C++23 起)

示例

#include <cmath>
#include <iostream>
void print_coordinates(int x, int y)
{
    std::cout << std::showpos
              << "(x:" << x << ", y:" << y << ") cartesian is "
              << "(r:" << std::hypot(x, y)
              << ", phi:" << std::atan2(y, x) << ") polar\n";
}
int main()
{
    // 常规用法:两个参数的符号决定象限
    print_coordinates(+1, +1); // atan2( 1,  1) =  +pi/4, 第一象限
    print_coordinates(-1, +1); // atan2( 1, -1) = +3pi/4, 第二象限
    print_coordinates(-1, -1); // atan2(-1, -1) = -3pi/4, 第三象限
    print_coordinates(+1, -1); // atan2(-1,  1) =  -pi/4, 第四象限
    // 特殊值
    std::cout << std::noshowpos
              << "atan2(0, 0) = " << atan2(0, 0) << '\n'
              << "atan2(0,-0) = " << atan2(0, -0.0) << '\n'
              << "atan2(7, 0) = " << atan2(7, 0) << '\n'
              << "atan2(7,-0) = " << atan2(7, -0.0) << '\n';
}

输出:

(x:+1, y:+1) cartesian is (r:1.41421, phi:0.785398) polar
(x:-1, y:+1) cartesian is (r:1.41421, phi:2.35619) polar
(x:-1, y:-1) cartesian is (r:1.41421, phi:-2.35619) polar
(x:+1, y:-1) cartesian is (r:1.41421, phi:-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)
(函数)
返回相位角
(函数模板)
对 valarray 和值应用函数 std::atan2
(函数模板)
C 文档 关于 atan2