GIL .. BIOSCI/Bionet News .. Biosequences .. Software .. FTP

# calculation of angles

John Osborn j.c.osborn at br.ac.uk
Mon Dec 20 06:22:40 EST 1999

```Lev Gorenstein wrote:
>
> Thus, if you use atan(sine(x)/cosine(x)), and, for example, the
> argument is positive, you would not know, whether it's because
> the angle is in the first quadrant (both sine and cosine are
> positive), or it is in the third quadrant (both are negative, thus
> giving a positive tangent).  The atan() function will return a
> value from the first quadrant.  It's my understanding that atan2
> function does the above mentioned analisys of signs (since the
> sine and cosine are supplied separately), and thus returns
> the true value of angle.

As Lev Gorenstein says, one advantage of ATAN2 over ATAN is that it
takes care of the quadrant. It also ensures that the result is
greater than -pi and not greater than pi, whereas in the example
given by Mark, atan2(sin(theta),cos(theta)), it is not clear that
the argument theta is necessarily in this range. The definition of
Fortran for which I do not have manuals at hand) is as follows:

ATAN2(X1,X2) = tan^-1(X1/X2) in radians = Y such that:
-pi < Y <= pi;
if X1 > 0, Y > 0;
if X1 < 0, Y < 0;
if X1 = 0 and X2 > 0, Y = 0;
if X1 = 0 and X2 < 0, Y = pi;
if X2 = 0 and X1 <> 0, |Y| = pi/2;
X1 and X2 cannot both equal 0.
(The symbol <= means "less than or equal to"; <> means "not equal to".)
Note that the definition allows for the possibility that X2 = 0,
whereas if one simply used ATAN(X1/X2) one would first need to test
for the possibility that X2 = 0 and handle it separately to avoid
an undefined intermediate result for X1/X2.

--
John Osborn