1*48919Scael.\" Copyright (c) 1991 The Regents of the University of California. 2*48919Scael.\" All rights reserved. 3*48919Scael.\" 4*48919Scael.\" %sccs.include.redist.man% 5*48919Scael.\" 6*48919Scael.\" @(#)atan2.3 5.1 (Berkeley) 05/02/91 7*48919Scael.\" 8*48919Scael.Dd 9*48919Scael.Dt ATAN2 3 10*48919Scael.Os 11*48919Scael.Sh NAME 12*48919Scael.Nm atan2 13*48919Scael.Nd arc tangent function of two variables 14*48919Scael.Sh SYNOPSIS 15*48919Scael.Fd #include <math.h> 16*48919Scael.Ft double 17*48919Scael.Fn atan2 "double y" "double x" 18*48919Scael.Sh DESCRIPTION 19*48919ScaelThe 20*48919Scael.Xr atan2 21*48919Scaelfunction computes the principal value of the arc tangent of 22*48919Scael.Ar y/ Ns Ar x , 23*48919Scaelusing the signs of both arguments to determine the quadrant of 24*48919Scaelthe return value. 25*48919Scael.Sh RETURN VALUES 26*48919ScaelThe 27*48919Scael.Xr atan2 28*48919Scaelfunction, if successful, 29*48919Scaelreturns the arc tangent of 30*48919Scael.Ar y/ Ns Ar x 31*48919Scaelin the range 32*48919Scael.Bk -words 33*48919Scael.Bq \&- Ns \*(Pi , \&+ Ns \*(Pi 34*48919Scael.Ek 35*48919Scaelradians. 36*48919ScaelIf both 37*48919Scael.Ar x 38*48919Scaeland 39*48919Scael.Ar y 40*48919Scaelare zero, the global variable 41*48919Scael.Va errno 42*48919Scaelis set to 43*48919Scael.Er EDOM . 44*48919ScaelOn the 45*48919Scael.Tn VAX : 46*48919Scael.Bl -column atan_(y,x)_:=____ sign(y)_(Pi_atan2(Xy_xX))___ 47*48919Scael.It Fn atan2 y x No := Ta 48*48919Scael.Fn atan y/x Ta 49*48919Scaelif 50*48919Scael.Ar x 51*48919Scael> 0, 52*48919Scael.It Ta sign( Ns Ar y Ns )*(\*(Pi - 53*48919Scael.Fn atan "\\*(Bay/x\\*(Ba" ) Ta 54*48919Scaelif 55*48919Scael.Ar x 56*48919Scael< 0, 57*48919Scael.It Ta 58*48919Scael.No 0 Ta 59*48919Scaelif x = y = 0, or 60*48919Scael.It Ta 61*48919Scael.Pf sign( Ar y Ns )*\\*(Pi/2 Ta 62*48919Scaelif 63*48919Scael.Ar x 64*48919Scael= 0 \*(!= 65*48919Scael.Ar y . 66*48919Scael.El 67*48919Scael.Sh NOTES 68*48919ScaelThe function 69*48919Scael.Fn atan2 70*48919Scaeldefines "if x > 0," 71*48919Scael.Fn atan2 0 0 72*48919Scael= 0 on a 73*48919Scael.Tn VAX 74*48919Scaeldespite that previously 75*48919Scael.Fn atan2 0 0 76*48919Scaelmay have generated an error message. 77*48919ScaelThe reasons for assigning a value to 78*48919Scael.Fn atan2 0 0 79*48919Scaelare these: 80*48919Scael.Bl -enum -offset indent 81*48919Scael.It 82*48919ScaelPrograms that test arguments to avoid computing 83*48919Scael.Fn atan2 0 0 84*48919Scaelmust be indifferent to its value. 85*48919ScaelPrograms that require it to be invalid are vulnerable 86*48919Scaelto diverse reactions to that invalidity on diverse computer systems. 87*48919Scael.It 88*48919ScaelThe 89*48919Scael.Fn atan2 90*48919Scaelfunction is used mostly to convert from rectangular (x,y) 91*48919Scaelto polar 92*48919Scael.if n\ 93*48919Scael(r,theta) 94*48919Scael.if t\ 95*48919Scael(r,\(*h) 96*48919Scaelcoordinates that must satisfy x = 97*48919Scael.if n\ 98*48919Scaelr\(**cos theta 99*48919Scael.if t\ 100*48919Scaelr\(**cos\(*h 101*48919Scaeland y = 102*48919Scael.if n\ 103*48919Scaelr\(**sin theta. 104*48919Scael.if t\ 105*48919Scaelr\(**sin\(*h. 106*48919ScaelThese equations are satisfied when (x=0,y=0) 107*48919Scaelis mapped to 108*48919Scael.if n \ 109*48919Scael(r=0,theta=0) 110*48919Scael.if t \ 111*48919Scael(r=0,\(*h=0) 112*48919Scaelon a VAX. In general, conversions to polar coordinates 113*48919Scaelshould be computed thus: 114*48919Scael.Bd -unfilled -offset indent 115*48919Scael.if n \{\ 116*48919Scaelr := hypot(x,y); ... := sqrt(x\(**x+y\(**y) 117*48919Scaeltheta := atan2(y,x). 118*48919Scael.\} 119*48919Scael.if t \{\ 120*48919Scaelr := hypot(x,y); ... := \(sr(x\u\s82\s10\d+y\u\s82\s10\d) 121*48919Scael\(*h := atan2(y,x). 122*48919Scael.\} 123*48919Scael.Ed 124*48919Scael.It 125*48919ScaelThe foregoing formulas need not be altered to cope in a 126*48919Scaelreasonable way with signed zeros and infinities 127*48919Scaelon a machine that conforms to 128*48919Scael.Tn IEEE 754 ; 129*48919Scaelthe versions of 130*48919Scael.Xr hypot 3 131*48919Scaeland 132*48919Scael.Fn atan2 133*48919Scaelprovided for 134*48919Scaelsuch a machine are designed to handle all cases. 135*48919ScaelThat is why 136*48919Scael.Fn atan2 \(+-0 \-0 137*48919Scael= \(+-\*(Pi 138*48919Scaelfor instance. 139*48919ScaelIn general the formulas above are equivalent to these: 140*48919Scael.Bd -unfilled -offset indent 141*48919Scael.if n \ 142*48919Scaelr := sqrt(x\(**x+y\(**y); if r = 0 then x := copysign(1,x); 143*48919Scael.if t \ 144*48919Scaelr := \(sr(x\(**x+y\(**y);\0\0if r = 0 then x := copysign(1,x); 145*48919Scael.Ed 146*48919Scael.El 147*48919Scael.Sh SEE ALSO 148*48919Scael.Xr acos 3 , 149*48919Scael.Xr asin 3 , 150*48919Scael.Xr atan 3 , 151*48919Scael.Xr cos 3 , 152*48919Scael.Xr cosh 3 , 153*48919Scael.Xr sin 3 , 154*48919Scael.Xr sinh 3 , 155*48919Scael.Xr tan 3 , 156*48919Scael.Xr tanh 3 , 157*48919Scael.Xr math 3 , 158*48919Scael.Sh STANDARDS 159*48919ScaelThe 160*48919Scael.Fn atan2 161*48919Scaelfunction conforms to 162*48919Scael.St -ansiC . 163