1
$\begingroup$

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?

  • 0
    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{}$2014-05-26

2 Answers 2

10

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

  • 0
    So, the answer to George's question is: you forgot the parentheses.2011-07-15
0

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