English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Reference Manual

C++ Library Function <cmath>

C ++acos() Function Usage and Example

This function returns the inverse cosine value of the number (parameter) in radians form in<cmath>Header file definition.

[Math] cos-1++]

acos() Prototype [From C ++ 11Standard Start]

double acos(double x);
float acos(float x);
long double acos(long double x);
double acos(T x); //as an integer

acos() parameter

the acos() function uses[-1,1]within the range of a single mandatory parameter. This is because the cosine value in1to-1of.

the range of the acos() return value

Assuming the parameter is in[-1,1]the range, then the acos() function returns a value within the range [0, π].

If the parameter is greater than1or less-1, then acos() returns NaN, which is not a number.

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

Example1: How does acos() work?

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
  double x = 0.0, result;
  result = acos(x);
  cout << "acos(x) = " << result << " radians" << endl;
  
  cout << "acos(x) = " << result*180/3.1415 << "degrees" << endl;
  
  return 0;
}

When the program is run, the output is:

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

Example2: The acos() function with integer type

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
  int x = -1;
  double result;
  result = acos(x);
  
  cout << "acos(x) = " << result << " radians" << endl;
  // Convert the result to degrees
  cout << "acos(x) = " << result*180/3.1415 << " " degrees;
  
  return 0;
}

When the program is run, the output is:

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

  C++ Library Function <cmath>