Next: hypot, Previous: frexp, Up: Math

### 1.26 `gamma`, `gammaf`, `lgamma`, `lgammaf`, `gamma_r`, `gammaf_r`, `lgamma_r`, `lgammaf_r`, `tgamma`, and `tgammaf`–logarithmic and plain gamma functions

Synopsis

         #include <math.h>
         double gamma(double x);
         float gammaf(float x);
         double lgamma(double x);
         float lgammaf(float x);
         double gamma_r(double x, int *signgamp);
         float gammaf_r(float x, int *signgamp);
         double lgamma_r(double x, int *signgamp);
         float lgammaf_r(float x, int *signgamp);
         double tgamma(double x);
         float tgammaf(float x);

Description
`gamma` calculates the natural logarithm of the gamma function of x. The gamma function (`exp(gamma(`x`))`) is a generalization of factorial, and retains the property that `exp(gamma(N))` is equivalent to `N*exp(gamma(N-1))`. Accordingly, the results of the gamma function itself grow very quickly. `gamma` is defined as the natural log of the gamma function, rather than the gamma function itself, to extend the useful range of results representable.

The sign of the result is returned in the global variable `signgam`, which is declared in math.h.

`gammaf` performs the same calculation as `gamma`, but uses and returns `float` values.

`lgamma` and `lgammaf` are alternate names for `gamma` and `gammaf`. The use of `lgamma` instead of `gamma` is a reminder that these functions compute the log of the gamma function, rather than the gamma function itself.

The functions `gamma_r`, `gammaf_r`, `lgamma_r`, and `lgammaf_r` are just like `gamma`, `gammaf`, `lgamma`, and `lgammaf`, respectively, but take an additional argument. This additional argument is a pointer to an integer. This additional argument is used to return the sign of the result, and the global variable `signgam` is not used. These functions may be used for reentrant calls (but they will still set the global variable `errno` if an error occurs).

`tgamma` and `tgammaf` are the "true gamma" functions, returning the gamma function of x–without a logarithm. (They are apparently so named because of the prior existence of the old, poorly-named `gamma` functions which returned the log of gamma up through BSD 4.2.)

Returns
Normally, the computed result is returned.

When x is a nonpositive integer, `gamma` returns `HUGE_VAL` and `errno` is set to `EDOM`. If the result overflows, `gamma` returns `HUGE_VAL` and `errno` is set to `ERANGE`.

You can modify this error treatment using `matherr`.

Portability
Neither `gamma` nor `gammaf` is ANSI C. It is better not to use either of these; use `lgamma` or `tgamma` instead.
`lgamma`, `lgammaf`, `tgamma`, and `tgammaf` are nominally C standard in terms of the base return values, although the `matherr` error-handling is not standard, nor is the signgam global for `lgamma`.