Mathematical constants
#include <flint/mp_real.h>Set res to a ball containing , , Euler's constant , , , Catalan's constant , , , , , resp. , accurate to about limbs. With cache = 0 the value is computed from scratch; with cache = 1 it is read from (and if necessary added to) a process-wide cache of floors, and the ball is the cached floor with a radius of one ulp.
The cache holds, per constant, for the largest computed so far by any thread. Floors nest (the top limbs of are for ), so a shorter request is a copy, and a longer one recomputes the entry at limbs, so that a sequence of growing requests costs a constant factor more than the last one. , , and are read from static tables up to 64 limbs. The last two serve the argument reductions modulo and .
Set res to exactly for the constants of mp_real_const_pi4 and its siblings, and err (if not NULL) to 1. The output has limbs for the constants below 1 (, , , , , ) and limbs, with a units limb, for the others. cache is as for the ball versions.
The floor is read off a ball computed with a few guard limbs, which is recomputed with more guard limbs until its radius determines the floor. The value 1 written to err bounds in ulps; the error is one-sided.
void _mp_real_const_clear_cache(void)sourceFree the shared caches of the constants. No other thread may be using them. flint_cleanup_master also frees them.
mp_real_const_funcThe type void (*)(mp_real_t, slong, int) of the ball constant functions (mp_real_const_pi4 and so on).
void _mp_real_const_arb(arb_t res, mp_real_const_func f, slong prec)sourceSet res to the constant computed by f with cache = 0, as an arb ball of prec bits. This is the entry point of the arb constants (arb_const_pi, arb_const_log2, arb_const_euler, ...), which keep their own caches.
mp_real computes twelve constants: , , Euler's constant , , , Catalan's constant , , , , , and . They are the implementations behind arb_const_pi, arb_const_log2, arb_const_euler, arb_const_e, arb_const_log10, arb_const_catalan, arb_const_apery, arb_const_zeta5 and the values of arb_hypgeom_gamma_fmpq, and they are used inside the module by the argument reductions of the elementary functions.
Two forms#
The ball functions mp_real_const_* return a ball for the constant accurate to about limbs. The limb functions _mp_real_const_* return the exact floor as a fixed-point number with fraction limbs, plus a units limb for the constants above 1, and set err to 1: the true value lies in ulps. An exact floor is a canonical value, independent of how it was computed. It is determined from a ball computed with a few guard limbs, recomputed with more guard limbs in the rare case that the ball contains a multiple of .
Caching#
With cache = 0 the constant is computed from scratch and nothing is kept. With cache = 1 the value comes from a process-wide cache that holds the floor at the largest precision requested so far. Floors nest: the top limbs of are for , so a shorter request is a copy of the top limbs. A longer request recomputes the entry at limbs, so that a sequence of growing requests costs a bounded factor more than the largest. All threads read the same entry; one that needs a longer entry builds it under the cache's lock, and readers never wait. The ball version with cache = 1 returns the cached floor with a radius of one ulp. , , and also come from static tables of 64 limbs, so that the elementary functions at small precision touch no cache at all. _mp_real_const_clear_cache (or flint_cleanup_master) frees the caches.
The arb constants call _mp_real_const_arb, which computes with cache = 0 and leaves caching to arb's own caches.
Algorithms#
All constants except are hypergeometric series summed by mp_real_hypgeom_series, with formulas chosen from y-cruncher's formula collection as the fastest in this implementation:
| Constant | Formula |
|---|---|
| Chudnovsky's series [CC1988], 47 bits per term | |
| Zuniga's series [Zun2025], 11.9 bits per term | |
| Brent–McMillan [BM1980] with the bound of [BJ2013], two splittings in dual numbers | |
| from a three-term Machin-type formula | |
| Pilehrood's series [PP2010] | |
| a series of Zuniga (2023), 2.05 bits per term | |
| Sun's identity | |
| with Guillera's series | |
| Ebisu's series, or from limbs |
The independent parts (the series and in and the values, the three logarithms of , the logarithms needed by ) run on separate threads. The details, including the dual-number splitting for , are in Constants in mp_real.
AlgorithmConstants in mp_realThe series and formulas behind the mp_real constants (π, log 2, e, Euler's constant, log 10, Catalan's constant, ζ(3), ζ(5), Γ(1/3), Γ(1/4)), the dual-number binary splitting for Euler's constant, and the cache of exact floors.Example#
#include <flint/flint.h>
#include <flint/mp_real.h>
#include <flint/arb.h>
int main(void)
{
mp_real_t c;
arb_t a;
ulong f[3], err;
slong n = mp_real_prec_bits(200);
mp_real_init(c);
arb_init(a);
/* zeta(3) as a ball, computed from scratch */
mp_real_const_zeta3(c, n, 0);
mp_real_get_arb(a, c);
flint_printf("zeta(3) = ");
arb_printn(a, 50, 0);
flint_printf("\n");
/* the cached floor of e B^2: two fraction limbs and a units limb */
_mp_real_const_e(f, &err, 2, 1);
flint_printf("floor(e B^2) = %wu %016wx %016wx, err = %wu\n",
f[2], f[1], f[0], err);
/* Catalan's constant through the arb entry point */
_mp_real_const_arb(a, mp_real_const_catalan, 128);
flint_printf("G = ");
arb_printn(a, 30, 0);
flint_printf("\n");
mp_real_clear(c);
arb_clear(a);
flint_cleanup_master();
return 0;
}zeta(3) = [1.2020569031595942853997381615114499907649862923405 +/- 1.12e-51] floor(e B^2) = 2 b7e151628aed2a6a bf7158809cf4f3c7, err = 1 G = [0.915965594177219015054603514932 +/- 3.85e-31]
The example assumes 64-bit limbs; the hexadecimal digits of are
Guides#
References#
- [CC1988] D. V. Chudnovsky and G. V. Chudnovsky. Approximations and complex multiplication according to Ramanujan. In Ramanujan Revisited, Academic Press, 1988, 375–472.
- [Zun2025] J. Zuniga, "Fast Ramanujan-type Series for Logarithms. Part I", 2025, https://arxiv.org/abs/2506.08245
- [BM1980] R. P. Brent and E. M. McMillan, "Some new algorithms for high-precision computation of Euler's constant", Mathematics of Computation 34 (1980) 305-312.
- [BJ2013] R. P. Brent and F. Johansson, "A bound for the error term in the Brent-McMillan algorithm", preprint (2013), https://arxiv.org/abs/1312.0039
- [PP2010] K. H. Pilehrood and T. H. Pilehrood. "Series acceleration formulas for beta values", Discrete Mathematics and Theoretical Computer Science, DMTCS, 12 (2) (2010), 223-236, https://hal.inria.fr/hal-00990465/
The FLINT team, “Mathematical constants (mp_real.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/mp_real/constants.html (generated 2026-11-02).
@misc{flint-doc-mp-real-constants,
author = {{The FLINT team}},
title = {{Mathematical constants (mp\_real.h)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/mp_real/constants.html},
note = {Generated 2026-11-02}
}