std::atan2, std::atan2f, std::atan2l
| Defined in header <cmath>
|
||
| (1) | ||
| float atan2 ( float y, float x ); |
||
| float atan2f( float y, float x ); |
(since C++11) | |
| double atan2 ( double y, double x ); |
(2) | |
| (3) | ||
| long double atan2 ( long double y, long double x ); |
||
| long double atan2l( long double y, long double x ); |
(since C++11) | |
| Promoted atan2 ( Arithmetic1 y, Arithmetic2 x ); |
(4) | (since C++11) |
y/x using the signs of arguments to determine the correct quadrant.Promoted is also long double, otherwise the return type is always double.Contents |
[edit] Parameters
| x, y | - | values of floating-point or integral types |
[edit] Return value
If no errors occur, the arc tangent ofy/x (arctan(| y |
| x |
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
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, NaN is returned
[edit] Notes
std::atan2(y, x) is equivalent to std::arg(std::complex<double>(x,y))
POSIX specifies that in case of underflow, 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.
[edit] Example
#include <iostream> #include <cmath> int main() { // normal usage: the signs of the two arguments determine the quadrant std::cout << "(x:+1,y:+1) cartesian is (r:" << hypot(1,1) << ",phi:" << atan2(1,1) << ") polar\n" // atan2(1,1) = +pi/4, Quad I << "(x:-1,y:+1) cartesian is (r:" << hypot(1,-1) << ",phi:" << atan2(1,-1) << ") polar\n" // atan2(1, -1) = +3pi/4, Quad II << "(x:-1,y:-1) cartesian is (r:" << hypot(-1,-1) << ",phi:" << atan2(-1,-1) << ") polar\n" // atan2(-1,-1) = -3pi/4, Quad III << "(x:+1,y:-1) cartesian is (r:" << hypot(-1,1) << ",phi:" << atan2(-1,1) << ") polar\n"; // atan2(-1, 1) = -pi/4, Quad IV // special values std::cout << "atan2(0, 0) = " << atan2(0,0) << " atan2(0,-0) = " << atan2(0,-0.0) << '\n' << "atan2(7, 0) = " << atan2(7,0) << " 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) |
| (C++11)(C++11) |
computes arc cosine (arccos(x)) (function) |
| (C++11)(C++11) |
computes arc tangent (arctan(x)) (function) |
| returns the phase angle (function template) | |
| applies the function std::atan2 to a valarray and a value (function template) | |
| C documentation for atan2
| |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
