LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package math | 5 package math |
6 | 6 |
7 // Atan2 returns the arc tangent of y/x, using | 7 // Atan2 returns the arc tangent of y/x, using |
8 // the signs of the two to determine the quadrant | 8 // the signs of the two to determine the quadrant |
9 // of the return value. | 9 // of the return value. |
10 // | 10 // |
(...skipping 11 matching lines...) Expand all Loading... |
22 // Atan2(+Inf, -Inf) = 3Pi/4 | 22 // Atan2(+Inf, -Inf) = 3Pi/4 |
23 // Atan2(-Inf, -Inf) = -3Pi/4 | 23 // Atan2(-Inf, -Inf) = -3Pi/4 |
24 // Atan2(y, +Inf) = 0 | 24 // Atan2(y, +Inf) = 0 |
25 // Atan2(y>0, -Inf) = +Pi | 25 // Atan2(y>0, -Inf) = +Pi |
26 // Atan2(y<0, -Inf) = -Pi | 26 // Atan2(y<0, -Inf) = -Pi |
27 // Atan2(+Inf, x) = +Pi/2 | 27 // Atan2(+Inf, x) = +Pi/2 |
28 // Atan2(-Inf, x) = -Pi/2 | 28 // Atan2(-Inf, x) = -Pi/2 |
29 func Atan2(y, x float64) float64 | 29 func Atan2(y, x float64) float64 |
30 | 30 |
31 func atan2(y, x float64) float64 { | 31 func atan2(y, x float64) float64 { |
32 // TODO(rsc): Remove manual inlining of IsNaN, IsInf | |
33 // when compiler does it for us | |
34 // special cases | 32 // special cases |
35 switch { | 33 switch { |
36 » case y != y || x != x: // IsNaN(y) || IsNaN(x): | 34 » case IsNaN(y) || IsNaN(x): |
37 return NaN() | 35 return NaN() |
38 case y == 0: | 36 case y == 0: |
39 if x >= 0 && !Signbit(x) { | 37 if x >= 0 && !Signbit(x) { |
40 return Copysign(0, y) | 38 return Copysign(0, y) |
41 } | 39 } |
42 return Copysign(Pi, y) | 40 return Copysign(Pi, y) |
43 case x == 0: | 41 case x == 0: |
44 return Copysign(Pi/2, y) | 42 return Copysign(Pi/2, y) |
45 » case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0): | 43 » case IsInf(x, 0): |
46 » » if x > MaxFloat64 { // IsInf(x, 1) { | 44 » » if IsInf(x, 1) { |
47 switch { | 45 switch { |
48 » » » case y < -MaxFloat64 || y > MaxFloat64: // IsInf(y, -1)
|| IsInf(y, 1): | 46 » » » case IsInf(y, 0): |
49 return Copysign(Pi/4, y) | 47 return Copysign(Pi/4, y) |
50 default: | 48 default: |
51 return Copysign(0, y) | 49 return Copysign(0, y) |
52 } | 50 } |
53 } | 51 } |
54 switch { | 52 switch { |
55 » » case y < -MaxFloat64 || y > MaxFloat64: // IsInf(y, -1) || IsInf
(y, 1): | 53 » » case IsInf(y, 0): |
56 return Copysign(3*Pi/4, y) | 54 return Copysign(3*Pi/4, y) |
57 default: | 55 default: |
58 return Copysign(Pi, y) | 56 return Copysign(Pi, y) |
59 } | 57 } |
60 » case y < -MaxFloat64 || y > MaxFloat64: //IsInf(y, 0): | 58 » case IsInf(y, 0): |
61 return Copysign(Pi/2, y) | 59 return Copysign(Pi/2, y) |
62 } | 60 } |
63 | 61 |
64 // Call atan and determine the quadrant. | 62 // Call atan and determine the quadrant. |
65 q := Atan(y / x) | 63 q := Atan(y / x) |
66 if x < 0 { | 64 if x < 0 { |
67 if q <= 0 { | 65 if q <= 0 { |
68 return q + Pi | 66 return q + Pi |
69 } | 67 } |
70 return q - Pi | 68 return q - Pi |
71 } | 69 } |
72 return q | 70 return q |
73 } | 71 } |
LEFT | RIGHT |