What is the difference between atan
and atan2
in C++?
11 Answers
From school mathematics we know that the tangent has the definition
tan(α) = sin(α) / cos(α)
and we differentiate between four quadrants based on the angle that we supply to the functions. The sign of the sin
, cos
and tan
have the following relationship (where we neglect the exact multiples of π/2
):
Quadrant Angle sin cos tan
-------------------------------------------------
I 0 < α < π/2 + + +
II π/2 < α < π + - -
III π < α < 3π/2 - - +
IV 3π/2 < α < 2π - + -
Given that the value of tan(α)
is positive, we cannot distinguish, whether the angle was from the first or third quadrant and if it is negative, it could come from the second or fourth quadrant. So by convention, atan()
returns an angle from the first or fourth quadrant (i.e. -π/2 <= atan() <= π/2
), regardless of the original input to the tangent.
In order to get back the full information, we must not use the result of the division sin(α) / cos(α)
but we have to look at the values of the sine and cosine separately. And this is what atan2()
does. It takes both, the sin(α)
and cos(α)
and resolves all four quadrants by adding π
to the result of atan()
whenever the cosine is negative.
Remark: The atan2(y, x)
function actually takes a y
and a x
argument, which is the projection of a vector with length v
and angle α
on the y- and x-axis, i.e.
y = v * sin(α)
x = v * cos(α)
which gives the relation
y/x = tan(α)
Conclusion:
atan(y/x)
holds back some information and one can only assume that the input came from quadrants I or IV. In contrast, atan2(y,x)
gets all the data and thus can resolve the correct angle.

- 125
- 1
- 12

- 8,208
- 2
- 26
- 38
-
7One small detail, the range `-π/2 <= atan() <= π/2` actually includes one point (`pi/2`) from quadrant II. – Z boson Jul 02 '15 at 11:30
std::atan2
allows calculating the arctangent of all four quadrants. std::atan
only allows calculating from quadrants 1 and 4.

- 15,395
- 32
- 113
- 196

- 219,335
- 46
- 382
- 435
The actual values are in radians but to interpret them in degrees it will be:
atan
= gives angle value between -90 and 90atan2
= gives angle value between -180 and 180
For my work which involves computation of various angles such as heading and bearing in navigation, atan2
in most cases does the job.

- 2,355
- 1
- 28
- 36
Another thing to mention is that atan2
is more stable when computing tangents using an expression like atan(y / x)
and x
is 0 or close to 0.

- 15,395
- 32
- 113
- 196

- 11,072
- 10
- 46
- 67
-
Interesting, do you have a source for this? Is this true in general or just for C++? – Gerard Jan 30 '14 at 23:38
atan(x) Returns the principal value of the arc tangent of x, expressed in radians.
atan2(y,x) Returns the principal value of the arc tangent of y/x, expressed in radians.
Notice that because of the sign ambiguity, a function cannot determine with certainty in which quadrant the angle falls only by its tangent value (atan alone). You can use atan2 if you need to determine the quadrant.

- 6,363
- 9
- 33
- 41
-
The range of principle values is `(-pi,pi]` but atan2 has the range `[-pi,pi]` so it includes one extra value `-pi` from another branch due to `atan2(-0.0,x)` for `x<0`. – Z boson Jul 02 '15 at 11:27
I guess the main question tries to figure out: "when should I use one or the other", or "which should I use", or "Am I using the right one"?
I guess the important point is atan only was intended to feed positive values in a right-upwards direction curve like for time-distance vectors. Zero is always at the bottom left, and thigs can only go up and right, just slower or faster. atan doesn't return negative numbers, so you can't trace things in the 4 directions on a screen just by adding/subtracting its result.
atan2 is intended for the origin to be in the middle, and things can go backwards or down. That's what you'd use in a screen representation, because it DOES matter what direction you want the curve to go. So atan2 can give you negative numbers, because its zero is in the center, and its result is something you can use to trace things in 4 directions.
Consider a right angled triangle. We label the hypotenuse r, the horizontal side y and the vertical side x. The angle of interest α is the angle between x and r.
C++ atan2(y, x)
will give us the value of angle α in radians.
atan
is used if we only know or are interested in y/x not y and x individually. So if p = y/x
then to get α we'd use atan(p)
.
You cannot use atan2
to determine the quadrant, you can use atan2
only if you already know which quadrant your in! In particular positive x and y imply the first quadrant, positive y and negative x, the second and so on. atan
or atan2
themselves simply return a positive or a negative number, nothing more.
-
4
-
@MarkRansom Sure, but it will be a tad slower and return no more information than `atan`, right? – Bill Kotsias Aug 27 '20 at 10:20
-
@BillKotsias not sure why it would be slower, but you're correct that it won't include the quadrant just like `atan`. – Mark Ransom Aug 27 '20 at 14:24
Mehrwolf below is correct, but here is a heuristic which may help:
If you are working in a 2-dimensional coordinate system, which is often the case for programming the inverse tangent, you should use definitely use atan2. It will give the full 2 pi range of angles and take care of zeros in the x coordinate for you.
Another way of saying this is that atan(y/x) is virtually always wrong. Only use atan if the argument cannot be thought of as y/x.

- 21
- 3
In atan2, the output is: -pi
< atan2(y,x)
<pi
and in atan, the output is: -pi/2
< atan(y/x)
< pi/2
//it dose NOT consider the quarter.
If you want to get the orientation between 0
and 2*pi
(like the high-school math), we need to use the atan2 and for negative values add the 2*pi
to get the final result between 0
and 2*pi
.
Here is the Java source code to explain it clearly:
System.out.println(Math.atan2(1,1)); //pi/4 in the 1st quarter
System.out.println(Math.atan2(1,-1)); //(pi/4)+(pi/2)=3*(pi/4) in the 2nd quarter
System.out.println(Math.atan2(-1,-1 ));//-3*(pi/4) and it is less than 0.
System.out.println(Math.atan2(-1,-1)+2*Math.PI); //5(pi/4) in the 3rd quarter
System.out.println(Math.atan2(-1,1 ));//-pi/4 and it is less than 0.
System.out.println(Math.atan2(-1,1)+2*Math.PI); //7*(pi/4) in the 4th quarter
System.out.println(Math.atan(1 ));//pi/4
System.out.println(Math.atan(-1 ));//-pi/4

- 21
- 2
atan2(y,x)
is generally used if you want to convert cartesian coordinates to polar coordinates. It will give you the angle, while sqrt(x*x+y*y)
or, if available, hypot(y,x)
will give you the size.
atan(x)
is simply the inverse of tan. In the annoying case you have to use atan(y/x)
because your system doesn't provide atan2
, you would have to do additional checks for the signs of x
and y
, and for x=0
, in order to get the correct angle.
Note: atan2(y,x)
is defined for all real values of y
and x
, except for the case when both arguments are zero.

- 6,669
- 5
- 27
- 39