atan2
The C++
atan2()
function returns the inverse tangent of a coordinate in radians. It is defined in <cmath> header file. Mathematically,atan2(y, x) = tan-1(y/x)
Syntax
atan2(double y, double x);
Parameters
The
atan2()
function takes the x and y parameters:
-
x
- It represents the proportion of x-coordinate (floating-point number) -
y
- It represents the proportion of y-coordinate (floating-point number)
Return value
The
atan2()
function returns
-
a
floating-point value in the range of[-π, π]
. -
0
if bothx
andy
are zero
atan2 prototype [As of C++ 11 standard]
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);
atan2 function
Example 1: C++ atan2()
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main () { double x, y, result; x = -44.0; y = 44.0; result = atan2 (y,x) * 180 / PI; printf ("The arc tangent (%f, %f) is %f degrees\n", x, y, result ); return 0; }
Output
The arc tangent (-44.000000, 44.000000) is 135.000000 degrees
atan2 math
Example 2 : C++ atan2() with different types
#include <iostream> #include <cmath> #define PI 3.141592654 using namespace std; int main() { double result; float x = -44.6; int y = 4; result = atan2(y, x); cout << "atan2(y/x) = " << result << " radians" << endl; // Result in degrees cout << "atan2(y/x) = " << result * (180 / PI) << " degrees"; return 0; }
Output
atan2(y/x) = 3.05215 radians atan2(y/x) = 174.875 degrees