xref: /freebsd/lib/msun/man/atan2.3 (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
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.\" $FreeBSD$
30.\"
31.Dd July 31, 2008
32.Dt ATAN2 3
33.Os
34.Sh NAME
35.Nm atan2 ,
36.Nm atan2f ,
37.Nm atan2l ,
38.Nm carg ,
39.Nm cargf ,
40.Nm cargl
41.Nd arc tangent and complex phase angle functions
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.Ft long double
51.Fn atan2l "long double y" "long double x"
52.In complex.h
53.Ft double
54.Fn carg "double complex z"
55.Ft float
56.Fn cargf "float complex z"
57.Ft long double
58.Fn cargl "long double complex z"
59.Sh DESCRIPTION
60The
61.Fn atan2 ,
62.Fn atan2f ,
63and
64.Fn atan2l
65functions compute the principal value of the arc tangent of
66.Fa y/ Ns Fa x ,
67using the signs of both arguments to determine the quadrant of
68the return value.
69.Pp
70The
71.Fn carg ,
72.Fn cargf ,
73and
74.Fn cargl
75functions compute the complex argument (or phase angle) of
76.Fa z .
77The complex argument is the number theta such that
78.Li z = r * e^(I * theta) ,
79where
80.Li r = cabs(z) .
81The call
82.Li carg(z)
83is equivalent to
84.Li atan2(cimag(z), creal(z)) ,
85and similarly for
86.Fn cargf
87and
88.Fn cargl .
89.Sh RETURN VALUES
90The
91.Fn atan2 ,
92.Fn atan2f ,
93and
94.Fn atan2l
95functions, if successful,
96return the arc tangent of
97.Fa y/ Ns Fa x
98in the range
99.Bk -words
100.Bq \&- Ns \*(Pi , \&+ Ns \*(Pi
101.Ek
102radians.
103Here are some of the special cases:
104.Bl -column atan_(y,x)_:=____  sign(y)_(Pi_atan2(Xy_xX))___
105.It Fn atan2 y x No := Ta
106.Fn atan y/x Ta
107if
108.Fa x
109> 0,
110.It Ta sign( Ns Fa y Ns )*(\*(Pi -
111.Fn atan "\*(Bay/x\*(Ba" ) Ta
112if
113.Fa x
114< 0,
115.It Ta
116.No 0 Ta
117if x = y = 0, or
118.It Ta
119.Pf sign( Fa y Ns )*\*(Pi/2 Ta
120if
121.Fa x
122= 0 \(!=
123.Fa y .
124.El
125.Sh NOTES
126The function
127.Fn atan2
128defines "if x > 0,"
129.Fn atan2 0 0
130= 0 despite that previously
131.Fn atan2 0 0
132may have generated an error message.
133The reasons for assigning a value to
134.Fn atan2 0 0
135are these:
136.Bl -enum -offset indent
137.It
138Programs that test arguments to avoid computing
139.Fn atan2 0 0
140must be indifferent to its value.
141Programs that require it to be invalid are vulnerable
142to diverse reactions to that invalidity on diverse computer systems.
143.It
144The
145.Fn atan2
146function is used mostly to convert from rectangular (x,y)
147to polar
148.if n\
149(r,theta)
150.if t\
151(r,\(*h)
152coordinates that must satisfy x =
153.if n\
154r\(**cos theta
155.if t\
156r\(**cos\(*h
157and y =
158.if n\
159r\(**sin theta.
160.if t\
161r\(**sin\(*h.
162These equations are satisfied when (x=0,y=0)
163is mapped to
164.if n \
165(r=0,theta=0).
166.if t \
167(r=0,\(*h=0).
168In general, conversions to polar coordinates
169should be computed thus:
170.Bd -unfilled -offset indent
171.if n \{\
172r	:= hypot(x,y);  ... := sqrt(x\(**x+y\(**y)
173theta	:= atan2(y,x).
174.\}
175.if t \{\
176r	:= hypot(x,y);  ... := \(sr(x\u\s82\s10\d+y\u\s82\s10\d)
177\(*h	:= atan2(y,x).
178.\}
179.Ed
180.It
181The foregoing formulas need not be altered to cope in a
182reasonable way with signed zeros and infinities
183on a machine that conforms to
184.Tn IEEE 754 ;
185the versions of
186.Xr hypot 3
187and
188.Fn atan2
189provided for
190such a machine are designed to handle all cases.
191That is why
192.Fn atan2 \(+-0 \-0
193= \(+-\*(Pi
194for instance.
195In general the formulas above are equivalent to these:
196.Bd -unfilled -offset indent
197.if n \
198r := sqrt(x\(**x+y\(**y); if r = 0 then x := copysign(1,x);
199.if t \
200r := \(sr(x\(**x+y\(**y);\0\0if r = 0 then x := copysign(1,x);
201.Ed
202.El
203.Sh SEE ALSO
204.Xr acos 3 ,
205.Xr asin 3 ,
206.Xr atan 3 ,
207.Xr cabs 3 ,
208.Xr cos 3 ,
209.Xr cosh 3 ,
210.Xr math 3 ,
211.Xr sin 3 ,
212.Xr sinh 3 ,
213.Xr tan 3 ,
214.Xr tanh 3
215.Sh STANDARDS
216The
217.Fn atan2 ,
218.Fn atan2f ,
219.Fn atan2l ,
220.Fn carg ,
221.Fn cargf ,
222and
223.Fn cargl
224functions conform to
225.St -isoC-99 .
226