Namespaces
Variants
Views
Actions

atan2

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(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)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       atan2f( float y, float x );
(since C99)
double      atan2( double y, double x );
(since C99)
long double atan2l( long double y, long double x );
(since C99)

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

Contents

[edit] Parameters

x, y - floating point value

[edit] Return value

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

atan2(+0, -0) return . atan2(-0, -0) return

atan2(+0, +0) return +0. atan2(-0, +0) return -0

atan2(+0, x) return if x<0 and +0 if x>0.

atan2(-0, x) return if x<0 and -0 if x>0.

atan2(y, 0) return -π/2 if y<0 and +π/2 if y>0.

atan2(y, -) returns if y>0 and if y<0

atan2(y, +) returns +0 if y>0 and -0 if y<0

atan2(+∞, x) returns +π/2. atan2(-∞, x) returns -π/2.

atan2(+∞, -) returns +3π/4. atan2(-∞, -) returns -3π/4.

atan2(+∞, +) returns +π/4. atan2(-∞, +) returns -π/4.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <string.h>
#define PI acos(-1)
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    double r2d = 180.0/(double)PI;   /* convert radians to degrees */
 
    /* The signs of the two arguments determine the quadrant.                        */
    /*    positive angle - counter clockwise rotation                   Angle   Quad */
    /*    negative angle - clockwise rotation                           -----   ---- */
    printf("atan2(+1.0,+1.0)*r2d = %+4.1f\n", atan2(+1.0,+1.0)*r2d); /*   +45      I */
    printf("atan2(+1.0,-1.0)*r2d = %+4.1f\n", atan2(+1.0,-1.0)*r2d); /*  +135     II */
    printf("atan2(-1.0,-1.0)*r2d = %+4.1f\n", atan2(-1.0,-1.0)*r2d); /*  -135    III */
    printf("atan2(-1.0,+1.0)*r2d = %+4.1f\n", atan2(-1.0,+1.0)*r2d); /*   -45     IV */ 
    printf("\n");
 
    /* both arguments zero: no floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("atan2(0.0,0.0)       = %f\n", atan2(0.0,0.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* x==0: no floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("atan2(1.0,0.0)       = %f\n", atan2(1.0,0.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

atan2(+1.0,+1.0)*r2d = +45.0
atan2(+1.0,-1.0)*r2d = +135.0
atan2(-1.0,-1.0)*r2d = -135.0
atan2(-1.0,+1.0)*r2d = -45.0
 
atan2(0.0,0.0)       = 0.000000
Success
current exceptions raised:  none
 
atan2(1.0,0.0)       = 1.570796
Success
current exceptions raised:  none

[edit] See also

computes arc tangent (arctan(x))
(function) [edit]
computes arc sine (arcsin(x))
(function) [edit]
computes arc cosine (arccos(x))
(function) [edit]
computes tangent (tan(x))
(function) [edit]