pub fn lgamma(comptime T: type, x: T) T T: typex: T95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141pub fn lgamma(comptime T: type, x: T) T {
if (T != f32 and T != f64) {
@compileError("gamma not implemented for " ++ @typeName(T));
}
// common integer case first
if (x == @trunc(x)) {
// lgamma(-inf) = +inf
// lgamma(n) = +inf for negative integers
// lgamma(+-0.0) = +inf
if (x <= 0) {
return std.math.inf(T);
}
// lgamma(1) = +0.0
// lgamma(2) = +0.0
if (x < integer_result_table.len) {
const i = @as(u8, @intFromFloat(x));
return @log(@as(T, @floatCast(integer_result_table[i])));
}
// lgamma(+inf) = +inf
if (std.math.isPositiveInf(x)) {
return x;
}
}
const abs = @abs(x);
// perfect precision here
if (abs < 0x1p-54) {
return -@log(abs);
}
// obvious approach when overflow is not a problem
const upper_bound = if (T == f64) 128 else 26;
if (abs < upper_bound) {
return @log(@abs(gamma(T, x)));
}
const log_base = @log(abs + lanczos_minus_half) - 1;
const exponent = abs - 0.5;
const log_series = @log(series(T, abs));
const initial = exponent * log_base + log_series - lanczos;
// use reflection formula for negatives
if (x < 0) {
const reflected = std.math.pi / (abs * sinpi(T, abs));
return @log(@abs(reflected)) - initial;
}
return initial;
}
Returns the natural logarithm of the absolute value of the gamma function.
Special Cases: