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 func isOddInt(x float64) bool { | 7 func isOddInt(x float64) bool { |
8 xi, xf := Modf(x) | 8 xi, xf := Modf(x) |
9 return xf == 0 && int64(xi)&1 == 1 | 9 return xf == 0 && int64(xi)&1 == 1 |
10 } | 10 } |
(...skipping 18 matching lines...) Expand all Loading... |
29 // Pow(-1, ±Inf) = 1 | 29 // Pow(-1, ±Inf) = 1 |
30 // Pow(x, +Inf) = +Inf for |x| > 1 | 30 // Pow(x, +Inf) = +Inf for |x| > 1 |
31 // Pow(x, -Inf) = +0 for |x| > 1 | 31 // Pow(x, -Inf) = +0 for |x| > 1 |
32 // Pow(x, +Inf) = +0 for |x| < 1 | 32 // Pow(x, +Inf) = +0 for |x| < 1 |
33 // Pow(x, -Inf) = +Inf for |x| < 1 | 33 // Pow(x, -Inf) = +Inf for |x| < 1 |
34 // Pow(+Inf, y) = +Inf for y > 0 | 34 // Pow(+Inf, y) = +Inf for y > 0 |
35 // Pow(+Inf, y) = +0 for y < 0 | 35 // Pow(+Inf, y) = +0 for y < 0 |
36 // Pow(-Inf, y) = Pow(-0, -y) | 36 // Pow(-Inf, y) = Pow(-0, -y) |
37 // Pow(x, y) = NaN for finite x < 0 and finite non-integer y | 37 // Pow(x, y) = NaN for finite x < 0 and finite non-integer y |
38 func Pow(x, y float64) float64 { | 38 func Pow(x, y float64) float64 { |
39 // TODO(rsc): Remove manual inlining of IsNaN, IsInf | |
40 // when compiler does it for us | |
41 switch { | 39 switch { |
42 case y == 0 || x == 1: | 40 case y == 0 || x == 1: |
43 return 1 | 41 return 1 |
44 case y == 1: | 42 case y == 1: |
45 return x | 43 return x |
46 case y == 0.5: | 44 case y == 0.5: |
47 return Sqrt(x) | 45 return Sqrt(x) |
48 case y == -0.5: | 46 case y == -0.5: |
49 return 1 / Sqrt(x) | 47 return 1 / Sqrt(x) |
50 » case x != x || y != y: // IsNaN(x) || IsNaN(y): | 48 » case IsNaN(x) || IsNaN(y): |
51 return NaN() | 49 return NaN() |
52 case x == 0: | 50 case x == 0: |
53 switch { | 51 switch { |
54 case y < 0: | 52 case y < 0: |
55 if isOddInt(y) { | 53 if isOddInt(y) { |
56 return Copysign(Inf(1), x) | 54 return Copysign(Inf(1), x) |
57 } | 55 } |
58 return Inf(1) | 56 return Inf(1) |
59 case y > 0: | 57 case y > 0: |
60 if isOddInt(y) { | 58 if isOddInt(y) { |
61 return x | 59 return x |
62 } | 60 } |
63 return 0 | 61 return 0 |
64 } | 62 } |
65 » case y > MaxFloat64 || y < -MaxFloat64: // IsInf(y, 0): | 63 » case IsInf(y, 0): |
66 switch { | 64 switch { |
67 case x == -1: | 65 case x == -1: |
68 return 1 | 66 return 1 |
69 case (Abs(x) < 1) == IsInf(y, 1): | 67 case (Abs(x) < 1) == IsInf(y, 1): |
70 return 0 | 68 return 0 |
71 default: | 69 default: |
72 return Inf(1) | 70 return Inf(1) |
73 } | 71 } |
74 » case x > MaxFloat64 || x < -MaxFloat64: // IsInf(x, 0): | 72 » case IsInf(x, 0): |
75 if IsInf(x, -1) { | 73 if IsInf(x, -1) { |
76 return Pow(1/x, -y) // Pow(-0, -y) | 74 return Pow(1/x, -y) // Pow(-0, -y) |
77 } | 75 } |
78 switch { | 76 switch { |
79 case y < 0: | 77 case y < 0: |
80 return 0 | 78 return 0 |
81 case y > 0: | 79 case y > 0: |
82 return Inf(1) | 80 return Inf(1) |
83 } | 81 } |
84 } | 82 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 128 |
131 // ans = a1*2**ae | 129 // ans = a1*2**ae |
132 // if flip { ans = 1 / ans } | 130 // if flip { ans = 1 / ans } |
133 // but in the opposite order | 131 // but in the opposite order |
134 if flip { | 132 if flip { |
135 a1 = 1 / a1 | 133 a1 = 1 / a1 |
136 ae = -ae | 134 ae = -ae |
137 } | 135 } |
138 return Ldexp(a1, ae) | 136 return Ldexp(a1, ae) |
139 } | 137 } |
LEFT | RIGHT |