Library contents

C++ std::acos

In C++, std::acos returns the inverse cosine in radians. For real inputs from −1 to 1, the result lies between 0 and π.

A simple example

std::acos

  1. Start with 1
  2. Do this Find the angle with cosine 1
  3. Result 0 radians
At zero radians cosine is 1. Inverse cosine recovers that angle.
lesson.cpp
#include <iostream>
#include <cmath>

int main() {
    std::cout << (std::acos(1.0)) << '\n';
}
Output
0

Syntax

C++ syntax
std::acos(reachFraction)
Run these examples in C++

Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.

c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lesson

Recover the angle of an awning arm

A designer is positioning an awning over a window. Relate the length of its arm to the horizontal distance it covers so a drawing can show the reach at a chosen angle.

From the programconst double reachFraction = 0.5
Result1.047
lesson.cpp
#include <cmath>
#include <cstdio>

int main() {
    const double reachFraction = 0.5;

    std::printf("%.3f\n", std::acos(reachFraction));
    return 0;
}
Output
1.047

A reach of half the arm length corresponds to about 1.047 radians, or 60 degrees.

Advertisements

Read a solar panel diagram

A model solar panel is 2 m long. Its horizontal projection is 1.6 m, and a student wants the tilt angle for a drawing.

GivenRun 1.6 m · length 2 m
Worked result0.644 rad

Divide the horizontal projection by the sloping length before taking inverse cosine.

Check a closed display lid

A desktop display lid lies flat in a model. Its projected width equals its full width, so a designer checks the angle reported by the model.

GivenRun 30 cm · length 30 cm
Worked result0.000 rad

A cosine ratio of one corresponds to zero tilt.

What to watch for

Clamp small measurement-rounding errors to the valid interval only if your application justifies doing so.

Documentation for std::acos