1 /* mpfr_log1p -- Compute log(1+x)
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 2.1 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.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 /* The computation of log1p is done by
30 mpfr_log1p (mpfr_ptr y
, mpfr_srcptr x
, mp_rnd_t rnd_mode
)
34 MPFR_SAVE_EXPO_DECL (expo
);
36 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x
)))
43 /* check for inf or -inf (result is not defined) */
44 else if (MPFR_IS_INF (x
))
60 MPFR_ASSERTD (MPFR_IS_ZERO (x
));
61 MPFR_SET_ZERO (y
); /* log1p(+/- 0) = +/- 0 */
62 MPFR_SET_SAME_SIGN (y
, x
);
67 ex
= MPFR_GET_EXP (x
);
68 if (ex
< 0) /* -0.5 < x < 0.5 */
70 /* For x > 0, abs(log(1+x)-x) < x^2/2.
71 For x > -0.5, abs(log(1+x)-x) < x^2. */
73 MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y
, x
, - ex
- 1, 0, 0, rnd_mode
, {});
75 MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y
, x
, - ex
, 0, 1, rnd_mode
, {});
78 comp
= mpfr_cmp_si (x
, -1);
79 /* log1p(x) is undefined for x < -1 */
80 if (MPFR_UNLIKELY(comp
<= 0))
83 /* x=0: log1p(-1)=-inf (division by zero) */
93 MPFR_SAVE_EXPO_MARK (expo
);
97 /* Declaration of the intermediary variable */
99 /* Declaration of the size variable */
100 mp_prec_t Ny
= MPFR_PREC(y
); /* target precision */
101 mp_prec_t Nt
; /* working precision */
102 mp_exp_t err
; /* error */
103 MPFR_ZIV_DECL (loop
);
105 /* compute the precision of intermediary variable */
106 /* the optimal number of bits : see algorithms.tex */
107 Nt
= Ny
+ MPFR_INT_CEIL_LOG2 (Ny
) + 6;
109 /* if |x| is smaller than 2^(-e), we will loose about e bits
114 /* initialise of intermediary variable */
117 /* First computation of log1p */
118 MPFR_ZIV_INIT (loop
, Nt
);
122 inexact
= mpfr_add_ui (t
, x
, 1, GMP_RNDN
); /* 1+x */
123 /* if inexact = 0, then t = x+1, and the result is simply log(t) */
126 inexact
= mpfr_log (y
, t
, rnd_mode
);
129 mpfr_log (t
, t
, GMP_RNDN
); /* log(1+x) */
131 /* the error is bounded by (1/2+2^(1-EXP(t))*ulp(t) (cf algorithms.tex)
132 if EXP(t)>=2, then error <= ulp(t)
133 if EXP(t)<=1, then error <= 2^(2-EXP(t))*ulp(t) */
134 err
= Nt
- MAX (0, 2 - MPFR_GET_EXP (t
));
136 if (MPFR_LIKELY (MPFR_CAN_ROUND (t
, err
, Ny
, rnd_mode
)))
139 /* increase the precision */
140 MPFR_ZIV_NEXT (loop
, Nt
);
141 mpfr_set_prec (t
, Nt
);
143 inexact
= mpfr_set (y
, t
, rnd_mode
);
146 MPFR_ZIV_FREE (loop
);
150 MPFR_SAVE_EXPO_FREE (expo
);
151 return mpfr_check_range (y
, inexact
, rnd_mode
);