Namespaces
Variants
Views
Actions

std::atan2

From cppreference.com
< cpp‎ | numeric‎ | math
Revision as of 13:44, 15 June 2012 by P12bot (Talk | contribs)

 
 
 
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <cmath>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
float       atan2( float y, float x );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
double      atan2( double y, double x );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
long double atan2( long double y, long double x );
</td>

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
Promoted    atan2( Arithmetic y, Arithmetic x );
</td>

<td > (4) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Computes the inverse tangent of y/x using the signs of arguments to correctly determine quadrant.

4) If any argument has integral type, it is cast to double. If any other argument is long double, then the return type is long double, otherwise it is double.

Contents

Parameters

x, y - floating point value

Return value

Arc tangent of y/x in radians in the range of [-π; π] radians.

Y argument
Return value
math-atan2.png
X argument

Example

#include <cmath>
#include <utility>
#include <iostream>
 
std::pair<double, double> cartesian_to_polar(double x, double y)
{
    return {std::hypot(x, y), std::atan2(y,x)};
}
 
int main()
{
    std::pair<double, double> polar = cartesian_to_polar(1, 1);
    std::cout << "(1,1) cartesian is (" << polar.first
               << "," << polar.second<< ") polar\n";
}

Output:

(1,1) cartesian is (1.41421,0.785398) polar


See also

Template:cpp/numeric/math/dcl list atanTemplate:cpp/numeric/math/dcl list asinTemplate:cpp/numeric/math/dcl list acosTemplate:cpp/numeric/math/dcl list tan