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