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
Call
What 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
Parameter
Description
x
Required. Any number, such as a slope. An integer is converted to double.
Return Value
Return Value
Description
Angle
The angle in radians from -pi / 2 to pi / 2 whose tangent is x.
pi / 2
For very large positive x and for inf. The result is -pi / 2 for large negative x.
NaN
Returned 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;
}
std::numeric_limits<double>::infinity() gives a real infinity that can be passed to a function.
The angle of an endless slope is a quarter turn, so atan(inf) returns 1.5708 and atan(-inf) returns -1.5708.
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.