Problems might arise when you are with boundary values, as said by the previous posters. I guess you are passing -0, 0 sometimes and 0,0 other times, as said by Alvaro.
Nevertheless, I don't think the problem is just that. Usually when you have problems with atan2 is because sometimes you get 180 and sometimes -180, which in the end are pretty different representations but for the same angle.
When your problems are between 0 and 180 (for example), it kind of suggests that you are doing something mathematically wrong.
Let my try to explain it.
Atan2(y,x) returns an angle a so that tan(a) = y/x.
This is important, because we have that:
tan(a) = sin(a)/cos(a) = y/x.
Now, this does *not* mean that y needs to be a sine or x need to be a cosine. But USUALLY when you need to call atan2 your two parameters are going to be exactly that. Or at least follow the same proportion (meaning x^2 + y^2 = 1).
So to wrap up: if you are sure your formula is correct, well, by all means go with it. But I would double check if I were you...
Edited by Javier Meseguer de Paz, 19 March 2014 - 07:26 PM.
“We should forget about small efficiencies, say about 97% of the time; premature optimization is the root of all evil” - Donald E. Knuth, Structured Programming with go to Statements
"First you learn the value of abstraction, then you learn the cost of abstraction, then you're ready to engineer" - Ken Beck, Twitter