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