Why the dramatic difference in the arc tangent?

Why are the two following calculations so dramatically different in results?

Given ATAN(y2 - y1 / x2 - x1)

Atan(165.377838 - 240 / 57.234100 - 160) = 0.8696708890423176

Atan(159.340393 - 240 / 61.901459 - 160) = -1.3538428308947705


I am calculating all of the points on a circle, and when I reach from approximately 45 to 135 degrees, rotating counterclockwise, all of my results go from positive to negative, and giving me sparatic results, such as above. The result of the arctangent, every where else in the rotation, is around a 1.43 to 1.53, but in this area they go negative and drop full integer.

Any ideas why this is happening?

-
The expression $165.377878-240/57.234100-160$ means $165.377878-(240/57.234100)-160$. It is not the same as $(165.377878-240)/(57.234100-160)$. ${}\qquad{}$ –  Michael Hardy May 26 at 16:30

There are parentheses missing. You probably intended ATAN((y2 - y1) / (x2 - x1)).

-
Actually, if he's doing what I think he's doing, the two-argument arctangent might be a better thing to use: atan2(y2 - y1, x2 - x1)... –  Ｊ. Ｍ. Jul 15 '11 at 3:51
Without the parentheses, these are Atan(1.183) and Atan(-4.537) and I get the values quoted. But why should they be close? –  Ross Millikan Jul 15 '11 at 4:28
So, the answer to George's question is: you forgot the parentheses. –  GEdgar Jul 15 '11 at 13:20

Take a look at a plot of the tangent function, e.g. http://commons.wikimedia.org/wiki/File:Tangent-plot.svg.

Here's a definition of tangent tan(x) = sin(x)/cos(x). For each x with cos(x)=0: tan(x) is undefined. That happens each half circle (180° or pi which is about 3.14159265).

Arctangent is supposed to revert this strange thing. It's plot looks like this: http://en.wikipedia.org/wiki/File:Arctangent_Arccotangent.svg

Your formulas can be resolved to atan(1.18453366) and atan(-4.53673694), because of the missing parentheses. Taking a look at the plot of arctangent again, I'd say, you got exactly what you asked for. You provided values differing by about 5.5. A difference of about 1.5 isn't as dramatic.

You can go with the correct parentheses set by Shaun Ault. However you have to expect atan to "jump" from -pi/2 to pi/2 (by about 3.14159265) and back for x2-x1 "swinging" around 0. That's the way atan works. Look at the plot and keep in mind 1/0is undefined for mathematicians or +/-infinity for engineers. So for "anything / something close to zero" you can go from the far left side of the plot directly to the far right.

The solution may be in the comment by J.M.

Actually, if he's doing what I think he's doing, the two-argument arctangent might be a better thing to use: atan2(y2 - y1, x2 - x1)

Here's the definition: http://en.wikipedia.org/wiki/Atan2#Definition_and_computation

-