summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2012-05-07 17:58:04 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2012-05-07 17:58:04 +0000
commit68d2a0c9246eef02b39e4dbbc7d9081b34d30d34 (patch)
treee6f48efee76704a50c89a2d885cdd76e07ef0b75 /src
parent0220288c9f63d5f22caf4de140057374441afa8d (diff)
downloadmpfr-68d2a0c9246eef02b39e4dbbc7d9081b34d30d34.tar.gz
[src/lngamma.c] Added mpfr_explgamma internal function to handle
overflows/underflows (intermediate or not) in mpfr_gamma. Added general overflow detection. [src/gamma.c] Added general underflow detection. [tests/tgamma.c] Added testcases for 32-bit and 64-bit machines. (merged changesets r8174,8179,8182-8183,8185-8189,8191-8199 from the trunk) git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/branches/3.1@8200 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'src')
-rw-r--r--src/gamma.c19
-rw-r--r--src/lngamma.c115
2 files changed, 119 insertions, 15 deletions
diff --git a/src/gamma.c b/src/gamma.c
index a346f8b1b..b955ca352 100644
--- a/src/gamma.c
+++ b/src/gamma.c
@@ -100,7 +100,8 @@ mpfr_gamma (mpfr_ptr gamma, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
mpfr_t xp, GammaTrial, tmp, tmp2;
mpz_t fact;
mpfr_prec_t realprec;
- int compared, inex, is_integer;
+ int compared, is_integer;
+ int inex = 0; /* 0 means: result gamma not set yet */
MPFR_GROUP_DECL (group);
MPFR_SAVE_EXPO_DECL (expo);
MPFR_ZIV_DECL (loop);
@@ -377,6 +378,15 @@ mpfr_gamma (mpfr_ptr gamma, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
mpfr_mul (GammaTrial, tmp2, xp, MPFR_RNDN); /* Pi*(2-x), error (1+u)^2 */
err_g = MPFR_GET_EXP(GammaTrial);
mpfr_sin (GammaTrial, GammaTrial, MPFR_RNDN); /* sin(Pi*(2-x)) */
+ /* If tmp is +Inf, we compute exp(lngamma(x)). */
+ if (mpfr_inf_p (tmp))
+ {
+ inex = mpfr_explgamma (gamma, x, &expo, tmp, tmp2, rnd_mode);
+ if (inex)
+ goto end;
+ else
+ goto ziv_next;
+ }
err_g = err_g + 1 - MPFR_GET_EXP(GammaTrial);
/* let g0 the true value of Pi*(2-x), g the computed value.
We have g = g0 + h with |h| <= |(1+u^2)-1|*g.
@@ -411,11 +421,16 @@ mpfr_gamma (mpfr_ptr gamma, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
if (MPFR_LIKELY (MPFR_CAN_ROUND (GammaTrial, realprec - err_g,
MPFR_PREC(gamma), rnd_mode)))
break;
+
+ ziv_next:
MPFR_ZIV_NEXT (loop, realprec);
}
+
+ end:
MPFR_ZIV_FREE (loop);
- inex = mpfr_set (gamma, GammaTrial, rnd_mode);
+ if (inex == 0)
+ inex = mpfr_set (gamma, GammaTrial, rnd_mode);
MPFR_GROUP_CLEAR (group);
mpz_clear (fact);
diff --git a/src/lngamma.c b/src/lngamma.c
index 35cfbb097..05f2b9ff8 100644
--- a/src/lngamma.c
+++ b/src/lngamma.c
@@ -49,9 +49,72 @@ mpfr_gamma_alpha (mpfr_t s, mpfr_prec_t p)
mpfr_set_ui_2exp (s, 9, -1, MPFR_RNDN); /* 4.5 */
}
-#ifndef IS_GAMMA
+#ifdef IS_GAMMA
+
+/* This function is called in case of intermediate overflow/underflow.
+ The s1 and s2 arguments are temporary MPFR numbers, having the
+ working precision. If the result could be determined, then the
+ flags are updated via pexpo, y is set to the result, and the
+ (non-zero) ternary value is returned. Otherwise 0 is returned
+ in order to perform the next Ziv iteration. */
static int
-unit_bit (mpfr_srcptr (x))
+mpfr_explgamma (mpfr_ptr y, mpfr_srcptr x, mpfr_save_expo_t *pexpo,
+ mpfr_ptr s1, mpfr_ptr s2, mpfr_rnd_t rnd)
+{
+ mpfr_t t1, t2;
+ int inex1, inex2, sign;
+ MPFR_BLOCK_DECL (flags1);
+ MPFR_BLOCK_DECL (flags2);
+ MPFR_GROUP_DECL (group);
+
+ MPFR_BLOCK (flags1, inex1 = mpfr_lgamma (s1, &sign, x, MPFR_RNDD));
+ MPFR_ASSERTN (inex1 != 0);
+ /* s1 = RNDD(lngamma(x)), inexact */
+ if (MPFR_UNLIKELY (MPFR_OVERFLOW (flags1)))
+ {
+ if (MPFR_SIGN (s1) > 0)
+ {
+ MPFR_SAVE_EXPO_UPDATE_FLAGS (*pexpo, MPFR_FLAGS_OVERFLOW);
+ return mpfr_overflow (y, rnd, sign);
+ }
+ else
+ {
+ MPFR_SAVE_EXPO_UPDATE_FLAGS (*pexpo, MPFR_FLAGS_UNDERFLOW);
+ return mpfr_underflow (y, rnd == MPFR_RNDN ? MPFR_RNDZ : rnd, sign);
+ }
+ }
+
+ mpfr_set (s2, s1, MPFR_RNDN); /* exact */
+ mpfr_nextabove (s2); /* v = RNDU(lngamma(z0)) */
+
+ if (sign < 0)
+ rnd = MPFR_INVERT_RND (rnd); /* since the result with be negated */
+ MPFR_GROUP_INIT_2 (group, MPFR_PREC (y), t1, t2);
+ MPFR_BLOCK (flags1, inex1 = mpfr_exp (t1, s1, rnd));
+ MPFR_BLOCK (flags2, inex2 = mpfr_exp (t2, s2, rnd));
+ /* t1 is the rounding with mode 'rnd' of a lower bound on |Gamma(x)|,
+ t2 is the rounding with mode 'rnd' of an upper bound, thus if both
+ are equal, so is the wanted result. If t1 and t2 differ or the flags
+ differ, at some point of Ziv's loop they should agree. */
+ if (mpfr_equal_p (t1, t2) && flags1 == flags2)
+ {
+ MPFR_ASSERTN ((inex1 > 0 && inex2 > 0) || (inex1 < 0 && inex2 < 0));
+ mpfr_set4 (y, t1, MPFR_RNDN, sign); /* exact */
+ if (sign < 0)
+ inex1 = - inex1;
+ MPFR_SAVE_EXPO_UPDATE_FLAGS (*pexpo, flags1);
+ }
+ else
+ inex1 = 0; /* couldn't determine the result */
+ MPFR_GROUP_CLEAR (group);
+
+ return inex1;
+}
+
+#else
+
+static int
+unit_bit (mpfr_srcptr x)
{
mpfr_exp_t expo;
mpfr_prec_t prec;
@@ -75,6 +138,7 @@ unit_bit (mpfr_srcptr (x))
return (x0 >> (prec % GMP_NUMB_BITS)) & 1;
}
+
#endif
/* lngamma(x) = log(gamma(x)).
@@ -99,12 +163,14 @@ GAMMA_FUNC (mpfr_ptr y, mpfr_srcptr z0, mpfr_rnd_t rnd)
mpfr_t s, t, u, v, z;
unsigned long m, k, maxm;
mpz_t *INITIALIZED(B); /* variable B declared as initialized */
- int inexact, compared;
+ int compared;
+ int inexact = 0; /* 0 means: result y not set yet */
mpfr_exp_t err_s, err_t;
unsigned long Bm = 0; /* number of allocated B[] */
unsigned long oldBm;
double d;
MPFR_SAVE_EXPO_DECL (expo);
+ MPFR_ZIV_DECL (loop);
compared = mpfr_cmp_ui (z0, 1);
@@ -122,7 +188,7 @@ GAMMA_FUNC (mpfr_ptr y, mpfr_srcptr z0, mpfr_rnd_t rnd)
if (MPFR_EXP(z0) <= - (mpfr_exp_t) MPFR_PREC(y))
{
mpfr_t l, h, g;
- int ok, inex2;
+ int ok, inex1, inex2;
mpfr_prec_t prec = MPFR_PREC(y) + 14;
MPFR_ZIV_DECL (loop);
@@ -157,14 +223,14 @@ GAMMA_FUNC (mpfr_ptr y, mpfr_srcptr z0, mpfr_rnd_t rnd)
mpfr_sub (h, h, g, MPFR_RNDD);
mpfr_mul (g, z0, z0, MPFR_RNDU);
mpfr_add (h, h, g, MPFR_RNDU);
- inexact = mpfr_prec_round (l, MPFR_PREC(y), rnd);
+ inex1 = mpfr_prec_round (l, MPFR_PREC(y), rnd);
inex2 = mpfr_prec_round (h, MPFR_PREC(y), rnd);
/* Caution: we not only need l = h, but both inexact flags should
agree. Indeed, one of the inexact flags might be zero. In that
case if we assume lngamma(z0) cannot be exact, the other flag
should be correct. We are conservative here and request that both
inexact flags agree. */
- ok = SAME_SIGN (inexact, inex2) && mpfr_cmp (l, h) == 0;
+ ok = SAME_SIGN (inex1, inex2) && mpfr_cmp (l, h) == 0;
if (ok)
mpfr_set (y, h, rnd); /* exact */
mpfr_clear (l);
@@ -172,8 +238,9 @@ GAMMA_FUNC (mpfr_ptr y, mpfr_srcptr z0, mpfr_rnd_t rnd)
mpfr_clear (g);
if (ok)
{
+ MPFR_ZIV_FREE (loop);
MPFR_SAVE_EXPO_FREE (expo);
- return mpfr_check_range (y, inexact, rnd);
+ return mpfr_check_range (y, inex1, rnd);
}
/* since we have log|gamma(x)| = - log|x| - gamma*x + O(x^2),
if x ~ 2^(-n), then we have a n-bit approximation, thus
@@ -205,9 +272,10 @@ GAMMA_FUNC (mpfr_ptr y, mpfr_srcptr z0, mpfr_rnd_t rnd)
thus lngamma(x) = log(Pi*(x-1)/sin(Pi*(2-x))) - lngamma(2-x) */
w = precy + MPFR_INT_CEIL_LOG2 (precy);
+ w += MPFR_INT_CEIL_LOG2 (w) + 14;
+ MPFR_ZIV_INIT (loop, w);
while (1)
{
- w += MPFR_INT_CEIL_LOG2 (w) + 14;
MPFR_ASSERTD(w >= 3);
mpfr_set_prec (s, w);
mpfr_set_prec (t, w);
@@ -288,7 +356,9 @@ GAMMA_FUNC (mpfr_ptr y, mpfr_srcptr z0, mpfr_rnd_t rnd)
+ (rnd == MPFR_RNDN)))
goto end;
}
+ MPFR_ZIV_NEXT (loop, w);
}
+ MPFR_ZIV_FREE (loop);
}
/* now z0 > 1 */
@@ -298,10 +368,10 @@ GAMMA_FUNC (mpfr_ptr y, mpfr_srcptr z0, mpfr_rnd_t rnd)
/* since k is O(w), the value of log(z0*...*(z0+k-1)) is about w*log(w),
so there is a cancellation of ~log(w) in the argument reconstruction */
w = precy + MPFR_INT_CEIL_LOG2 (precy);
-
- do
+ w += MPFR_INT_CEIL_LOG2 (w) + 13;
+ MPFR_ZIV_INIT (loop, w);
+ while (1)
{
- w += MPFR_INT_CEIL_LOG2 (w) + 13;
MPFR_ASSERTD (w >= 3);
/* argument reduction: we compute gamma(z0 + k), where the series
@@ -441,6 +511,15 @@ GAMMA_FUNC (mpfr_ptr y, mpfr_srcptr z0, mpfr_rnd_t rnd)
#ifdef IS_GAMMA
err_s = MPFR_GET_EXP(s);
mpfr_exp (s, s, MPFR_RNDN);
+ /* If s is +Inf, we compute exp(lngamma(z0)). */
+ if (mpfr_inf_p (s))
+ {
+ inexact = mpfr_explgamma (y, z0, &expo, s, t, rnd);
+ if (inexact)
+ goto end0;
+ else
+ goto ziv_next;
+ }
/* before the exponential, we have s = s0 + h where
|h| <= (2m+48)*ulp(s), thus exp(s0) = exp(s) * exp(-h).
For |h| <= 1/4, we have |exp(h)-1| <= 1.2*|h| thus
@@ -480,16 +559,26 @@ GAMMA_FUNC (mpfr_ptr y, mpfr_srcptr z0, mpfr_rnd_t rnd)
err_s = (err_t == err_s) ? 1 + err_s : ((err_t > err_s) ? err_t : err_s);
err_s += 1 - MPFR_GET_EXP(s);
#endif
+ if (MPFR_LIKELY (MPFR_CAN_ROUND (s, w - err_s, precy, rnd)))
+ break;
+#ifdef IS_GAMMA
+ ziv_next:
+#endif
+ MPFR_ZIV_NEXT (loop, w);
}
- while (MPFR_UNLIKELY (!MPFR_CAN_ROUND (s, w - err_s, precy, rnd)));
+#ifdef IS_GAMMA
+ end0:
+#endif
oldBm = Bm;
while (Bm--)
mpz_clear (B[Bm]);
(*__gmp_free_func) (B, oldBm * sizeof (mpz_t));
end:
- inexact = mpfr_set (y, s, rnd);
+ if (inexact == 0)
+ inexact = mpfr_set (y, s, rnd);
+ MPFR_ZIV_FREE (loop);
mpfr_clear (s);
mpfr_clear (t);