atan2.3 5.1 KB

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