Namespaces
Variants
Views
Actions

std::atan2

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Exponential functions
(C++11)
(C++11)
(C++11)
(C++11)
Power functions
(C++11)
(C++11)
Trigonometric and hyperbolic functions
(C++11)
(C++11)
(C++11)
Error and gamma functions
(C++11)
(C++11)
(C++11)
(C++11)
Nearest integer floating point operations
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Floating point manipulation functions
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
Classification/Comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Macro constants
(C++11)(C++11)(C++11)(C++11)(C++11)
 
Defined in header <cmath>
float       atan2( float y, float x );
(1)
double      atan2( double y, double x );
(2)
long double atan2( long double y, long double x );
(3)
Promoted    atan2( Arithmetic1 y, Arithmetic2 x );
(4) (since C++11)

Computes the arc tangent of y/x using the signs of arguments to determine the correct quadrant.

4) A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by 1-3). If any argument has integral type, it is cast to double. If any argument is long double, then the return type Promoted is also long double, otherwise the return type is always double.

Contents

[edit] Parameters

x, y - values of floating-point or integral types

[edit] Return value

If no errors occur, the arc tangent of y/x (arctan(
y
x
)
) in the range [-π ; +π] radians, is returned.
Y argument
Return value
math-atan2.png
X argument

If a domain error occurs, an implementation-defined value is returned (NaN where supported)

If a range error occurs due to underflow, the correct result (after rounding) is returned.

[edit] Error handling

Errors are reported as specified in math_errhandling

Domain error may occur if x and y are both zero.

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

  • If x and y are both zero, domain error does not occur
  • If x and y are both zero, range error does not occur either
  • If y is zero, pole error does not occur
  • If y is ±0 and x is negative or -0, ±π is returned
  • If y is ±0 and x is positive or +0, ±0 is returned
  • If y is ±∞ and x is finite, ±π/2 is returned
  • If y is ±∞ and x is -∞, ±3π/4 is returned
  • If y is ±∞ and x is +∞, ±π/4 is returned
  • If x is ±0 and y is negative, -π/2 is returned
  • If x is ±0 and y is positive, +π/2 is returned
  • If x is -∞ and y is finite and positive, is returned
  • If x is -∞ and y is finite and negative, is returned
  • If x is +∞ and y is finite and positive, +0 is returned
  • If x is +∞ and y is finite and negative, -0 is returned
  • If either x is NaN or y is NaN, NaN is returned

[edit] Notes

std::atan2(y, x) is equivalent to std::arg(std::complex<double>(x,y))

POSIX specifies that in case of underflow, y/x is the value returned, and if that is not supported, and implementation-defined value no greater than DBL_MIN, FLT_MIN, and LDBL_MIN is returned.

[edit] Example

#include <iostream>
#include <cmath>
 
int main()
{
    // normal usage: the signs of the two arguments determine the quadrant
    std::cout << "(+1,+1) cartesian is (" << hypot(1,1)
              << ',' << atan2(1,1) << ") polar\n"  // atan2(1,1) = +pi/4, Quad I
              << "(+1,-1) cartesian is (" << hypot(1,-1)
              << ',' << atan2(1,-1) << ") polar\n" // atan2(1, -1) = +3pi/4, Quad II
              << "(-1,-1) cartesian is (" << hypot(-1,-1)
              << ',' << atan2(-1,-1) << ") polar\n" // atan2(-1,-1) = -3pi/4, Quad III
              << "(-1,1) cartesian is (" << hypot(-1,1)
              << ',' << atan2(-1,1) << ") polar\n"; // atan2(-1,-1) = -pi/4, Quad IV
    // special values
    std::cout << "atan2(0, 0) = " << atan2(0,0)
              << " atan2(0,-0) = " << atan2(0,-0.0) << '\n'
              << "atan2(7, 0) = " << atan2(7,0)
              << " atan2(7,-0) = " << atan2(7,-0.0) << '\n';
}

Output:

(+1,+1) cartesian is (1.41421,0.785398) polar
(+1,-1) cartesian is (1.41421,2.35619) polar
(-1,-1) cartesian is (1.41421,-2.35619) polar
(-1,1) cartesian is (1.41421,-0.785398) polar
atan2(0, 0) = 0 atan2(0,-0) = 3.14159
atan2(7, 0) = 1.5708 atan2(7,-0) = 1.5708

[edit] See also

computes arc sine (arcsin(x))
(function) [edit]
computes arc cosine (arccos(x))
(function) [edit]
computes arc tangent (arctan(x))
(function) [edit]
returns the phase angle
(function template) [edit]
applies the function std::atan2 to a valarray and a value
(function template) [edit]
C documentation for atan2