C++ acos()

Last Updated On Thursday 21st Apr 2022

acos domain

The C++ acos() function returns the the inverse cosine of a number in radians. It is defined in <cmath> header file.

Syntax

	acos(x);
	

Parameters

The acos() function takes a one argument in the range [-1, 1], means the value of cosine is in the range of 1 and -1.

acos

Return value

The argument is in the range [-1, 1],

  • acos() function returns the value in the range of [0, π].

std acos

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
	

Example 1: C++ acos()

	#include <stdio.h>
#include <math.h>

#define PI 3.14159265

int main () {
  double param, result;
  param = 0.1;
  result = acos(param) * 180.0 / PI;
  printf ("The arc cosine of %f is %f degrees.\n", param, result);
  return 0;
}
	

Output

	The arc cosine of 0.100000 is 84.260830 degrees.
	

acos function

Example 2 : C++ acos() ( Argument greater than 1 )

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

	#include <stdio.h>
#include <math.h>

#define PI 3.14159265

int main () {
  double param, result;
  param = 4.4;
  result = acos(param) * 180.0 / PI;
  printf ("The arc cosine of %f is %f degrees.\n", param, result);
  return 0;
}
	

Output

  • The Above Code returns NaN (Not a Number)
	The arc cosine of 4.400000 is nan degrees.
	
References