1 /* mpfr_const_catalan -- compute Catalan's constant.
3 Copyright 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 /* Declare the cache */
27 MPFR_DECL_INIT_CACHE(__gmpfr_cache_const_catalan, mpfr_const_catalan_internal);
29 /* Set User Interface */
30 #undef mpfr_const_catalan
32 mpfr_const_catalan (mpfr_ptr x, mp_rnd_t rnd_mode) {
33 return mpfr_cache (x, __gmpfr_cache_const_catalan, rnd_mode);
36 /* return T, Q such that T/Q = sum(k!^2/(2k)!/(2k+1)^2, k=n1..n2-1) */
38 S (mpz_t T, mpz_t P, mpz_t Q, unsigned long n1, unsigned long n2)
49 mpz_set_ui (P, 2 * n1 - 1);
50 mpz_mul_ui (P, P, n1);
51 mpz_ui_pow_ui (Q, 2 * n1 + 1, 2);
52 mpz_mul_2exp (Q, Q, 1);
58 unsigned long m = (n1 + n2) / 2;
64 S (T2, P2, Q2, m, n2);
76 /* Don't need to save/restore exponent range: the cache does it.
77 Catalan's constant is G = sum((-1)^k/(2*k+1)^2, k=0..infinity).
78 We compute it using formula (31) of Victor Adamchik's page
79 "33 representations for Catalan's constant"
80 http://www-2.cs.cmu.edu/~adamchik/articles/catalan/catalan.htm
82 G = Pi/8*log(2+sqrt(3)) + 3/8*sum(k!^2/(2k)!/(2k+1)^2,k=0..infinity)
85 mpfr_const_catalan_internal (mpfr_ptr g, mp_rnd_t rnd_mode)
92 MPFR_GROUP_DECL (group);
94 MPFR_LOG_FUNC (("rnd_mode=%d", rnd_mode), ("g[%#R]=%R inex=%d", g, g, inex));
96 /* Here are the WC (max prec = 100.000.000)
97 Once we have found a chain of 11, we only look for bigger chain.
104 Found 14 '1' at 12762
105 Found 15 '1' at 152561
106 Found 16 '0' at 171725
107 Found 18 '0' at 525355
108 Found 20 '0' at 529245
109 Found 21 '1' at 6390133
110 Found 22 '0' at 7806417
111 Found 25 '1' at 11936239
112 Found 27 '1' at 51752950
115 p = pg + MPFR_INT_CEIL_LOG2 (pg) + 7;
117 MPFR_GROUP_INIT_3 (group, p, x, y, z);
122 MPFR_ZIV_INIT (loop, p);
124 mpfr_sqrt_ui (x, 3, GMP_RNDU);
125 mpfr_add_ui (x, x, 2, GMP_RNDU);
126 mpfr_log (x, x, GMP_RNDU);
127 mpfr_const_pi (y, GMP_RNDU);
128 mpfr_mul (x, x, y, GMP_RNDN);
129 S (T, P, Q, 0, (p - 1) / 2);
130 mpz_mul_ui (T, T, 3);
131 mpfr_set_z (y, T, GMP_RNDU);
132 mpfr_set_z (z, Q, GMP_RNDD);
133 mpfr_div (y, y, z, GMP_RNDN);
134 mpfr_add (x, x, y, GMP_RNDN);
135 mpfr_div_2ui (x, x, 3, GMP_RNDN);
137 if (MPFR_LIKELY (MPFR_CAN_ROUND (x, p - 5, pg, rnd_mode)))
140 MPFR_ZIV_NEXT (loop, p);
141 MPFR_GROUP_REPREC_3 (group, p, x, y, z);
143 MPFR_ZIV_FREE (loop);
144 inex = mpfr_set (g, x, rnd_mode);
146 MPFR_GROUP_CLEAR (group);