1 /* mpfr_gamma -- gamma function
2
3 Copyright 2001-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
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 3 of the License, or (at your
11 option) any later version.
12
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.
17
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.LESSER. If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
25
26 #define IS_GAMMA
27 #include "lngamma.c"
28 #undef IS_GAMMA
29
30 /* return a sufficient precision such that 2-x is exact, assuming x < 0
31 and x is not an integer */
32 static mpfr_prec_t
mpfr_gamma_2_minus_x_exact(mpfr_srcptr x)33 mpfr_gamma_2_minus_x_exact (mpfr_srcptr x)
34 {
35 /* Since x < 0, 2-x = 2+y with y := -x.
36 If y < 2, a precision w >= PREC(y) + EXP(2)-EXP(y) = PREC(y) + 2 - EXP(y)
37 is enough, since no overlap occurs in 2+y, so no carry happens.
38 If y >= 2, either ULP(y) <= 2, and we need w >= PREC(y)+1 since a
39 carry can occur, or ULP(y) > 2, and we need w >= EXP(y)-1:
40 (a) if EXP(y) <= 1, w = PREC(y) + 2 - EXP(y)
41 (b) if EXP(y) > 1 and EXP(y)-PREC(y) <= 1, w = PREC(y) + 1
42 (c) if EXP(y) > 1 and EXP(y)-PREC(y) > 1, w = EXP(y) - 1.
43
44 Note: case (c) cannot happen in practice since this would imply that
45 y is integer, thus x is negative integer */
46 return (MPFR_GET_EXP(x) <= 1) ? MPFR_PREC(x) + 2 - MPFR_GET_EXP(x)
47 : MPFR_PREC(x) + 1;
48 }
49
50 /* return a sufficient precision such that 1-x is exact, assuming x < 1
51 and x is not an integer */
52 static mpfr_prec_t
mpfr_gamma_1_minus_x_exact(mpfr_srcptr x)53 mpfr_gamma_1_minus_x_exact (mpfr_srcptr x)
54 {
55 if (MPFR_IS_POS(x))
56 return MPFR_PREC(x) - MPFR_GET_EXP(x);
57 else if (MPFR_GET_EXP(x) <= 0)
58 return MPFR_PREC(x) + 1 - MPFR_GET_EXP(x);
59 else /* necessarily MPFR_PREC(x) > MPFR_GET_EXP(x) since otherwise
60 x would be an integer */
61 return MPFR_PREC(x) + 1;
62 }
63
64 /* returns a lower bound of the number of significant bits of n!
65 (not counting the low zero bits).
66 We know n! >= (n/e)^n*sqrt(2*Pi*n) for n >= 1, and the number of zero bits
67 is floor(n/2) + floor(n/4) + floor(n/8) + ...
68 This approximation is exact for n <= 500000, except for n = 219536, 235928,
69 298981, 355854, 464848, 493725, 498992 where it returns a value 1 too small.
70 */
71 static unsigned long
bits_fac(unsigned long n)72 bits_fac (unsigned long n)
73 {
74 mpfr_t x, y;
75 unsigned long r, k;
76 MPFR_SAVE_EXPO_DECL (expo);
77
78 MPFR_ASSERTD (n >= 1);
79
80 MPFR_SAVE_EXPO_MARK (expo);
81 mpfr_init2 (x, 38);
82 mpfr_init2 (y, 38);
83 mpfr_set_ui (x, n, MPFR_RNDZ);
84 mpfr_set_str_binary (y, "10.101101111110000101010001011000101001"); /* upper bound of e */
85 mpfr_div (x, x, y, MPFR_RNDZ);
86 mpfr_pow_ui (x, x, n, MPFR_RNDZ);
87 mpfr_const_pi (y, MPFR_RNDZ);
88 mpfr_mul_ui (y, y, 2 * n, MPFR_RNDZ);
89 mpfr_sqrt (y, y, MPFR_RNDZ);
90 mpfr_mul (x, x, y, MPFR_RNDZ);
91 mpfr_log2 (x, x, MPFR_RNDZ);
92 r = mpfr_get_ui (x, MPFR_RNDU); /* lower bound on ceil(x) */
93 for (k = 2; k <= n; k *= 2)
94 {
95 /* Note: the approximation is accurate enough so that the
96 subtractions do not wrap. */
97 MPFR_ASSERTD (r >= n / k);
98 r -= n / k;
99 }
100 mpfr_clear (x);
101 mpfr_clear (y);
102 MPFR_SAVE_EXPO_FREE (expo);
103
104 return r;
105 }
106
107 /* We use the reflection formula
108 Gamma(1+t) Gamma(1-t) = - Pi t / sin(Pi (1 + t))
109 in order to treat the case x <= 1,
110 i.e. with x = 1-t, then Gamma(x) = -Pi*(1-x)/sin(Pi*(2-x))/GAMMA(2-x)
111 */
112 int
mpfr_gamma(mpfr_ptr gamma,mpfr_srcptr x,mpfr_rnd_t rnd_mode)113 mpfr_gamma (mpfr_ptr gamma, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
114 {
115 mpfr_t xp, GammaTrial, tmp, tmp2;
116 mpz_t fact;
117 mpfr_prec_t realprec;
118 int compared, is_integer;
119 int inex = 0; /* 0 means: result gamma not set yet */
120 MPFR_GROUP_DECL (group);
121 MPFR_SAVE_EXPO_DECL (expo);
122 MPFR_ZIV_DECL (loop);
123
124 MPFR_LOG_FUNC
125 (("x[%Pd]=%.*Rg rnd=%d", mpfr_get_prec (x), mpfr_log_prec, x, rnd_mode),
126 ("gamma[%Pd]=%.*Rg inexact=%d",
127 mpfr_get_prec (gamma), mpfr_log_prec, gamma, inex));
128
129 /* Trivial cases */
130 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
131 {
132 if (MPFR_IS_NAN (x))
133 {
134 MPFR_SET_NAN (gamma);
135 MPFR_RET_NAN;
136 }
137 else if (MPFR_IS_INF (x))
138 {
139 if (MPFR_IS_NEG (x))
140 {
141 /* gamma(x) has a pole at negative integers, thus even if it goes to zero
142 for other values, we return NaN */
143 MPFR_SET_NAN (gamma);
144 MPFR_RET_NAN;
145 }
146 else
147 {
148 MPFR_SET_INF (gamma);
149 MPFR_SET_POS (gamma);
150 MPFR_RET (0); /* exact */
151 }
152 }
153 else /* x is zero */
154 {
155 MPFR_ASSERTD(MPFR_IS_ZERO(x));
156 MPFR_SET_INF(gamma);
157 MPFR_SET_SAME_SIGN(gamma, x);
158 MPFR_SET_DIVBY0 ();
159 MPFR_RET (0); /* exact */
160 }
161 }
162
163 /* Check for tiny arguments, where gamma(x) ~ 1/x - euler + ... can be
164 approximated by 1/x, with some error term ~= - euler.
165 We need to make sure that there are no breakpoints (discontinuity
166 points of the rounding function) between gamma(x) and 1/x (included),
167 where the possible breakpoints (for all rounding modes) are the numbers
168 that fit on PREC(gamma)+1 bits. There will be a special case when |x|
169 is a power of two, since such values are breakpoints. We will choose n
170 minimum such that x fits on n bits and the breakpoints fit on n+1 bits,
171 thus
172 n = MAX(MPFR_PREC(x), MPFR_PREC(gamma)).
173 We know from "Bound on Runs of Zeros and Ones for Algebraic Functions",
174 Proceedings of Arith15, T. Lang and J.-M. Muller, 2001, that the maximal
175 number of consecutive zeroes or ones after the round bit for 1/x is n-1
176 for an input x of n bits [this is an actually much older result!].
177 But we need a more precise lower bound. Assume that 1/x is near a
178 breakpoint y. From the definition of n, the input x fits on n bits
179 and the breakpoint y fits on of n+1 bits. We can write x = X*2^e,
180 y = Y/2^f with X, Y integers of n and n+1 bits respectively.
181 Thus X*Y^2^(e-f) is near 1, i.e., X*Y is near the integer 2^(f-e).
182 Two cases can happen:
183 (i) either X*Y is exactly 2^(f-e), but this can happen only if X and Y
184 are themselves powers of two, i.e., x is a power of two;
185 (ii) or X*Y is at distance at least one from 2^(f-e), thus
186 |xy-1| >= 2^(e-f), or |y-1/x| >= 2^(e-f)/x = 2^(-f)/X >= 2^(-f-n).
187 Since ufp(y) = 2^(n-f) [ufp = unit in first place], this means
188 that the distance |y-1/x| >= 2^(-2n) ufp(y).
189 Now, assuming |gamma(x)-1/x| < 1, which is true for 0 < x <= 1,
190 if 2^(-2n) ufp(y) >= 1, then gamma(x) and 1/x round in the same
191 way, so that rounding 1/x gives the correct result and correct
192 (non-zero) ternary value.
193 If x < 2^E, then y >= 2^(-E), thus ufp(y) >= 2^(-E).
194 A sufficient condition is thus EXP(x) <= -2n, where
195 n = MAX(MPFR_PREC(x), MPFR_PREC(gamma)).
196 */
197 /* TODO: The above proof uses the same precision for input and output.
198 Without this assumption, one might obtain a bound like
199 PREC(x) + PREC(y) instead of 2 MAX(PREC(x),PREC(y)). */
200 /* TODO: Handle the very small arguments that do not satisfy the condition,
201 by using the approximation 1/x - euler and a Ziv loop. Otherwise, after
202 some tests, even Gamma(1+x)/x would be faster than the generic code. */
203 if (MPFR_GET_EXP (x)
204 <= -2 * (mpfr_exp_t) MAX(MPFR_PREC(x), MPFR_PREC(gamma)))
205 {
206 int sign = MPFR_SIGN (x); /* retrieve sign before possible override */
207 int special;
208 MPFR_BLOCK_DECL (flags);
209
210 MPFR_SAVE_EXPO_MARK (expo);
211
212 /* for overflow cases, see below; this needs to be done
213 before x possibly gets overridden. */
214 special =
215 MPFR_GET_EXP (x) == 1 - MPFR_EMAX_MAX &&
216 MPFR_IS_POS_SIGN (sign) &&
217 MPFR_IS_LIKE_RNDD (rnd_mode, sign) &&
218 mpfr_powerof2_raw (x);
219
220 MPFR_BLOCK (flags, inex = mpfr_ui_div (gamma, 1, x, rnd_mode));
221 if (inex == 0) /* |x| is a power of two */
222 {
223 /* return RND(1/x - euler) = RND(+/- 2^k - eps) with eps > 0 */
224 if (rnd_mode == MPFR_RNDN || MPFR_IS_LIKE_RNDU (rnd_mode, sign))
225 inex = 1;
226 else
227 {
228 mpfr_nextbelow (gamma);
229 inex = -1;
230 }
231 }
232 else if (MPFR_UNLIKELY (MPFR_OVERFLOW (flags)))
233 {
234 /* Overflow in the division 1/x. This is a real overflow, except
235 in RNDZ or RNDD when 1/x = 2^emax, i.e. x = 2^(-emax): due to
236 the "- euler", the rounded value in unbounded exponent range
237 is 0.111...11 * 2^emax (not an overflow). */
238 if (!special)
239 MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, flags);
240 }
241 MPFR_SAVE_EXPO_FREE (expo);
242 /* Note: an overflow is possible with an infinite result;
243 in this case, the overflow flag will automatically be
244 restored by mpfr_check_range. */
245 return mpfr_check_range (gamma, inex, rnd_mode);
246 }
247
248 is_integer = mpfr_integer_p (x);
249 /* gamma(x) for x a negative integer gives NaN */
250 if (is_integer && MPFR_IS_NEG(x))
251 {
252 MPFR_SET_NAN (gamma);
253 MPFR_RET_NAN;
254 }
255
256 compared = mpfr_cmp_ui (x, 1);
257 if (compared == 0)
258 return mpfr_set_ui (gamma, 1, rnd_mode);
259
260 /* if x is an integer that fits into an unsigned long, use mpfr_fac_ui
261 if argument is not too large.
262 If precision is p, fac_ui costs O(u*p), whereas gamma costs O(p*M(p)),
263 so for u <= M(p), fac_ui should be faster.
264 We approximate here M(p) by p*log(p)^2, which is not a bad guess.
265 Warning: since the generic code does not handle exact cases,
266 we want all cases where gamma(x) is exact to be treated here.
267 */
268 if (is_integer && mpfr_fits_ulong_p (x, MPFR_RNDN))
269 {
270 unsigned long int u;
271 mpfr_prec_t p = MPFR_PREC(gamma);
272 u = mpfr_get_ui (x, MPFR_RNDN);
273 MPFR_ASSERTD (u >= 2);
274 if (u < 44787929UL && bits_fac (u - 1) <= p + (rnd_mode == MPFR_RNDN))
275 /* bits_fac: lower bound on the number of bits of m,
276 where gamma(x) = (u-1)! = m*2^e with m odd. */
277 return mpfr_fac_ui (gamma, u - 1, rnd_mode);
278 /* if bits_fac(...) > p (resp. p+1 for rounding to nearest),
279 then gamma(x) cannot be exact in precision p (resp. p+1).
280 FIXME: remove the test u < 44787929UL after changing bits_fac
281 to return a mpz_t or mpfr_t. */
282 }
283
284 MPFR_SAVE_EXPO_MARK (expo);
285
286 /* check for overflow: according to (6.1.37) in Abramowitz & Stegun,
287 gamma(x) >= exp(-x) * x^(x-1/2) * sqrt(2*Pi)
288 >= 2 * (x/e)^x / x for x >= 1 */
289 if (compared > 0)
290 {
291 mpfr_t yp, zp;
292 mpfr_exp_t expxp;
293 MPFR_BLOCK_DECL (flags);
294 MPFR_GROUP_DECL (group);
295
296 /* quick test for the default exponent range */
297 if (mpfr_get_emax () >= 1073741823UL && MPFR_GET_EXP(x) <= 25)
298 {
299 MPFR_SAVE_EXPO_FREE (expo);
300 return mpfr_gamma_aux (gamma, x, rnd_mode);
301 }
302
303 MPFR_GROUP_INIT_3 (group, 53, xp, yp, zp);
304 /* 1/e rounded down to 53 bits */
305 mpfr_set_str_binary (zp,
306 "0.010111100010110101011000110110001011001110111100111");
307 mpfr_mul (xp, x, zp, MPFR_RNDZ);
308 mpfr_sub_ui (yp, x, 2, MPFR_RNDZ);
309 mpfr_pow (xp, xp, yp, MPFR_RNDZ); /* (x/e)^(x-2) */
310 mpfr_mul (xp, xp, zp, MPFR_RNDZ); /* x^(x-2) / e^(x-1) */
311 mpfr_mul (xp, xp, zp, MPFR_RNDZ); /* x^(x-2) / e^x */
312 mpfr_mul (xp, xp, x, MPFR_RNDZ); /* lower bound on x^(x-1) / e^x */
313 MPFR_BLOCK (flags, mpfr_mul_2ui (xp, xp, 1, MPFR_RNDZ));
314 expxp = MPFR_GET_EXP (xp);
315 MPFR_GROUP_CLEAR (group);
316 MPFR_SAVE_EXPO_FREE (expo);
317 return MPFR_OVERFLOW (flags) || expxp > __gmpfr_emax ?
318 mpfr_overflow (gamma, rnd_mode, 1) :
319 mpfr_gamma_aux (gamma, x, rnd_mode);
320 }
321
322 /* now compared < 0 */
323
324 /* check for underflow: for x < 1,
325 gamma(x) = Pi*(x-1)/sin(Pi*(2-x))/gamma(2-x).
326 Since gamma(2-x) >= 2 * ((2-x)/e)^(2-x) / (2-x), we have
327 |gamma(x)| <= Pi*(1-x)*(2-x)/2/((2-x)/e)^(2-x) / |sin(Pi*(2-x))|
328 <= 12 * ((2-x)/e)^x / |sin(Pi*(2-x))|.
329 To avoid an underflow in ((2-x)/e)^x, we compute the logarithm.
330 */
331 if (MPFR_IS_NEG(x))
332 {
333 int underflow = 0, sgn, ck;
334 mpfr_prec_t w;
335
336 mpfr_init2 (xp, 53);
337 mpfr_init2 (tmp, 53);
338 mpfr_init2 (tmp2, 53);
339 /* we want an upper bound for x * [log(2-x)-1].
340 since x < 0, we need a lower bound on log(2-x) */
341 mpfr_ui_sub (xp, 2, x, MPFR_RNDD);
342 mpfr_log (xp, xp, MPFR_RNDD);
343 mpfr_sub_ui (xp, xp, 1, MPFR_RNDD);
344 mpfr_mul (xp, xp, x, MPFR_RNDU);
345
346 /* we need an upper bound on 1/|sin(Pi*(2-x))|,
347 thus a lower bound on |sin(Pi*(2-x))|.
348 If 2-x is exact, then the error of Pi*(2-x) is (1+u)^2 with u = 2^(-p)
349 thus the error on sin(Pi*(2-x)) is less than 1/2ulp + 3Pi(2-x)u,
350 assuming u <= 1, thus <= u + 3Pi(2-x)u */
351
352 w = mpfr_gamma_2_minus_x_exact (x); /* 2-x is exact for prec >= w */
353 w += 17; /* to get tmp2 small enough */
354 mpfr_set_prec (tmp, w);
355 mpfr_set_prec (tmp2, w);
356 MPFR_DBGRES (ck = mpfr_ui_sub (tmp, 2, x, MPFR_RNDN));
357 MPFR_ASSERTD (ck == 0); /* tmp = 2-x exactly */
358 mpfr_const_pi (tmp2, MPFR_RNDN);
359 mpfr_mul (tmp2, tmp2, tmp, MPFR_RNDN); /* Pi*(2-x) */
360 mpfr_sin (tmp, tmp2, MPFR_RNDN); /* sin(Pi*(2-x)) */
361 sgn = mpfr_sgn (tmp);
362 mpfr_abs (tmp, tmp, MPFR_RNDN);
363 mpfr_mul_ui (tmp2, tmp2, 3, MPFR_RNDU); /* 3Pi(2-x) */
364 mpfr_add_ui (tmp2, tmp2, 1, MPFR_RNDU); /* 3Pi(2-x)+1 */
365 mpfr_div_2ui (tmp2, tmp2, mpfr_get_prec (tmp), MPFR_RNDU);
366 /* if tmp2<|tmp|, we get a lower bound */
367 if (mpfr_cmp (tmp2, tmp) < 0)
368 {
369 mpfr_sub (tmp, tmp, tmp2, MPFR_RNDZ); /* low bnd on |sin(Pi*(2-x))| */
370 mpfr_ui_div (tmp, 12, tmp, MPFR_RNDU); /* upper bound */
371 mpfr_log2 (tmp, tmp, MPFR_RNDU);
372 mpfr_add (xp, tmp, xp, MPFR_RNDU);
373 /* The assert below checks that expo.saved_emin - 2 always
374 fits in a long. FIXME if we want to allow mpfr_exp_t to
375 be a long long, for instance. */
376 MPFR_ASSERTN (MPFR_EMIN_MIN - 2 >= LONG_MIN);
377 underflow = mpfr_cmp_si (xp, expo.saved_emin - 2) <= 0;
378 }
379
380 mpfr_clear (xp);
381 mpfr_clear (tmp);
382 mpfr_clear (tmp2);
383 if (underflow) /* the sign is the opposite of that of sin(Pi*(2-x)) */
384 {
385 MPFR_SAVE_EXPO_FREE (expo);
386 return mpfr_underflow (gamma, (rnd_mode == MPFR_RNDN) ? MPFR_RNDZ : rnd_mode, -sgn);
387 }
388 }
389
390 realprec = MPFR_PREC (gamma);
391 /* we want both 1-x and 2-x to be exact */
392 {
393 mpfr_prec_t w;
394 w = mpfr_gamma_1_minus_x_exact (x);
395 if (realprec < w)
396 realprec = w;
397 w = mpfr_gamma_2_minus_x_exact (x);
398 if (realprec < w)
399 realprec = w;
400 }
401 realprec = realprec + MPFR_INT_CEIL_LOG2 (realprec) + 20;
402 MPFR_ASSERTD(realprec >= 5);
403
404 MPFR_GROUP_INIT_4 (group, realprec + MPFR_INT_CEIL_LOG2 (realprec) + 20,
405 xp, tmp, tmp2, GammaTrial);
406 mpz_init (fact);
407 MPFR_ZIV_INIT (loop, realprec);
408 for (;;)
409 {
410 mpfr_exp_t err_g;
411 int ck;
412 MPFR_GROUP_REPREC_4 (group, realprec, xp, tmp, tmp2, GammaTrial);
413
414 /* reflection formula: gamma(x) = Pi*(x-1)/sin(Pi*(2-x))/gamma(2-x) */
415
416 ck = mpfr_ui_sub (xp, 2, x, MPFR_RNDN); /* 2-x, exact */
417 MPFR_ASSERTD(ck == 0); (void) ck; /* use ck to avoid a warning */
418 mpfr_gamma (tmp, xp, MPFR_RNDN); /* gamma(2-x), error (1+u) */
419 mpfr_const_pi (tmp2, MPFR_RNDN); /* Pi, error (1+u) */
420 mpfr_mul (GammaTrial, tmp2, xp, MPFR_RNDN); /* Pi*(2-x), error (1+u)^2 */
421 err_g = MPFR_GET_EXP(GammaTrial);
422 mpfr_sin (GammaTrial, GammaTrial, MPFR_RNDN); /* sin(Pi*(2-x)) */
423 /* If tmp is +Inf, we compute exp(lngamma(x)). */
424 if (mpfr_inf_p (tmp))
425 {
426 inex = mpfr_explgamma (gamma, x, &expo, tmp, tmp2, rnd_mode);
427 if (inex)
428 goto end;
429 else
430 goto ziv_next;
431 }
432 err_g = err_g + 1 - MPFR_GET_EXP(GammaTrial);
433 /* let g0 the true value of Pi*(2-x), g the computed value.
434 We have g = g0 + h with |h| <= |(1+u^2)-1|*g.
435 Thus sin(g) = sin(g0) + h' with |h'| <= |(1+u^2)-1|*g.
436 The relative error is thus bounded by |(1+u^2)-1|*g/sin(g)
437 <= |(1+u^2)-1|*2^err_g. <= 2.25*u*2^err_g for |u|<=1/4.
438 With the rounding error, this gives (0.5 + 2.25*2^err_g)*u. */
439 ck = mpfr_sub_ui (xp, x, 1, MPFR_RNDN); /* x-1, exact */
440 MPFR_ASSERTD(ck == 0); (void) ck; /* use ck to avoid a warning */
441 mpfr_mul (xp, tmp2, xp, MPFR_RNDN); /* Pi*(x-1), error (1+u)^2 */
442 mpfr_mul (GammaTrial, GammaTrial, tmp, MPFR_RNDN);
443 /* [1 + (0.5 + 2.25*2^err_g)*u]*(1+u)^2 = 1 + (2.5 + 2.25*2^err_g)*u
444 + (0.5 + 2.25*2^err_g)*u*(2u+u^2) + u^2.
445 For err_g <= realprec-2, we have (0.5 + 2.25*2^err_g)*u <=
446 0.5*u + 2.25/4 <= 0.6875 and u^2 <= u/4, thus
447 (0.5 + 2.25*2^err_g)*u*(2u+u^2) + u^2 <= 0.6875*(2u+u/4) + u/4
448 <= 1.8*u, thus the rel. error is bounded by (4.5 + 2.25*2^err_g)*u. */
449 mpfr_div (GammaTrial, xp, GammaTrial, MPFR_RNDN);
450 /* the error is of the form (1+u)^3/[1 + (4.5 + 2.25*2^err_g)*u].
451 For realprec >= 5 and err_g <= realprec-2, [(4.5 + 2.25*2^err_g)*u]^2
452 <= 0.71, and for |y|<=0.71, 1/(1-y) can be written 1+a*y with a<=4.
453 (1+u)^3 * (1+4*(4.5 + 2.25*2^err_g)*u)
454 = 1 + (21 + 9*2^err_g)*u + (57+27*2^err_g)*u^2 + (55+27*2^err_g)*u^3
455 + (18+9*2^err_g)*u^4
456 <= 1 + (21 + 9*2^err_g)*u + (57+27*2^err_g)*u^2 + (56+28*2^err_g)*u^3
457 <= 1 + (21 + 9*2^err_g)*u + (59+28*2^err_g)*u^2
458 <= 1 + (23 + 10*2^err_g)*u.
459 The final error is thus bounded by (23 + 10*2^err_g) ulps,
460 which is <= 2^6 for err_g<=2, and <= 2^(err_g+4) for err_g >= 2. */
461 err_g = (err_g <= 2) ? 6 : err_g + 4;
462
463 if (MPFR_LIKELY (MPFR_CAN_ROUND (GammaTrial, realprec - err_g,
464 MPFR_PREC(gamma), rnd_mode)))
465 break;
466
467 ziv_next:
468 MPFR_ZIV_NEXT (loop, realprec);
469 }
470
471 end:
472 MPFR_ZIV_FREE (loop);
473
474 if (inex == 0)
475 inex = mpfr_set (gamma, GammaTrial, rnd_mode);
476 MPFR_GROUP_CLEAR (group);
477 mpz_clear (fact);
478
479 MPFR_SAVE_EXPO_FREE (expo);
480 return mpfr_check_range (gamma, inex, rnd_mode);
481 }
482