149230Scael.\" Copyright (c) 1985, 1991 Regents of the University of California.
248378Scael.\" All rights reserved.
324694Smckusick.\"
449230Scael.\" %sccs.include.redist.roff%
524694Smckusick.\"
6*50713Scael.\"     @(#)lgamma.3	6.5 (Berkeley) 07/31/91
748378Scael.\"
849230Scael.Dd
949230Scael.Dt LGAMMA 3
1049230Scael.Os BSD 4.3
1149230Scael.Sh NAME
1249230Scael.Nm lgamma
1349230Scael.Nd log gamma function
1449230Scael.Sh SYNOPSIS
1549230Scael.Fd #include <math.h>
1649230Scael.Ft double
1749230Scael.Fn lgamma "double x"
1849230Scael.Sh DESCRIPTION
1924694Smckusick.ta \w'Lgamma returns ln\||\(*G(x)| where'u+1n +1.7i
2024694Smckusick.if t \{\
2149230ScaelLgamma returns ln\||\(*G(x)| where
2249230Scael.Bd -unfilled -offset indent
2349230Scael\(*G(x) = \(is\d\s8\z0\s10\u\u\s8\(if\s10\d t\u\s8x\-1\s10\d e\u\s8\-t\s10\d dt	for x > 0 and
2424694Smckusick.br
25*50713Scael\(*G(x) = \(*p/(\(*G(1\-x)\|sin(\(*px))	for x < 1.
2649230Scael.Ed
27*50713Scael.\}
2824694Smckusick.if n \
2924694SmckusickLgamma returns ln\||\(*G(x)|.
3024694Smckusick.ta
3149230Scael.Pp
3249230ScaelThe external integer
3349230Scael.Fa signgam
3449230Scaelreturns the sign of
3524694Smckusick\(*G(x) .
3649230Scael.Sh IDIOSYNCRASIES
3749230ScaelDo
3849230Scael.Em not
3949230Scaeluse the expression
4049230Scael.Dq Li signgam\(**exp(lgamma(x))
4124694Smckusickto compute g := \(*G(x).  Instead use a program like this (in C):
4249230Scael.Bd -literal -offset indent
4324694Smckusicklg = lgamma(x); g = signgam\(**exp(lg);
4449230Scael.Ed
4549230Scael.Pp
4649230ScaelOnly after
4749230Scael.Fn lgamma
4849230Scaelhas returned can signgam be correct.
4924694SmckusickNote too that \(*G(x) must overflow when x is large enough,
5024694Smckusickunderflow when \-x is large enough, and spawn a division by zero
5124694Smckusickwhen x is a nonpositive integer.
5249230Scael.Pp
5349230ScaelOnly in the
5449230Scael.Tn UNIX
5549230Scaelmath library for C was the name gamma ever attached
5649230Scaelto ln\(*G.  Elsewhere, for instance in
5749230Scael.Tn IBM Ns 's
5849230Scael.Tn FORTRAN
5949230Scaellibrary, the name
6049230Scael.Tn GAMMA
6149230Scaelbelongs to \(*G and the name
6249230Scael.Tn ALGAMA
6349230Scaelto ln\(*G in single precision;
6449230Scaelin double the names are
6549230Scael.Tn DGAMMA
6649230Scaeland
6749230Scael.Tn DLGAMA .
6849230ScaelWhy should C be different?
6949230Scael.Pp
7049230ScaelArchaeological records suggest that C's
7149230Scael.Em gamma
7249230Scaeloriginally delivered
7324694Smckusickln(\(*G(|x|)).  Later, the program gamma was changed to
7424694Smckusickcope with negative arguments x in a more conventional way, but
7524694Smckusickthe documentation did not reflect that change correctly.  The most
7624694Smckusickrecent change corrects inaccurate values when x is almost a
7724694Smckusicknegative integer, and lets \(*G(x) be computed without
7824694Smckusickconditional expressions.  Programmers should not assume that
7949230Scael.Fn lgamma
8049230Scaelhas settled down.
8149230Scael.Pp
8249230ScaelAt some time in the future, the name
8349230Scael.Em gamma
8449230Scaelwill be rehabilitated
8549230Scaeland used for the gamma function, just as is done in
8649230Scael.Tn FORTRAN .
8749230ScaelThe reason for this is not so much compatibility with
8849230Scael.Tn FORTRAN
8949230Scaelas a
9024694Smckusickdesire to achieve greater speed for smaller values of |x| and greater
9124694Smckusickaccuracy for larger values.
9249230Scael.Pp
9349230ScaelMeanwhile, programmers who have to use the name
9449230Scael.Em gamma
9549230Scaelin its former
9649230Scaelsense, for what is now
9749230Scael.Fn lgamma ,
9849230Scaelhave two choices:
9949230Scael.Bl -enum -width indent
10049230Scael.It
10149230ScaelUse the old math library,
10249230Scael.Pa libom .
10349230Scael.It
10424694SmckusickAdd the following program to your others:
10549230Scael.Bd -literal -offset indent
10649230Scael#include <math.h>
10724694Smckusickdouble gamma(x)
10824694Smckusickdouble x;
10924694Smckusick{
11049230Scael	return (lgamma(x));
11149230Scael}
11249230Scael.Ed
113*50713Scael.El
11449230Scael.Sh DIAGNOSTICS
11549230ScaelThe reserved operand is returned on a
11649230Scael.Tn VAX
11749230Scaelfor negative integer arguments,
11849230Scael.Va errno
11949230Scaelis set to
12049230Scael.Er ERANGE ;
12149230Scaelfor very large arguments over/underflows will
12249230Scaeloccur inside the
12349230Scael.Fn lgamma
12449230Scaelroutine.
12549230Scael.Sh SEE ALSO
12649230Scael.Xr math 3 ,
12749230Scael.Xr infnan 3
12849230Scael.Sh HISTORY
12949230ScaelThe
13049230Scael.Nm
13149230Scaelfunction appeared in
13249230Scael.Bx 4.3 .
133