summaryrefslogtreecommitdiff
path: root/Build/source/libs/mpfr/mpfr-src/src/log1p.c
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/libs/mpfr/mpfr-src/src/log1p.c')
-rw-r--r--Build/source/libs/mpfr/mpfr-src/src/log1p.c117
1 files changed, 101 insertions, 16 deletions
diff --git a/Build/source/libs/mpfr/mpfr-src/src/log1p.c b/Build/source/libs/mpfr/mpfr-src/src/log1p.c
index 4ef43c2ae68..96c2f6831ed 100644
--- a/Build/source/libs/mpfr/mpfr-src/src/log1p.c
+++ b/Build/source/libs/mpfr/mpfr-src/src/log1p.c
@@ -23,8 +23,80 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
#define MPFR_NEED_LONGLONG_H
#include "mpfr-impl.h"
- /* The computation of log1p is done by
- log1p(x)=log(1+x) */
+/* Put in y an approximation of log(1+x) for x small.
+ We assume |x| < 1, in which case:
+ |x/2| <= |log(1+x)| = |x - x^2/2 + x^3/3 - x^4/4 + ...| <= |x|.
+ Return k such that the error is bounded by 2^k*ulp(y).
+*/
+static int
+mpfr_log1p_small (mpfr_ptr y, mpfr_srcptr x)
+{
+ mpfr_prec_t p = MPFR_PREC(y), err;
+ mpfr_t t, u;
+ unsigned long i;
+ int k;
+
+ MPFR_ASSERTD(MPFR_GET_EXP (x) <= 0); /* ensures |x| < 1 */
+
+ /* in the following, theta represents a value with |theta| <= 2^(1-p)
+ (might be a different value each time) */
+
+ mpfr_init2 (t, p);
+ mpfr_init2 (u, p);
+ mpfr_set (t, x, MPFR_RNDF); /* t = x * (1 + theta) */
+ mpfr_set (y, t, MPFR_RNDF); /* exact */
+ for (i = 2; ; i++)
+ {
+ mpfr_mul (t, t, x, MPFR_RNDF); /* t = x^i * (1 + theta)^i */
+ mpfr_div_ui (u, t, i, MPFR_RNDF); /* u = x^i/i * (1 + theta)^(i+1) */
+ if (MPFR_GET_EXP (u) <= MPFR_GET_EXP (y) - p) /* |u| < ulp(y) */
+ break;
+ if (i & 1)
+ mpfr_add (y, y, u, MPFR_RNDF); /* error <= ulp(y) */
+ else
+ mpfr_sub (y, y, u, MPFR_RNDF); /* error <= ulp(y) */
+ }
+ /* We assume |(1 + theta)^(i+1)| <= 2.
+ The neglected part is at most |u| + |u|/2 + ... <= 2|u| < 2 ulp(y)
+ which has to be multiplied by |(1 + theta)^(i+1)| <= 2, thus at most
+ 4 ulp(y).
+ The rounding error on y is bounded by:
+ * for the (i-2) add/sub, each error is bounded by ulp(y),
+ and since |y| <= |x|, this yields (i-2)*ulp(x)
+ * from Lemma 3.1 from [Higham02] (see algorithms.tex),
+ the relative error on u at step i is bounded by:
+ (i+1)*epsilon/(1-(i+1)*epsilon) where epsilon = 2^(1-p).
+ If (i+1)*epsilon <= 1/2, then the relative error on u at
+ step i is bounded by 2*(i+1)*epsilon, and since |u| <= 1/2^(i+1)
+ at step i, this gives an absolute error bound of;
+ 2*epsilon*x*(3/2^3 + 4/2^4 + 5/2^5 + ...) <= 2*2^(1-p)*x =
+ 4*2^(-p)*x <= 4*ulp(x).
+
+ If (i+1)*epsilon <= 1/2, then the relative error on u at step i
+ is bounded by (i+1)*epsilon/(1-(i+1)*epsilon) <= 1, thus it follows
+ |(1 + theta)^(i+1)| <= 2.
+
+ Finally the total error is bounded by 4*ulp(y) + (i-2)*ulp(x) + 4*ulp(x)
+ = 4*ulp(y) + (i+2)*ulp(x).
+ Since x/2 <= y, we have ulp(x) <= 2*ulp(y), thus the error is bounded by:
+ (2*i+8)*ulp(y).
+ */
+ err = 2 * i + 8;
+ k = __gmpfr_int_ceil_log2 (err);
+ MPFR_ASSERTN(k < p);
+ /* if k < p, since k = ceil(log2(err)), we have err <= 2^k <= 2^(p-1),
+ thus i+4 = err/2 <= 2^(p-2), thus (i+4)*epsilon <= 1/2, which implies
+ our assumption (i+1)*epsilon <= 1/2. */
+ mpfr_clear (t);
+ mpfr_clear (u);
+ return k;
+}
+
+/* The computation of log1p is done by
+ log1p(x) = log(1+x)
+ except when x is very small, in which case log1p(x) = x + tiny error,
+ or when x is small, where we use directly the Taylor expansion.
+*/
int
mpfr_log1p (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
@@ -89,7 +161,7 @@ mpfr_log1p (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
{
MPFR_SET_INF (y);
MPFR_SET_NEG (y);
- mpfr_set_divby0 ();
+ MPFR_SET_DIVBY0 ();
MPFR_RET (0);
}
MPFR_SET_NAN (y);
@@ -117,27 +189,40 @@ mpfr_log1p (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
if (MPFR_EXP(x) < 0)
Nt += -MPFR_EXP(x);
- /* initialise of intermediary variable */
+ /* initialize of intermediary variable */
mpfr_init2 (t, Nt);
/* First computation of log1p */
MPFR_ZIV_INIT (loop, Nt);
for (;;)
{
- /* compute log1p */
- inexact = mpfr_add_ui (t, x, 1, MPFR_RNDN); /* 1+x */
- /* if inexact = 0, then t = x+1, and the result is simply log(t) */
- if (inexact == 0)
+ int k;
+ /* small case: assuming the AGM algorithm used by mpfr_log uses
+ log2(p) steps for a precision of p bits, we try the special
+ variant whenever EXP(x) <= -p/log2(p). */
+ k = 1 + __gmpfr_int_ceil_log2 (Ny); /* the +1 avoids a division by 0
+ when Ny=1 */
+ if (MPFR_GET_EXP (x) <= - (mpfr_exp_t) (Ny / k))
+ /* this implies EXP(x) <= 0 thus x < 1 */
+ err = Nt - mpfr_log1p_small (t, x);
+ else
{
- inexact = mpfr_log (y, t, rnd_mode);
- goto end;
+ /* compute log1p */
+ inexact = mpfr_add_ui (t, x, 1, MPFR_RNDN); /* 1+x */
+ /* if inexact = 0, then t = x+1, and the result is simply log(t) */
+ if (inexact == 0)
+ {
+ inexact = mpfr_log (y, t, rnd_mode);
+ goto end;
+ }
+ mpfr_log (t, t, MPFR_RNDN); /* log(1+x) */
+
+ /* the error is bounded by (1/2+2^(1-EXP(t))*ulp(t)
+ (cf algorithms.tex)
+ if EXP(t)>=2, then error <= ulp(t)
+ if EXP(t)<=1, then error <= 2^(2-EXP(t))*ulp(t) */
+ err = Nt - MAX (0, 2 - MPFR_GET_EXP (t));
}
- mpfr_log (t, t, MPFR_RNDN); /* log(1+x) */
-
- /* the error is bounded by (1/2+2^(1-EXP(t))*ulp(t) (cf algorithms.tex)
- if EXP(t)>=2, then error <= ulp(t)
- if EXP(t)<=1, then error <= 2^(2-EXP(t))*ulp(t) */
- err = Nt - MAX (0, 2 - MPFR_GET_EXP (t));
if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
break;