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. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" from: @(#)atan2.3 5.1 (Berkeley) 5/2/91 33.\" $NetBSD: atan2.3,v 1.7 1997/11/01 06:42:53 mycroft Exp $ 34.\" 35.Dd May 2, 1991 36.Dt ATAN2 3 37.Os 38.Sh NAME 39.Nm atan2 40.Nd arc tangent function of two variables 41.Sh SYNOPSIS 42.Fd #include <math.h> 43.Ft double 44.Fn atan2 "double y" "double x" 45.Ft float 46.Fn atan2f "float y" "float x" 47.Sh DESCRIPTION 48The 49.Fn atan2 50and 51.Fn atan2f 52functions compute the principal value of the arc tangent of 53.Ar y/ Ns Ar x , 54using the signs of both arguments to determine the quadrant of 55the return value. 56.Sh RETURN VALUES 57The 58.Xr atan2 59function, if successful, 60returns the arc tangent of 61.Ar y/ Ns Ar x 62in the range 63.Bk -words 64.Bq \&- Ns \*(Pi , \&+ Ns \*(Pi 65.Ek 66radians. 67If both 68.Ar x 69and 70.Ar y 71are zero, the global variable 72.Va errno 73is set to 74.Er EDOM . 75On the 76.Tn VAX : 77.Bl -column atan_(y,x)_:=____ sign(y)_(Pi_atan2(Xy_xX))___ 78.It Fn atan2 y x No := Ta 79.Fn atan y/x Ta 80if 81.Ar x 82> 0, 83.It Ta sign( Ns Ar y Ns )*(\*(Pi - 84.Fn atan "\\*(Bay/x\\*(Ba" ) Ta 85if 86.Ar x 87< 0, 88.It Ta 89.No 0 Ta 90if x = y = 0, or 91.It Ta 92.Pf sign( Ar y Ns )*\\*(Pi/2 Ta 93if 94.Ar x 95= 0 \*(!= 96.Ar y . 97.El 98.Sh NOTES 99The function 100.Fn atan2 101defines "if x > 0," 102.Fn atan2 0 0 103= 0 on a 104.Tn VAX 105despite that previously 106.Fn atan2 0 0 107may have generated an error message. 108The reasons for assigning a value to 109.Fn atan2 0 0 110are these: 111.Bl -enum -offset indent 112.It 113Programs that test arguments to avoid computing 114.Fn atan2 0 0 115must be indifferent to its value. 116Programs that require it to be invalid are vulnerable 117to diverse reactions to that invalidity on diverse computer systems. 118.It 119The 120.Fn atan2 121function is used mostly to convert from rectangular (x,y) 122to polar 123.if n\ 124(r,theta) 125.if t\ 126(r,\(*h) 127coordinates that must satisfy x = 128.if n\ 129r\(**cos theta 130.if t\ 131r\(**cos\(*h 132and y = 133.if n\ 134r\(**sin theta. 135.if t\ 136r\(**sin\(*h. 137These equations are satisfied when (x=0,y=0) 138is mapped to 139.if n \ 140(r=0,theta=0) 141.if t \ 142(r=0,\(*h=0) 143on a VAX. In general, conversions to polar coordinates 144should be computed thus: 145.Bd -unfilled -offset indent 146.if n \{\ 147r := hypot(x,y); ... := sqrt(x\(**x+y\(**y) 148theta := atan2(y,x). 149.\} 150.if t \{\ 151r := hypot(x,y); ... := \(sr(x\u\s82\s10\d+y\u\s82\s10\d) 152\(*h := atan2(y,x). 153.\} 154.Ed 155.It 156The foregoing formulas need not be altered to cope in a 157reasonable way with signed zeros and infinities 158on a machine that conforms to 159.Tn IEEE 754 ; 160the versions of 161.Xr hypot 3 162and 163.Fn atan2 164provided for 165such a machine are designed to handle all cases. 166That is why 167.Fn atan2 \(+-0 \-0 168= \(+-\*(Pi 169for instance. 170In general the formulas above are equivalent to these: 171.Bd -unfilled -offset indent 172.if n \ 173r := sqrt(x\(**x+y\(**y); if r = 0 then x := copysign(1,x); 174.if t \ 175r := \(sr(x\(**x+y\(**y);\0\0if r = 0 then x := copysign(1,x); 176.Ed 177.El 178.Sh SEE ALSO 179.Xr acos 3 , 180.Xr asin 3 , 181.Xr atan 3 , 182.Xr cos 3 , 183.Xr cosh 3 , 184.Xr sin 3 , 185.Xr sinh 3 , 186.Xr tan 3 , 187.Xr tanh 3 , 188.Xr math 3 189.Sh STANDARDS 190The 191.Fn atan2 192function conforms to 193.St -ansiC . 194