loggamma
This page explains how to use the loggamma function in APL.
Use the loggamma function in APL to compute the natural logarithm of the absolute value of the gamma function. This is equivalent to log(abs(gamma(x))) but avoids the numeric overflow that occurs when gamma(x) itself becomes too large to represent as a floating-point number.
loggamma is useful when you work with large inputs to the gamma function in statistical calculations, such as log-likelihood computations, Bayesian modeling, or custom anomaly scoring. Use it whenever gamma(x) would overflow to infinity.
Usage
Syntax
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
x | real | Yes | The input value. Must not be zero or a negative integer. |
Returns
The natural logarithm of the absolute value of the gamma function of x.
Example
Use loggamma to compute the log-gamma value of a duration in seconds without risking numeric overflow.
Query
Output
| _time | id | req_duration_ms | loggamma_val |
|---|---|---|---|
| 2024-11-14 10:00:00 | user-1 | 1000.0 | 0.0000 |
| 2024-11-14 10:01:00 | user-2 | 2000.0 | 0.0000 |
| 2024-11-14 10:02:00 | user-3 | 10000.0 | 16.1181 |
List of related functions
- gamma: Returns the gamma function directly. Use it for small inputs where overflow isn't a concern.
- log: Returns the natural logarithm. Use it for general log-transformation without the gamma relationship.
- exp: Returns e^x. Use it to exponentiate
loggammaresults back to the original gamma scale when the values are small enough. - isfinite: Returns whether a value is finite. Use it to verify that
loggammaresults are valid for downstream calculations. - abs: Returns the absolute value. Note that
loggamma(x)equalslog(abs(gamma(x))), soabsis implicitly applied togamma(x)before the log.