std::atan2, std::atan2f, std::atan2l
| 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) |
| 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) |
| Additional overloads (since C++11) |
||
| Defined in header <cmath>
|
||
template< class Integer >
double atan2 ( Integer y, Integer x );
|
(A) | (constexpr since C++26) |
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)|
S) The SIMD overload performs an element-wise
std::atan2 on v_yand v_x.
|
(since C++26) |
|
A) Additional overloads are provided for all integer types, which are treated as
double. |
(since C++11) |
Parameters
| y, x | - | floating-point or integer values |
Return value
If no errors occur, the arc tangent of y / x (
| y |
| x |
) in the range [-π, +π] radians, is returned.
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.
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
xandyare both zero, domain error does not occur. - If
xandyare both zero, range error does not occur either. - If
yis zero, pole error does not occur. - If
yis ±0 andxis negative or -0, ±π is returned. - If
yis ±0 andxis positive or +0, ±0 is returned. - If
yis ±∞ andxis finite, ±π/2 is returned. - If
yis ±∞ andxis -∞, ±3π/4 is returned. - If
yis ±∞ andxis +∞, ±π/4 is returned. - If
xis ±0 andyis negative, -π/2 is returned. - If
xis ±0 andyis positive, +π/2 is returned. - If
xis -∞ andyis finite and positive, +π is returned. - If
xis -∞ andyis finite and negative, -π is returned. - If
xis +∞ andyis finite and positive, +0 is returned. - If
xis +∞ andyis finite and negative, -0 is returned. - If either
xis NaN oryis NaN, NaN is returned.
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:
|
(until C++23) |
|
If 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) |
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
