1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Atan2 returns the arc tangent of y/x, using
8 // the signs of the two to determine the quadrant
9 // of the return value.
11 // Special cases are (in order):
12 // Atan2(y, NaN) = NaN
13 // Atan2(NaN, x) = NaN
14 // Atan2(+0, x>=0) = +0
15 // Atan2(-0, x>=0) = -0
16 // Atan2(+0, x<=-0) = +Pi
17 // Atan2(-0, x<=-0) = -Pi
18 // Atan2(y>0, 0) = +Pi/2
19 // Atan2(y<0, 0) = -Pi/2
20 // Atan2(+Inf, +Inf) = +Pi/4
21 // Atan2(-Inf, +Inf) = -Pi/4
22 // Atan2(+Inf, -Inf) = 3Pi/4
23 // Atan2(-Inf, -Inf) = -3Pi/4
25 // Atan2(y>0, -Inf) = +Pi
26 // Atan2(y<0, -Inf) = -Pi
27 // Atan2(+Inf, x) = +Pi/2
28 // Atan2(-Inf, x) = -Pi/2
29 func Atan2(y, x float64) float64 {
30 // TODO(rsc): Remove manual inlining of IsNaN, IsInf
31 // when compiler does it for us
34 case y != y || x != x: // IsNaN(y) || IsNaN(x):
37 if x >= 0 && !Signbit(x) {
40 return Copysign(Pi, y)
42 return Copysign(Pi/2, y)
43 case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0):
44 if x > MaxFloat64 { // IsInf(x, 1) {
46 case y < -MaxFloat64 || y > MaxFloat64: // IsInf(y, -1) || IsInf(y, 1):
47 return Copysign(Pi/4, y)
53 case y < -MaxFloat64 || y > MaxFloat64: // IsInf(y, -1) || IsInf(y, 1):
54 return Copysign(3*Pi/4, y)
56 return Copysign(Pi, y)
58 case y < -MaxFloat64 || y > MaxFloat64: //IsInf(y, 0):
59 return Copysign(Pi/2, y)
62 // Call atan and determine the quadrant.