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