lgamma, gamma, lgammal, gammal - log gamma function
double gamma(double x);
double lgamma(double x);
long double gammal(long double x);
long double lgammal(long double x);
extern int signgam;
extern int signgaml;
lgamma and gamma return ln|(GAMMA(x))|, where GAMMA is the
mathematical gamma function.
gammal and lgammal are the long double versions of the log gamma
gamma and lgamma are identical, as are gammal and lgammal.
The external integer signgam returns the sign of GAMMA(x), and the
external integer signgaml returns the sign of GAMMAL(x).
Do not use the expression signgam*exp(gamma(x)) to compute g := GAMMA(x).
Instead use a program like this (in C):
lg = gamma(x); g = signgam*exp(lg);
Only after gamma has returned can signgam be correct. Note too that
GAMMA(x) must overflow when x is large enough, and is undefined when x is
a nonpositive integer.
Analogous rules apply to gammal and signgaml.
The following C program fragment might be used to calculate G if the
overflow needs to be detected:
if ((y = gamma(x)) > LN_MAXDOUBLE)
y = signgam * exp(y);
where LN_MAXDOUBLE is the least value that causes exp(3M) to overflow and
is defined in the <values.h> header file.
Only in the UNIX math library for C was the name gamma ever attached to
ln G. Elsewhere (in some FORTRAN libraries) the name GAMMA belongs to G
and the name ALGAMMA to ln G in single precision; in double the usual
names are DGAMMA and DLGAMMA in FORTRAN. Why should C be different?
Archeological records suggest that C's gamma originally delivered ln(G
(x)). Later, the program gamma was changed to cope with negative
arguments in a more conventional way, but the documentation did not
reflect that change correctly. The most recent change corrects
inaccurate values when x is almost a negative integer.
Some math libraries have changed the name of this function to lgamma() to
suggest its real functionality.
On SGI systems, the name gamma is currently maintained as a synonym to
lgamma for compatibility. It may disappear in a future release.
lgamma and gamma are not ANSI-C functions. If you compile with flag
-ansi or -ansiposix, you must supply your own prototypes for these
functions or you will get incorrect results. (See the prototypes defined
In the diagnostics below, functions in the standard math library libm.a,
are referred to as -lm versions, those in math library libmx.a are
referred to as -lmx versions, and those in the the BSD math library
libm43.a are referred to as -lm43 versions. The -lm and -lmx versions
always return the default Quiet NaN and set errno to EDOM when a NaN is
used as an argument. A NaN argument usually causes the -lm43 versions to
return the same argument. The -lm43 versions never set errno. The value
of HUGE_VAL is IEEE Infinity.
The gamma functions return HUGE_VAL when the argument is zero or a
negative integer. The -lm and -lmx versions also set errno to EDOM.
When the correct value would overflow, the gamma functions return
HUGE_VAL. The -lm and -lmx versions also set errno to ERANGE.
See matherr(3M) for a description of error handling for -lmx functions.
PPPPaaaaggggeeee 2222 [ Back ]