How can I get math.atan() to work properly?

I have trying to figure out how to use math.atan(), which I assumed would the inverse of tangent or tan^-1(). However, the numbers they are returning don’t seem to what the right answers.

In degree mode in my calculator tan^-1(1) returns 45, so the inverse tangent of 1 is 45 degrees. In radian mode in my calculator tan^-1(1) returns 0.78539.

However, whenever I try to do the same thing on Roblox, it doesn’t seem to work at all?

indent preformatted text by 4 spaces
print(math.tan(math.rad(45))) --Equivalent to putting tan(45) in degree mode on a calculator.

local tan = math.atan(1) --This returns 0.7853, which is what you get when you input tan^-1(1) on radian

local otherTan = math.atan(math.rad(1)) --Returns 0.01745 for some reason.

My question is, how am I suppose to get it to return 45 degrees like it is supposed to?

math.atan and the rest of the inverse trigonometric functions accepts a ratio from a right angle triangle sides remembering SOHCAHTOA, and outputs an angle in radians. For math.atan its TOA opposite/adjacent.

So it’s always in radians mode for the output.

--Converting 45 degrees to radians
45° × π/180 = 0.7854rad

To make it output a degree just turn the output into degrees.

local tan = math.atan(1) 
--45° output

The reason otherTan doesn’t work is because the ratio 1 opposite/adjacent is not being inputted into the function which is the ratio of y/x.

What is actually being inputted is 0.0174533 because of the math.rad(1).


Also, using math.atan2(y, x) rather than math.atan(y / x) is a lot more useful when you’re working with stuff like coordinates since it takes into account which quadrant your X and Y values are in.

It also handles the cases where x would be zero:

print(math.atan2(1, 0)) --> 1.5707...
print(math.atan2(-1, 0)) --> -1.5707...