std::atan2,...std::atan2l(3)
π NAME π
std::atan2, std::atan2f, std::atan2l β like, that's the whole name, bestie, it's giving "the function is the title". This page lives in the C++ Standard Library manual, section 3. Periodt.
β¨ Synopsis β¨
okay so first things first, it's all chillin' in this header:
Defined in header <cmath>
and here's the whole roster of overloads, no cap:
float atan2 ( float y, float x );
double atan2 ( double y, double x ); (until C++23)
long double atan2 ( long double y, long double x );
/* floating-point-type */
atan2 ( /* floating-point-type */ y,
/* floating-point-type */ x ); (1) (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 > (A)
double atan2 ( Integer y, Integer x ); (constexpr since C++26)
and here's what the numbers mean, gorl:
1-3)β Computes the arc tangent ofy / xusing the signs of the arguments to determine the correct quadrant. Yeah, the signs β they're not just for vibes, they're load-bearing π₯- The library provides overloads of
std::atan2for 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 asdouble. (since C++11)
π Parameters π
y,xβ floating-point or integer values π«Ά
πΈ Return value πΈ
If nothing goes wrong (and honestly? icon behaviour), the arc tangent of y / x β that's arctan(y / x) β is returned, in the range [-Ο, +Ο] radians. Range so cute it could be a Spotify wrapped.
There's a diagram, math-atan2.png, with the axes labelled y argument, Return value, and x argument β very main character, very helpful β¨
If a domain error occurs, an implementation-defined value is returned (NaN where supported). Not the slay we wanted but it's what we got π
If a range error occurs due to underflow, the correct result (after rounding) is returned. we love a recovery arc π
π¦ Error handling π¦
Errors are reported as specified in math_errhandling. yes, the whole system.
A domain error may occur if x and y are both zero. May. It's giving "it depends" π
If the implementation supports IEEE floating-point arithmetic (IEC 60559), then boom, receipts:
- 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Β±0andxis negative or-0,Β±Οis returned. - If
yisΒ±0andxis positive or+0,Β±0is returned. - If
yisΒ±βandxis finite,Β±Ο/2is returned. - If
yisΒ±βandxis-β,Β±3Ο/4is returned. - If
yisΒ±βandxis+β,Β±Ο/4is returned. - If
xisΒ±0andyis negative,-Ο/2is returned. - If
xisΒ±0andyis positive,+Ο/2is returned. - If
xis-βandyis finite and positive,+Οis returned. - If
xis-βandyis finite and negative,-Οis returned. - If
xis+βandyis finite and positive,+0is returned. - If
xis+βandyis finite and negative,-0is returned. - If either
xis NaN oryis NaN,NaNis returned.
not a single one of those escaped. we keep them all. ate. π
π Notes π
std::atan2(y, x) is equivalent to this absolute mouthful:
std::arg(std::complex<std::common_type_t<decltype(x), decltype(y)>>(x, y))
like okay math girl, we see you π
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. so small. so tiny. so demure.
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
num1ornum2has typelong double, thenstd::atan2(num1, num2)has the same effect asstd::atan2(static_cast<long double>(num1), static_cast<long double>(num2)). - Otherwise, if
num1and/ornum2has typedoubleor an integer type, thenstd::atan2(num1, num2)has the same effect asstd::atan2(static_cast<double>(num1), static_cast<double>(num2)). (until C++23) - Otherwise, if
num1ornum2has typefloat, thenstd::atan2(num1, num2)has the same effect asstd::atan2(static_cast<float>(num1), static_cast<float>(num2)).
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. (since C++23)
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. π that's the "no thoughts, head empty, no candidate" ending. tragic. fr fr.
π₯ Example π₯
go off, 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 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
look at those quadrants. Quad I through IV. all four siblings showed up. iconic. π¦
π See also π
asin,asinf,asinlβ computes arc sine (arcsin(x)) (function) (C++11) (C++11)acos,acosf,acoslβ computes arc cosine (arccos(x)) (function) (C++11) (C++11)atan,atanf,atanlβ computes arc tangent (arctan(x)) (function) (C++11) (C++11)argβ returns the phase angle (function template)atan2(std::valarray)β applies the functionstd::atan2to avalarrayand a value (function template)- C documentation for
atan2β for when you wanna hang with the C girlies too
okay that's the whole page, bestie, you're so smart now π this masterpiece came to you from http://cppreference.com, version 2024.06.10 β gatekeep it, don't β go forth and compute your quadrants πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms