1.\" Copyright (c) 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" from: @(#)atan2.3 5.1 (Berkeley) 5/2/91 29.\" 30.Dd July 31, 2008 31.Dt ATAN2 3 32.Os 33.Sh NAME 34.Nm atan2 , 35.Nm atan2f , 36.Nm atan2l , 37.Nm carg , 38.Nm cargf , 39.Nm cargl 40.Nd arc tangent and complex phase angle functions 41.Sh LIBRARY 42.Lb libm 43.Sh SYNOPSIS 44.In math.h 45.Ft double 46.Fn atan2 "double y" "double x" 47.Ft float 48.Fn atan2f "float y" "float x" 49.Ft long double 50.Fn atan2l "long double y" "long double x" 51.In complex.h 52.Ft double 53.Fn carg "double complex z" 54.Ft float 55.Fn cargf "float complex z" 56.Ft long double 57.Fn cargl "long double complex z" 58.Sh DESCRIPTION 59The 60.Fn atan2 , 61.Fn atan2f , 62and 63.Fn atan2l 64functions compute the principal value of the arc tangent of 65.Fa y/ Ns Fa x , 66using the signs of both arguments to determine the quadrant of 67the return value. 68.Pp 69The 70.Fn carg , 71.Fn cargf , 72and 73.Fn cargl 74functions compute the complex argument (or phase angle) of 75.Fa z . 76The complex argument is the number theta such that 77.Li z = r * e^(I * theta) , 78where 79.Li r = cabs(z) . 80The call 81.Li carg(z) 82is equivalent to 83.Li atan2(cimag(z), creal(z)) , 84and similarly for 85.Fn cargf 86and 87.Fn cargl . 88.Sh RETURN VALUES 89The 90.Fn atan2 , 91.Fn atan2f , 92and 93.Fn atan2l 94functions, if successful, 95return the arc tangent of 96.Fa y/ Ns Fa x 97in the range 98.Bk -words 99.Bq \&- Ns \*(Pi , \&+ Ns \*(Pi 100.Ek 101radians. 102Here are some of the special cases: 103.Bl -column atan_(y,x)_:=____ sign(y)_(Pi_atan2(Xy_xX))___ 104.It Fn atan2 y x No := Ta 105.Fn atan y/x Ta 106if 107.Fa x 108> 0, 109.It Ta sign( Ns Fa y Ns )*(\*(Pi - 110.Fn atan "\*(Bay/x\*(Ba" ) Ta 111if 112.Fa x 113< 0, 114.It Ta 115.No 0 Ta 116if x = y = 0, or 117.It Ta 118.Pf sign( Fa y Ns )*\*(Pi/2 Ta 119if 120.Fa x 121= 0 \(!= 122.Fa y . 123.El 124.Sh NOTES 125The function 126.Fn atan2 127defines "if x > 0," 128.Fn atan2 0 0 129= 0 despite that previously 130.Fn atan2 0 0 131may have generated an error message. 132The reasons for assigning a value to 133.Fn atan2 0 0 134are these: 135.Bl -enum -offset indent 136.It 137Programs that test arguments to avoid computing 138.Fn atan2 0 0 139must be indifferent to its value. 140Programs that require it to be invalid are vulnerable 141to diverse reactions to that invalidity on diverse computer systems. 142.It 143The 144.Fn atan2 145function is used mostly to convert from rectangular (x,y) 146to polar 147.if n\ 148(r,theta) 149.if t\ 150(r,\(*h) 151coordinates that must satisfy x = 152.if n\ 153r\(**cos theta 154.if t\ 155r\(**cos\(*h 156and y = 157.if n\ 158r\(**sin theta. 159.if t\ 160r\(**sin\(*h. 161These equations are satisfied when (x=0,y=0) 162is mapped to 163.if n \ 164(r=0,theta=0). 165.if t \ 166(r=0,\(*h=0). 167In general, conversions to polar coordinates 168should be computed thus: 169.Bd -unfilled -offset indent 170.if n \{\ 171r := hypot(x,y); ... := sqrt(x\(**x+y\(**y) 172theta := atan2(y,x). 173.\} 174.if t \{\ 175r := hypot(x,y); ... := \(sr(x\u\s82\s10\d+y\u\s82\s10\d) 176\(*h := atan2(y,x). 177.\} 178.Ed 179.It 180The foregoing formulas need not be altered to cope in a 181reasonable way with signed zeros and infinities 182on a machine that conforms to 183.Tn IEEE 754 ; 184the versions of 185.Xr hypot 3 186and 187.Fn atan2 188provided for 189such a machine are designed to handle all cases. 190That is why 191.Fn atan2 \(+-0 \-0 192= \(+-\*(Pi 193for instance. 194In general the formulas above are equivalent to these: 195.Bd -unfilled -offset indent 196.if n \ 197r := sqrt(x\(**x+y\(**y); if r = 0 then x := copysign(1,x); 198.if t \ 199r := \(sr(x\(**x+y\(**y);\0\0if r = 0 then x := copysign(1,x); 200.Ed 201.El 202.Sh SEE ALSO 203.Xr acos 3 , 204.Xr asin 3 , 205.Xr atan 3 , 206.Xr cabs 3 , 207.Xr cos 3 , 208.Xr cosh 3 , 209.Xr math 3 , 210.Xr sin 3 , 211.Xr sinh 3 , 212.Xr tan 3 , 213.Xr tanh 3 214.Sh STANDARDS 215The 216.Fn atan2 , 217.Fn atan2f , 218.Fn atan2l , 219.Fn carg , 220.Fn cargf , 221and 222.Fn cargl 223functions conform to 224.St -isoC-99 . 225