acos() function in C++ STL

C++ProgrammingServer Side Programming

The acos() function returns the inverse cosine of an angle given in radians. It is an inbuilt function in C++ STL.

The syntax of the acos() function is given as follows.

acos(var)

As can be seen from the syntax, the function acos() accepts a parameter var of data type float, double or long double. The value of this parameter should be between -1 and 1. It returns the inverse cosine of var in the range of -pi to pi.

A program that demonstrates acos() in C++ is given as follows.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
   double d = 0.75, ans;
   ans = acos(d);
   cout << "acos("<< d <<") = " << ans << endl;
   return 0;
}

Output

acos(0.75) = 0.722734

In the above program, first the variable d is initialized. Then inverse cosine of d is found using acos() and stored in ans. Finally the value of ans is displayed. This is demonstrated by the following code snippet.

double d = 0.75, ans;
ans = acos(d);
cout << "acos("<< d <<") = " << ans << endl;

The result obtained by using the acos() function can be converted into degrees and displayed. A program to demonstrate this is as follows.

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
   double d = 0.75, ans;
   ans = acos(d);
   ans = ans*180/3.14159;
   cout << "acos("<< d <<") = " << ans << endl;
   return 0;
}

Output

acos(0.75) = 41.4097

In the above program, the inverse cosine is obtained using acos(). Then this value is converted into degrees. Finally, the output is displayed. This is demonstrated by the following code snippet.

double d = 0.75, ans;
ans = acos(d);
ans = ans*180/3.14159;
cout << "acos("<< d <<") = " << ans << endl;
raja
Published on 09-Oct-2018 10:59:08
Advertisements