atan2 inconsistencies

Started by
9 comments, last by jmpep 10 years ago


When your problems are between 0 and 180 (for example), it kind of suggests that you are doing something mathematically wrong.

Not necessarily. The resulting angle is just the direction of a vector, and there is a magnitude, too. The magnitude is tiny, e.g. less than 10-10 in the OP's example. With the angle being 0° the vector points along the positive x axis and is very close to 0, and with the angle being 180° it points along the negative x axis but is still very close to 0. It is just so that due to the tiny magnitude and the fact that the mean value is 0, little inaccuracies cause the sign to toggle. Atan2 (as well as atan) depends on the sign of its argument(s) due to the symmetry of sine. Although the angle seems to alter dramatically, it is not necessarily a problem if also the magnitude is considered.

Yes, that's the reason the function is behaving the way it is doing it, I completely understand that.

What I was saying is that, the same way having a program with a lots of downcastings suggest a problem with the design of the program, having atan2 being so unstable suggest a problem with the formula being used. For example, that a vector that should have been normalized wasn't, which can be a problem elsewhere also.

Maybe it is only my case because I almost always end up working with normalized vectors whenever I need to also work with angles. But I thought this was a pretty general case... Anyway, I said "suggest" because, as you point out, there might be plenty of reasons to pass parameters to atan2 which causes erratic behaviour such as this.

Maybe I should have said that it suggested problems to me; I am a little paranoid with checking my formulas :P

“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

This topic is closed to new replies.

Advertisement