# 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.

## 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 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 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 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 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 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 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 on other sites

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

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 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 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 9
• ### Forum Statistics

• Total Topics
634084
• Total Posts
3015403
×