ATAN2(3) BSD Programmer's Manual ATAN2(3) NNAAMMEE aattaann22 - arc tangent function of two variables SSYYNNOOPPSSIISS ##iinncclluuddee <<mmaatthh..hh>> _d_o_u_b_l_e aattaann22(_d_o_u_b_l_e _y, _d_o_u_b_l_e _x); DDEESSCCRRIIPPTTIIOONN The atan2 function computes the principal value of the arc tangent of _y_/_x, using the signs of both arguments to determine the quadrant of the return value. RREETTUURRNN VVAALLUUEESS The atan2 function, if successful, returns the arc tangent of _y_/_x in the range [-pi, +pi] radians. If both _x and _y are zero, the global variable _e_r_r_n_o is set to EDOM. On the VAX: aattaann22(_y, _x):= aattaann(_y_/_x) if _x > 0, sign(_y)*(pi - aattaann(|_y_/_x|)) if _x < 0, 0 if x = y = 0, or sign(_y)*pi/2 if _x = 0 _y. NNOOTTEESS The function aattaann22() defines "if x > 0," aattaann22(_0, _0) = 0 on a VAX despite that previously aattaann22(_0, _0) may have generated an error message. The reasons for assigning a value to aattaann22(_0, _0) are these: 1. Programs that test arguments to avoid computing aattaann22(_0, _0) must be indifferent to its value. Programs that require it to be invalid are vulnerable to diverse reactions to that inva- lidity on diverse computer systems. 2. The aattaann22() function is used mostly to convert from rectangu- lar (x,y) to polar (r,theta) coordinates that must satisfy x = r*cos theta and y = r*sin theta. These equations are satis- fied when (x=0,y=0) is mapped to (r=0,theta=0) on a VAX. In general, conversions to polar coordinates should be computed thus: r := hypot(x,y); ... := sqrt(x*x+y*y) theta := atan2(y,x). 3. The foregoing formulas need not be altered to cope in a rea- sonable way with signed zeros and infinities on a machine that conforms to IEEE 754; the versions of hypot(3) and aattaann22() provided for such a machine are designed to handle all cases. That is why aattaann22(_+_-_0, _-_0) = +-pi for instance. In general the formulas above are equivalent to these: r := sqrt(x*x+y*y); if r = 0 then x := copysign(1,x); SSEEEE AALLSSOO acos(3), asin(3), atan(3), cos(3), cosh(3), sin(3), sinh(3), tan(3), tanh(3), math(3), SSTTAANNDDAARRDDSS The aattaann22() function conforms to ANSI C X3.159-1989 (``ANSI C ''). 4.4BSD June 4, 1993 1