1 /* mpfr_log1p -- Compute log(1+x) 2 3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4 Contributed by the Arenaire and Caramel projects, INRIA. 5 6 This file is part of the GNU MPFR Library. 7 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. 12 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. 17 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. */ 22 23 #define MPFR_NEED_LONGLONG_H 24 #include "mpfr-impl.h" 25 26 /* The computation of log1p is done by 27 log1p(x)=log(1+x) */ 28 29 int 30 mpfr_log1p (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode) 31 { 32 int comp, inexact; 33 mpfr_exp_t ex; 34 MPFR_SAVE_EXPO_DECL (expo); 35 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)); 40 41 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x))) 42 { 43 if (MPFR_IS_NAN (x)) 44 { 45 MPFR_SET_NAN (y); 46 MPFR_RET_NAN; 47 } 48 /* check for inf or -inf (result is not defined) */ 49 else if (MPFR_IS_INF (x)) 50 { 51 if (MPFR_IS_POS (x)) 52 { 53 MPFR_SET_INF (y); 54 MPFR_SET_POS (y); 55 MPFR_RET (0); 56 } 57 else 58 { 59 MPFR_SET_NAN (y); 60 MPFR_RET_NAN; 61 } 62 } 63 else /* x is zero */ 64 { 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); 69 } 70 } 71 72 ex = MPFR_GET_EXP (x); 73 if (ex < 0) /* -0.5 < x < 0.5 */ 74 { 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, {}); 81 } 82 83 comp = mpfr_cmp_si (x, -1); 84 /* log1p(x) is undefined for x < -1 */ 85 if (MPFR_UNLIKELY(comp <= 0)) 86 { 87 if (comp == 0) 88 /* x=0: log1p(-1)=-inf (divide-by-zero exception) */ 89 { 90 MPFR_SET_INF (y); 91 MPFR_SET_NEG (y); 92 mpfr_set_divby0 (); 93 MPFR_RET (0); 94 } 95 MPFR_SET_NAN (y); 96 MPFR_RET_NAN; 97 } 98 99 MPFR_SAVE_EXPO_MARK (expo); 100 101 /* General case */ 102 { 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); 110 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; 114 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); 119 120 /* initialise of intermediary variable */ 121 mpfr_init2 (t, Nt); 122 123 /* First computation of log1p */ 124 MPFR_ZIV_INIT (loop, Nt); 125 for (;;) 126 { 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) 131 { 132 inexact = mpfr_log (y, t, rnd_mode); 133 goto end; 134 } 135 mpfr_log (t, t, MPFR_RNDN); /* log(1+x) */ 136 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)); 141 142 if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode))) 143 break; 144 145 /* increase the precision */ 146 MPFR_ZIV_NEXT (loop, Nt); 147 mpfr_set_prec (t, Nt); 148 } 149 inexact = mpfr_set (y, t, rnd_mode); 150 151 end: 152 MPFR_ZIV_FREE (loop); 153 mpfr_clear (t); 154 } 155 156 MPFR_SAVE_EXPO_FREE (expo); 157 return mpfr_check_range (y, inexact, rnd_mode); 158 } 159