beta-0.89.2
[luatex.git] / source / libs / mpfr / mpfr-3.1.3 / src / log1p.c
blob6ef0e866ca8c2897be2e0096f8587e5201f56628
1 /* mpfr_log1p -- Compute log(1+x)
3 Copyright 2001-2015 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel 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 3 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.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 /* The computation of log1p is done by
27 log1p(x)=log(1+x) */
29 int
30 mpfr_log1p (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
32 int comp, inexact;
33 mpfr_exp_t ex;
34 MPFR_SAVE_EXPO_DECL (expo);
36 MPFR_LOG_FUNC
37 (("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (x), mpfr_log_prec, x, rnd_mode),
38 ("y[%Pu]=%.*Rg inexact=%d", mpfr_get_prec (y), mpfr_log_prec, y,
39 inexact));
41 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
43 if (MPFR_IS_NAN (x))
45 MPFR_SET_NAN (y);
46 MPFR_RET_NAN;
48 /* check for inf or -inf (result is not defined) */
49 else if (MPFR_IS_INF (x))
51 if (MPFR_IS_POS (x))
53 MPFR_SET_INF (y);
54 MPFR_SET_POS (y);
55 MPFR_RET (0);
57 else
59 MPFR_SET_NAN (y);
60 MPFR_RET_NAN;
63 else /* x is zero */
65 MPFR_ASSERTD (MPFR_IS_ZERO (x));
66 MPFR_SET_ZERO (y); /* log1p(+/- 0) = +/- 0 */
67 MPFR_SET_SAME_SIGN (y, x);
68 MPFR_RET (0);
72 ex = MPFR_GET_EXP (x);
73 if (ex < 0) /* -0.5 < x < 0.5 */
75 /* For x > 0, abs(log(1+x)-x) < x^2/2.
76 For x > -0.5, abs(log(1+x)-x) < x^2. */
77 if (MPFR_IS_POS (x))
78 MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, - ex - 1, 0, 0, rnd_mode, {});
79 else
80 MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, - ex, 0, 1, rnd_mode, {});
83 comp = mpfr_cmp_si (x, -1);
84 /* log1p(x) is undefined for x < -1 */
85 if (MPFR_UNLIKELY(comp <= 0))
87 if (comp == 0)
88 /* x=0: log1p(-1)=-inf (divide-by-zero exception) */
90 MPFR_SET_INF (y);
91 MPFR_SET_NEG (y);
92 mpfr_set_divby0 ();
93 MPFR_RET (0);
95 MPFR_SET_NAN (y);
96 MPFR_RET_NAN;
99 MPFR_SAVE_EXPO_MARK (expo);
101 /* General case */
103 /* Declaration of the intermediary variable */
104 mpfr_t t;
105 /* Declaration of the size variable */
106 mpfr_prec_t Ny = MPFR_PREC(y); /* target precision */
107 mpfr_prec_t Nt; /* working precision */
108 mpfr_exp_t err; /* error */
109 MPFR_ZIV_DECL (loop);
111 /* compute the precision of intermediary variable */
112 /* the optimal number of bits : see algorithms.tex */
113 Nt = Ny + MPFR_INT_CEIL_LOG2 (Ny) + 6;
115 /* if |x| is smaller than 2^(-e), we will loose about e bits
116 in log(1+x) */
117 if (MPFR_EXP(x) < 0)
118 Nt += -MPFR_EXP(x);
120 /* initialise of intermediary variable */
121 mpfr_init2 (t, Nt);
123 /* First computation of log1p */
124 MPFR_ZIV_INIT (loop, Nt);
125 for (;;)
127 /* compute log1p */
128 inexact = mpfr_add_ui (t, x, 1, MPFR_RNDN); /* 1+x */
129 /* if inexact = 0, then t = x+1, and the result is simply log(t) */
130 if (inexact == 0)
132 inexact = mpfr_log (y, t, rnd_mode);
133 goto end;
135 mpfr_log (t, t, MPFR_RNDN); /* log(1+x) */
137 /* the error is bounded by (1/2+2^(1-EXP(t))*ulp(t) (cf algorithms.tex)
138 if EXP(t)>=2, then error <= ulp(t)
139 if EXP(t)<=1, then error <= 2^(2-EXP(t))*ulp(t) */
140 err = Nt - MAX (0, 2 - MPFR_GET_EXP (t));
142 if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
143 break;
145 /* increase the precision */
146 MPFR_ZIV_NEXT (loop, Nt);
147 mpfr_set_prec (t, Nt);
149 inexact = mpfr_set (y, t, rnd_mode);
151 end:
152 MPFR_ZIV_FREE (loop);
153 mpfr_clear (t);
156 MPFR_SAVE_EXPO_FREE (expo);
157 return mpfr_check_range (y, inexact, rnd_mode);