libm/math/
tgamma.rs
1extern crate core;
26use super::{exp, floor, k_cos, k_sin, pow};
27
28const PI: f64 = 3.141592653589793238462643383279502884;
29
30fn sinpi(mut x: f64) -> f64 {
32 let mut n: isize;
33
34 x = x * 0.5;
37 x = 2.0 * (x - floor(x));
38
39 n = (4.0 * x) as isize;
41 n = div!(n + 1, 2);
42 x -= (n as f64) * 0.5;
43
44 x *= PI;
45 match n {
46 1 => k_cos(x, 0.0),
47 2 => k_sin(-x, 0.0, 0),
48 3 => -k_cos(x, 0.0),
49 0 | _ => k_sin(x, 0.0, 0),
50 }
51}
52
53const N: usize = 12;
54const GMHALF: f64 = 5.524680040776729583740234375;
56const SNUM: [f64; N + 1] = [
57 23531376880.410759688572007674451636754734846804940,
58 42919803642.649098768957899047001988850926355848959,
59 35711959237.355668049440185451547166705960488635843,
60 17921034426.037209699919755754458931112671403265390,
61 6039542586.3520280050642916443072979210699388420708,
62 1439720407.3117216736632230727949123939715485786772,
63 248874557.86205415651146038641322942321632125127801,
64 31426415.585400194380614231628318205362874684987640,
65 2876370.6289353724412254090516208496135991145378768,
66 186056.26539522349504029498971604569928220784236328,
67 8071.6720023658162106380029022722506138218516325024,
68 210.82427775157934587250973392071336271166969580291,
69 2.5066282746310002701649081771338373386264310793408,
70];
71const SDEN: [f64; N + 1] = [
72 0.0,
73 39916800.0,
74 120543840.0,
75 150917976.0,
76 105258076.0,
77 45995730.0,
78 13339535.0,
79 2637558.0,
80 357423.0,
81 32670.0,
82 1925.0,
83 66.0,
84 1.0,
85];
86const FACT: [f64; 23] = [
88 1.0,
89 1.0,
90 2.0,
91 6.0,
92 24.0,
93 120.0,
94 720.0,
95 5040.0,
96 40320.0,
97 362880.0,
98 3628800.0,
99 39916800.0,
100 479001600.0,
101 6227020800.0,
102 87178291200.0,
103 1307674368000.0,
104 20922789888000.0,
105 355687428096000.0,
106 6402373705728000.0,
107 121645100408832000.0,
108 2432902008176640000.0,
109 51090942171709440000.0,
110 1124000727777607680000.0,
111];
112
113fn s(x: f64) -> f64 {
115 let mut num: f64 = 0.0;
116 let mut den: f64 = 0.0;
117
118 if x < 8.0 {
120 for i in (0..=N).rev() {
121 num = num * x + i!(SNUM, i);
122 den = den * x + i!(SDEN, i);
123 }
124 } else {
125 for i in 0..=N {
126 num = num / x + i!(SNUM, i);
127 den = den / x + i!(SDEN, i);
128 }
129 }
130 return num / den;
131}
132
133#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
134pub fn tgamma(mut x: f64) -> f64 {
135 let u: u64 = x.to_bits();
136 let absx: f64;
137 let mut y: f64;
138 let mut dy: f64;
139 let mut z: f64;
140 let mut r: f64;
141 let ix: u32 = ((u >> 32) as u32) & 0x7fffffff;
142 let sign: bool = (u >> 63) != 0;
143
144 if ix >= 0x7ff00000 {
146 return x + core::f64::INFINITY;
148 }
149 if ix < ((0x3ff - 54) << 20) {
150 return 1.0 / x;
152 }
153
154 if x == floor(x) {
157 if sign {
158 return 0.0 / 0.0;
159 }
160 if x <= FACT.len() as f64 {
161 return i!(FACT, (x as usize) - 1);
162 }
163 }
164
165 if ix >= 0x40670000 {
168 if sign {
170 let x1p_126 = f64::from_bits(0x3810000000000000); force_eval!((x1p_126 / x) as f32);
172 if floor(x) * 0.5 == floor(x * 0.5) {
173 return 0.0;
174 } else {
175 return -0.0;
176 }
177 }
178 let x1p1023 = f64::from_bits(0x7fe0000000000000); x *= x1p1023;
180 return x;
181 }
182
183 absx = if sign { -x } else { x };
184
185 y = absx + GMHALF;
187 if absx > GMHALF {
188 dy = y - absx;
189 dy -= GMHALF;
190 } else {
191 dy = y - GMHALF;
192 dy -= absx;
193 }
194
195 z = absx - 0.5;
196 r = s(absx) * exp(-y);
197 if x < 0.0 {
198 r = -PI / (sinpi(absx) * absx * r);
201 dy = -dy;
202 z = -z;
203 }
204 r += dy * (GMHALF + 0.5) * r / y;
205 z = pow(y, 0.5 * z);
206 y = r * z * z;
207 return y;
208}