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.\" $FreeBSD$ 34.\" 35.Dd May 2, 1991 36.Dt ATAN2 3 37.Os 38.Sh NAME 39.Nm atan2 , 40.Nm atan2f 41.Nd arc tangent functions of two variables 42.Sh LIBRARY 43.Lb libm 44.Sh SYNOPSIS 45.In math.h 46.Ft double 47.Fn atan2 "double y" "double x" 48.Ft float 49.Fn atan2f "float y" "float x" 50.Sh DESCRIPTION 51The 52.Fn atan2 53and the 54.Fn atan2f 55functions compute the principal value of the arc tangent of 56.Fa y/ Ns Ar x , 57using the signs of both arguments to determine the quadrant of 58the return value. 59.Sh RETURN VALUES 60The 61.Fn atan2 62and the 63.Fn atan2f 64functions, if successful, 65return the arc tangent of 66.Fa y/ Ns Ar x 67in the range 68.Bk -words 69.Bq \&- Ns \*(Pi , \&+ Ns \*(Pi 70.Ek 71radians. 72If both 73.Fa x 74and 75.Fa y 76are zero, the global variable 77.Va errno 78is set to 79.Er EDOM . 80On the 81.Tn VAX : 82.Bl -column atan_(y,x)_:=____ sign(y)_(Pi_atan2(Xy_xX))___ 83.It Fn atan2 y x No := Ta 84.Fn atan y/x Ta 85if 86.Ar x 87> 0, 88.It Ta sign( Ns Ar y Ns )*(\*(Pi - 89.Fn atan "\\*(Bay/x\\*(Ba" ) Ta 90if 91.Ar x 92< 0, 93.It Ta 94.No 0 Ta 95if x = y = 0, or 96.It Ta 97.Pf sign( Ar y Ns )*\\*(Pi/2 Ta 98if 99.Ar x 100= 0 \(!= 101.Ar y . 102.El 103.Sh NOTES 104The function 105.Fn atan2 106defines "if x > 0," 107.Fn atan2 0 0 108= 0 on a 109.Tn VAX 110despite that previously 111.Fn atan2 0 0 112may have generated an error message. 113The reasons for assigning a value to 114.Fn atan2 0 0 115are these: 116.Bl -enum -offset indent 117.It 118Programs that test arguments to avoid computing 119.Fn atan2 0 0 120must be indifferent to its value. 121Programs that require it to be invalid are vulnerable 122to diverse reactions to that invalidity on diverse computer systems. 123.It 124The 125.Fn atan2 126function is used mostly to convert from rectangular (x,y) 127to polar 128.if n\ 129(r,theta) 130.if t\ 131(r,\(*h) 132coordinates that must satisfy x = 133.if n\ 134r\(**cos theta 135.if t\ 136r\(**cos\(*h 137and y = 138.if n\ 139r\(**sin theta. 140.if t\ 141r\(**sin\(*h. 142These equations are satisfied when (x=0,y=0) 143is mapped to 144.if n \ 145(r=0,theta=0) 146.if t \ 147(r=0,\(*h=0) 148on a VAX. In general, conversions to polar coordinates 149should be computed thus: 150.Bd -unfilled -offset indent 151.if n \{\ 152r := hypot(x,y); ... := sqrt(x\(**x+y\(**y) 153theta := atan2(y,x). 154.\} 155.if t \{\ 156r := hypot(x,y); ... := \(sr(x\u\s82\s10\d+y\u\s82\s10\d) 157\(*h := atan2(y,x). 158.\} 159.Ed 160.It 161The foregoing formulas need not be altered to cope in a 162reasonable way with signed zeros and infinities 163on a machine that conforms to 164.Tn IEEE 754 ; 165the versions of 166.Xr hypot 3 167and 168.Fn atan2 169provided for 170such a machine are designed to handle all cases. 171That is why 172.Fn atan2 \(+-0 \-0 173= \(+-\*(Pi 174for instance. 175In general the formulas above are equivalent to these: 176.Bd -unfilled -offset indent 177.if n \ 178r := sqrt(x\(**x+y\(**y); if r = 0 then x := copysign(1,x); 179.if t \ 180r := \(sr(x\(**x+y\(**y);\0\0if r = 0 then x := copysign(1,x); 181.Ed 182.El 183.Sh SEE ALSO 184.Xr acos 3 , 185.Xr asin 3 , 186.Xr atan 3 , 187.Xr cos 3 , 188.Xr cosh 3 , 189.Xr math 3 , 190.Xr sin 3 , 191.Xr sinh 3 , 192.Xr tan 3 , 193.Xr tanh 3 194.Sh STANDARDS 195The 196.Fn atan2 197function conforms to 198.St -isoC . 199