Cloud

gamma

The gamma() function returns the gamma function of a given number. The gamma function extends the factorial to real numbers: for a positive integer n, gamma(n) equals (n - 1)!.

Syntax

GAMMA(number)

Arguments

  • number: A required double precision value. gamma() accepts real values and computes them as double precision.

Return type

gamma() returns a double precision value, or NULL if the argument is NULL. If the result cannot be represented as a double precision value, gamma() returns an error instead. See Out-of-range inputs.

Examples

These examples show how to use the gamma() function.

Gamma of a positive integer

For a positive integer n, gamma(n) equals (n - 1)!, so gamma(5) equals 4! = 24:

SELECT gamma(5);
 gamma
-------
    24
(1 row)

Gamma of a fractional value

For non-integer inputs, gamma() returns a fractional result. For example, gamma(0.5) returns the square root of pi (approximately 1.772).

Out-of-range inputs

The gamma function is undefined or unrepresentable for some inputs. In these cases gamma() returns an error rather than infinity or NaN:

  • Zero and the negative integers are poles of the gamma function. gamma(0) and, for example, gamma(-2) fail with:

    value out of range: overflow
  • Inputs whose result is too large for double precision fail with the same value out of range: overflow error. The largest integer with a representable result is 171, so gamma(172) overflows.

  • Large negative, non-integer inputs whose result is too small to represent fail with:

    value out of range: underflow

For the special floating-point values, gamma() returns NaN for NaN and positive infinity for positive infinity. Negative infinity fails with value out of range: overflow.