C++ std::acos() Function

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

Definition

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
CallWhat 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

ParameterDescription
xRequired. A number from -1 to 1. Anything else is outside the domain. An integer is converted to double.

Return Value

Return ValueDescription
AngleThe angle in radians from 0 to pi whose cosine is x.
NaNReturned when x is below -1 or above 1. This is a domain error.
0acos(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;
}

How It Works

  1. Each number is a cosine value, and acos() returns the angle that has this cosine.
  2. acos(1) is 0, because the cosine of a zero angle is 1. acos(0.5) is 1.0472, or 60 degrees.
  3. acos(0) is 1.5708 (pi / 2), and acos(-1) is 3.14159 (pi), a half turn.

Example 2 — Angle Between Two Vectors

The cosine of the angle between two vectors is their dot product divided by the product of their lengths. Use acos() to get the angle.

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

double angleDegrees(double ax, double ay, double bx, double by) {
    double dot = ax * bx + ay * by;
    double lengths = std::hypot(ax, ay) * std::hypot(bx, by);
    return std::acos(dot / lengths) * 180.0 / std::numbers::pi;
}

int main() {
    std::cout << std::fixed << std::setprecision(2);
    std::cout << "(1,0) and (1,1):  " << angleDegrees(1, 0, 1, 1) << '\n';
    std::cout << "(1,0) and (0,1):  " << angleDegrees(1, 0, 0, 1) << '\n';
    std::cout << "(1,0) and (-1,0): " << angleDegrees(1, 0, -1, 0) << '\n';
    return 0;
}

How It Works

  1. The helper function finds the dot product and divides it by the lengths. std::hypot() gives each length.
  2. For (1,0) and (1,1) the cosine is 0.7071, and acos() turns this into 45.00 degrees. Vectors at right angles give 90.00.
  3. Two vectors in opposite directions have a cosine of -1, so the angle is 180.00 degrees. The formula works in any direction.

Example 3 — Angle of a Triangle from Three Sides

The law of cosines gives the angle opposite side c: acos((a*a + b*b - c*c) / (2*a*b)).

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

double angleC(double a, double b, double c) {
    double cosC = (a * a + b * b - c * c) / (2 * a * b);
    return std::acos(cosC) * 180.0 / std::numbers::pi;
}

int main() {
    std::cout << std::fixed << std::setprecision(2);
    std::cout << "Sides 3, 4, 5: " << angleC(3, 4, 5) << " degrees\n";
    std::cout << "Sides 5, 5, 5: " << angleC(5, 5, 5) << " degrees\n";
    std::cout << "Sides 2, 2, 3: " << angleC(2, 2, 3) << " degrees\n";
    return 0;
}

How It Works

  1. 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.
  2. A triangle with three equal sides has a cosine of 0.5 at every corner, so each angle is 60.00 degrees.
  3. 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.

Next: acosh()

Keep going through the <cmath> functions. Inverse hyperbolic cosine of a number.

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

8 people found this page helpful