Namespaces
Variants

atan2, atan2f, atan2l

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
Definido en el encabezado <math.h>
float atan2f ( float y, float x ) ;
(1) (desde C99)
double atan2 ( double y, double x ) ;
(2)
long double atan2l ( long double y, long double x ) ;
(3) (desde C99)
_Decimal32  atan2d32 ( _Decimal32 y, _Decimal32 x ) ;
(4) (desde C23)
_Decimal64  atan2d64 ( _Decimal64 y, _Decimal64 x ) ;
(5) (desde C23)
_Decimal128 atan2d128 ( _Decimal128 y, _Decimal128 x ) ;
(6) (desde C23)
Definido en el encabezado <tgmath.h>
#define atan2( y, x )
(7) (desde C99)
1-6) Calcula el arcotangente de y / x utilizando los signos de los argumentos para determinar el cuadrante correcto.
7) Macro genérico de tipos: Si algún argumento tiene tipo long double , (3) ( atan2l ) es llamado. De lo contrario, si algún argumento tiene tipo entero o tiene tipo double , (2) ( atan2 ) es llamado. De lo contrario, (1) ( atan2f ) es llamado.

Las funciones (4-6) se declaran si y solo si la implementación predefine __STDC_IEC_60559_DFP__ (es decir, la implementación admite números de punto flotante decimales).

(desde C23)

Contenidos

Parámetros

x, y - valor de punto flotante

Valor de retorno

If no errors occur, the arc tangent of y / x ( arctan(
y
x
)
) in the range [-π ; +π] radians, is returned.
Argumento Y
Valor de retorno
Argumento X

Si ocurre un error de dominio, se devuelve un valor definido por la implementación.

Si ocurre un error de rango debido a desbordamiento inferior, se devuelve el resultado correcto (después del redondeo).

Manejo de errores

Los errores se reportan como se especifica en math_errhandling .

Puede ocurrir un error de dominio si x y y son ambos cero.

Si la implementación soporta aritmética de punto flotante IEEE (IEC 60559):

  • Si x e y son ambos cero, el error de dominio no ocurre;
  • Si x e y son ambos cero, el error de rango tampoco ocurre;
  • Si y es cero, el error de polo no ocurre;
  • Si y es ±0 y x es negativo o -0 , ±π se devuelve;
  • Si y es ±0 y x es positivo o +0 , ±0 se devuelve;
  • Si y es ±∞ y x es finito, ±π/2 se devuelve;
  • Si y es ±∞ y x es -∞ , ±3π/4 se devuelve;
  • Si y es ±∞ y x es +∞ , ±π/4 se devuelve;
  • Si x es ±0 y y es negativo, -π/2 se devuelve;
  • Si x es ±0 y y es positivo, +π/2 se devuelve;
  • Si x es -∞ y y es finito y positivo, se devuelve;
  • Si x es -∞ y y es finito y negativo, se devuelve;
  • Si x es +∞ y y es finito y positivo, +0 se devuelve;
  • Si x es +∞ y y es finito y negativo, -0 se devuelve;
  • Si x es NaN o y es NaN, se devuelve NaN.

Notas

atan2 ( y, x ) es equivalente a carg ( x + I * y ) .

POSIX especifica que en caso de subdesbordamiento, y / x es el valor devuelto, y si eso no es compatible, se devuelve un valor definido por la implementación no mayor que DBL_MIN , FLT_MIN , y LDBL_MIN .

Ejemplo

#include <math.h>
#include <stdio.h>
int main(void)
{
    // uso normal: los signos de los dos argumentos determinan el cuadrante
    // atan2(1,1) = +pi/4, Cuadrante I
    printf("(+1,+1) cartesian is (%f,%f) polar\n", hypot( 1, 1), atan2( 1, 1));
    // atan2(1, -1) = +3pi/4, Cuadrante II
    printf("(+1,-1) cartesian is (%f,%f) polar\n", hypot( 1,-1), atan2( 1,-1));
    // atan2(-1,-1) = -3pi/4, Cuadrante III
    printf("(-1,-1) cartesian is (%f,%f) polar\n", hypot(-1,-1), atan2(-1,-1));
    // atan2(-1,-1) = -pi/4, Cuadrante IV
    printf("(-1,+1) cartesian is (%f,%f) polar\n", hypot(-1, 1), atan2(-1, 1));
    // valores especiales
    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));
}

Salida:

(+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

Referencias

  • Estándar C23 (ISO/IEC 9899:2024):
  • 7.12.4.4 Las funciones atan2 (p: TBD)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: TBD)
  • F.10.1.4 Las funciones atan2 (p: TBD)
  • Estándar C17 (ISO/IEC 9899:2018):
  • 7.12.4.4 Las funciones atan2 (p: 174)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: 272-273)
  • F.10.1.4 Las funciones atan2 (p: 378)
  • Estándar C11 (ISO/IEC 9899:2011):
  • 7.12.4.4 Las funciones atan2 (p: 239)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: 373-375)
  • F.10.1.4 Las funciones atan2 (p: 519)
  • Estándar C99 (ISO/IEC 9899:1999):
  • 7.12.4.4 Las funciones atan2 (p: 219)
  • 7.22 Matemáticas genéricas de tipo <tgmath.h> (p: 335-337)
  • F.9.1.4 Las funciones atan2 (p: 456)
  • Estándar C89/C90 (ISO/IEC 9899:1990):
  • 4.5.2.4 La función atan2

Véase también

(C99) (C99)
calcula el arco seno ( arcsin(x) )
(función)
(C99) (C99)
calcula el arco coseno ( arccos(x) )
(función)
(C99) (C99)
calcula el arco tangente ( arctan(x) )
(función)
(C99) (C99) (C99)
calcula el ángulo de fase de un número complejo
(función)