# Atan2

**Atan2** 是计算中的双参数反正切函数：给定一对实数 (y, x)，它返回从正 x 轴到点 (x, y) 的角度，以弧度表示，位于区间 [−π, +π] 内，并利用两个参数的符号来确定象限<sup>[1](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)</sup>。等价地说，它计算复数 x + iy 的辐角（极角）的主值<sup>[2](https://gcc.gnu.org/onlinedocs/gfortran/ATAN2.html)</sup><sup> • </sup><sup>[3](https://grouper.ieee.org/groups/1788/email/pdfqxwP6snEVc.pdf)</sup>。几乎每一种编程语言和数学库都提供该函数，名称如 `atan2`、`ATAN2` 和 `Math.atan2`。

| 关键事实 | 详情 |
|---|---|
| 定义 | 返回 arctan(y/x)，位于区间 [−π, +π] 弧度内，象限由两个参数的符号确定<sup>[1](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)</sup> |
| 几何意义 | 从原点到点 (x, y) 的向量与正 x 轴所成的角<sup>[4](https://www.mathworks.com/help/mcb/ref/atan2.html)</sup> |
| 与 atan 的对比 | atan(y/x) 仅限于 [−π/2, π/2]，无法区分 (x, y) 与 (−x, −y)；atan2 覆盖全部四个象限，并避免除以零<sup>[5](https://www.mathworks.com/help/matlab/ref/double.atan2.html)</sup><sup> • </sup><sup>[6](https://raw.org/book/algorithms/fast-atan2-algorithms/)</sup> |
| 起源 | ATAN2 出现于 IBM 1958 年的 FORTRAN 库（参考手册 C28-6000-2），ATAN2F 至 1961 年出现<sup>[7](https://chapel.discourse.group/t/inverse-trigonometric-tangent/36734)</sup> |
| 标准化 | C 定义了 atan2、atan2f 和 atan2l；IEEE 754-2008 将 atan2 列入推荐的正确舍入函数，定义域为 [−∞, +∞] × [−∞, +∞]<sup>[1](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)</sup><sup> • </sup><sup>[8](https://pub.sergev.org/doc/ieee754-2008.pdf)</sup> |
| 实际精度 | 基于随机采样，glibc 的双精度实现最大误差约为 0.524 ULP<sup>[9](https://codebrowser.dev/glibc/glibc/sysdeps/ieee754/dbl-64/e_atan2.c.html)</sup> |
| 典型用途 | 使物体指向目标、由坐标差计算方位角、从信号的实部与虚部导出相位，以及将笛卡尔坐标转换为极坐标形式<sup>[10](https://calcul.io/function/atan2/)</sup> |

## 为什么一个参数不够

普通的反正切 atan(y/x) 回答的是一个严格更小的问题。由于 tan 以 π 为周期重复，仅凭比值 y/x 无法区分第一象限中的方向与第三象限中的相反方向，且结果被限制在 [−π/2, π/2] 内<sup>[5](https://www.mathworks.com/help/matlab/ref/double.atan2.html)</sup>。第二个问题是当 x 为 0 时除以零<sup>[6](https://raw.org/book/algorithms/fast-atan2-algorithms/)</sup>。

Atan2 通过分别接收 y 和 x 消除了这两个限制。C 标准将该计算描述为 y/x 的反正切，利用两个参数的符号来确定返回值的象限<sup>[1](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)</sup>。具体而言，对于非零的 x，当 x 为正时结果为 arctan(y/x)，当 x 为负时结果为 arctan(y/x) 偏移 π（沿 y 的符号方向）；当 x 为零时，结果的绝对值为 π/2<sup>[2](https://gcc.gnu.org/onlinedocs/gfortran/ATAN2.html)</sup>。GNU Fortran 文档将同一函数概括为复数 X + iY 的辐角主值，可用于从笛卡尔坐标变换为极坐标<sup>[2](https://gcc.gnu.org/onlinedocs/gfortran/ATAN2.html)</sup>。

该函数对第一个操作数是奇函数：atan2(−y, x) = −atan2(y, x)，IEEE 754 标准对 atan2 和 atan2Pi 明确陈述了这一性质<sup>[8](https://pub.sergev.org/doc/ieee754-2008.pdf)</sup>。

## 特殊值与原点

平面上大部分区域没有争议，但坐标轴和原点需要约定。在 IEEE 浮点运算下，若 y 为 ±0 且 x 为负数或 −0，结果为 ±π；若 y 为零，不发生极点错误；若 x 和 y 均为零，则既不发生定义域错误也不发生值域错误<sup>[11](https://cppreference.com/c/numeric/math/atan2)</sup>。C 标准仅规定若两个参数均为零，*可能*产生定义域错误<sup>[1](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)</sup>，ISO C 原理文件给出了推理：即使 x 等于 0，atan2(y, x) 仍然有良好定义且有限；唯一有歧义的情形是两个参数均为 0，因为此时函数值域内的任何值在逻辑上都是合理的选择<sup>[12](https://www.lysator.liu.se/c/rat/d5.html)</sup>。

IEEE 运算的有符号零加剧了这种歧义。Fortran 文档规定，若 Y 为零，则当 X 严格为正时返回值为零，当 X 为负且 Y 为正零（或处理器不处理有符号零）时为 π，当 X 为负且 Y 为负零时为 −π<sup>[2](https://gcc.gnu.org/onlinedocs/gfortran/ATAN2.html)</sup>。即便如此，各实现也并不完全一致：MATLAB 对 atan2(0, −0) 和 atan2(−0, −0) 均返回 0，而 IEEE 754 的建议分别是 π 和 −π<sup>[5](https://www.mathworks.com/help/matlab/ref/double.atan2.html)</sup>。

## 历史

Atan2 通过 FORTRAN 进入计算领域。1958 年的 IBM 704 数据处理手册（C28-6000-2）已经定义了 ATAN2，ATAN2F 至 1961 年出现在 IBM 手册中<sup>[7](https://chapel.discourse.group/t/inverse-trigonometric-tangent/36734)</sup>。该名称是作为 atan 的变体选定的，有人指出，重点在于两个函数之间的形式相似性，而非从二维笛卡尔坐标返回极角的几何作用<sup>[7](https://chapel.discourse.group/t/inverse-trigonometric-tangent/36734)</sup>。Fortran 自 1977 标准起一直保留该函数，通用名为 ATAN2，双精度专用名为 DATAN2<sup>[2](https://gcc.gnu.org/onlinedocs/gfortran/ATAN2.html)</sup>。

从 FORTRAN 开始，双参数形式传播到几乎所有后来的语言和库，包括 C 数学库（atan2、atan2f、atan2l，以及自 C23 起的十进制浮点变体）<sup>[1](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)</sup><sup> • </sup><sup>[11](https://cppreference.com/c/numeric/math/atan2)</sup>、Python 的 math 模块<sup>[13](https://docs.python.org/3/library/math.html)</sup>、MATLAB<sup>[5](https://www.mathworks.com/help/matlab/ref/double.atan2.html)</sup>，以及电子表格函数如 Excel 的 ATAN2<sup>[14](https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oe376/4f927398-b2f8-4a57-9946-a3d1bab8bd83)</sup>。

## 参数顺序与命名

参数顺序是常见的错误来源。C、Python 和 MATLAB 以 y 为第一个参数，因此从一点到另一点的方位角写作 atan2(y − y₀, x − x₀)；参数颠倒的调用会静默地返回错误的角度<sup>[13](https://docs.python.org/3/library/math.html)</sup><sup> • </sup><sup>[5](https://www.mathworks.com/help/matlab/ref/double.atan2.html)</sup>。某些环境有所不同：Excel 规范定义 ATAN2(x, y)，以 x 为第一个参数，当 x 为 0 且 y 非零时给出 (π/2)·sgn(y)<sup>[14](https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oe376/4f927398-b2f8-4a57-9946-a3d1bab8bd83)</sup>。

唐·科克斯（Don Koks）是国防科学与技术组（Defence Science and Technology Group）的物理学家，维护相对论常见问题资料，并于 2022 年在《工程学报》（*The Journal of Engineering*）上发表文章，他认为该计算语言名称与数学并不匹配：研究人员将“atan2”借入公式时没有统一的参数顺序约定，而计算极角这一操作与反正切函数毫无关系，因此 atan2 这样的名称在数学上具有误导性；他呼吁为这个双分量函数取一个与语言无关的名称<sup>[15](https://ietresearch.onlinelibrary.wiley.com/doi/full/10.1049/tje2.12190)</sup>。

## 实现如何计算

库实现将问题归结为单参数反正切加上情况分析。例如，LLVM libc 的实现首先利用 y 的符号（因为 atan 是奇函数）和 x 的符号将点定位到四个象限之一，然后应用倒数恒等式 atan(1/x) = π/2 − atan(x)，使传递给 atan 的参数至多为 1，最后重新组合符号和象限<sup>[16](https://llvm.googlesource.com/llvm-project/%2B/c8b6ddf3a3b32283d8c31f988aadbed291a38c80/libc/src/__support/math/atan2.h)</sup>。通用库实现相对昂贵：它必须按象限分支、防范 x = 0，并以接近机器精度的水平计算一个超越函数<sup>[6](https://raw.org/book/algorithms/fast-atan2-algorithms/)</sup>。

主流实现的精度很高，但并非总能正确舍入。glibc 的双精度 atan2 文档记载基于随机采样的最大误差约为 0.524 ULP<sup>[9](https://codebrowser.dev/glibc/glibc/sysdeps/ieee754/dbl-64/e_atan2.c.html)</sup>，一项对 Julia 初等函数的调查在测试的函数集中发现 binary32 和 binary64 的误差为 0.5 至 2.4 ULP<sup>[17](https://arxiv.org/abs/2509.05666)</sup>。IEEE 754-2008 将 atan2 列入推荐的正确舍入函数，还定义了 atan2Pi，以半圈为单位度量角度，返回 atan2(y, x)/π<sup>[8](https://pub.sergev.org/doc/ieee754-2008.pdf)</sup>。

在硬件和嵌入式环境中，CORDIC 算法（坐标旋转数字计算机，COordinate Rotation DIgital Computer）在向量模式下仅使用迭代的移位加法运算来计算 atan2，这使其对 FPGA 和电机控制模块具有吸引力；例如，MATLAB 的电机控制模块集中的 atan2 模块在精确计算之外还提供 CORDIC 和查找表近似方法<sup>[4](https://www.mathworks.com/help/mcb/ref/atan2.html)</sup><sup> • </sup><sup>[18](https://docs.amd.com/r/en-US/ug958-vivado-sysgen-ref/CORDIC-ATAN)</sup>。当精度不如速度重要时，存在更廉价的近似方法，因为完整函数的计算代价很高<sup>[6](https://raw.org/book/algorithms/fast-atan2-algorithms/)</sup>。

## 应用

Atan2 出现在一切需要从分量中恢复方向的地方。在机器人学中，它作为单参数反正切的标准替代品：根据定义，atan2(dy, dx) 返回将一个坐标系旋转到位移 (dx, dy) 上的角度 θ，在逆运动学中用于选择具有正确全转范围的关节角<sup>[19](https://archive.air.cs.tum.edu/Main/Publications/Mayer2008c.pdf)</sup>。在移动机器人和导航中，它计算到地标的方位角和航点之间的航向<sup>[20](https://www.ensta-bretagne.fr/jaulin/ensi_isterobV2.pdf)</sup><sup> • </sup><sup>[10](https://calcul.io/function/atan2/)</sup>。在信号处理中，它从信号的实部和虚部导出相位；在复数运算中，它与模一起将复数转换为极坐标形式<sup>[10](https://calcul.io/function/atan2/)</sup><sup> • </sup><sup>[5](https://www.mathworks.com/help/matlab/ref/double.atan2.html)</sup>。

这些用途中有一个共同的实践警示：数对 (0, 0) 没有几何方向，因此导航或向量归一化等应用应显式处理该情形，而不是依赖库的约定<sup>[10](https://calcul.io/function/atan2/)</sup>。

## References

1. [ISO/IEC 9899:2024 (C23), §7.12.4.4 The atan2 functions, N3220 working draft](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf)
2. [ATAN2 — Arctangent function, The GNU Fortran Compiler](https://gcc.gnu.org/onlinedocs/gfortran/ATAN2.html)
3. [IEEE Std P1788 interval arithmetic standard, definition of atan2](https://grouper.ieee.org/groups/1788/email/pdfqxwP6snEVc.pdf)
4. [atan2 — Compute four-quadrant arctangent, MathWorks Motor Control Blockset](https://www.mathworks.com/help/mcb/ref/atan2.html)
5. [atan2 — Four-quadrant inverse tangent, MathWorks MATLAB](https://www.mathworks.com/help/matlab/ref/double.atan2.html)
6. [Fast atan2 Algorithms, RAW](https://raw.org/book/algorithms/fast-atan2-algorithms/)
7. [Inverse trigonometric tangent, Chapel community discussion of IBM 704 manuals](https://chapel.discourse.group/t/inverse-trigonometric-tangent/36734)
8. [IEEE Std 754-2008, IEEE Standard for Floating-Point Arithmetic](https://pub.sergev.org/doc/ieee754-2008.pdf)
9. [glibc sysdeps/ieee754/dbl-64/e_atan2.c source browser](https://codebrowser.dev/glibc/glibc/sysdeps/ieee754/dbl-64/e_atan2.c.html)
10. [atan2() Function & Examples, Calcul.io](https://calcul.io/function/atan2/)
11. [atan2, atan2f, atan2l, cppreference.com](https://cppreference.com/c/numeric/math/atan2)
12. [Rationale for ANSI C, §4.5 Mathematics](https://www.lysator.liu.se/c/rat/d5.html)
13. [math — Mathematical functions, Python 3 documentation](https://docs.python.org/3/library/math.html)
14. [MS-OE376 Part 4 Section 3.17.7.15, ATAN2, Microsoft Learn](https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oe376/4f927398-b2f8-4a57-9946-a3d1bab8bd83)
15. [Don Koks (2022). Wanted: A name for a commonly used trigonometry function. The Journal of Engineering.](https://ietresearch.onlinelibrary.wiley.com/doi/full/10.1049/tje2.12190)
16. [LLVM libc, src/__support/math/atan2.h](https://llvm.googlesource.com/llvm-project/%2B/c8b6ddf3a3b32283d8c31f988aadbed291a38c80/libc/src/__support/math/atan2.h)
17. [Mikaitis, M. and Rizyal, S. Accuracy of Mathematical Functions in Julia. arXiv:2509.05666](https://arxiv.org/abs/2509.05666)
18. [CORDIC ATAN reference block, AMD Vivado System Generator UG958](https://docs.amd.com/r/en-US/ug958-vivado-sysgen-ref/CORDIC-ATAN)
19. [Mayer et al., Robotics publications, TU München](https://archive.air.cs.tum.edu/Main/Publications/Mayer2008c.pdf)
20. [Jaulin, Mobile robotics course notes, ENSTA Bretagne](https://www.ensta-bretagne.fr/jaulin/ensi_isterobV2.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Number systems › Real and complex number constructions › Complex plane and polar representation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/atan2

English original: https://www.edgepedia.org/atan2
