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:
Your formulas can be resolved to
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 (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: