1 /* 2 * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* double log1p(double x) 27 * 28 * Method : 29 * 1. Argument Reduction: find k and f such that 30 * 1+x = 2^k * (1+f), 31 * where sqrt(2)/2 < 1+f < sqrt(2) . 32 * 33 * Note. If k=0, then f=x is exact. However, if k!=0, then f 34 * may not be representable exactly. In that case, a correction 35 * term is need. Let u=1+x rounded. Let c = (1+x)-u, then 36 * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), 37 * and add back the correction term c/u. 38 * (Note: when x > 2**53, one can simply return log(x)) 39 * 40 * 2. Approximation of log1p(f). 41 * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 42 * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 43 * = 2s + s*R 44 * We use a special Reme algorithm on [0,0.1716] to generate 45 * a polynomial of degree 14 to approximate R The maximum error 46 * of this polynomial approximation is bounded by 2**-58.45. In 47 * other words, 48 * 2 4 6 8 10 12 14 49 * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s 50 * (the values of Lp1 to Lp7 are listed in the program) 51 * and 52 * | 2 14 | -58.45 53 * | Lp1*s +...+Lp7*s - R(z) | <= 2 54 * | | 55 * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. 56 * In order to guarantee error in log below 1ulp, we compute log 57 * by 58 * log1p(f) = f - (hfsq - s*(hfsq+R)). 59 * 60 * 3. Finally, log1p(x) = k*ln2 + log1p(f). 61 * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) 62 * Here ln2 is split into two floating point number: 63 * ln2_hi + ln2_lo, 64 * where n*ln2_hi is always exact for |n| < 2000. 65 * 66 * Special cases: 67 * log1p(x) is NaN with signal if x < -1 (including -INF) ; 68 * log1p(+INF) is +INF; log1p(-1) is -INF with signal; 69 * log1p(NaN) is that NaN with no signal. 70 * 71 * Accuracy: 72 * according to an error analysis, the error is always less than 73 * 1 ulp (unit in the last place). 74 * 75 * Constants: 76 * The hexadecimal values are the intended ones for the following 77 * constants. The decimal values may be used, provided that the 78 * compiler will convert from decimal to binary accurately enough 79 * to produce the hexadecimal values shown. 80 * 81 * Note: Assuming log() return accurate answer, the following 82 * algorithm can be used to compute log1p(x) to within a few ULP: 83 * 84 * u = 1+x; 85 * if(u==1.0) return x ; else 86 * return log(u)*(x/(u-1.0)); 87 * 88 * See HP-15C Advanced Functions Handbook, p.193. 89 */ 90 91 #include "fdlibm.h" 92 93 #ifdef __STDC__ 94 static const double 95 #else 96 static double 97 #endif 98 ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ 99 ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ 100 two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ 101 Lp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ 102 Lp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ 103 Lp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ 104 Lp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ 105 Lp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ 106 Lp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ 107 Lp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ 108 109 static double zero = 0.0; 110 111 #ifdef __STDC__ log1p(double x)112 double log1p(double x) 113 #else 114 double log1p(x) 115 double x; 116 #endif 117 { 118 double hfsq,f=0,c=0,s,z,R,u; 119 int k,hx,hu=0,ax; 120 121 hx = __HI(x); /* high word of x */ 122 ax = hx&0x7fffffff; 123 124 k = 1; 125 if (hx < 0x3FDA827A) { /* x < 0.41422 */ 126 if(ax>=0x3ff00000) { /* x <= -1.0 */ 127 /* 128 * Added redundant test against hx to work around VC++ 129 * code generation problem. 130 */ 131 if(x==-1.0 && (hx==0xbff00000)) /* log1p(-1)=-inf */ 132 return -two54/zero; 133 else 134 return (x-x)/(x-x); /* log1p(x<-1)=NaN */ 135 } 136 if(ax<0x3e200000) { /* |x| < 2**-29 */ 137 if(two54+x>zero /* raise inexact */ 138 &&ax<0x3c900000) /* |x| < 2**-54 */ 139 return x; 140 else 141 return x - x*x*0.5; 142 } 143 if(hx>0||hx<=((int)0xbfd2bec3)) { 144 k=0;f=x;hu=1;} /* -0.2929<x<0.41422 */ 145 } 146 if (hx >= 0x7ff00000) return x+x; 147 if(k!=0) { 148 if(hx<0x43400000) { 149 u = 1.0+x; 150 hu = __HI(u); /* high word of u */ 151 k = (hu>>20)-1023; 152 c = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */ 153 c /= u; 154 } else { 155 u = x; 156 hu = __HI(u); /* high word of u */ 157 k = (hu>>20)-1023; 158 c = 0; 159 } 160 hu &= 0x000fffff; 161 if(hu<0x6a09e) { 162 __HI(u) = hu|0x3ff00000; /* normalize u */ 163 } else { 164 k += 1; 165 __HI(u) = hu|0x3fe00000; /* normalize u/2 */ 166 hu = (0x00100000-hu)>>2; 167 } 168 f = u-1.0; 169 } 170 hfsq=0.5*f*f; 171 if(hu==0) { /* |f| < 2**-20 */ 172 if(f==zero) { if(k==0) return zero; 173 else {c += k*ln2_lo; return k*ln2_hi+c;}} 174 R = hfsq*(1.0-0.66666666666666666*f); 175 if(k==0) return f-R; else 176 return k*ln2_hi-((R-(k*ln2_lo+c))-f); 177 } 178 s = f/(2.0+f); 179 z = s*s; 180 R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); 181 if(k==0) return f-(hfsq-s*(hfsq+R)); else 182 return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); 183 } 184