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