std::atan2, std::atan2f, std::atan2l
定义于头文件 <cmath> |
||
(1) | ||
float atan2 ( float y, float x ); double atan2 ( double y, double x ); |
(直至 C++23) | |
/*floating-point-type*/ atan2 ( /*floating-point-type*/ y, |
(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 /*math-common-simd-t*/<V0, V1> |
(S) | (C++26 起) |
额外重载 (自 C++11 起) |
||
定义于头文件 <cmath> |
||
template< class Integer > double atan2 ( Integer y, Integer x ); |
(A) | (C++26 起为 constexpr) |
std::atan2
的重载。(C++23 起)
S) SIMD 重载对 v_y 和 v_x 执行逐元素
std::atan2 。
|
(C++26 起) |
A) 为所有整数类型提供了额外的重载,它们被视为 double。
|
(C++11 起) |
目录 |
[编辑] 参数
y, x | - | 浮点数或整数值 |
[编辑] 返回值
如果没有错误发生,则返回 y / x 的反正切(arctan(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
|
(直至 C++23) |
如果 num1 和 num2 具有算术类型,则 std::atan2(num1, num2) 的效果与 std::atan2(static_cast</*common-floating-point-type*/>(num1), 如果不存在具有最高等级和次等级的浮点类型,则重载决议不会从提供的重载中产生可用的候选函数。 |
(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() { // normal usage: the signs of the two arguments determine the quadrant print_coordinates(+1, +1); // atan2( 1, 1) = +pi/4, Quad I print_coordinates(-1, +1); // atan2( 1, -1) = +3pi/4, Quad II print_coordinates(-1, -1); // atan2(-1, -1) = -3pi/4, Quad III print_coordinates(+1, -1); // atan2(-1, 1) = -pi/4, Quad IV // special values 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)) (函数) |
返回相角 (函数模板) | |
将函数 std::atan2 应用于 valarray 和一个值 (函数模板) | |
C 文档 关于 atan2
|