Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::atan2,std::atan2f,std::atan2l

      From cppreference.com
      <cpp‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      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)
      Classification and comparison
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Types
      (C++11)
      (C++11)
      (C++11)
      Macro constants
       
      Defined in header<cmath>
      (1)
      float       atan2(float y,float x);

      double      atan2(double y,double x);

      longdouble atan2(longdouble y,longdouble x);
      (until C++23)
      /*floating-point-type*/

                  atan2(/*floating-point-type*/ y,

                         /*floating-point-type*/ x);
      (since C++23)
      (constexpr since C++26)
      float       atan2f(float y,float x);
      (2)(since C++11)
      (constexpr since C++26)
      longdouble atan2l(longdouble y,longdouble x);
      (3)(since C++11)
      (constexpr since C++26)
      SIMD overload(since C++26)
      Defined in header<simd>
      template<class V0,class V1>

      constexpr/*math-common-simd-t*/<V0, V1>

                  atan2(const V0& v_y,const V1& v_x);
      (S)(since C++26)
      Defined in header<cmath>
      template<class Integer>
      double      atan2( Integer y, Integer x);
      (A)(constexpr since C++26)
      1-3) Computes the arc tangent ofy/ x using the signs of arguments to determine the correct quadrant. The library provides overloads ofstd::atan2 for all cv-unqualified floating-point types as the type of the parameters.(since C++23)
      S) The SIMD overload performs an element-wisestd::atan2 onv_yandv_x.
      (Seemath-common-simd-t for its definition.)
      (since C++26)
      A) Additional overloads are provided for all integer types, which are treated asdouble.
      (since C++11)

      Contents

      [edit]Parameters

      y, x - floating-point or integer values

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

      std::atan2(y, x) is equivalent tostd::arg(std::complex<std::common_type_t<decltype(x), decltype(y)>>(x, y)).

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

      The additional overloads are not required to be provided exactly as(A). They only need to be sufficient to ensure that for their first argumentnum1 and second argumentnum2:

      • Ifnum1 ornum2 has typelongdouble, thenstd::atan2(num1, num2) has the same effect asstd::atan2(static_cast<longdouble>(num1),
                   static_cast<longdouble>(num2))
        .
      • Otherwise, ifnum1 and/ornum2 has typedouble or an integer type, thenstd::atan2(num1, num2) has the same effect asstd::atan2(static_cast<double>(num1),
                   static_cast<double>(num2))
        .
      • Otherwise, ifnum1 ornum2 has typefloat, thenstd::atan2(num1, num2) has the same effect asstd::atan2(static_cast<float>(num1),
                   static_cast<float>(num2))
        .
      (until C++23)

      Ifnum1 andnum2 have arithmetic types, thenstd::atan2(num1, num2) has the same effect asstd::atan2(static_cast</*common-floating-point-type*/>(num1),
                 static_cast</*common-floating-point-type*/>(num2))
      , where/*common-floating-point-type*/ is the floating-point type with the greatestfloating-point conversion rank and greatestfloating-point conversion subrank between the types ofnum1 andnum2, arguments of integer type are considered to have the same floating-point conversion rank asdouble.

      If no such floating-point type with the greatest rank and subrank exists, thenoverload resolution does not result in a usable candidate from the overloads provided.

      (since C++23)

      [edit]Example

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

      Output:

      (x:+1, y:+1) cartesian is (r:1.41421, phi:0.785398) polar(x:-1, y:+1) cartesian is (r:1.41421, phi:2.35619) polar(x:-1, y:-1) cartesian is (r:1.41421, phi:-2.35619) polar(x:+1, y:-1) cartesian is (r:1.41421, phi:-0.785398) polaratan2(0, 0) = 0atan2(0,-0) = 3.14159atan2(7, 0) = 1.5708atan2(7,-0) = 1.5708

      [edit]See also

      (C++11)(C++11)
      computes arc sine (\({\small\arcsin{x}}\)arcsin(x))
      (function)[edit]
      (C++11)(C++11)
      computes arc cosine (\({\small\arccos{x}}\)arccos(x))
      (function)[edit]
      (C++11)(C++11)
      computes arc tangent (\({\small\arctan{x}}\)arctan(x))
      (function)[edit]
      returns the phase angle
      (function template)[edit]
      applies the functionstd::atan2 to a valarray and a value
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/atan2&oldid=160746"

      [8]ページ先頭

      ©2009-2025 Movatter.jp