LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 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 // The original C code, the long comment, and the constants | 7 // The original C code, the long comment, and the constants |
8 // below are from http://netlib.sandia.gov/cephes/cprob/gamma.c. | 8 // below are from http://netlib.sandia.gov/cephes/cprob/gamma.c. |
9 // The go code is a simplified version of the original C. | 9 // The go code is a simplified version of the original C. |
10 // | 10 // |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 // | 114 // |
115 // Special cases are: | 115 // Special cases are: |
116 // Gamma(±Inf) = ±Inf | 116 // Gamma(±Inf) = ±Inf |
117 // Gamma(NaN) = NaN | 117 // Gamma(NaN) = NaN |
118 // Large values overflow to +Inf. | 118 // Large values overflow to +Inf. |
119 // Negative integer values equal ±Inf. | 119 // Negative integer values equal ±Inf. |
120 func Gamma(x float64) float64 { | 120 func Gamma(x float64) float64 { |
121 const Euler = 0.57721566490153286060651209008240243104215933593992 // A0
01620 | 121 const Euler = 0.57721566490153286060651209008240243104215933593992 // A0
01620 |
122 // special cases | 122 // special cases |
123 switch { | 123 switch { |
124 » case x < -MaxFloat64 || x != x: // IsInf(x, -1) || IsNaN(x): | 124 » case IsInf(x, -1) || IsNaN(x): |
125 return x | 125 return x |
126 case x < -170.5674972726612 || x > 171.61447887182298: | 126 case x < -170.5674972726612 || x > 171.61447887182298: |
127 return Inf(1) | 127 return Inf(1) |
128 } | 128 } |
129 q := Abs(x) | 129 q := Abs(x) |
130 p := Floor(q) | 130 p := Floor(q) |
131 if q > 33 { | 131 if q > 33 { |
132 if x >= 0 { | 132 if x >= 0 { |
133 return stirling(x) | 133 return stirling(x) |
134 } | 134 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gam
P[5])*x + _gamP[6] | 178 p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gam
P[5])*x + _gamP[6] |
179 q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_ga
mQ[5])*x+_gamQ[6])*x + _gamQ[7] | 179 q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_ga
mQ[5])*x+_gamQ[6])*x + _gamQ[7] |
180 return z * p / q | 180 return z * p / q |
181 | 181 |
182 small: | 182 small: |
183 if x == 0 { | 183 if x == 0 { |
184 return Inf(1) | 184 return Inf(1) |
185 } | 185 } |
186 return z / ((1 + Euler*x) * x) | 186 return z / ((1 + Euler*x) * x) |
187 } | 187 } |
LEFT | RIGHT |