C++ std::atan2() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::atan2(y, x) finds the angle of a point (x, y) as seen from the origin. It is like atan(y / x), but it looks at the signs of y and x separately, so it knows which of the four quarters (quadrants) the point is in.

The result is in radians, from -pi to pi. A point to the right gives 0, straight up gives pi / 2, to the left gives pi, and straight down gives -pi / 2. Note the order: y comes first, then x.

std::atan2() is declared in <cmath> and is part of the group Trigonometry. Trigonometric functions use radians, not degrees. To convert degrees, multiply by std::numbers::pi / 180 (C++20).

Syntax

Include <cmath> and call std::atan2. Start with these lines.

C++
double a = std::atan2(1.0, 1.0);     // 0.785398 (45 degrees)
double b = std::atan2(1.0, -1.0);    // 2.35619  (135 degrees)
float  c = std::atan2(1.0f, 0.0f);   // 1.5708f  (90 degrees)
CallWhat it does
std::atan2(1.0, 1.0)The point (1, 1) lies at 45 degrees. Returns 0.785398.
std::atan2(1.0, -1.0)The point (-1, 1) is in the top left. Returns 2.35619, or 135 degrees.
std::atan2(1.0, 0.0)A point straight up. atan(1 / 0) would fail, but this returns 1.5708.

Remember the order of the arguments: y first, then x. This is the opposite of how a point is written, (x, y).

Library declarations
C++
float       atan2(float x, float y);
double      atan2(double x, double y);
long double atan2(long double x, long double y);
// plus overloads for mixed and integer arguments

Parameters

ParameterDescription
yRequired. The vertical value (the rise) of the point.
xRequired. The horizontal value (the run) of the point.

Return Value

Return ValueDescription
AngleThe angle in radians from -pi to pi, measured from the positive x axis.
0atan2(0, 0) returns 0, and atan2(+0, +x) returns 0.
pi / -piatan2(+0, -x) returns pi, and atan2(-0, -x) returns -pi.

C++ Examples

Example 1 — Angles in Four Quadrants

Print the angle in degrees for one point in each quadrant.

C++
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>

int main() {
    const double toDeg = 180.0 / std::numbers::pi;
    double points[][2] = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};

    std::cout << std::fixed << std::setprecision(2);
    for (auto& p : points) {
        double x = p[0], y = p[1];
        std::cout << "(" << x << ", " << y << ") -> "
                  << std::atan2(y, x) * toDeg << '\n';
    }
    return 0;
}

How It Works

  1. Each point has an x and a y, and the call is atan2(y, x) with y first.
  2. The top right point gives 45.00 degrees. The top left (-1, 1) gives 135.00 degrees, which means more than a quarter turn.
  3. The bottom points get negative angles: -135.00 and -45.00. The function covers the full circle from -180 to 180 degrees.

Example 2 — Why atan(y / x) Is Not Enough

The point (-1, 1) is in the top left, but atan(y / x) puts it in the bottom right.

C++
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>

int main() {
    const double toDeg = 180.0 / std::numbers::pi;
    double x = -1.0, y = 1.0;

    std::cout << std::fixed << std::setprecision(2);
    std::cout << "atan(y / x)  = " << std::atan(y / x) * toDeg << '\n';
    std::cout << "atan2(y, x)  = " << std::atan2(y, x) * toDeg << '\n';
    return 0;
}

How It Works

  1. y / x is 1 / -1, which is -1. The two signs combine into one, so the information about the quadrant is lost.
  2. atan(-1) gives -45.00 degrees, pointing to the bottom right. That is the wrong direction for a point in the top left.
  3. atan2(y, x) sees y positive and x negative, so it returns the correct 135.00 degrees.

Example 3 — Direction from a Player to a Target

A player stands at (2, 3). Find the direction to three targets. Notice that a target straight above or to the left also works.

C++
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>

int main() {
    double px = 2.0, py = 3.0;
    double targets[][2] = {{5, 7}, {2, 8}, {-1, 3}};

    std::cout << std::fixed << std::setprecision(2);
    for (auto& t : targets) {
        double angle = std::atan2(t[1] - py, t[0] - px) * 180.0 / std::numbers::pi;
        std::cout << "To (" << t[0] << ", " << t[1] << "): " << angle << " degrees\n";
    }
    return 0;
}

How It Works

  1. Subtracting the player's position turns each target into a vector: dx = t[0] - px and dy = t[1] - py.
  2. The first target has dx = 3 and dy = 4, so the angle is 53.13 degrees. The second has dx = 0, where a division would fail, but atan2() returns 90.00.
  3. The third target is directly to the left, with dy = 0 and dx = -3. atan2() returns exactly pi, which is 180.00 degrees.

Frequently Asked Questions

std::atan2(y, x) returns the angle, in radians, of the point (x, y) measured from the positive x axis. It gives the right quadrant, and the result is between -pi and pi.
An angle in radians from -pi to pi. atan2(0, 0) returns 0, and the sign of a zero argument is used to pick between 0 and pi.
The y value comes first, then x. So std::atan2(y, x). Swapping them is a common bug, and it gives the angle from the y axis.
It returns 0 in most implementations, and it may set an error. The angle of the origin is not defined, so check for the point (0, 0) yourself if it can happen.
Include <cmath> and write std::atan2(). That always works and picks the C++ overloads. The global name ::atan2 often works too, but the standard does not promise it, and it may only take a double.

Next: atanh()

Keep going through the <cmath> functions. Inverse hyperbolic tangent of a number.

atanh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful