The gamma function of a real ball
#include <flint/arb_hypgeom.h>Sets res to the gamma function . The result is indeterminate if contains a pole .
arb_hypgeom_gamma passes exact arguments that are multiples of (and above 1000 bits, of ) to arb_hypgeom_gamma_fmpq; otherwise it uses the tabulated Taylor series of arb_hypgeom_gamma_taylor if and prec are small enough, and the Stirling series of arb_hypgeom_gamma_stirling otherwise. arb_hypgeom_gamma_fmpq computes as an exact factorial for moderate integers, reduces arguments with denominator 2, 3, 4 or 6 to the constants , and , uses a hypergeometric series summed by binary splitting at very high precision for other denominators, and otherwise the Taylor or Stirling series. The selection is described in The gamma function of a real ball. arb_gamma, arb_gamma_fmpq and arb_gamma_fmpz of arb call these functions.
Sets res to the reciprocal gamma function , which is entire and zero at the nonpositive integers.
Uses the same methods as arb_hypgeom_gamma, applied to the reciprocal, so that may contain a pole of . This is the function behind arb_rgamma.
Sets res to for . The result is indeterminate unless is positive; acb_hypgeom_lgamma computes the log-gamma function of negative and complex arguments.
Computes directly from the Stirling series, with argument reduction by the logarithm of a rising factorial, so that huge cause no overflow; exact rational arguments and arguments for which the Taylor series applies go through the gamma function followed by a logarithm. This is the function behind arb_lgamma.
arb_hypgeom_gamma, arb_hypgeom_rgamma and arb_hypgeom_lgamma compute , and for a real ball ; arb_hypgeom_gamma_fmpq and arb_hypgeom_gamma_fmpz take an exact rational or integer argument. They are the implementations behind arb_gamma, arb_rgamma, arb_lgamma, arb_gamma_fmpq and arb_gamma_fmpz. Each combines three methods: exact or hypergeometric formulas for rational arguments with small denominators, a tabulated Taylor series of at low and medium precision, and the Stirling series with argument reduction for large arguments and high precision. The algorithms are described with their error bounds in The gamma function and in [Joh2023g].
Values and domains#
- has poles at ; if contains one,
arb_hypgeom_gammareturns an indeterminate result. - is entire;
arb_hypgeom_rgammareturns exactly zero at the poles of and a finite enclosure for balls containing them (if necessary by computing ). arb_hypgeom_lgammais defined only for , where ; for other the result is indeterminate. The log-gamma function of negative and complex arguments, which is the principal branch (not ), isacb_hypgeom_lgamma.- Ball exponents are arbitrary-precision integers, so the functions do not overflow: is a ball with a binary exponent of about , and
arb_hypgeom_lgammacomputes without forming unless is an exact small fraction. Arguments larger than about give an indeterminate result.
Choice of method#
arb_hypgeom_gamma and arb_hypgeom_rgamma proceed as follows.
- Exact small fractions. If is exact, and is an integer (above 1000 bits also if has at most fractional bits), is converted to a rational number and passed to
arb_hypgeom_gamma_fmpq. Infinite and NaN midpoints, and the poles, are handled at this point too. - Taylor series.
arb_hypgeom_gamma_tayloris tried next. It applies to , up to a working precision of 3456 bits, as long as the shift of the argument reduction (the nearest integer to ) satisfies . In this range it is the fastest method by far: the work is one polynomial evaluation with tabulated coefficients and at most a short rising factorial. - Stirling series. Otherwise
arb_hypgeom_gamma_stirlingapplies the reflection formula for , shifts the argument up to with , and sums the Stirling series.
For inexact , the Taylor method evaluates at the midpoint and adds a bound for the propagated error, while the Stirling method lowers the working precision to the accuracy of the input plus 20 bits; very wide balls are bounded directly using the monotonicity of on each side of its minimum at .
arb_hypgeom_gamma_fmpq uses the following methods for :
- computed exactly for a positive integer when or is small compared to the precision;
- for and not too large, reduction to with by a rising factorial, and , , and the reflection and multiplication formulas for the others; the two constants are computed once per precision and cached;
- for other denominators above about bits, a hypergeometric series for summed by binary splitting, which costs at bits;
- otherwise the Taylor or Stirling series at the rational argument rounded to a ball.
arb_hypgeom_lgamma uses the same exact and Taylor methods followed by a logarithm, and otherwise a Stirling series for , so that no exponential of a huge number is formed.
Example#
The program computes , of a ball, the values at a pole and :
#include <flint/fmpq.h>
#include <flint/arb.h>
#include <flint/arb_hypgeom.h>
int main(void)
{
arb_t x, y;
fmpq_t q;
arb_init(x);
arb_init(y);
fmpq_init(q);
/* Gamma(1/3) from the cached constant */
fmpq_set_si(q, 1, 3);
arb_hypgeom_gamma_fmpq(y, q, 128);
flint_printf("Gamma(1/3) = "); arb_printn(y, 35, 0); flint_printf("\n");
/* a ball argument: the result contains Gamma(t) for all t in x */
arb_set_str(x, "3.25 +/- 1e-10", 128);
arb_hypgeom_gamma(y, x, 128);
flint_printf("Gamma(3.25+-e) = "); arb_printn(y, 20, 0); flint_printf("\n");
/* the reciprocal gamma function is zero at the poles */
arb_set_si(x, -3);
arb_hypgeom_rgamma(y, x, 128);
flint_printf("1/Gamma(-3) = "); arb_printn(y, 20, 0); flint_printf("\n");
arb_hypgeom_gamma(y, x, 128);
flint_printf("Gamma(-3) = "); arb_printn(y, 20, 0); flint_printf("\n");
/* log Gamma(10^30) does not overflow */
arb_set_str(x, "1e30", 128);
arb_hypgeom_lgamma(y, x, 128);
flint_printf("lgamma(10^30) = "); arb_printn(y, 38, 0); flint_printf("\n");
arb_clear(x);
arb_clear(y);
fmpq_clear(q);
flint_cleanup();
return 0;
}Gamma(1/3) = [2.6789385347077476336556929409746776 +/- 4.42e-35] Gamma(3.25+-e) = [2.549256967 +/- 5.45e-10] 1/Gamma(-3) = 0 Gamma(-3) = nan lgamma(10^30) = [68077552789821370520539743640497.306390 +/- 3.10e-7]
For the ball , the radius of the result is of the order of : the error propagated from the input dominates, and the midpoint is printed only to the digits that the radius justifies. The value has 126 correct bits, the accuracy requested.
Guides#
- Using acb_hypgeom (
acb_hypgeom)
References#
- [Joh2023g] F. Johansson. Arbitrary-precision computation of the gamma function. Maple Transactions 3:1 (2023), article 14591. https://doi.org/10.5206/mt.v3i1.14591 (preprint: https://arxiv.org/abs/2109.08392)
The FLINT team, “The gamma function of a real ball (arb_hypgeom.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/arb_hypgeom/gamma.html (generated 2026-11-02).
@misc{flint-doc-arb-hypgeom-gamma,
author = {{The FLINT team}},
title = {{The gamma function of a real ball (arb\_hypgeom.h)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/arb_hypgeom/gamma.html},
note = {Generated 2026-11-02}
}