[c++] What is the difference between atan and atan2 in C++?

What is the difference between atan and atan2 in C++?

This question is related to c++ math.h

The answer is


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.


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. Cero 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 cero is in the center, and its result is something you can use to trace things in 4 directions.


With atan2 you can determine the quadrant as stated here.

You can use atan2 if you need to determine the quadrant.


With atan2 you can determine the quadrant as stated here.

You can use atan2 if you need to determine the quadrant.


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.


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.


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.


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.


With atan2 you can determine the quadrant as stated here.

You can use atan2 if you need to determine the quadrant.


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.


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.


The actual values are in radians but to interpret them in degrees it will be:

  • atan = gives angle value between -90 and 90
  • atan2 = 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.


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

From school mathematics we know that the tangent has the definition

tan(a) = sin(a) / cos(a)

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 p/2):

  Quadrant    Angle              sin   cos   tan
-------------------------------------------------
  I           0    < a < p/2      +     +     +
  II          p/2  < a < p        +     -     -
  III         p    < a < 3p/2     -     -     +
  IV          3p/2 < a < 2p       -     +     -

Given that the value of tan(a) 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. -p/2 <= atan() <= p/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(a) / cos(a) 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(a) and cos(a) and resolves all four quadrants by adding p 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 a on the y- and x-axis, i.e.

y = v * sin(a)
x = v * cos(a)

which gives the relation

y/x = tan(a)

Conclusion: atan(y/x) is held back some information and 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.


The actual values are in radians but to interpret them in degrees it will be:

  • atan = gives angle value between -90 and 90
  • atan2 = 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.


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.


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.


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.


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.


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.


With atan2 you can determine the quadrant as stated here.

You can use atan2 if you need to determine the quadrant.


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.


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

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.


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. Cero 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 cero is in the center, and its result is something you can use to trace things in 4 directions.


From school mathematics we know that the tangent has the definition

tan(a) = sin(a) / cos(a)

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 p/2):

  Quadrant    Angle              sin   cos   tan
-------------------------------------------------
  I           0    < a < p/2      +     +     +
  II          p/2  < a < p        +     -     -
  III         p    < a < 3p/2     -     -     +
  IV          3p/2 < a < 2p       -     +     -

Given that the value of tan(a) 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. -p/2 <= atan() <= p/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(a) / cos(a) 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(a) and cos(a) and resolves all four quadrants by adding p 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 a on the y- and x-axis, i.e.

y = v * sin(a)
x = v * cos(a)

which gives the relation

y/x = tan(a)

Conclusion: atan(y/x) is held back some information and 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.