Home  >  Article  >  Backend Development  >  C++ program to find arc cosine of a given value

C++ program to find arc cosine of a given value

王林
王林forward
2023-08-26 10:01:06682browse

C++ program to find arc cosine of a 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 will show you how to use C's arccosine function to convert a cosine value to an angle in radians.

The acos() function

The arc cosine function is used to calculate angles using the acos() method. This function can be found in the C standard library. In order to use this method, we must import the cmath library. This function accepts a cosine value as a parameter and returns the angle in radians. The following uses simple syntax:

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 the cosine value x as input
  • Use acos(x) to calculate 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 returns the angle in radian format. Using the following formula, we convert this output from radians to degrees.

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

Conclusion

To perform inverse trigonometric operations from cosines, we use the acos() function from the cmath library. This function takes a cosine value as input and returns the given angle in radians. In older versions of C/C++, the return type was double, but later C++ versions also used overloaded forms for float and long-double. When an integer value is passed as a parameter, it will convert the input parameter to double and call the acos() method corresponding to the double type parameter.

The above is the detailed content of C++ program to find arc cosine of a given value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete