Javatpoint Logo
Javatpoint Logo

atan2() Function in C

There are numerous functions in the C language's mathematical library that you can use to tackle various trigonometrical math issues. These features make this language a potent tool for creating programs targeted at the scientific community across various fields. atan2() function is one of them numerous functions.

In this article, we will go over the function's syntax, input and output arguments, the kinds of data that each one accepts, and its theoretical component. After that, we'll put what we've learnt into practice with a real-world example that includes code snippets and pictures that demonstrate the step-by-step procedure for utilizing the C language's atan2() function to compute the arc tangent of a coordinate.

What is atan2() function?

Atan2() function returns the arc tangent, or inverse tangent, of the y/x coordinates between intervals of -/2 and /2. It calculates the angle in radians for the quadrant, and the returned result is expressed in radians, whereas atan() returns the arc tangent of y.

The atan2() function is available in the "math.h" header file, which is a component of the C math library. Its use must be stated in our ".c" code or the ".h" header beforehand with the following clause:

We can utilize the atan() function and related asinl() and asinf() methods supplied by the C math library after including the "math.h" header in the code. The math library and the "libm" library both contain this function. You must call the "-lm" command in gcc in the compile command.

Syntax of Atan2() function:

It has the following syntax:

Here, we discussed the parameters of the syntax:

Double x: It represents the X coordinate.

Double y: It represents the Y coordinate.

How to use the Atan2() function to find the Arc Tangent of the Y and X Coordinates

In this example, we'll show how to define the appropriate constants and variables step by step. After that, we will use the atan2() function to get the arc tangent of the y and x coordinates.

To use these functions, we must first include all required headers in our code.

We must declare the Double type variables for y, x, and arc_tan where the atan2() function provides its result after including the libraries. In this example, the coordinates will be x = -6 and y = 6.

Now, we call the atan2() function and pass the previously defined coordinates as the input argument and arc_tan as the output parameter to get the arc tangent of the x and y coordinates. After that, the printf() function is used to output the outcome to the command console:

Output:

The arc tangent of y,x = 2.356194

How to use the Atan2() function to get the Arc Tangent Expressed in Degrees

In C, the mathematical operations that deal with angle measurements using radians as the unit of measure are solved. It is more convenient to translate the outputs of these functions from radians to degrees because the technical data sheet components in all domains often provide the formulas for the computations in degrees.

Here is a quick way to convert the atan2() function's result to degrees. The conversion formula is as follows:

Degrees = radians * (180 / 𝝿)

As shown in the following, we define a macro called asin_deg in the head of the code that calls the atan2() function and multiplies the result by 180 / 𝝿 to obtain the result of the arc tangent of a coordinate in degrees:

Output:

The arc tangent in degrees of y, x is = 135.000000

In this manner, we performed a quick computation to convert radians to degrees and then presented the outcome in this unit on the screen.

Conclusion:

In this C language tutorial, we demonstrated how to use the atan2() function to obtain the inverse tangent of the arc tangent of the x and y coordinates. We investigated the syntax for the output arguments, the data type that this function processes, and the theoretical underpinnings of the function. Additionally, we demonstrated how to translate the outcomes returned by this function into degrees.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA