Annotation of OpenXM_contrib/gmp/mpfr/const_euler.c, Revision 1.1.1.1
1.1 ohara 1: /* mpfr_const_euler -- Euler's constant
2:
3: Copyright 2001 Free Software Foundation.
4:
5: This file is part of the MPFR Library.
6:
7: The MPFR Library is free software; you can redistribute it and/or modify
8: it under the terms of the GNU Lesser General Public License as published by
9: the Free Software Foundation; either version 2.1 of the License, or (at your
10: option) any later version.
11:
12: The MPFR Library is distributed in the hope that it will be useful, but
13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15: License for more details.
16:
17: You should have received a copy of the GNU Lesser General Public License
18: along with the MPFR Library; see the file COPYING.LIB. If not, write to
19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20: MA 02111-1307, USA. */
21:
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include "gmp.h"
25: #include "gmp-impl.h"
26: #include "longlong.h"
27: #include "mpfr.h"
28: #include "mpfr-impl.h"
29:
30: static void mpfr_const_euler_S _PROTO ((mpfr_ptr, unsigned long));
31: static void mpfr_const_euler_R _PROTO ((mpfr_ptr, unsigned long));
32:
33: int
34: mpfr_const_euler (mpfr_t x, mp_rnd_t rnd)
35: {
36: mp_prec_t prec = MPFR_PREC(x), m, log2m;
37: mpfr_t y, z;
38: unsigned long n;
39:
40: log2m = _mpfr_ceil_log2 ((double) prec);
41: m = prec + log2m;
42:
43: mpfr_init (y);
44: mpfr_init (z);
45:
46: do
47: {
48: m += BITS_PER_MP_LIMB;
49: n = 1 + (unsigned long)((double) m * LOG2 / 2.0);
50: if (n < 9)
51: n = 9;
52: MPFR_ASSERTD (n >= 9);
53: mpfr_set_prec (y, m + log2m);
54: mpfr_set_prec (z, m + log2m);
55: mpfr_const_euler_S (y, n);
56: mpfr_set_ui (z, n, GMP_RNDN);
57: mpfr_log (z, z, GMP_RNDD);
58: mpfr_sub (y, y, z, GMP_RNDN); /* S'(n) - log(n) */
59: mpfr_set_prec (z, m);
60: mpfr_const_euler_R (z, n);
61: mpfr_sub (y, y, z, GMP_RNDN);
62: }
63: while (!mpfr_can_round (y, m - 3, GMP_RNDN, rnd, prec));
64:
65: mpfr_set (x, y, rnd);
66:
67: mpfr_clear (y);
68: mpfr_clear (z);
69:
70: return 1; /* always inexact */
71: }
72:
73: /* computes S(n) = sum(n^k*(-1)^(k-1)/k!/k, k=1..ceil(4.319136566 * n))
74: with an error of at most ulp(x).
75: [S(n) >= 2 for n >= 5]
76: */
77: void
78: mpfr_const_euler_S (mpfr_t x, unsigned long n)
79: {
80: unsigned long N, k, m;
81: mpz_t a, s, t;
82:
83: N = (long) (ALPHA * (double) n + 1.0); /* ceil(alpha * n) */
84:
85: m = MPFR_PREC(x) + (unsigned long) ((double) n / LOG2)
86: + _mpfr_ceil_log2 ((double) N) + 1;
87:
88: mpz_init_set_ui (a, 1);
89: mpz_mul_2exp (a, a, m); /* a=-2^m where m is the precision of x */
90: mpz_init_set_ui (s, 0);
91: mpz_init (t);
92:
93: /* here, a and s are exact */
94: for (k = 1; k <= N; k++)
95: {
96: mpz_mul_ui (a, a, n);
97: mpz_div_ui (a, a, k);
98: mpz_div_ui (t, a, k);
99: if (k % 2)
100: mpz_add (s, s, t);
101: else
102: mpz_sub (s, s, t);
103: }
104:
105: /* the error on s is at most N (e^n + 1),
106: thus that the error on x is at most one ulp */
107:
108: mpfr_set_z (x, s, GMP_RNDD);
109: mpfr_div_2ui (x, x, m, GMP_RNDD);
110:
111: mpz_clear (a);
112: mpz_clear (s);
113: mpz_clear (t);
114: }
115:
116: /* computes R(n) = exp(-n)/n * sum(k!/(-n)^k, k=0..n-2)
117: with error at most 4*ulp(x). Assumes n>=2.
118: Since x <= exp(-n)/n <= 1/8, then 4*ulp(x) <= ulp(1).
119: */
120: void
121: mpfr_const_euler_R (mpfr_t x, unsigned long n)
122: {
123: unsigned long k, m;
124: mpz_t a, s;
125: mpfr_t y;
126:
127: MPFR_ASSERTN (n >= 2); /* ensures sum(k!/(-n)^k, k=0..n-2) >= 2/3 */
128:
129: /* as we multiply the sum by exp(-n), we need only PREC(x) - n/LOG2 bits */
130: m = MPFR_PREC(x) - (unsigned long) ((double) n / LOG2);
131:
132: mpz_init_set_ui (a, 1);
133: mpz_mul_2exp (a, a, m);
134: mpz_init_set (s, a);
135:
136: for (k = 1; k <= n; k++)
137: {
138: mpz_mul_ui (a, a, k);
139: mpz_div_ui (a, a, n);
140: /* the error e(k) on a is e(k) <= 1 + k/n*e(k-1) with e(0)=0,
141: i.e. e(k) <= k */
142: if (k % 2)
143: mpz_sub (s, s, a);
144: else
145: mpz_add (s, s, a);
146: }
147: /* the error on s is at most 1+2+...+n = n*(n+1)/2 */
148: mpz_div_ui (s, s, n); /* err <= 1 + (n+1)/2 */
149: if (MPFR_PREC(x) < mpz_sizeinbase(s, 2))
150: {
151: fprintf (stderr, "prec(x) is too small in mpfr_const_euler_R\n");
152: exit (1);
153: }
154: mpfr_set_z (x, s, GMP_RNDD); /* exact */
155: mpfr_div_2ui (x, x, m, GMP_RNDD);
156: /* now x = 1/n * sum(k!/(-n)^k, k=0..n-2) <= 1/n */
157: /* err(x) <= (n+1)/2^m <= (n+1)*exp(n)/2^PREC(x) */
158:
159: mpfr_init2 (y, m);
160: mpfr_set_si (y, -n, GMP_RNDD); /* assumed exact */
161: mpfr_exp (y, y, GMP_RNDD); /* err <= ulp(y) <= exp(-n)*2^(1-m) */
162: mpfr_mul (x, x, y, GMP_RNDD);
163: /* err <= ulp(x) + (n + 1 + 2/n) / 2^prec(x)
164: <= ulp(x) + (n + 1 + 2/n) ulp(x)/x since x*2^(-prec(x)) < ulp(x)
165: <= ulp(x) + (n + 1 + 2/n) 3/(2n) ulp(x) since x >= 2/3*n for n >= 2
166: <= 4 * ulp(x) for n >= 2 */
167: mpfr_clear (y);
168:
169: mpz_clear (a);
170: mpz_clear (s);
171: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>