In C++, std::acos(x) is the inverse cosine (also called arccosine). It undoes std::cos(): if the cosine of an angle is 0.5, acos(0.5) gives back that angle, about 1.0472 radians, which is 60 degrees.
The input must be between -1 and 1. The answer is an angle in radians between 0 and pi. It is the standard way to find the angle between two vectors, or an angle of a triangle when you know all three sides.
std::acos() 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::acos. Start with these lines.
C++
double a = std::acos(0.5); // double version
float b = std::acos(0.5f); // float version
Call
What it does
std::acos(0.5)
Passes a double and returns a double. Here the result is 1.0472.
std::acos(0.5f)
Passes a float, so the float overload runs and returns a float.
std::acos(0)
An integer is converted to double. Returns 1.5708 (pi / 2).
Write std::acos so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float acos(float x);
double acos(double x);
long double acos(long double x);
double acos(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. A number from -1 to 1. Anything else is outside the domain. An integer is converted to double.
Return Value
Return Value
Description
Angle
The angle in radians from 0 to pi whose cosine is x.
NaN
Returned when x is below -1 or above 1. This is a domain error.
0
acos(1) returns exactly 0.
C++ Examples
Example 1 — Inverse Cosine of Common Values
Print acos() for a few numbers inside its range. The results are in radians.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {1.0, 0.5, 0.0, -1.0};
for (double v : values) {
std::cout << "acos(" << v << ") = " << std::acos(v) << '\n';
}
return 0;
}
For sides 3, 4, 5 the top part is 9 + 16 - 25, which is 0. The cosine is 0 and the angle is 90.00 degrees, a right angle.
A triangle with three equal sides has a cosine of 0.5 at every corner, so each angle is 60.00 degrees.
For 2, 2, 3 the cosine is -0.125, and the wide angle between the two equal sides is 97.18 degrees.
Frequently Asked Questions
std::acos(x) returns the angle, in radians, whose cosine is x. The input must be between -1 and 1, and the result is between 0 and pi.
An angle in radians from 0 to pi. If x is outside the range -1 to 1, the result is NaN.
The input must be from -1 to 1, and the result is from 0 to pi radians (0 to 180 degrees). Other inputs give NaN.
Clamp the input before calling it. In C++17 use std::acos(std::clamp(x, -1.0, 1.0)). Tiny rounding errors can push a ratio to 1.0000000000000002.
Include <cmath> and write std::acos(). That always works and picks the C++ overloads. The global name ::acos often works too, but the standard does not promise it, and it may only take a double.