amath  1.8.5
Simple command line calculator
log1p.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  * The original source code can be obtained from:
29  * http://www.netlib.org/fdlibm/s_log1p.c
30  *
31  * =================================================================
32  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
33  *
34  * Developed at SunSoft, a Sun Microsystems, Inc. business.
35  * Permission to use, copy, modify, and distribute this
36  * software is freely granted, provided that this notice
37  * is preserved.
38  * =================================================================
39  */
40 
41 #include "prim.h"
42 
43 /* double log1p(double x)
44  *
45  * Method :
46  * 1. Argument Reduction: find k and f such that
47  * 1+x = 2^k * (1+f),
48  * where sqrt(2)/2 < 1+f < sqrt(2) .
49  *
50  * Note. If k=0, then f=x is exact. However, if k!=0, then f
51  * may not be representable exactly. In that case, a correction
52  * term is need. Let u=1+x rounded. Let c = (1+x)-u, then
53  * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
54  * and add back the correction term c/u.
55  * (Note: when x > 2**53, one can simply return log(x))
56  *
57  * 2. Approximation of log1p(f).
58  * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
59  * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
60  * = 2s + s*R
61  * We use a special Remes algorithm on [0,0.1716] to generate
62  * a polynomial of degree 14 to approximate R The maximum error
63  * of this polynomial approximation is bounded by 2**-58.45. In
64  * other words,
65  * 2 4 6 8 10 12 14
66  * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
67  * (the values of Lp1 to Lp7 are listed in the program)
68  * and
69  * | 2 14 | -58.45
70  * | Lp1*s +...+Lp7*s - R(z) | <= 2
71  * | |
72  * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
73  * In order to guarantee error in log below 1ulp, we compute log
74  * by
75  * log1p(f) = f - (hfsq - s*(hfsq+R)).
76  *
77  * 3. Finally, log1p(x) = k*ln2 + log1p(f).
78  * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
79  * Here ln2 is split into two floating point number:
80  * ln2_hi + ln2_lo,
81  * where n*ln2_hi is always exact for |n| < 2000.
82  *
83  * Special cases:
84  * log1p(x) is NaN with signal if x < -1 (including -INF) ;
85  * log1p(+INF) is +INF; log1p(-1) is -INF with signal;
86  * log1p(NaN) is that NaN with no signal.
87  *
88  * Accuracy:
89  * according to an error analysis, the error is always less than
90  * 1 ulp (unit in the last place).
91  *
92  * Constants:
93  * The hexadecimal values are the intended ones for the following
94  * constants. The decimal values may be used, provided that the
95  * compiler will convert from decimal to binary accurately enough
96  * to produce the hexadecimal values shown.
97  *
98  * Note: Assuming log() return accurate answer, the following
99  * algorithm can be used to compute log1p(x) to within a few ULP:
100  *
101  * u = 1+x;
102  * if(u==1.0) return x ; else
103  * return log(u)*(x/(u-1.0));
104  *
105  * See HP-15C Advanced Functions Handbook, p.193.
106  */
107 
108 static const double
109 ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
110 ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
111 two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
112 Lp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
113 Lp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
114 Lp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
115 Lp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
116 Lp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
117 Lp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
118 Lp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
119 
120 static double zero = 0.0;
121 
122 double log1p(double x)
123 {
124  double hfsq,f,c,s,z,R,u;
125  int32_t k,hx,hu,ax;
126 
127  f = 0.0;
128  c = 0.0;
129  hu = 0;
130 
131  GET_HIGH_WORD(hx,x); /* high word of x */
132  ax = hx&0x7fffffff;
133 
134  k = 1;
135  if (hx < 0x3FDA827A) { /* x < 0.41422 */
136  if(ax>=0x3ff00000) { /* x <= -1.0 */
137  if(x==-1.0) return -two54/zero; /* log1p(-1)=+inf */
138  else return (x-x)/(x-x); /* log1p(x<-1)=NaN */
139  }
140  if(ax<0x3e200000) { /* |x| < 2**-29 */
141  if(two54+x>zero /* raise inexact */
142  &&ax<0x3c900000) /* |x| < 2**-54 */
143  return x;
144  else
145  return x - x*x*0.5;
146  }
147  if(hx>0||hx<=((int)0xbfd2bec3)) {
148  k=0;
149  f=x;
150  hu=1;
151  } /* -0.2929<x<0.41422 */
152  }
153  if (hx >= 0x7ff00000) return x+x;
154  if(k!=0) {
155  if(hx<0x43400000) {
156  u = 1.0+x;
157  GET_HIGH_WORD(hu,u); /* high word of u */
158  k = (hu>>20)-1023;
159  c = (k>0)? 1.0-(u-x):x-(u-1.0);/* correction term */
160  c /= u;
161  } else {
162  u = x;
163  GET_HIGH_WORD(hu,u); /* high word of u */
164  k = (hu>>20)-1023;
165  c = 0;
166  }
167  hu &= 0x000fffff;
168  if(hu<0x6a09e) {
169  SET_HIGH_WORD(u, hu|0x3ff00000); /* normalize u */
170  } else {
171  k += 1;
172  SET_HIGH_WORD(u, hu|0x3fe00000); /* normalize u/2 */
173  hu = (0x00100000-hu)>>2;
174  }
175  f = u-1.0;
176  }
177  hfsq=0.5*f*f;
178  if(hu==0) { /* |f| < 2**-20 */
179  if(f==zero) {
180  if(k==0) return zero;
181  else {
182  c += k*ln2_lo;
183  return k*ln2_hi+c;
184  }
185  }
186  R = hfsq*(1.0-0.66666666666666666*f);
187  if(k==0) return f-R;
188  else
189  return k*ln2_hi-((R-(k*ln2_lo+c))-f);
190  }
191  s = f/(2.0+f);
192  z = s*s;
193  R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7))))));
194  if(k==0) return f-(hfsq-s*(hfsq+R));
195  else
196  return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f);
197 }
static const double Lp6
Definition: log1p.c:117
static double zero
Definition: log1p.c:120
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
static const double Lp4
Definition: log1p.c:115
double log1p(double x)
Definition: log1p.c:122
static const double ln2_lo
Definition: log1p.c:110
static const double Lp3
Definition: log1p.c:114
static const double Lp5
Definition: log1p.c:116
static const double two54
Definition: log1p.c:111
static const double Lp2
Definition: log1p.c:113
static const double ln2_hi
Definition: log1p.c:109
static const double Lp7
Definition: log1p.c:118
static const double Lp1
Definition: log1p.c:112
#define SET_HIGH_WORD(d, v)
Set the more significant 32 bits of a double from an int.
Definition: prim.h:198