*** empty log message ***
[gnulib.git] / lib / expl.c
1 /* Emulation for expl.
2    Contributed by Paolo Bonzini
3
4    Copyright 2002, 2003 Free Software Foundation, Inc.
5
6    This file is part of gnulib.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License along
19    with this program; if not, write to the Free Software Foundation,
20    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
21
22 #include <float.h>
23 #include <math.h>
24
25 #include "mathl.h"
26
27 static const long double C[] = {
28 /* Chebyshev polynom coeficients for (exp(x)-1)/x */
29 #define P1 C[0]
30 #define P2 C[1]
31 #define P3 C[2]
32 #define P4 C[3]
33 #define P5 C[4]
34 #define P6 C[5]
35  0.5L,
36  1.66666666666666666666666666666666683E-01L,
37  4.16666666666666666666654902320001674E-02L,
38  8.33333333333333333333314659767198461E-03L,
39  1.38888888889899438565058018857254025E-03L,
40  1.98412698413981650382436541785404286E-04L,
41
42 /* Smallest integer x for which e^x overflows.  */
43 #define himark C[6]
44  11356.523406294143949491931077970765L,
45
46 /* Largest integer x for which e^x underflows.  */
47 #define lomark C[7]
48 -11433.4627433362978788372438434526231L,
49
50 /* very small number */
51 #define TINY C[8]
52  1.0e-4900L,
53
54 /* 2^16383 */
55 #define TWO16383 C[9]
56  5.94865747678615882542879663314003565E+4931L};
57
58 long double
59 expl (long double x)
60 {
61   /* Check for usual case.  */
62   if (x < himark && x > lomark)
63     {
64       int exponent;
65       long double t, x22;
66       int k = 1;
67       long double result = 1.0;
68
69       /* Compute an integer power of e with a granularity of 0.125.  */
70       exponent = (int) floorl (x * 8.0L);
71       x -= exponent / 8.0L;
72
73       if (x > 0.0625)
74         {
75           exponent++;
76           x -= 0.125L;
77         }
78
79       if (exponent < 0)
80         {
81           t = 0.8824969025845954028648921432290507362220L; /* e^-0.25 */
82           exponent = -exponent;
83         }
84       else
85         t = 1.1331484530668263168290072278117938725655L; /* e^0.25 */
86
87       while (exponent)
88         {
89           if (exponent & k)
90             {
91               result *= t;
92               exponent ^= k;
93             }
94           t *= t;
95           k <<= 1;
96         }
97
98       /* Approximate (e^x - 1)/x, using a seventh-degree polynomial,
99          with maximum error in [-2^-16-2^-53,2^-16+2^-53]
100          less than 4.8e-39.  */
101       x22 = x + x*x*(P1+x*(P2+x*(P3+x*(P4+x*(P5+x*P6)))));
102
103       return result + result * x22;
104     }
105   /* Exceptional cases:  */
106   else if (x < himark)
107     {
108       if (x + x == x)
109         /* e^-inf == 0, with no error.  */
110         return 0;
111       else
112         /* Underflow */
113         return TINY * TINY;
114     }
115   else
116     /* Return x, if x is a NaN or Inf; or overflow, otherwise.  */
117     return TWO16383*x;
118 }
119
120 #if 0
121 int
122 main (void)
123 {
124   printf ("%.16Lg\n", expl(1.0L));
125   printf ("%.16Lg\n", expl(-1.0L));
126   printf ("%.16Lg\n", expl(2.0L));
127   printf ("%.16Lg\n", expl(4.0L));
128   printf ("%.16Lg\n", expl(-2.0L));
129   printf ("%.16Lg\n", expl(-4.0L));
130   printf ("%.16Lg\n", expl(0.0625L));
131   printf ("%.16Lg\n", expl(0.3L));
132   printf ("%.16Lg\n", expl(0.6L));
133 }
134 #endif