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


The ratios we use the most in trigonometry include sine, cosine, tangent, and a few more. You can calculate these ratios using an angle. If we are aware of the ratio values, we may also calculate the angle using inverse trigonometric functions.

This lesson will show you how to compute the angle using the tangent value, in radians, using C++'s inverse-tangent (arctan) function.

The atan() function

The angle is calculated using the atan() technique and the inverse trigonometric tangent function. The C++ standard library contains this function. We must import the cmath library before we can utilize this approach. This method returns the angle in radians and takes a tangent value as an argument. The following uses the simple syntax −

Syntax

#include < cmath >
atan( <tangent value> )

The cosine value must be in the range [-infinity to infinity]. The returned value will be in the range $\mathrm{[-\:\frac{\pi}{2},\frac{\pi}{2}]}$ (both included)

Algorithm

  • Take tangent value x as input
  • Use atan( x ) to calculate the tan−1(x)
  • Return result.

Example

#include <iostream> #include <cmath> using namespace std; float solve( float x ) { float answer; answer = atan( x ); return answer; } int main() { float angle, ang_deg; angle = solve( 1 ); ang_deg = angle * 180 / 3.14159; cout << "The angle (in radian) for given tangent 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 tangent value 0 is: " << angle << " = " << ang_deg << " (in degrees)" << endl; angle = solve( 999999 ); ang_deg = angle * 180 / 3.14159; cout << "The angle (in radian) for given tangent value 999999 is: " << angle << " = " << ang_deg << " (in degrees)" << endl; angle = solve( -999999 ); ang_deg = angle * 180 / 3.14159; cout << "The angle (in radian) for given tangent value -999999 is: " << angle << " = " << ang_deg << " (in degrees)" << endl; }

Output

The angle (in radian) for given tangent value 1 is: 0.785398 = 45 (in degrees)
The angle (in radian) for given tangent value 0 is: 0 = 0 (in degrees)
The angle (in radian) for given tangent value 999999 is: 1.5708 = 90 (in degrees)
The angle (in radian) for given tangent value -999999 is: -1.5708 = -90 (in degrees)

The atan() method, which receives the tangent value in this case, returns the angle in radian format. We converted this output from radians to degrees using the formula below.

$$\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

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements