Jump to content

  • Log In with Google      Sign In   
  • Create Account

atan2 inconsistencies


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 neeker   Members   -  Reputation: 222

Like
0Likes
Like

Posted 19 March 2014 - 12:19 PM

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?



Sponsor:

#2 Javier Meseguer de Paz   Members   -  Reputation: 371

Like
1Likes
Like

Posted 19 March 2014 - 12:44 PM

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


“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


#3 neeker   Members   -  Reputation: 222

Like
0Likes
Like

Posted 19 March 2014 - 01:32 PM

   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. 
 



#4 Álvaro   Crossbones+   -  Reputation: 13326

Like
0Likes
Like

Posted 19 March 2014 - 01:58 PM

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.



#5 haegarr   Crossbones+   -  Reputation: 4313

Like
1Likes
Like

Posted 19 March 2014 - 01:59 PM

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, 19 March 2014 - 01:59 PM.


#6 Pink Horror   Members   -  Reputation: 1203

Like
0Likes
Like

Posted 19 March 2014 - 02:19 PM

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



#7 neeker   Members   -  Reputation: 222

Like
0Likes
Like

Posted 19 March 2014 - 02:19 PM

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.



#8 frob   Moderators   -  Reputation: 21335

Like
1Likes
Like

Posted 19 March 2014 - 02:46 PM

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.


Check out my personal indie blog at bryanwagstaff.com.

#9 Javier Meseguer de Paz   Members   -  Reputation: 371

Like
0Likes
Like

Posted 19 March 2014 - 07:22 PM

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


#10 haegarr   Crossbones+   -  Reputation: 4313

Like
0Likes
Like

Posted 20 March 2014 - 02:27 AM


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.



#11 Javier Meseguer de Paz   Members   -  Reputation: 371

Like
0Likes
Like

Posted 20 March 2014 - 09:40 AM

 


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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS