amath  1.8.5
Simple command line calculator
log.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/e_log.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 static const double
44 ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
45 ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
46 two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
47 Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
48 Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
49 Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
50 Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
51 Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
52 Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
53 Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
54 
55 static double zero = 0.0;
56 
57 /**
58  * @brief Natural logarithm function (base e)
59  * @details
60  * <pre>
61  * Method
62  * 1. Argument Reduction: find k and f such that
63  * x = 2^k * (1+f),
64  * where sqrt(2)/2 < 1+f < sqrt(2) .
65  *
66  * 2. Approximation of log(1+f).
67  * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
68  * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
69  * = 2s + s*R
70  * We use a special Remes algorithm on [0,0.1716] to generate
71  * a polynomial of degree 14 to approximate R The maximum error
72  * of this polynomial approximation is bounded by 2**-58.45. In
73  * other words,
74  * 2 4 6 8 10 12 14
75  * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
76  * (the values of Lg1 to Lg7 are listed in the program)
77  * and
78  * | 2 14 | -58.45
79  * | Lg1*s +...+Lg7*s - R(z) | <= 2
80  * | |
81  * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
82  * In order to guarantee error in log below 1ulp, we compute log
83  * by
84  * log(1+f) = f - s*(f - R) (if f is not too large)
85  * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
86  *
87  * 3. Finally, log(x) = k*ln2 + log(1+f).
88  * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
89  * Here ln2 is split into two floating point number:
90  * ln2_hi + ln2_lo,
91  * where n*ln2_hi is always exact for |n| < 2000.
92  *
93  * Special cases:
94  * log(x) is NaN with signal if x < 0 (including -INF) ;
95  * log(+INF) is +INF; log(0) is -INF with signal;
96  * log(NaN) is that NaN with no signal.
97  *
98  * Accuracy:
99  * according to an error analysis, the error is always less than
100  * 1 ulp (unit in the last place).
101  *
102  * Constants:
103  * The hexadecimal values are the intended ones for the following
104  * constants. The decimal values may be used, provided that the
105  * compiler will convert from decimal to binary accurately enough
106  * to produce the hexadecimal values shown.
107  * </pre>
108  */
109 double log(double x)
110 {
111  double hfsq,f,s,z,R,w,t1,t2,dk;
112  int32_t k,hx,i,j;
113  uint32_t lx;
114 
115  EXTRACT_WORDS(hx,lx,x);
116 
117  k=0;
118  if (hx < 0x00100000) { /* x < 2**-1022 */
119  if (((hx&0x7fffffff)|lx)==0)
120  return -two54/zero; /* log(+-0)=-inf */
121  if (hx<0) return (x-x)/zero; /* log(-#) = NaN */
122  k -= 54;
123  x *= two54; /* subnormal number, scale up x */
124  GET_HIGH_WORD(hx,x); /* high word of x */
125  }
126  if (hx >= 0x7ff00000) return x+x;
127  k += (hx>>20)-1023;
128  hx &= 0x000fffff;
129  i = (hx+0x95f64)&0x100000;
130  SET_HIGH_WORD(x,hx|(i^0x3ff00000)); /* normalize x or x/2 */
131  k += (i>>20);
132  f = x-1.0;
133  if((0x000fffff&(2+hx))<3) { /* |f| < 2**-20 */
134  if(f==zero) {
135  if(k==0)
136  return zero;
137  else {
138  dk=(double)k;
139  return dk*ln2_hi+dk*ln2_lo;
140  }
141  }
142  R = f*f*(0.5-0.33333333333333333*f);
143  if(k==0) return f-R;
144  else {
145  dk=(double)k;
146  return dk*ln2_hi-((R-dk*ln2_lo)-f);
147  }
148  }
149  s = f/(2.0+f);
150  dk = (double)k;
151  z = s*s;
152  i = hx-0x6147a;
153  w = z*z;
154  j = 0x6b851-hx;
155  t1= w*(Lg2+w*(Lg4+w*Lg6));
156  t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
157  i |= j;
158  R = t2+t1;
159  if(i>0) {
160  hfsq=0.5*f*f;
161  if(k==0) return f-(hfsq-s*(hfsq+R));
162  else
163  return dk*ln2_hi-((hfsq-(s*(hfsq+R)+dk*ln2_lo))-f);
164  } else {
165  if(k==0) return f-s*(f-R);
166  else
167  return dk*ln2_hi-((s*(f-R)-dk*ln2_lo)-f);
168  }
169 }
double log(double x)
Natural logarithm function (base e)
Definition: log.c:109
static const double Lg7
Definition: log.c:53
static const double Lg6
Definition: log.c:52
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
static const double two54
Definition: log.c:46
static const double Lg2
Definition: log.c:48
#define EXTRACT_WORDS(ix0, ix1, d)
Get two 32 bit ints from a double.
Definition: prim.h:156
static double zero
Definition: log.c:55
static const double Lg3
Definition: log.c:49
static const double ln2_hi
Definition: log.c:44
static const double Lg4
Definition: log.c:50
static const double Lg1
Definition: log.c:47
#define SET_HIGH_WORD(d, v)
Set the more significant 32 bits of a double from an int.
Definition: prim.h:198
static const double ln2_lo
Definition: log.c:45
static const double Lg5
Definition: log.c:51