C++ atan2()

The atan2() function in C++ returns the inverse tangent of a coordinate in radians. It is defined in the cmath header file.

Mathematically, atan2(y, x) = tan-1(y/x).

Example


atan2() Syntax

The syntax of the atan2() function is:

atan2(double y, double x);

atan2() Parameters

The atan2() function takes the following parameters:

  • x - floating-point number that represents the proportion of the x-coordinate
  • y - floating-point number that represents the proportion of the y-coordinate

atan2() Return Value

The atan2() function returns:

  • a floating-point value in the range of [-π, π].
  • 0 if both x and y are zero

atan2() Prototypes

The prototypes of atan2() as defined in the cmath header file are:

double atan2(double y, double x);

float atan2(float y, float x);

long double atan2(long double y, long double x);

// for combinations of arithmetic types
double atan2(Type1 y, Type2 x);

Example 1: C++ atan2()

Output

atan2(y/x) = -0.785398 radians
atan2(y/x) = -45 degrees

Example 2: C++ atan2() with Different Types

In this program, we will use arguments of different data types with the atan2() function.

Output

atan2(y/x) = 3.04694 radians
atan2(y/x) = 174.577 degrees