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

来自cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
在标头 <cmath> 定义
(1)
float       atan2 (float y, float x );

double      atan2 (double y, double x );

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

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

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

constexpr/*math-common-simd-t*/<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_yv_x 实施逐元素 std::atan2
(参见 math-common-simd-t 的定义。)
(C++26 起)
A) 为所有整数类型提供额外重载,将它们当做 double
(C++11 起)

目录

[编辑]参数

y, x - 浮点数或整数

[编辑]返回值

如果没有发生错误,那么返回 y / x[-π, +π] 弧度范围中的弧(反)正切(arctan(
y
x
)
)。
y 实参
返回值
math-atan2.png
x 实参

如果发生定义域错误,那么返回值由实现定义(在支持的平台上是 NaN)。

如果发生下溢导致的值域错误,那么返回(舍入后的)正确结果。

[编辑]错误处理

报告 math_errhandling 中指定的错误。

如果 xy 均为零,那么可能发生定义域错误。

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

  • 如果 xy 均为零,那么不发生定义域错误
  • 如果 xy 均为零,那么也不发生值域错误
  • 如果 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 是 NaN 或 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_MINFLT_MINLDBL_MIN 的由实现定义的值。

额外重载不需要以 (A) 的形式提供。它们只需要能够对它们的第一个实参 num1 和第二个实参 num2 满足以下要求:

  • 如果 num1num2 具有 longdouble 类型,那么 std::atan2(num1, num2)std::atan2(static_cast<longdouble>(num1),
               static_cast<longdouble>(num2))
    的效果相同。
  • 否则,如果 num1 和/或 num2 具有 double 或整数类型,那么 std::atan2(num1, num2)std::atan2(static_cast<double>(num1),
               static_cast<double>(num2))
    的效果相同。
  • 否则,如果 num1num2 具有 float 类型,那么 std::atan2(num1, num2)std::atan2(static_cast<float>(num1),
               static_cast<float>(num2))
    的效果相同。
(C++23 前)

如果 num1num2 具有算术类型,那么 std::atan2(num1, num2)std::atan2(static_cast</*公共浮点数类型*/>(num1),
           static_cast</*公共浮点数类型*/>(num2))
的效果相同,其中 /*公共浮点数类型*/num1num2 的类型中浮点数转换等级浮点数转换子等级最高的浮点数类型,整数类型的实参被视为具有与 double 相等的浮点数转换等级。

如果不存在等级和子等级最高的浮点数类型,那么在重载决议时不会从提供的重载中产生可用的候选。

(C++23 起)

[编辑]示例

#include <cmath>#include <iostream>   void print_coordinates(int x, int y){std::cout<<std::showpos<<"笛卡尔坐标 (x:"<< x <<", y:"<< y <<") "<<"是极坐标 (r:"<<std::hypot(x, y)<<", phi:"<< std::atan2(y, x)<<")\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) 是极坐标 (r:1.41421, phi:0.785398) 笛卡尔坐标 (x:-1, y:+1) 是极坐标 (r:1.41421, phi:2.35619) 笛卡尔坐标 (x:-1, y:-1) 是极坐标 (r:1.41421, phi:-2.35619) 笛卡尔坐标 (x:+1, y:-1) 是极坐标 (r:1.41421, phi:-0.785398) 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 和一个值
(函数模板)[编辑]
atan2 的 C 文档
close