atan2, atan2f, atan2l

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       atan2f( float y, float x );
(1) (since C99)
double      atan2( double y, double x );
(2)
long double atan2l( long double y, long double x );
(3) (since C99)
Defined in header <tgmath.h>
#define atan2( arg )
(4) (since C99)
1-3) Computes the arc tangent of y/x using the signs of arguments to determine the correct quadrant.
4) Type-generic macro: If the argument has type long double, atan2l is called. Otherwise, if the argument has integer type or the type double, atan2 is called. Otherwise, atan2f is called.

Contents

[edit] Parameters

x, y - floating point value

[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.

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 negative 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

atan2(y, x) is equivalent to carg(x + I*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 <stdio.h>
#include <math.h>
 
int main(void)
{
    // normal usage: the signs of the two arguments determine the quadrant
    // atan2(1,1) = +pi/4, Quad I
    printf("(+1,+1) cartesian is (%f,%f) polar\n", hypot( 1, 1), atan2( 1, 1));
    // atan2(1, -1) = +3pi/4, Quad II
    printf("(+1,-1) cartesian is (%f,%f) polar\n", hypot( 1,-1), atan2( 1,-1));
    // atan2(-1,-1) = -3pi/4, Quad III
    printf("(-1,-1) cartesian is (%f,%f) polar\n", hypot(-1,-1), atan2(-1,-1));
    // atan2(-1,-1) = -pi/4, Quad IV
    printf("(-1,+1) cartesian is (%f,%f) polar\n", hypot(-1, 1), atan2(-1, 1));
 
    // special values
    printf("atan2(0, 0) = %f atan2(0, -0)=%f\n", atan2(0,0), atan2(0,-0.0));
    printf("atan2(7, 0) = %f atan2(7, -0)=%f\n", atan2(7,0), atan2(7,-0.0));
}

Output:

(+1,+1) cartesian is (1.414214,0.785398) polar
(+1,-1) cartesian is (1.414214,2.356194) polar
(-1,-1) cartesian is (1.414214,-2.356194) polar
(-1,+1) cartesian is (1.414214,-0.785398) polar
atan2(0, 0) = 0.000000 atan2(0, -0)=3.141593
atan2(7, 0) = 1.570796 atan2(7, -0)=1.570796

[edit] See also

(C99)(C99)
computes arc sine (arcsin(x))
(function)
(C99)(C99)
computes arc cosine (arccos(x))
(function)
(C99)(C99)
computes arc tangent (arctan(x))
(function)
(C99)(C99)(C99)
computes the phase angle of a complex number
(function)
C++ documentation for atan2