amath  1.8.5
Simple command line calculator
expm1.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_expm1.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 /* expm1(x)
44  * Returns exp(x)-1, the exponential of x minus 1.
45  *
46  * Method
47  * 1. Argument reduction:
48  * Given x, find r and integer k such that
49  *
50  * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
51  *
52  * Here a correction term c will be computed to compensate
53  * the error in r when rounded to a floating-point number.
54  *
55  * 2. Approximating expm1(r) by a special rational function on
56  * the interval [0,0.34658]:
57  * Since
58  * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
59  * we define R1(r*r) by
60  * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
61  * That is,
62  * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
63  * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
64  * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
65  * We use a special Remes algorithm on [0,0.347] to generate
66  * a polynomial of degree 5 in r*r to approximate R1. The
67  * maximum error of this polynomial approximation is bounded
68  * by 2**-61. In other words,
69  * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
70  * where Q1 = -1.6666666666666567384E-2,
71  * Q2 = 3.9682539681370365873E-4,
72  * Q3 = -9.9206344733435987357E-6,
73  * Q4 = 2.5051361420808517002E-7,
74  * Q5 = -6.2843505682382617102E-9;
75  * (where z=r*r, and the values of Q1 to Q5 are listed below)
76  * with error bounded by
77  * | 5 | -61
78  * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
79  * | |
80  *
81  * expm1(r) = exp(r)-1 is then computed by the following
82  * specific way which minimize the accumulation rounding error:
83  * 2 3
84  * r r [ 3 - (R1 + R1*r/2) ]
85  * expm1(r) = r + --- + --- * [--------------------]
86  * 2 2 [ 6 - r*(3 - R1*r/2) ]
87  *
88  * To compensate the error in the argument reduction, we use
89  * expm1(r+c) = expm1(r) + c + expm1(r)*c
90  * ~ expm1(r) + c + r*c
91  * Thus c+r*c will be added in as the correction terms for
92  * expm1(r+c). Now rearrange the term to avoid optimization
93  * screw up:
94  * ( 2 2 )
95  * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
96  * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
97  * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
98  * ( )
99  *
100  * = r - E
101  * 3. Scale back to obtain expm1(x):
102  * From step 1, we have
103  * expm1(x) = either 2^k*[expm1(r)+1] - 1
104  * = or 2^k*[expm1(r) + (1-2^-k)]
105  * 4. Implementation notes:
106  * (A). To save one multiplication, we scale the coefficient Qi
107  * to Qi*2^i, and replace z by (x^2)/2.
108  * (B). To achieve maximum accuracy, we compute expm1(x) by
109  * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
110  * (ii) if k=0, return r-E
111  * (iii) if k=-1, return 0.5*(r-E)-0.5
112  * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
113  * else return 1.0+2.0*(r-E);
114  * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
115  * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
116  * (vii) return 2^k(1-((E+2^-k)-r))
117  *
118  * Special cases:
119  * expm1(INF) is INF, expm1(NaN) is NaN;
120  * expm1(-INF) is -1, and
121  * for finite argument, only expm1(0)=0 is exact.
122  *
123  * Accuracy:
124  * according to an error analysis, the error is always less than
125  * 1 ulp (unit in the last place).
126  *
127  * Misc. info.
128  * For IEEE double
129  * if x > 7.09782712893383973096e+02 then expm1(x) overflow
130  *
131  * Constants:
132  * The hexadecimal values are the intended ones for the following
133  * constants. The decimal values may be used, provided that the
134  * compiler will convert from decimal to binary accurately enough
135  * to produce the hexadecimal values shown.
136  */
137 
138 static const double
139  one = 1.0,
140  huge = 1.0e+300,
141  tiny = 1.0e-300,
142  o_threshold = 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */
143  ln2_hi = 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */
144  ln2_lo = 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */
145  invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */
146  /* scaled coefficients related to expm1 */
147  Q1 = -3.33333333333331316428e-02, /* BFA11111 111110F4 */
148  Q2 = 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
149  Q3 = -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
150  Q4 = 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
151  Q5 = -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */
152 
153 double expm1(double x)
154 {
155  double y, hi, lo, c, t, e, hxs, hfx, r1;
156  int32_t k, xsb;
157  uint32_t hx;
158 
159  c = 0.0;
160 
161  GET_HIGH_WORD(hx, x); /* high word of x */
162  xsb = hx & 0x80000000; /* sign bit of x */
163  hx &= 0x7fffffff; /* high word of |x| */
164 
165  /* filter out huge and non-finite argument */
166  if (hx >= 0x4043687A)
167  { /* if |x|>=56*ln2 */
168  if (hx >= 0x40862E42)
169  { /* if |x|>=709.78... */
170  if (hx >= 0x7ff00000)
171  {
172  uint32_t low;
173  GET_LOW_WORD(low, x);
174  if (((hx & 0xfffff) | low) != 0)
175  return x + x; /* NaN */
176  else
177  return (xsb == 0) ? x : -1.0; /* exp(+-inf)={inf,-1} */
178  }
179  if (x > o_threshold)
180  return huge * huge; /* overflow */
181  }
182  if (xsb != 0)
183  { /* x < -56*ln2, return -1.0 with inexact */
184  if (x + tiny < 0.0) /* raise inexact */
185  return tiny - one; /* return -1 */
186  }
187  }
188 
189  /* argument reduction */
190  if (hx > 0x3fd62e42)
191  { /* if |x| > 0.5 ln2 */
192  if (hx < 0x3FF0A2B2)
193  { /* and |x| < 1.5 ln2 */
194  if (xsb == 0)
195  {
196  hi = x - ln2_hi;
197  lo = ln2_lo;
198  k = 1;
199  }
200  else
201  {
202  hi = x + ln2_hi;
203  lo = -ln2_lo;
204  k = -1;
205  }
206  }
207  else
208  {
209  k = (int32_t)(invln2 * x + ((xsb == 0) ? 0.5 : -0.5));
210  t = k;
211  hi = x - t * ln2_hi; /* t*ln2_hi is exact here */
212  lo = t * ln2_lo;
213  }
214  x = hi - lo;
215  c = (hi - x) - lo;
216  }
217  else if (hx < 0x3c900000)
218  { /* when |x|<2**-54, return x */
219  t = huge + x; /* return x with inexact flags when x!=0 */
220  return x - (t - (huge + x));
221  }
222  else
223  k = 0;
224 
225  /* x is now in primary range */
226  hfx = 0.5 * x;
227  hxs = x * hfx;
228  r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
229  t = 3.0 - r1 * hfx;
230  e = hxs * ((r1 - t) / (6.0 - x * t));
231  if (k == 0)
232  return x - (x * e - hxs); /* c is 0 */
233  else
234  {
235  e = (x * (e - c) - c);
236  e -= hxs;
237  if (k == -1)
238  return 0.5 * (x - e) - 0.5;
239  if (k == 1)
240  {
241  if (x < -0.25)
242  return -2.0 * (e - (x + 0.5));
243  else
244  return one + 2.0 * (x - e);
245  }
246  if (k <= -2 || k > 56)
247  { /* suffice to return exp(x)-1 */
248  uint32_t hy;
249 
250  y = one - (e - x);
251  GET_HIGH_WORD(hy, y);
252  SET_HIGH_WORD(y, hy + (k << 20)); /* add k to y's exponent */
253  return y - one;
254  }
255  t = one;
256  if (k < 20)
257  {
258  uint32_t hy;
259 
260  SET_HIGH_WORD(t, 0x3ff00000 - (0x200000 >> k)); /* t=1-2^-k */
261  y = t - (e - x);
262  GET_HIGH_WORD(hy, y);
263  SET_HIGH_WORD(y, hy + (k << 20)); /* add k to y's exponent */
264  }
265  else
266  {
267  uint32_t hy;
268 
269  SET_HIGH_WORD(t, (0x3ff - k) << 20); /* 2^-k */
270  y = x - (e + t);
271  y += one;
272  GET_HIGH_WORD(hy, y);
273  SET_HIGH_WORD(y, hy + (k << 20)); /* add k to y's exponent */
274  }
275  }
276  return y;
277 }
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
static const double tiny
Definition: expm1.c:141
static const double Q5
Definition: expm1.c:151
static const double Q1
Definition: expm1.c:147
#define GET_LOW_WORD(i, d)
Get the less significant 32 bit int from a double.
Definition: prim.h:177
static const double Q2
Definition: expm1.c:148
double expm1(double x)
Definition: expm1.c:153
static const double one
Definition: expm1.c:139
static const double o_threshold
Definition: expm1.c:142
static const double ln2_hi
Definition: expm1.c:143
static const double huge
Definition: expm1.c:140
static const double invln2
Definition: expm1.c:145
static const double ln2_lo
Definition: expm1.c:144
static const double Q4
Definition: expm1.c:150
#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 Q3
Definition: expm1.c:149