1
/* mpfr_log1p -- Compute log(1+x)
3
Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5
This file is part of the MPFR Library.
7
The MPFR Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The MPFR Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the MPFR Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
22
#define MPFR_NEED_LONGLONG_H
23
#include "mpfr-impl.h"
25
/* The computation of log1p is done by
29
mpfr_log1p (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
32
MPFR_SAVE_EXPO_DECL (expo);
34
if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
41
/* check for inf or -inf (result is not defined) */
42
else if (MPFR_IS_INF (x))
58
MPFR_ASSERTD (MPFR_IS_ZERO (x));
59
MPFR_SET_ZERO (y); /* log1p(+/- 0) = +/- 0 */
60
MPFR_SET_SAME_SIGN (y, x);
65
/* log(1+x) = x-x^2/2 + ... so the error is < 2^(2*EXP(x)-1) */
66
MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, x, -MPFR_GET_EXP (x)+1,0,rnd_mode,{});
68
comp = mpfr_cmp_si (x, -1);
69
/* log1p(x) is undefined for x < -1 */
70
if (MPFR_UNLIKELY(comp <= 0))
73
/* x=0: log1p(-1)=-inf (division by zero) */
83
MPFR_SAVE_EXPO_MARK (expo);
87
/* Declaration of the intermediary variable */
89
/* Declaration of the size variable */
90
mp_prec_t Ny = MPFR_PREC(y); /* target precision */
91
mp_prec_t Nt; /* working precision */
92
mp_exp_t err; /* error */
95
/* compute the precision of intermediary variable */
96
/* the optimal number of bits : see algorithms.tex */
97
Nt = Ny + MPFR_INT_CEIL_LOG2 (Ny) + 6;
99
/* if |x| is smaller than 2^(-e), we will loose about e bits
104
/* initialise of intermediary variable */
107
/* First computation of cosh */
108
MPFR_ZIV_INIT (loop, Nt);
112
mpfr_add_ui (t, x, 1, GMP_RNDN); /* 1+x */
113
mpfr_log (t, t, GMP_RNDN); /* log(1+x)*/
115
/* estimation of the error */
116
/*err=Nt-(__gmpfr_ceil_log2(1+pow(2,1-MPFR_GET_EXP(t))));*/
117
err = Nt - (MAX (1 - MPFR_GET_EXP (t), 0) + 1);
119
if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
122
/* increase the precision */
123
MPFR_ZIV_NEXT (loop, Nt);
124
mpfr_set_prec (t, Nt);
126
MPFR_ZIV_FREE (loop);
127
inexact = mpfr_set (y, t, rnd_mode);
132
MPFR_SAVE_EXPO_FREE (expo);
133
return mpfr_check_range (y, inexact, rnd_mode);