C++ Program to find the arccosine of the given value


Sine, cosine, tangent, and a few more ratios are some of the ones we utilize the most in trigonometry. These ratios can be computed from an angle. However, we can also determine the angle using inverse trigonometric functions if we know the ratio values.

In this tutorial, we'll go through how to use C++'s inverse-cosine (arccosine) function to convert a cosine value to an angle in radians.

The acos() function

The inverse trigonometric cosine function is used to calculate the angle using the acos() method. This function can be found in the C++ standard library. To use this method, we must import the cmath library. This function accepts the cosine value as an argument and returns the angle in radians. Simple syntax is used in the following −

Syntax

#include < cmath >
acos( <cosine value> )

The cosine value must be in the range [-1 to +1] (both included). Otherwise, a domain error will be raised, and it will return Not-A-Number (nan). The returned value will be in the range [0, π] (both included)

Algorithm

  • Take cosine value x as input
  • Use acos( x ) to calculate the cos−1(x)
  • Return result.

Example

#include <iostream> #include <cmath> using namespace std; float solve( float x ) { float answer; answer = acos( x ); return answer; } int main() { float angle, ang_deg; angle = solve( 0.7071067 ); ang_deg = angle * 180 / 3.14159; cout << "The angle (in radian) for given cosine value 0.7071067 is: " << angle << " = " << ang_deg << " (in degrees)" << endl; angle = solve( 0.866025 ); ang_deg = angle * 180 / 3.14159; cout << "The angle (in radian) for given cosine value 0.866025 is: " << angle << " = " << ang_deg << " (in degrees)" << endl; angle = solve( 1 ); ang_deg = angle * 180 / 3.14159; cout << "The angle (in radian) for given cosine value 1 is: " << angle << " = " << ang_deg << " (in degrees)" << endl; angle = solve( 0 ); ang_deg = angle * 180 / 3.14159; cout << "The angle (in radian) for given cosine value 0 is: " << angle << " = " << ang_deg << " (in degrees)" << endl; }

Output

The angle (in radian) for given cosine value 0.7071067 is: 0.785398 = 45 (in degrees)
The angle (in radian) for given cosine value 0.866025 is: 0.5236 = 30.0001 (in degrees)
The angle (in radian) for given cosine value 1 is: 0 = 0 (in degrees)
The angle (in radian) for given cosine value 0 is: 1.5708 = 90.0001 (in degrees)

Here, the sine value is passed to the acos() method, which then returns the angle in radian format. Using the following formula, we have transformed this output from radians to degrees.

$$\mathrm{\theta_{deg}\:=\:\theta_{rad}\:\times\:\frac{180}{\pi}}$$

Conclusion

To perform the inverse trigonometric operation from the cosine value we use the acos() function from the cmath library. This function takes the cosine value as input and returns the given angle in the radian unit. In older versions of C / C++, the return type was double, but later versions in C++ used the overloaded form for float and long-double additionally. While an integer value is passed as an argument, it will cast the input parameter into double and call the acos() method corresponding to the double-type parameter.

Updated on: 19-Oct-2022

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements