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