| by Arround The Web | No comments

Atan C++

Math, statistics, and computer programming languages work hand-in-hand. Formula used in stats or math sometimes need to be implemented in computer programming languages. These functions and formula work the same in all subjects. The only difference is the implementation. This article is designed to teach how to implement atan() function in the c++ programming language.

What is atan() C++ Function?

The atan() function is an inverse tangent function of Math. It is used to find the inverse tangent value of a number in the c++ programming language. In Mathematics, atan() is the tan-1a. ‘a’ is the number and tan-1 finds the inverse tangent of ‘a’.

Similarly, when we find the atan() in the c++ programming language, a number ‘a’ is provided to the function atan() and calculates the inverse tangent of ‘a’. The atan() function of c++ returns the tangent inverse of the given number in radians format. The radian value is equal to 180/pi degrees. So, when you calculate a tangent inverse on a calculator, you will find a different result as the calculator returns the result in the form of a degree. But the output of atan() is in radians. If you want the same result as the calculator provides, you need to convert the radian result into the degree.

Syntax of atan() C++ Function

The syntax of the atan() c++ function is given below:

To define the atan() function, we need to provide the data type as well. Float will be used for the floating number and double will be used for the double floating number.

Here is the syntax of both float and double float data type parameters:

The ‘float’ and ‘double’ are the data types of function atan() and parameters. If the parameter is in the ‘float’ type, the return type for the function will also be float. Similarly, if the type of parameter is ‘double’ then the return type of the function will be ‘double’. The atan() function returns the result in either a floating point or a double float. So, if you provide an int number, the result will be in decimal points.

How Does the atan() C++ Function Work?

The atan() function takes a number as input and computes its inverse tangent and returns the calculated result. It returns the computed inverse tangent value in the form of radians. To understand how the atan() function works better, see the examples given below.

Example 1:

The first example simply takes a number as input and passes it on to the atan() function to calculate the inverse tangent of that provided number. The code is given below.

Starting with the first line of code, the ‘iostream’ is a standard c++ library that provides the functionality of taking input and giving output to the user: cin, cout, and etc. The next line is importing the ‘cmath’ library into the program. The ‘cmath’ library of the c++ programming language provides the Math functions like atan(), asin(), etc. The ‘using namespace std’ allows the program to use all the things provided by the ‘std namespace’.

The program starts with the main() function, followed by the opening and closing brackets, {}. All lines of code go in between these brackets. As we explained above, the type of input parameter and return parameter are generally the same. So, ‘double a = 12.57, out’ represents the input and output variables in double data type form. The ‘a’ parameter is holding the input and the ‘out’ parameter will keep the output of the atan() function. Using the ‘cout’ method, we will print the output of the atan() function. The ‘return 0’ is provided at the end so that the function successfully returns the result or throws an exception in case of any error.

Let’s see the result of the program given below:

The atan(12.57) function returned 1.49141 in the radian format. If you calculate the atan(12.57) on the calculator, you will get 85.45 as the calculator gives the result in degrees. Let us convert the radian into a degree in the next example.

Example 2:

As discussed above the radian is equal to 180/pi, so we will use the same formula to convert the radian into a degree. See the code below first and then we will explain each line separately.

Note that we used the same code and sample data as in the first example. So, let us just explain the additional line, ‘cout << “atan(“<<a<<“) in Degrees = ” << out*180/3.1415 << endl;’. The ‘out*180/3.1415’ is used to convert the radian result ‘1.49141’ into the degree. The ‘out’ variable is holding the result of the atan() function in radians, and ‘3.1415’ is the value of ‘pi’. After calculating the value of ‘out*180/3.1415’, we will get ‘85.45’, the same as the calculator result. The ‘endl’ is used to move the cursor to the start of the next line.

Let us see the output below:

Example 3:

Let us see how the atan() function works if an integer number is provided as input. Normally, it should not raise any error since the integer number works well with the float or double float data types. Check the attached code below.

If you notice, we again used the same code but changed the input data to an integer. The ‘int’ data type is used in the c++ programming language to define integer numbers. In the previous examples, we used floating numbers. So here, we used the int number to see the result of the atan() function.

Let us check the result of the atan() function for an integer number given below. Note that the atan() function calculated the inverse tangent of the integer number successfully without raising an error since the inverse tangent can be found in an integer number.

Conclusion

This article is designed to discuss the working of atan() function in the c++ programming language. The atan() function computes the tangent inverse of a given number and returns the result in radians. We incorporated three unique examples for your understanding.

Share Button

Source: linuxhint.com

Leave a Reply