Sign in to follow this  
neeker

atan2 inconsistencies

Recommended Posts

Hi,

 

I'm using atan2 from math.h and I'm getting weird results.  I call it once using a set of parameters and I get the result of 0 degrees (which is correct), but when I call it again with the exact same parameters I get the result of -180 or +180 degrees.  Is this a known issue and are there work arounds?

Share this post


Link to post
Share on other sites

Could you show a code showing the issue? Because 0 is pretty different from +/- 180, that would not be a mere known issue, it would be an explosive bug... I am inclined to believe that there is a bug in your code...

Share this post


Link to post
Share on other sites
   Logf("vEyePosition = %g, %g, %g", vEyePosition.x, vEyePosition.y, vEyePosition.z);
   Logf("vStartLookAt = %g, %g, %g", vStartLookAt.x, vStartLookAt.y, vStartLookAt.z);
   fViewRotY = atan2(vEyePosition.y - vStartLookAt.y, vStartLookAt.z - vEyePosition.z);
   fViewRotX =atan2(vStartLookAt.x - vEyePosition.x, vStartLookAt.z - vEyePosition.z);
   Logf("Post fVeiwRotX = %g", fViewRotX);

The log messages are:

 

vEyePosition = 1.57361e-007, 50, 9.7
vStartLookAt = 1.57361e-007, 0, 9.7
fVeiwRotX = -9.68575e-008

 

...and...

 

vEyePosition = 1.57361e-007, 50, 9.7
vStartLookAt = 1.57361e-007, 0, 9.7
fVeiwRotX = 3.14159

respectively. 
 

Share this post


Link to post
Share on other sites

You are passing numbers that are very very close to 0. Chances are you are passing a positive or zero value as difference of z coordinates in the first case and a negative value in the second case.

Share this post


Link to post
Share on other sites

atan2() does not dice ;) It is probable that the values shown by Logf() are rounded when being displayed, i.e. not all significant bits are shown. You may try to enhance the output resolution, or try to output the values as layered uint32_t like in

    *(static_cast<uint32_t*>( &vEyePosition.x))

to see whether they differ.

 

The expected minimal difference in the values then cause a minimal positive or negative value, resp., what then means 0° or 180°, resp.

 

This is a typical problem with differences of floating point numbers.

Edited by haegarr

Share this post


Link to post
Share on other sites

atan2() does not dice ;) It is probable that the values shown by Logf() are rounded when being displayed, i.e. not all significant bits are shown. You may try to enhance the output resolution, or try to output the values as layered uint32_t like in

    *(static_cast<uint32_t*>( &vEyePosition.x))

to see whether they differ.

 

The expected minimal difference in the values then cause a minimal positive or negative value, resp., what then means 0° or 180°, resp.

 

This is a typical problem with differences of floating point numbers.

 

Good idea with the casting; that revealed that there are (very slight) differences.

Share this post


Link to post
Share on other sites

To give a more visual answer, CppReference has this beautiful little image:

 

285px-math-atan2.png

 

All the trig functions have boundaries that you need to be aware of, and floating point is always an approximation. Any time the approximation gets close to a boundary, unexpected results often follow.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

 


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this