Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      atan2, atan2f, atan2l

      From cppreference.com
      <c‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Functions
      Basic operations
      (C99)
      (C99)
      (C99)
      (C99)(C99)(C99)(C23)
      Maximum/minimum operations
      (C99)
      (C99)
      Exponential functions
      (C23)
      (C99)
      (C99)
      (C23)
      (C23)

      (C99)
      (C99)(C23)
      (C23)
      (C23)
      Power functions
      (C99)
      (C23)
      (C23)

      (C99)
      (C23)
      (C23)
      Trigonometric and hyperbolic functions
      (C23)
      (C23)
      (C23)
      (C23)
      (C99)
      (C99)
      (C99)
      Nearest integer floating-point
      (C99)(C99)(C99)
      (C23)(C23)(C23)(C23)
      Floating-point manipulation
      (C99)(C99)
      (C99)(C23)
      (C99)
      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
      (C99)(C23)
      Arguments and return values
      Error handling
      Fast operation indicators
       
      Defined in header<math.h>
      float       atan2f(float y,float x);
      (1)(since C99)
      double      atan2(double y,double x);
      (2)
      longdouble atan2l(longdouble y,longdouble x);
      (3)(since C99)
      _Decimal32  atan2d32( _Decimal32 y, _Decimal32 x);
      (4)(since C23)
      _Decimal64  atan2d64( _Decimal64 y, _Decimal64 x);
      (5)(since C23)
      _Decimal128 atan2d128( _Decimal128 y, _Decimal128 x);
      (6)(since C23)
      Defined in header<tgmath.h>
      #define atan2( y, x )
      (7)(since C99)
      1-6) Computes the arc tangent ofy/ x using the signs of arguments to determine the correct quadrant.
      7) Type-generic macro: If any argument has typelongdouble,(3) (atan2l) is called. Otherwise, if any argument has integer type or has typedouble,(2) (atan2) is called. Otherwise,(1) (atan2f) is called.

      The functions(4-6) are declared if and only if the implementation predefines__STDC_IEC_60559_DFP__ (i.e. the implementation supports decimal floating-point numbers).

      (since C23)

      Contents

      [edit]Parameters

      x, y - floating-point value

      [edit]Return value

      If no errors occur, the arc tangent ofy/ 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 inmath_errhandling.

      Domain error may occur ifx andy are both zero.

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

      • Ifx andy are both zero, domain errordoes not occur;
      • Ifx andy are both zero, range error does not occur either;
      • Ify is zero, pole error does not occur;
      • Ify is±0 andx is negative or-0,±π is returned;
      • Ify is±0 andx is positive or+0,±0 is returned;
      • Ify is±∞ andx is finite,±π/2 is returned;
      • Ify is±∞ andx is-∞,±3π/4 is returned;
      • Ify is±∞ andx is+∞,±π/4 is returned;
      • Ifx is±0 andy is negative,-π/2 is returned;
      • Ifx is±0 andy is positive,+π/2 is returned;
      • Ifx is-∞ andy is finite and positive, is returned;
      • Ifx is-∞ andy is finite and negative, is returned;
      • Ifx is+∞ andy is finite and positive,+0 is returned;
      • Ifx is+∞ andy is finite and negative,-0 is returned;
      • If eitherx is NaN ory is NaN, NaN is returned.

      [edit]Notes

      atan2(y, x) is equivalent tocarg(x+ I*y).

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

      [edit]Example

      Run this code
      #include <math.h>#include <stdio.h> int main(void){// normal usage: the signs of the two arguments determine the quadrant// atan2(1,1) = +pi/4, Quad Iprintf("(+1,+1) cartesian is (%f,%f) polar\n",hypot(1,1), atan2(1,1));// atan2(1, -1) = +3pi/4, Quad IIprintf("(+1,-1) cartesian is (%f,%f) polar\n",hypot(1,-1), atan2(1,-1));// atan2(-1,-1) = -3pi/4, Quad IIIprintf("(-1,-1) cartesian is (%f,%f) polar\n",hypot(-1,-1), atan2(-1,-1));// atan2(-1,-1) = -pi/4, Quad IVprintf("(-1,+1) cartesian is (%f,%f) polar\n",hypot(-1,1), atan2(-1,1)); // special valuesprintf("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) polaratan2(0, 0) = 0.000000 atan2(0, -0)=3.141593atan2(7, 0) = 1.570796 atan2(7, -0)=1.570796

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.12.4.4 The atan2 functions (p: TBD)
      • 7.25 Type-generic math <tgmath.h> (p: TBD)
      • F.10.1.4 The atan2 functions (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.12.4.4 The atan2 functions (p: 174)
      • 7.25 Type-generic math <tgmath.h> (p: 272-273)
      • F.10.1.4 The atan2 functions (p: 378)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.12.4.4 The atan2 functions (p: 239)
      • 7.25 Type-generic math <tgmath.h> (p: 373-375)
      • F.10.1.4 The atan2 functions (p: 519)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.12.4.4 The atan2 functions (p: 219)
      • 7.22 Type-generic math <tgmath.h> (p: 335-337)
      • F.9.1.4 The atan2 functions (p: 456)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.5.2.4 The atan2 function

      [edit]See also

      (C99)(C99)
      computes arc sine (\({\small\arcsin{x} }\)arcsin(x))
      (function)[edit]
      (C99)(C99)
      computes arc cosine (\({\small\arccos{x} }\)arccos(x))
      (function)[edit]
      (C99)(C99)
      computes arc tangent (\({\small\arctan{x} }\)arctan(x))
      (function)[edit]
      (C99)(C99)(C99)
      computes the phase angle of a complex number
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/atan2&oldid=150042"

      [8]ページ先頭

      ©2009-2025 Movatter.jp