C++ acos()

This function is defined in <cmath> header file.


[Mathematics] cos-1x = acos(x) [In C++ Programming];

acos() prototype [As of C++ 11 standard]

double acos(double x);
float acos(float x);
long double acos(long double x);
double acos (T x); // For integral type

acos() Parameters

The acos() function takes a single mandatory argument in the range [-1, 1]. It is because the value of cosine is in the range of 1 and -1.


acos() Return value

Given that the argument is in the range [-1, 1], the acos() function returns the value in the range of [0, π].

If the argument is greater than 1 or less than -1, acos() returns NaN i.e. not a number.

Parameter (x) Return Value
x = [-1, 1] [0, π] in radians
-1 > x or x > 1 NaN (Not a Number)

Example 1: How acos() works?

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  double x = 0.0, result;

  result = acos(x);
  cout << "acos(x) = " << result << " radians" << endl;
  
  // result in degrees
  cout << "acos(x) = " << result*180/3.1415 << " degrees" << endl;
  
  return 0;
}

When you run the program, the output will be:

acos(x) = 1.5708 radians
acos(x) = 90.0027 degrees

Example 2: acos() function with integral type

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  int x = -1;
  double result;

  result = acos(x);
  
  cout << "acos(x) = " << result << " radians" << endl;
  // Converting result to degrees
  cout << "acos(x) = " << result*180/3.1415 << " degrees";
  
  return 0;
}

When you run the program, the output will be:

acos(x) = 3.14159 radians
acos(x) = 180.005 degrees

Also Read:

Did you find this article helpful?