C++ std::atan() Function

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

Definition

In C++, std::atan(x) is the inverse tangent (also called arctangent). It undoes std::tan(). If a line has a slope of 1 (it rises as much as it runs), atan(1) gives its angle: 0.785398 radians, which is 45 degrees.

Unlike asin() and acos(), it accepts any number, so it never gives NaN for a normal input. The answer is always between -pi / 2 and pi / 2. When you have separate y and x values and need the full circle, use std::atan2() instead.

std::atan() 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::atan. Start with these lines.

C++
double a = std::atan(1.0);    // double version
float  b = std::atan(1.0f);   // float version
CallWhat it does
std::atan(1.0)Passes a double and returns a double. Here the result is 0.785398.
std::atan(1.0f)Passes a float, so the float overload runs and returns a float.
std::atan(1)An integer is converted to double. Returns 0.785398 (pi / 4).

Write std::atan so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       atan(float x);
double      atan(double x);
long double atan(long double x);
double      atan(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. Any number, such as a slope. An integer is converted to double.

Return Value

Return ValueDescription
AngleThe angle in radians from -pi / 2 to pi / 2 whose tangent is x.
pi / 2For very large positive x and for inf. The result is -pi / 2 for large negative x.
NaNReturned only when x is NaN.

C++ Examples

Example 1 — Inverse Tangent of Common Values

Print atan() for some slopes, including a very steep one.

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

int main() {
    double values[] = {0.0, 1.0, -1.0, 1000.0};

    for (double v : values) {
        std::cout << "atan(" << v << ") = " << std::atan(v) << '\n';
    }
    return 0;
}

How It Works

  1. Each number is a slope (a tangent value), and atan() gives the angle of a line with that slope.
  2. A slope of 0 is flat, so the angle is 0. A slope of 1 is 0.785398 radians, which is 45 degrees, and -1 is the same angle going down.
  3. A very steep slope of 1000 gives 1.5698, which is just short of pi / 2 (1.5708), almost straight up.

Example 2 — Slope to Angle in Degrees

A roof rises by some height over a run of 1. Turn each slope (rise divided by run) into an angle in degrees.

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

int main() {
    double rise[] = {0.1, 0.5, 1.0};
    double run = 1.0;

    std::cout << std::fixed << std::setprecision(2);
    for (double r : rise) {
        double angle = std::atan(r / run) * 180.0 / std::numbers::pi;
        std::cout << "Slope " << r / run << " -> " << angle << " degrees\n";
    }
    return 0;
}

How It Works

  1. r / run is the slope. Here the run is 1, so the slope is the same as the rise.
  2. atan() returns radians, and * 180.0 / std::numbers::pi turns them into degrees.
  3. A slope of 0.10 is a gentle 5.71 degrees, 0.50 is 26.57 degrees, and 1.00 is exactly 45.00 degrees.

Example 3 — The Limits of atan()

No matter how large the input is, the answer never goes beyond pi / 2.

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

int main() {
    double inf = std::numeric_limits<double>::infinity();

    std::cout << "atan(inf)  = " << std::atan(inf) << '\n';
    std::cout << "atan(-inf) = " << std::atan(-inf) << '\n';
    std::cout << "pi / 2     = " << std::numbers::pi / 2 << '\n';
    return 0;
}

How It Works

  1. std::numeric_limits<double>::infinity() gives a real infinity that can be passed to a function.
  2. The angle of an endless slope is a quarter turn, so atan(inf) returns 1.5708 and atan(-inf) returns -1.5708.
  3. The last line prints pi / 2 for comparison. It matches, which shows that atan() approaches but never passes this limit.

Frequently Asked Questions

std::atan(x) returns the angle, in radians, whose tangent is x. It accepts any number, and the result is between -pi/2 and pi/2.
An angle in radians from -pi/2 to pi/2. atan(inf) is pi/2, and atan(-inf) is -pi/2.
atan(x) takes one number, usually y / x, and gives an angle from -pi/2 to pi/2. atan2(y, x) takes both numbers, finds the correct quadrant, and gives an angle from -pi to pi. It also works when x is 0.
It returns 0.785398, which is pi / 4 or 45 degrees. So 4 * std::atan(1.0) is a classic way to get the value of pi.
Include <cmath> and write std::atan(). That always works and picks the C++ overloads. The global name ::atan often works too, but the standard does not promise it, and it may only take a double.

Next: atan2()

Keep going through the <cmath> functions. Angle of a point (x, y), in the correct quadrant.

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

12 people found this page helpful