View Single Post
  #15   Spotlight this post!  
Unread 13-07-2011, 21:42
EricVanWyk EricVanWyk is offline
Registered User
no team
 
Join Date: Jan 2007
Rookie Year: 2000
Location: Boston
Posts: 1,597
EricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond repute
Send a message via AIM to EricVanWyk
Re: Cartesian to angle

Quote:
Originally Posted by ajd View Post
Why would you want atan2(0,0) to return 0, as opposed to any other value? If your point is at the origin, the line between the origin and your point could point in any direction. What is the use case for atan2(0,0) = 0 being helpful?
Some atan2 implementations throw an exception at (0,0), which is more "correct" mathematically but a pain to deal with as a programmer. You either need to pre-test for (0,0) or wrap it with a try..catch.

Some implementations return NaN (Not a Number). You need to test for this, since NaN can "poison" your result - the result of operations with NaN as an operand is NaN.

By returning a valid number you can avoid having to explicitly test for (0,0) when you don't care. For example, in cartesian -> polar conversions. This makes those calculations a bit easier. For cases where you do care about (0,0), you need to explicitly test before the calculation - but you would have had to do this in the previous methods any way.

As you said, it could point in any direction. The decision to return 0 is arbitrary but convenient. It could just as easily return 0.1234567... but that would confuse us humans.


The wikipedia article goes a little bit into the history if you are interested: http://en.wikipedia.org/wiki/Atan2


I did an fixed-point assembly implementation of Atan2 for the M3-Cortex when I worked for TI. I had a(n extremely nerdy) blast doing it - the arctrig functions have some cool features that make their approximations interesting.