C Math atan() Function

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

What You’ll Learn

The atan() function computes the arc tangent (inverse tangent) of a number. Given a slope or tangent ratio, it returns the angle in radians whose tangent equals that value—ideal for line angles, ramps, and graphics.

01

Inverse tan

Angle from slope.

02

math.h

Link with -lm.

03

Any real x

Full domain.

04

Range

[−π/2, π/2].

05

Radians out

Not degrees.

06

vs atan2

Quadrants.

Definition and Usage

atan (arc tangent) answers: “What angle has this tangent?” If tan(θ) = x, then atan(x) = θ (in radians, principal value).

On a right triangle, tangent is opposite ÷ adjacent (rise over run). If a hill rises 3 units for every 4 units forward, the slope is 3/4 and atan(3.0/4.0) gives the incline angle.

💡
Beginner Tip

The input x is a ratio (tangent value), not an angle. The output is the angle in radians. For a point (x, y) in any quadrant, use atan2(y, x) instead.

📝 Syntax

Standard C declaration:

C
double atan(double x);

Related variants

C
float atanf(float x);           /* <math.h> */
long double atanl(long double x); /* <math.h> */

Parameters

  • x — tangent value (slope ratio); any real number.

Return Value

  • Angle in radians in the range [−π/2, π/2].
  • atan(0) returns 0; atan(1) returns π/4.
  • atan(±∞) approaches ±π/2 (infinity handled by the library).

Headers and linking

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

⚡ Quick Reference

CallMeaningResult (approx.)
atan(0)tan(0°) = 00 radians
atan(1)tan(45°) = 10.7854 (π/4)
atan(0.5)Slope 1:20.4636 rad (~26.6°)
atan(-1)tan(−45°) = −1-0.7854 (−π/4)
atan(x) * 180 / M_PIConvert to degreesAngle in °
Inverse
atan(x)

Angle from tan

Forward
tan(angle)

Tan from angle

Quadrants
atan2(y, x)

Full plane

Slope
atan(rise/run)

Incline

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Define _USE_MATH_DEFINES before <math.h> on MSVC for M_PI.

📚 Getting Started

Compute arc tangent and display radians and degrees.

Example 1 — Basic Arc Tangent with Degrees

Find the angle whose tangent is 0.5.

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

int main(void) {
    double x = 0.5;
    double rad = atan(x);
    double deg = rad * 180.0 / M_PI;

    printf("The arctangent of %.2f is %.2f radians or %.2f degrees.\n",
           x, rad, deg);

    return 0;
}

How It Works

atan(0.5) returns about 0.464 radians (26.57°). The input is a tangent ratio, not an angle—a common beginner mistake corrected from the old reference.

Example 2 — Special Tangent Values

Key reference points every trig programmer memorizes.

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

int main(void) {
    printf("atan( 0) = %.6f radians\n", atan(0.0));
    printf("atan( 1) = %.6f radians\n", atan(1.0));
    printf("atan(-1) = %.6f radians\n", atan(-1.0));

    return 0;
}

How It Works

atan(1) is π/4 (45°). Output stays within [−π/2, π/2]—never 135° or 225°, which is why atan2 exists for full-circle angles.

📈 Practical Patterns

Slope angles, inverse verification, and quadrant comparison.

Example 3 — Angle of a Line (Rise over Run)

A ramp rises 3 units for every 4 units forward.

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

int main(void) {
    double rise = 3.0;
    double run  = 4.0;
    double slope = rise / run;
    double angle_deg = atan(slope) * 180.0 / M_PI;

    printf("Slope = %.0f / %.0f = %.2f\n", rise, run, slope);
    printf("Incline angle = %.1f degrees\n", angle_deg);

    return 0;
}

How It Works

atan(rise/run) converts a slope ratio into an angle. This pattern appears in road grades, roof pitch, and 2D game rotation when you know vertical and horizontal displacement.

Example 4 — Verify atan() with tan()

Inverse and forward tangent undo each other on valid inputs.

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

int main(void) {
    double values[] = { -2.0, -0.5, 0.0, 0.5, 2.0 };
    size_t i;

    for (i = 0; i < sizeof values / sizeof values[0]; i++) {
        double x = values[i];
        double angle = atan(x);
        double back = tan(angle);

        printf("x=%5.1f  atan(x)=%.4f  tan(atan(x))=%.6f\n",
               x, angle, back);
    }

    return 0;
}

How It Works

tan(atan(x)) equals x for every real x. The reverse atan(tan(θ)) only equals θ when θ is in the principal range.

Example 5 — When atan() Is Not Enough: atan2()

Same ratio, different quadrants—atan cannot tell them apart.

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

int main(void) {
  /* Both points have slope y/x = -0.75 */
  double x1 =  4.0, y1 = -3.0;
  double x2 = -4.0, y2 =  3.0;

  printf("Point A (%.0f, %.0f):\n", x1, y1);
  printf("  atan(y/x)  = %.2f rad\n", atan(y1 / x1));
  printf("  atan2(y,x) = %.2f rad\n", atan2(y1, x1));

  printf("Point B (%.0f, %.0f):\n", x2, y2);
  printf("  atan(y/x)  = %.2f rad\n", atan(y2 / x2));
  printf("  atan2(y,x) = %.2f rad\n", atan2(y2, x2));

  return 0;
}

How It Works

atan(y/x) loses quadrant information. atan2(y, x) uses both signs to return the correct angle in (−π, π]. Learn more on the atan2() tutorial page.

🚀 Common Use Cases

  • Line and slope angles — convert rise/run into an incline angle.
  • 2D graphics — rotation from a known tangent when quadrant is fixed.
  • Robotics — heading adjustments from velocity component ratios.
  • Geometry — find angles in right triangles via opposite/adjacent.
  • Pair with tan() — forward and inverse tangent operations.

🧠 How atan() Works

1

Receive ratio x

Any real tangent value (slope) is accepted.

Input
2

Invert tangent

Find θ such that tan(θ) = x.

Compute
3

Principal value

Return angle in [−π/2, π/2].

Range
=

Angle in radians

Use with tan(), rotation, or degree conversion.

📝 Notes

  • Input x is a tangent ratio, not an angle in radians.
  • Output is in [−π/2, π/2] radians—limited to one half of the circle.
  • For points in all four quadrants, use atan2(y, x) instead of atan(y/x).
  • Link with -lm when using GCC/Clang on Unix-like systems.
  • Use atanf or atanl for float or long double data.

⚡ Optimization

atan() is implemented in the platform math library and is already optimized. Prefer atan2(y, x) over atan(y/x) when you have separate coordinates—it avoids a division and handles x = 0 safely while preserving quadrant information.

Conclusion

atan() turns a tangent ratio into an angle in radians. Remember the input is a slope, not an angle, and reach for atan2() when you need full-circle direction from (x, y).

Next up: atan2() for quadrant-aware angles, or explore tan() for the forward tangent function.

💡 Best Practices

✅ Do

  • Include <math.h> and link with -lm
  • Pass tangent ratios to atan, not angles
  • Convert output to degrees for user display
  • Use atan2(y, x) for (x, y) coordinates
  • Verify with tan(atan(x)) while learning

❌ Don’t

  • Pass angles in degrees to atan()
  • Use atan(y/x) when quadrant matters
  • Forget -lm on Linux builds
  • Expect output outside [−90°, 90°]
  • Divide by zero manually—use atan2 instead

Key Takeaways

Knowledge Unlocked

Five things to remember about atan()

Inverse tangent in C, explained simply.

5
Core concepts
📚 02

Ratio in

Not degrees.

Input
🎯 03

[−π/2, π/2]

Output range.

Range
📈 04

rise/run

Slope angle.

Pattern
🚀 05

atan2

Quadrants.

Next

❓ Frequently Asked Questions

atan() returns the arc tangent (inverse tangent) of x—the angle in radians whose tangent equals x. For example, atan(1) is pi/4 (45 degrees) because tan(45°) = 1.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double atan(double x).
No. The argument x is a tangent ratio (rise over run), not an angle. The return value is the angle in radians. The old misconception 'x in radians' is incorrect.
The result is always between -pi/2 and pi/2 radians (about -90° to 90°). atan(0) is 0; atan(1) is pi/4; atan(-1) is -pi/4.
atan(x) takes one ratio and returns an angle in [-pi/2, pi/2]. atan2(y, x) takes two coordinates, handles all four quadrants, and returns an angle in (-pi, pi]. Use atan2 for (x, y) direction vectors.
tan() takes an angle in radians and returns its tangent. atan() does the reverse: it takes a tangent value and returns the angle. They are inverse operations.
Did you know?

Because tangent is not one-to-one over all angles, atan() returns only the principal value between −90° and 90°. That is why two different directions in the plane can share the same y/x ratio but need different angles—atan2 was designed exactly for that job.

Explore C Math Functions

Continue with atan2() 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.

5 people found this page helpful