 # 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

``````#include <iostream>
#include <cmath>
using namespace std;

int main() {

// get the value of tan-1(5.0 / 2.0)
cout << atan2(5.0, 2.0);

return 0;
}

// Output: 1.19029``````

## 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()

``````#include <iostream>
#include <cmath>
using namespace std;

int main() {
double x = 10.0, y = -10.0;

double result = atan2(y, x);

// convert result to degrees
double degree = result * (180 / 3.141592);

cout << "atan2(y/x) = " << result << " radians" << endl;
cout << "atan2(y/x) = " << degree << " degrees";

return 0;
}``````

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.

``````#include <iostream>
#include <cmath>
#define PI 3.141592654
using namespace std;

int main() {
double result;
float x = -31.6;
int y = 3;

// atan2() with float and int arguments
result = atan2(y, x);

cout << "atan2(y/x) = " << result << " radians" << endl;

// Display result in degrees
cout << "atan2(y/x) = " << result * (180 / PI) << " degrees";

return 0;
}``````

Output

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