Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/math/atan2"

From cppreference.com
< cpp‎ | numeric‎ | math
(-f and -l variants)
m (., fmt)
 
(12 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 
{{cpp/title|atan2|atan2f|atan2l}}
 
{{cpp/title|atan2|atan2f|atan2l}}
 
{{cpp/numeric/math/navbar}}
 
{{cpp/numeric/math/navbar}}
{{dcl begin}}
+
{{cpp/numeric/math/declarations
{{dcl header | cmath}}
+
|family=atan2
{{dcl rev multi | num = 1
+
|param1=y
| until1= | dcl1=
+
|param2=x
float      atan2 ( float y, float x );
+
|constexpr_since=26
| since2=c++11 | dcl2=
+
|desc=Computes the arc tangent of {{c|y / x}} using the signs of arguments to determine the correct quadrant.
float      atan2f( float y, float x );
+
 
}}
 
}}
{{dcl | num = 2 |
 
double      atan2 ( double y, double x );
 
}}
 
{{dcl rev multi | num = 3
 
| until1= | dcl1=
 
long double atan2 ( long double y, long double x );
 
| since2=c++11 | dcl2=
 
long double atan2l( long double y, long double x );
 
}}
 
{{dcl | num = 4 | since=c++11 |
 
Promoted    atan2 ( Arithmetic1 y, Arithmetic2 x );
 
}}
 
{{dcl end}}
 
 
@1-3@ Computes the arc tangent of {{tt|y/x}} using the signs of arguments to determine the correct quadrant.
 
@4@ A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by 1-3). If any argument has [[cpp/types/is_integral|integral type]], it is cast to {{c|double}}. If any argument is {{c|long double}}, then the return type {{tt|Promoted}} is also {{c|long double}}, otherwise the return type is always {{c|double}}.
 
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | x, y | values of floating-point or [[cpp/types/is_integral|integral types]]}}
+
{{par|y, x|floating-point or integer values}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
If no errors occur, the arc tangent of {{tt|y/x}} ({{math|arctan({{mfrac|y|x}})}}) in the range {{math|(-&pi; , +&pi;]}} radians, is returned.
+
If no errors occur, the arc tangent of {{c|y / x}} ({{math|arctan({{mfrac|y|x}})}}) in the range {{math|[-&pi;, +&pi;]}} radians, is returned.
  
{{plot | math-atan2.png | left=Y argument | bottom=X argument | right=Return value | size=x240px}}
+
{{plot|math-atan2.png|left={{c|y}} argument|bottom={{c|x}} argument|right=Return value|size=x240px}}
  
If a domain error occurs, an implementation-defined value is returned (NaN where supported)
+
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.
 
If a range error occurs due to underflow, the correct result (after rounding) is returned.
Line 43: Line 26:
 
Errors are reported as specified in {{lc|math_errhandling}}.
 
Errors are reported as specified in {{lc|math_errhandling}}.
  
Domain error may occur if {{tt|x}} and {{tt|y}} are both zero.
+
Domain error may occur if {{c|x}} and {{c|y}} are both zero.
  
 
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
 
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
* If {{tt|x}} and {{tt|y}} are both zero, domain error ''does not'' occur
+
* If {{c|x}} and {{c|y}} are both zero, domain error ''does not'' occur.
* If {{tt|x}} and {{tt|y}} are both zero, range error does not occur either
+
* If {{c|x}} and {{c|y}} are both zero, range error does not occur either.
* If {{tt|y}} is zero, pole error does not occur
+
* If {{c|y}} is zero, pole error does not occur.
* If {{tt|y}} is {{tt|±0}} and {{tt|x}} is negative or {{tt|-0}}, {{tt|±&pi;}} is returned
+
* If {{c|y}} is ±0 and {{c|x}} is negative or -0, ±&pi; is returned.
* If {{tt|y}} is {{tt|±0}} and {{tt|x}} is positive or {{tt|+0}}, {{tt|±0}} is returned
+
* If {{c|y}} is ±0 and {{c|x}} is positive or +0, ±0 is returned.
* If {{tt|y}} is {{tt|±∞}} and {{tt|x}} is finite, {{tt|±&pi;/2}} is returned
+
* If {{c|y}} is ±∞ and {{c|x}} is finite, ±&pi;/2 is returned.
* If {{tt|y}} is {{tt|±∞}} and {{tt|x}} is {{tt|-∞}}, {{tt|±3&pi;/4}} is returned
+
* If {{c|y}} is ±∞ and {{c|x}} is -∞, ±3&pi;/4 is returned.
* If {{tt|y}} is {{tt|±∞}} and {{tt|x}} is {{tt|+∞}}, {{tt|±&pi;/4}} is returned
+
* If {{c|y}} is ±∞ and {{c|x}} is +∞, ±&pi;/4 is returned.
* If {{tt|x}} is {{tt|±0}} and {{tt|y}} is negative, {{tt|-&pi;/2}} is returned
+
* If {{c|x}} is ±0 and {{c|y}} is negative, -&pi;/2 is returned.
* If {{tt|x}} is {{tt|±0}} and {{tt|y}} is positive, {{tt|+&pi;/2}} is returned
+
* If {{c|x}} is ±0 and {{c|y}} is positive, +&pi;/2 is returned.
* If {{tt|x}} is {{tt|-∞}} and {{tt|y}} is finite and positive, {{tt|+&pi;}} is returned
+
* If {{c|x}} is -∞ and {{c|y}} is finite and positive, +&pi; is returned.
* If {{tt|x}} is {{tt|-∞}} and {{tt|y}} is finite and negative, {{tt|-&pi;}} is returned
+
* If {{c|x}} is -∞ and {{c|y}} is finite and negative, -&pi; is returned.
* If {{tt|x}} is {{tt|+∞}} and {{tt|y}} is finite and positive, {{tt|+0}} is returned
+
* If {{c|x}} is +∞ and {{c|y}} is finite and positive, +0 is returned.
* If {{tt|x}} is {{tt|+∞}} and {{tt|y}} is finite and negative, {{tt|-0}} is returned
+
* If {{c|x}} is +∞ and {{c|y}} is finite and negative, -0 is returned.
* If either {{tt|x}} is NaN or {{tt|y}} is NaN, NaN is returned
+
* If either {{c|x}} is NaN or {{c|y}} is NaN, NaN is returned.
  
 
===Notes===
 
===Notes===
{{c|std::atan2(y, x)}} is equivalent to {{c|std::arg(std::complex<double>(x,y))}}
+
{{c|std::atan2(y, x)}} is equivalent to {{c|std::arg(std::complex<std::common_type_t<decltype(x), decltype(y)>>(x, y))}}.
  
[http://pubs.opengroup.org/onlinepubs/9699919799/functions/atan2.html POSIX specifies] that in case of underflow, {{tt|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.
+
[https://pubs.opengroup.org/onlinepubs/9699919799/functions/atan2.html POSIX specifies] that in case of underflow, the value {{c|y / x}} is returned, and if that is not supported, an implementation-defined value no greater than {{lc|DBL_MIN}}, {{lc|FLT_MIN}}, and {{lc|LDBL_MIN}} is returned.
 +
 
 +
{{cpp/numeric/math/additional overload note|atan2}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <iostream>
+
 
#include <cmath>
 
#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()
 
int main()
 
{
 
{
 
     // normal usage: the signs of the two arguments determine the quadrant
 
     // normal usage: the signs of the two arguments determine the quadrant
     std::cout << "(+1,+1) cartesian is (" << hypot(1,1)
+
     print_coordinates(+1, +1); // atan2( 1, 1) = +pi/4, Quad I
              << ',' << atan2(1,1) << ") polar\n"  // atan2(1,1) = +pi/4, Quad I
+
    print_coordinates(-1, +1); // atan2( 1, -1) = +3pi/4, Quad II
              << "(+1,-1) cartesian is (" << hypot(1,-1)
+
    print_coordinates(-1, -1); // atan2(-1, -1) = -3pi/4, Quad III
              << ',' << atan2(1,-1) << ") polar\n" // atan2(1, -1) = +3pi/4, Quad II
+
    print_coordinates(+1, -1); // atan2(-1, 1) = -pi/4, Quad IV
              << "(-1,-1) cartesian is (" << hypot(-1,-1)
+
   
              << ',' << atan2(-1,-1) << ") polar\n" // atan2(-1,-1) = -3pi/4, Quad III
+
              << "(-1,+1) cartesian is (" << hypot(-1,1)
+
              << ',' << atan2(-1,1) << ") polar\n"; // atan2(-1,-1) = -pi/4, Quad IV
+
 
     // special values
 
     // special values
     std::cout << "atan2(0, 0) = " << atan2(0,0)
+
     std::cout << std::noshowpos
               << " atan2(0,-0) = " << atan2(0,-0.0) << '\n'
+
              << "atan2(0, 0) = " << atan2(0, 0) << '\n'
               << "atan2(7, 0) = " << atan2(7,0)
+
               << "atan2(0,-0) = " << atan2(0, -0.0) << '\n'
               << " atan2(7,-0) = " << atan2(7,-0.0) << '\n';
+
               << "atan2(7, 0) = " << atan2(7, 0) << '\n'
 +
               << "atan2(7,-0) = " << atan2(7, -0.0) << '\n';
 
}
 
}
| output=
+
|output=
(+1,+1) cartesian is (1.41421,0.785398) polar
+
(x:+1, y:+1) cartesian is (r:1.41421, phi:0.785398) polar
(+1,-1) cartesian is (1.41421,2.35619) polar
+
(x:-1, y:+1) cartesian is (r:1.41421, phi:2.35619) polar
(-1,-1) cartesian is (1.41421,-2.35619) polar
+
(x:-1, y:-1) cartesian is (r:1.41421, phi:-2.35619) polar
(-1,+1) cartesian is (1.41421,-0.785398) polar
+
(x:+1, y:-1) cartesian is (r:1.41421, phi:-0.785398) polar
atan2(0, 0) = 0 atan2(0,-0) = 3.14159
+
atan2(0, 0) = 0
atan2(7, 0) = 1.5708 atan2(7,-0) = 1.5708
+
atan2(0,-0) = 3.14159
 +
atan2(7, 0) = 1.5708
 +
atan2(7,-0) = 1.5708
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/math/dsc asin}}
+
{{dsc inc|cpp/numeric/math/dsc asin}}
{{dsc inc | cpp/numeric/math/dsc acos}}
+
{{dsc inc|cpp/numeric/math/dsc acos}}
{{dsc inc | cpp/numeric/math/dsc atan}}
+
{{dsc inc|cpp/numeric/math/dsc atan}}
{{dsc inc | cpp/numeric/complex/dsc arg}}
+
{{dsc inc|cpp/numeric/complex/dsc arg}}
{{dsc inc | cpp/numeric/valarray/dsc atan2}}
+
{{dsc inc|cpp/numeric/valarray/dsc atan2}}
{{dsc see c | c/numeric/math/atan2}}
+
{{dsc see c|c/numeric/math/atan2}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}

Latest revision as of 09:46, 15 October 2023

 
 
 
 
Defined in header <cmath>
(1)
float       atan2 ( float y, float x );

double      atan2 ( double y, double x );

long double atan2 ( long double y, long double 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)
long double atan2l( long double y, long double x );
(3) (since C++11)
(constexpr since C++26)
Additional overloads (since C++11)
Defined in header <cmath>
template< class Integer >
double      atan2 ( Integer y, Integer x );
(A) (constexpr since C++26)
1-3) Computes the arc tangent of y / x using the signs of arguments to determine the correct quadrant. The library provides overloads of std::atan2 for all cv-unqualified floating-point types as the type of the parameters.(since C++23)
A) Additional overloads are provided for all integer types, which are treated as double.
(since C++11)

Contents

[edit] Parameters

y, x - floating-point or integer values

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

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

POSIX specifies that in case of underflow, the value y / x is returned, and if that is not supported, an implementation-defined value no greater than DBL_MIN, FLT_MIN, and LDBL_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 argument num1 and second argument num2:

  • If num1 or num2 has type long double, then std::atan2(num1, num2) has the same effect as std::atan2(static_cast<long double>(num1),
               static_cast<long double>(num2))
    .
  • Otherwise, if num1 and/or num2 has type double or an integer type, then std::atan2(num1, num2) has the same effect as std::atan2(static_cast<double>(num1),
               static_cast<double>(num2))
    .
  • Otherwise, if num1 or num2 has type float, then std::atan2(num1, num2) has the same effect as std::atan2(static_cast<float>(num1),
               static_cast<float>(num2))
    .
(until C++23)

If num1 and num2 have arithmetic types, then std::atan2(num1, num2) has the same effect as std::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 greatest floating-point conversion rank and greatest floating-point conversion subrank between the types of num1 and num2, arguments of integer type are considered to have the same floating-point conversion rank as double.

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

(since C++23)

[edit] Example

#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 values
    std::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) polar
atan2(0, 0) = 0
atan2(0,-0) = 3.14159
atan2(7, 0) = 1.5708
atan2(7,-0) = 1.5708

[edit] See also

(C++11)(C++11)
computes arc sine (arcsin(x))
(function) [edit]
(C++11)(C++11)
computes arc cosine (arccos(x))
(function) [edit]
(C++11)(C++11)
computes arc tangent (arctan(x))
(function) [edit]
returns the phase angle
(function template) [edit]
applies the function std::atan2 to a valarray and a value
(function template) [edit]
C documentation for atan2