Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/math/atan2"

From cppreference.com
< cpp‎ | numeric‎ | math
m (typo)
m (Text replace - "{{example cpp" to "{{example")
Line 31: Line 31:
  
 
===Example===
 
===Example===
{{example cpp
+
{{example
 
  |  
 
  |  
 
  | code=
 
  | code=

Revision as of 17:05, 19 April 2012

Template:cpp/numeric/math/sidebar 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 Template:cpp. If any other argument is Template:cpp, then the return type is Template:cpp, otherwise it is Template:cpp.

Contents

Parameters

x, y - floating point value

Return value

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

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