arctangent function with two arguments
atan2 - Wikipedia
atan2atan
{\textstyle \theta =\operatorname {atan2} (y,x)=\arctan \left(y/x\right).}
In computing and mathematics, the function atan2 is the 2-argument arctangent. By definition, ... {\displaystyle \operatorname {atan2} (y,x)} is the argument (also called phase or angle) of the complex number ... {\displaystyle … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Atan2
atan2 - Wikipedia
January 20, 2026 - The C function atan2, and most ... or when given positive zero arguments, it is normally defined as 0. It will always return a value in the range [−π, π] rather than raising an error or returning a NaN (Not a Number)....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › atan2
Math.atan2() - JavaScript - MDN Web Docs
December 29, 2025 - In addition, for points in the second and third quadrants (x < 0), Math.atan2() would output an angle less than
Discussions

math - How to map atan2() to degrees 0-360 - Stack Overflow
Add 360° if the answer from atan2 is less than 0°. ... Which is the same as "just add 2 * PI" if you're having one of those days. 2014-10-08T14:43:36.02Z+00:00 ... Just adding the code to what dave4420 said: x = (x < 0) ? (x + 360) : x; 2021-06-10T11:59:43.71Z+00:00 ... Or if you don't like branching, negate the two parameters and add 180° to the answer. (Adding 180° to the return value puts it nicely in the 0-360 range... More on stackoverflow.com
🌐 stackoverflow.com
c++ - Using atan2 to turn a range from - 1 to 1 into degrees - Stack Overflow
I'm trying to use atan2 to turn a range of - 1 to 1 into radians and then from radians into degrees. However atan2(0,1) is equal to 0 when it should be equal 90.0 what am I doing wrong here? float More on stackoverflow.com
🌐 stackoverflow.com
How does Math.atan2 work?
if I understood right this method returns result in radians. Like angle of 0 degrees is 0, angle of 180 is 3,14... This supposed to help you to convert coordinates from cartesian system (x, y) to polar coordinates (radius, angle) it can be really helpful in some cases. So that for point (0, 90) polar representation is like (90, 1.57) For some reason atan2 method gets arguments in reversed manner. Y then X. More on reddit.com
🌐 r/learnjava
2
4
February 22, 2021
Changing the atan function so that it ranges from 0 to 2*pi
I need to write a function mfile to set the built-in matlab function atan in the range of 0 to 2*pi without using atan2. More on mathworks.com
🌐 mathworks.com
6
1
June 12, 2011
🌐
ArcGIS Pro
pro.arcgis.com › en › pro-app › latest › help › analysis › raster-functions › atan2-function.htm
ATan2 function—ArcGIS Pro | Documentation
Range: -pi < [out_value] ≤ piThis domain applies to both inputs. If both input values are 0, the output will be NoData. If the first input value is 0, the output will be 0. The input values to the ATan2 function are interpreted as being in linear units; to give meaningful results, they should ...
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › atan2.html
std::atan2, std::atan2f, std::atan2l - cppreference.com
The library provides overloads of std::atan2 for all cv-unqualified floating-point types as the type of the parameters.(since C++23) If no errors occur, the arc tangent of y / x (arctan( ... 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.
🌐
Cplusplus
cplusplus.com › reference › cmath › atan2
atan2
Returns the principal value of the arc tangent of y/x, expressed in radians. To compute the value, the function takes into account the sign of both arguments in order to determine the quadrant. In C++, this function is overloaded in <valarray> (see valarray atan2).
Find elsewhere
🌐
ArcGIS Pro
pro.arcgis.com › en › pro-app › latest › tool-reference › spatial-analyst › atan2.htm
ATan2 (Spatial Analyst)—ArcGIS Pro | Documentation
The output values from each function also have a defined range. For this tool, the following are true: ... This domain applies to both inputs. ... Note that -∞ and ∞ represent the smallest negative and largest positive values supported by the particular raster format, respectively. If both input values are 0, the output will be NoData. If first input value is 0, the output will be 0. The input values to ATan2 are interpreted as being in linear units, and to give meaningful results, they should both be in the same unit.
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 009695299 › functions › atan2.html
atan2
Upon successful completion, these functions shall return the arc tangent of y/ x in the range [-,] radians.
🌐
Programiz
programiz.com › cpp-programming › library-function › cmath › atan2
C++ atan2() - C++ Standard Library
The atan2() function returns: a floating-point value in the range of [-π, π]. 0 if both x and y are zero ·
Top answer
1 of 1
6

You're passing in the arguments in the wrong order. std::atan2 expects the arguments in the order y,x, not x,y.

Yes, that is incredibly dumb, but it's related to how the tangent function is defined in the first place (which is defined as a ratio of the y-component to the x-component, not the other way around), and like many notational mistakes in mathematics, inertia set in thousands of years ago and you can't push back against it without being a crank.

So write your code like this:

float radians = atan2(1, 0);

Or, if you want everything as explicit as possible:

float x = 0, y = 1;
float radians = atan2(y, x); //Yes, that's the correct order, don't @ me

And you'll get the results you expect.


Your second problem is that the values that atan2 correspond to don't match up with the directions you want. What you want is a circle where the top is 0°, the right side is 90°, the bottom is 180°, and the left side is 270°. Punching the values into atan2 is instead going to produce values where the right side is 0°, up is 90°, left is 180°, and down is 270°.

Also, in comparing with my own hardware, my y-axis is flipped compared to yours. Mine is y+↑, whereas your setup appears to be y-↑

So if you want to transform the normal atan2 rotation into the rotation you want, you'll need to transform it like this:

float radians = atan2(yaxisval, xaxisval);
float degrees = (radians * 180 / PI);
degrees = 90 - degrees;
if(degrees < 0)
    degrees += 360;

Then, all you have to do from there is possibly transform the y-axis depending on whether you expect the joystick pushed up to return a positive or negative value. That's up to the domain of your program.

🌐
p5.js
p5js.org › reference › p5 › atan2
atan2
By default, atan2() returns values in the range -π (about -3.14) to π (3.14).
🌐
W3Schools
w3schools.com › python › ref_math_atan2.asp
Python math.atan2() Method
# Import math Library import math # Return the arc tangent of y/x in radians print(math.atan2(8, 5)) print(math.atan2(20, 10)) print(math.atan2(34, -7)) Try it Yourself »
🌐
MKSSoftware
mkssoftware.com › docs › man3 › atan2.3.asp
atan2(), atan2f() -- arc tangent function of two variables
atan2() function returns the arc tangent of y/x, in the range [-pi, +pi] radians. If both y and x are 0.0, the result is undefined. If in POSIX mode, errno is set to EDOM. If the value of y or x is NaN, NaN is returned. If the result underflows, 0.0 is returned.
🌐
Geosciences LibreTexts
geo.libretexts.org › campus bookshelves › university of california davis › gel 56: introduction to geophysics › geophysics is everywhere in geology... › back matter
Arctan vs Arctan2 - Geosciences LibreTexts
November 6, 2024 - The output of the function range from 0 to ±180\(^{\circ}\) (±\(\pi\)/2), with positive values (east longitudes) for positive y values, and negative values (west longitudes) for negative y values.
🌐
Reddit
reddit.com › r/learnjava › how does math.atan2 work?
r/learnjava on Reddit: How does Math.atan2 work?
February 22, 2021 -

I've been calculating rotation needed between two 3d points, and I'm having some slight trouble.

You can use very basic sohcahtoa to find the angle needed between the two points by making a right triangle and just doing arctan of opposite/adjacent (the two sides). Now this only works if all of the values are positive, as, for example, arctan(-5/-4) == arctan(5/4). I do remember learning something about fixing this back when I learned about trig identities in school, and how to fix it, but my friend sent me some code, and he used Math.atan2 instead of tangent.

Now his code works perfectly, and was not too different from mine, and it really made me wonder what Math.atan2 does. I searched up on the internet, and didn't understand a word of the descriptions. Can someone show me what Math.atan2 is in normal math writing?

🌐
W3Schools
w3schools.com › c › ref_math_atan2.php
C Math atan2() Function
The atan2() function returns the ... into account negative values of x so that it can return angles outside of the range -PI/2 to PI/2....