Advertisement Jump to content
Sign in to follow this  
neeker

atan2 inconsistencies

This topic is 1765 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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

Log the actual values being passed in to atan2 - the differences - and it'll be clear what's going on. Your logs have not proven that it is being called with the "exact same parameters".

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!