C++ Math atan2()

Last Updated : 19 May 2026

Math atan2() Function

In C++, the atan2() function is defined in the <cmath> or <math.h> header file and is used to calculate the inverse tangent of the ratio y/x. Unlike the atan() function, atan2() considers the signs of both coordinates to determine the correct quadrant of the angle.

The atan2() function is commonly used in trigonometry, navigation, robotics, computer graphics, and geometry to determine the angle of a point relative to the origin. The returned angle is expressed in radians.

atan2(y,x) = tan-1(y/x);

Syntax

It has the following syntax.

Suppose the coordinate is (x,y). Syntax would be:

Parameters

y: It represents the y-coordinate value.

x: It represents the x-coordinate value.

Return value

It returns the value in the range[-?, ?] and if the values of both x and y are zero then it returns zero value.

  • If any argument is of integral type, it is cast to double.
  • If any argument is of long double type, it is cast to long double.

Examples of the List atan2() Function

Here, we are going to discuss several examples to demonstrate the List atan2() Function Function.

Example 1: Calculate atan2() When Both x and y are Zero

This example demonstrates how the atan2() function behaves when both x and y coordinates are zero.

Output:

Value of tan(y/x) is : 0
Value of tan-1(y/x) is : 0

Explanation:

In this example, atan2() calculates the inverse tangent when both 'x' and 'y' are zero.

Example 2: Calculate atan2() Using Different Data Types

This example demonstrates how the atan2() function works when the x-coordinate is of integer type and the y-coordinate is of float type.

Output:

Value of tan(y/x) is : 3.6021
Value of tan1(y/x) is : 0.915101

Explanation:

In this example, atan2() function finds the inverse of a tangent when x is of integer type and y is of float type.

Example 3: Calculate atan2() for Positive Coordinates

Output:

Value of tan(y/x) is : -2.18504
Value of tan-1(y/x) is : 1.10715

Explanation:

This example demonstrates how to calculate the inverse tangent for positive x and y coordinates using the atan2() function.