s_log1p.c revision 22993
1230557Sjimharris/* @(#)s_log1p.c 5.1 93/09/24 */ 2230557Sjimharris/* 3230557Sjimharris * ==================================================== 4230557Sjimharris * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5230557Sjimharris * 6230557Sjimharris * Developed at SunPro, a Sun Microsystems, Inc. business. 7230557Sjimharris * Permission to use, copy, modify, and distribute this 8230557Sjimharris * software is freely granted, provided that this notice 9230557Sjimharris * is preserved. 10230557Sjimharris * ==================================================== 11230557Sjimharris */ 12230557Sjimharris 13230557Sjimharris#ifndef lint 14230557Sjimharrisstatic char rcsid[] = "$Id$"; 15230557Sjimharris#endif 16230557Sjimharris 17230557Sjimharris/* double log1p(double x) 18230557Sjimharris * 19230557Sjimharris * Method : 20230557Sjimharris * 1. Argument Reduction: find k and f such that 21230557Sjimharris * 1+x = 2^k * (1+f), 22230557Sjimharris * where sqrt(2)/2 < 1+f < sqrt(2) . 23230557Sjimharris * 24230557Sjimharris * Note. If k=0, then f=x is exact. However, if k!=0, then f 25230557Sjimharris * may not be representable exactly. In that case, a correction 26230557Sjimharris * term is need. Let u=1+x rounded. Let c = (1+x)-u, then 27230557Sjimharris * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), 28230557Sjimharris * and add back the correction term c/u. 29230557Sjimharris * (Note: when x > 2**53, one can simply return log(x)) 30230557Sjimharris * 31230557Sjimharris * 2. Approximation of log1p(f). 32230557Sjimharris * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 33230557Sjimharris * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 34230557Sjimharris * = 2s + s*R 35230557Sjimharris * We use a special Reme algorithm on [0,0.1716] to generate 36230557Sjimharris * a polynomial of degree 14 to approximate R The maximum error 37230557Sjimharris * of this polynomial approximation is bounded by 2**-58.45. In 38230557Sjimharris * other words, 39230557Sjimharris * 2 4 6 8 10 12 14 40230557Sjimharris * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s 41230557Sjimharris * (the values of Lp1 to Lp7 are listed in the program) 42230557Sjimharris * and 43230557Sjimharris * | 2 14 | -58.45 44230557Sjimharris * | Lp1*s +...+Lp7*s - R(z) | <= 2 45230557Sjimharris * | | 46230557Sjimharris * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. 47230557Sjimharris * In order to guarantee error in log below 1ulp, we compute log 48230557Sjimharris * by 49230557Sjimharris * log1p(f) = f - (hfsq - s*(hfsq+R)). 50230557Sjimharris * 51230557Sjimharris * 3. Finally, log1p(x) = k*ln2 + log1p(f). 52230557Sjimharris * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) 53230557Sjimharris * Here ln2 is split into two floating point number: 54230557Sjimharris * ln2_hi + ln2_lo, 55230557Sjimharris * where n*ln2_hi is always exact for |n| < 2000. 56230557Sjimharris * 57230557Sjimharris * Special cases: 58230557Sjimharris * log1p(x) is NaN with signal if x < -1 (including -INF) ; 59230557Sjimharris * log1p(+INF) is +INF; log1p(-1) is -INF with signal; 60230557Sjimharris * log1p(NaN) is that NaN with no signal. 61230557Sjimharris * 62230557Sjimharris * Accuracy: 63230557Sjimharris * according to an error analysis, the error is always less than 64230557Sjimharris * 1 ulp (unit in the last place). 65230557Sjimharris * 66230557Sjimharris * Constants: 67230557Sjimharris * The hexadecimal values are the intended ones for the following 68230557Sjimharris * constants. The decimal values may be used, provided that the 69230557Sjimharris * compiler will convert from decimal to binary accurately enough 70230557Sjimharris * to produce the hexadecimal values shown. 71230557Sjimharris * 72230557Sjimharris * Note: Assuming log() return accurate answer, the following 73230557Sjimharris * algorithm can be used to compute log1p(x) to within a few ULP: 74230557Sjimharris * 75230557Sjimharris * u = 1+x; 76230557Sjimharris * if(u==1.0) return x ; else 77230557Sjimharris * return log(u)*(x/(u-1.0)); 78230557Sjimharris * 79230557Sjimharris * See HP-15C Advanced Functions Handbook, p.193. 80230557Sjimharris */ 81230557Sjimharris 82230557Sjimharris#include "math.h" 83230557Sjimharris#include "math_private.h" 84230557Sjimharris 85230557Sjimharris#ifdef __STDC__ 86230557Sjimharrisstatic const double 87230557Sjimharris#else 88230557Sjimharrisstatic double 89230557Sjimharris#endif 90230557Sjimharrisln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */ 91230557Sjimharrisln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */ 92230557Sjimharristwo54 = 1.80143985094819840000e+16, /* 43500000 00000000 */ 93230557SjimharrisLp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */ 94230557SjimharrisLp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */ 95230557SjimharrisLp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */ 96230557SjimharrisLp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */ 97230557SjimharrisLp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */ 98230557SjimharrisLp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */ 99230557SjimharrisLp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */ 100230557Sjimharris 101230557Sjimharris#ifdef __STDC__ 102230557Sjimharrisstatic const double zero = 0.0; 103230557Sjimharris#else 104230557Sjimharrisstatic double zero = 0.0; 105230557Sjimharris#endif 106230557Sjimharris 107230557Sjimharris#ifdef __STDC__ 108230557Sjimharris double log1p(double x) 109230557Sjimharris#else 110230557Sjimharris double log1p(x) 111230557Sjimharris double x; 112230557Sjimharris#endif 113230557Sjimharris{ 114230557Sjimharris double hfsq,f,c,s,z,R,u; 115230557Sjimharris int32_t k,hx,hu,ax; 116230557Sjimharris 117230557Sjimharris GET_HIGH_WORD(hx,x); 118230557Sjimharris ax = hx&0x7fffffff; 119230557Sjimharris 120230557Sjimharris k = 1; 121230557Sjimharris if (hx < 0x3FDA827A) { /* x < 0.41422 */ 122230557Sjimharris if(ax>=0x3ff00000) { /* x <= -1.0 */ 123230557Sjimharris if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */ 124230557Sjimharris else return (x-x)/(x-x); /* log1p(x<-1)=NaN */ 125230557Sjimharris } 126230557Sjimharris if(ax<0x3e200000) { /* |x| < 2**-29 */ 127230557Sjimharris if(two54+x>zero /* raise inexact */ 128230557Sjimharris &&ax<0x3c900000) /* |x| < 2**-54 */ 129230557Sjimharris return x; 130230557Sjimharris else 131230557Sjimharris return x - x*x*0.5; 132230557Sjimharris } 133230557Sjimharris if(hx>0||hx<=((int32_t)0xbfd2bec3)) { 134230557Sjimharris k=0;f=x;hu=1;} /* -0.2929<x<0.41422 */ 135230557Sjimharris } 136230557Sjimharris if (hx >= 0x7ff00000) return x+x; 137230557Sjimharris if(k!=0) { 138230557Sjimharris if(hx<0x43400000) { 139230557Sjimharris u = 1.0+x; 140230557Sjimharris GET_HIGH_WORD(hu,u); 141230557Sjimharris k = (hu>>20)-1023; 142230557Sjimharris c = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */ 143230557Sjimharris c /= u; 144230557Sjimharris } else { 145230557Sjimharris u = x; 146230557Sjimharris GET_HIGH_WORD(hu,u); 147230557Sjimharris k = (hu>>20)-1023; 148230557Sjimharris c = 0; 149230557Sjimharris } 150230557Sjimharris hu &= 0x000fffff; 151230557Sjimharris if(hu<0x6a09e) { 152230557Sjimharris SET_HIGH_WORD(u,hu|0x3ff00000); /* normalize u */ 153230557Sjimharris } else { 154230557Sjimharris k += 1; 155230557Sjimharris SET_HIGH_WORD(u,hu|0x3fe00000); /* normalize u/2 */ 156230557Sjimharris hu = (0x00100000-hu)>>2; 157230557Sjimharris } 158230557Sjimharris f = u-1.0; 159230557Sjimharris } 160230557Sjimharris hfsq=0.5*f*f; 161230557Sjimharris if(hu==0) { /* |f| < 2**-20 */ 162230557Sjimharris if(f==zero) if(k==0) return zero; 163230557Sjimharris else {c += k*ln2_lo; return k*ln2_hi+c;} 164230557Sjimharris R = hfsq*(1.0-0.66666666666666666*f); 165230557Sjimharris if(k==0) return f-R; else 166230557Sjimharris return k*ln2_hi-((R-(k*ln2_lo+c))-f); 167230557Sjimharris } 168230557Sjimharris s = f/(2.0+f); 169230557Sjimharris z = s*s; 170230557Sjimharris R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); 171230557Sjimharris if(k==0) return f-(hfsq-s*(hfsq+R)); else 172230557Sjimharris return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); 173230557Sjimharris} 174230557Sjimharris