C Math atan2() Function

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

The atan2() function computes the angle from (0, 0) to a point (x, y) in the plane. It is the go-to C function for graphics, robotics, and game development when you need a direction angle that respects all four quadrants and handles x = 0 safely.

01

Two args

y then x.

02

Quadrants

Full circle.

03

Range

(−π, π].

04

x = 0

No crash.

05

vs atan

Better choice.

06

Radians

Output angle.

Definition and Usage

atan2(y, x) returns the counterclockwise angle θ (in radians) from the positive x-axis to the ray through (x, y). Equivalently, it is the arc tangent of y/x with the signs of both arguments used to select the correct quadrant.

If you only know the slope y/x, atan(y/x) loses quadrant information. atan2 keeps y and x separate so (-1, -1) and (1, 1) produce different angles even though their ratios are equal.

💡
Beginner Tip

Remember the order: atan2(y, x)—vertical first, horizontal second. For a displacement from A to B, use atan2(by - ay, bx - ax).

📝 Syntax

Standard C declaration:

C
double atan2(double y, double x);

Related variants

C
float atan2f(float y, float x);           /* <math.h> */
long double atan2l(long double y, long double x); /* <math.h> */

Parameters

  • y — y-coordinate (vertical component, “rise”).
  • x — x-coordinate (horizontal component, “run”).

Return Value

  • Angle in radians in the range (−π, π].
  • atan2(0, 1) returns 0 (point on positive x-axis).
  • atan2(1, 0) returns π/2 (point straight up).
  • atan2(0, 0) is implementation-defined—avoid in production.

Headers and linking

  • #include <math.h>
  • Compile: gcc atan2.c -std=c11 -o atan2 -lm

⚡ Quick Reference

CallPoint (x, y)Angle (approx.)
atan2(0, 1)Right on x-axis0 rad (0°)
atan2(1, 1)First quadrant0.7854 (π/4, 45°)
atan2(1, 0)Straight up1.5708 (π/2, 90°)
atan2(0, -1)Left on x-axis3.1416 (π, 180°)
atan2(-1, -1)Third quadrant-2.356 (−3π/4)
Direction
atan2(y, x)

Angle to point

Delta
atan2(dy, dx)

From offset

Degrees
rad * 180 / M_PI

Convert

Avoid
atan(y/x)

Lost quadrant

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. atan2 is one of the most practical functions in <math.h>.

📚 Getting Started

Angle to the point (1, 1) in radians and degrees.

Example 1 — Basic atan2 with Degrees

Angle to the point where x = 1 and y = 1.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double y = 1.0;
    double x = 1.0;
    double rad = atan2(y, x);
    double deg = rad * 180.0 / M_PI;

    printf("atan2(%.1f, %.1f) = %.6f radians\n", y, x, rad);
    printf("atan2(%.1f, %.1f) = %.1f degrees\n", y, x, deg);

    return 0;
}

How It Works

atan2(1, 1) is π/4 (45°)—the angle to the northeast direction. Note the argument order: y first, then x.

Example 2 — All Four Quadrants

Same slope magnitude, different directions.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    struct { double x, y; const char *name; } pts[] = {
        { 1,  1, "Q1 (+,+)" },
        { -1,  1, "Q2 (-,+)" },
        { -1, -1, "Q3 (-,-)" },
        {  1, -1, "Q4 (+,-)" }
    };
    size_t i;

    for (i = 0; i < 4; i++) {
        double deg = atan2(pts[i].y, pts[i].x) * 180.0 / M_PI;
        printf("%s  atan2(%.0f,%.0f) = %.1f deg\n",
               pts[i].name, pts[i].y, pts[i].x, deg);
    }

    return 0;
}

How It Works

Each quadrant gets a distinct angle in (−180°, 180°]. This is the core reason atan2 exists instead of plain atan.

📈 Practical Patterns

Comparison with atan, aiming, and vertical-line cases.

Example 3 — atan2() vs atan(y/x)

Both points have ratio y/x = 0.75, but different angles.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = -4.0, y = -3.0;

    printf("Point (%.0f, %.0f), slope y/x = %.2f\n", x, y, y / x);
    printf("atan(y/x)  = %.2f rad (%.1f deg)\n",
           atan(y / x), atan(y / x) * 180.0 / M_PI);
    printf("atan2(y,x) = %.2f rad (%.1f deg)\n",
           atan2(y, x), atan2(y, x) * 180.0 / M_PI);

    return 0;
}

How It Works

atan(0.75) only knows the slope—it cannot see the third quadrant. atan2(-3, -4) returns the correct direction (−143°).

Example 4 — Aim from Player to Target

Classic game pattern: angle from position A toward B.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double px = 10.0, py = 20.0;
    double tx = 50.0, ty = 35.0;
    double dx = tx - px;
    double dy = ty - py;
    double aim_deg = atan2(dy, dx) * 180.0 / M_PI;

    printf("Player (%.0f, %.0f) -> Target (%.0f, %.0f)\n", px, py, tx, ty);
    printf("Aim angle = %.1f degrees\n", aim_deg);

    return 0;
}

How It Works

Subtract positions to get displacement, then atan2(dy, dx). This pattern appears in turrets, character facing, and pathfinding.

Example 5 — Vertical Line (x = 0) Safely

atan(y/x) would divide by zero; atan2 does not.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    printf("atan2( 5, 0) = %.4f rad (%.1f deg)\n",
           atan2(5.0, 0.0), atan2(5.0, 0.0) * 180.0 / M_PI);
    printf("atan2(-5, 0) = %.4f rad (%.1f deg)\n",
           atan2(-5.0, 0.0), atan2(-5.0, 0.0) * 180.0 / M_PI);

    return 0;
}

How It Works

When x = 0, atan2 returns ±π/2 based on the sign of y. Never write atan(y/x) when x might be zero.

🚀 Common Use Cases

  • 2D game graphics — rotate a sprite to face a target.
  • Robotics — heading angle from wheel odometry deltas.
  • Touch and mouse input — angle from center of joystick to touch point.
  • Navigation — bearing between two GPS-style coordinates.
  • Vector math — convert (x, y) components to polar angle.

🧠 How atan2() Works

1

Read y and x

Inspect signs and handle x = 0 without dividing.

Input
2

Select quadrant

Combine ratio y/x with sign information.

Logic
3

Return θ

Angle in (−π, π] from +x axis to (x, y).

Result
=

Direction angle

Ready for rotation matrices, aiming, or polar coordinates.

📝 Notes

  • Argument order is atan2(y, x)—y first, not x first.
  • Prefer atan2 over atan(y/x) for coordinates.
  • Output range is (−π, π], not limited to [−π/2, π/2] like atan.
  • atan2(0, 0) is implementation-defined—guard against zero length.
  • Link with -lm when using GCC/Clang on Unix-like systems.

⚡ Optimization

atan2() is highly optimized in standard math libraries. It avoids the extra division and branch logic you would write by hand with atan(y/x). For hot loops with float data, use atan2f() to match your precision.

Conclusion

atan2(y, x) is the standard way to get a full-circle angle from Cartesian coordinates. Remember y-before-x, avoid atan(y/x) for direction problems, and guard the (0, 0) case.

Continue with atanh() for inverse hyperbolic tangent, or review atan() for the single-argument version.

💡 Best Practices

✅ Do

  • Use atan2(dy, dx) for direction vectors
  • Pass y before x
  • Check for zero length before atan2(0, 0)
  • Convert to degrees for UI display
  • Include <math.h> and link -lm

❌ Don’t

  • Swap arguments to atan2(x, y)
  • Use atan(y/x) when quadrant matters
  • Divide by x manually when x may be 0
  • Assume output is in degrees
  • Call atan2(0, 0) without a guard

Key Takeaways

Knowledge Unlocked

Five things to remember about atan2()

Quadrant-aware angles in C, explained simply.

5
Core concepts
🌐 02

4 quadrants

Full plane.

Power
🎯 03

(−π, π]

Output range.

Range
🛡 04

x = 0 safe

No div-by-0.

Safety
🎮 05

Aim angle

dy, dx.

Pattern

❓ Frequently Asked Questions

atan2(y, x) returns the angle in radians from the positive x-axis to the point (x, y). For example, atan2(1, 1) is pi/4 (45 degrees) because the point (1, 1) lies in the first quadrant at that angle.
C follows the mathematical convention atan2(y, x) for the angle whose tangent is y/x, while using both signs to pick the correct quadrant. Think 'rise (y), then run (x)'.
atan(x) takes one ratio and returns an angle in [-pi/2, pi/2]. atan2(y, x) uses separate coordinates, handles all four quadrants, returns (-pi, pi], and safely handles x = 0.
The result is in (-pi, pi] radians (about -180° to 180°). atan2(0, 1) is 0; atan2(1, 0) is pi/2; atan2(0, -1) is pi.
atan2(0, 0) is implementation-defined. Do not rely on a specific value—check for the zero vector before calling atan2 in production code.
Yes on many systems: gcc program.c -o program -lm. Include <math.h> for the declaration double atan2(double y, double x).
Did you know?

Many languages copied C’s atan2(y, x) signature—Python, JavaScript, Java, and Rust all use the same y-then-x order. Once you learn it in C, you can transfer the pattern directly to graphics code in other languages.

Explore C Math Functions

Continue with atanh() or browse the full math function reference.

Math Functions Index →

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.

6 people found this page helpful