IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
s_log1p.cs
Go to the documentation of this file.
1/*
2 * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/* double log1p(double x)
27 *
28 * Method :
29 * 1. Argument Reduction: find k and f such that
30 * 1+x = 2^k * (1+f),
31 * where sqrt(2)/2 < 1+f < sqrt(2) .
32 *
33 * Note. If k=0, then f=x is exact. However, if k!=0, then f
34 * may not be representable exactly. In that case, a correction
35 * term is need. Let u=1+x rounded. Let c = (1+x)-u, then
36 * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
37 * and add back the correction term c/u.
38 * (Note: when x > 2**53, one can simply return log(x))
39 *
40 * 2. Approximation of log1p(f).
41 * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
42 * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
43 * = 2s + s*R
44 * We use a special Reme algorithm on [0,0.1716] to generate
45 * a polynomial of degree 14 to approximate R The maximum error
46 * of this polynomial approximation is bounded by 2**-58.45. In
47 * other words,
48 * 2 4 6 8 10 12 14
49 * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
50 * (the values of Lp1 to Lp7 are listed in the program)
51 * and
52 * | 2 14 | -58.45
53 * | Lp1*s +...+Lp7*s - R(z) | <= 2
54 * | |
55 * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
56 * In order to guarantee error in log below 1ulp, we compute log
57 * by
58 * log1p(f) = f - (hfsq - s*(hfsq+R)).
59 *
60 * 3. Finally, log1p(x) = k*ln2 + log1p(f).
61 * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
62 * Here ln2 is split into two floating point number:
63 * ln2_hi + ln2_lo,
64 * where n*ln2_hi is always exact for |n| < 2000.
65 *
66 * Special cases:
67 * log1p(x) is NaN with signal if x < -1 (including -INF) ;
68 * log1p(+INF) is +INF; log1p(-1) is -INF with signal;
69 * log1p(NaN) is that NaN with no signal.
70 *
71 * Accuracy:
72 * according to an error analysis, the error is always less than
73 * 1 ulp (unit in the last place).
74 *
75 * Constants:
76 * The hexadecimal values are the intended ones for the following
77 * constants. The decimal values may be used, provided that the
78 * compiler will convert from decimal to binary accurately enough
79 * to produce the hexadecimal values shown.
80 *
81 * Note: Assuming log() return accurate answer, the following
82 * algorithm can be used to compute log1p(x) to within a few ULP:
83 *
84 * u = 1+x;
85 * if(u==1.0) return x ; else
86 * return log(u)*(x/(u-1.0));
87 *
88 * See HP-15C Advanced Functions Handbook, p.193.
89 */
90
92{
93 static partial class fdlibm
94 {
95 internal static double log1p(double x)
96 {
97 const double
98 ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
99 ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
100 two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
101 Lp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
102 Lp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
103 Lp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
104 Lp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
105 Lp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
106 Lp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
107 Lp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
108
109 const double zero = 0.0;
110
111 double hfsq, f = 0, c = 0, s, z, R, u;
112 int k, hx, hu = 0, ax;
113
114 hx = __HI(x); /* high word of x */
115 ax = hx & 0x7fffffff;
116
117 k = 1;
118 if (hx < 0x3FDA827A)
119 { /* x < 0.41422 */
120 if (ax >= 0x3ff00000)
121 { /* x <= -1.0 */
122 /*
123 * Added redundant test against hx to work around VC++
124 * code generation problem.
125 */
126 if (x == -1.0 && (hx == unchecked((int)0xbff00000))) /* log1p(-1)=-inf */
127 return -two54 / zero;
128 else
129 return (x - x) / (x - x); /* log1p(x<-1)=NaN */
130 }
131 if (ax < 0x3e200000)
132 { /* |x| < 2**-29 */
133 if (two54 + x > zero /* raise inexact */
134 && ax < 0x3c900000) /* |x| < 2**-54 */
135 return x;
136 else
137 return x - x * x * 0.5;
138 }
139 if (hx > 0 || hx <= (unchecked((int)0xbfd2bec3)))
140 {
141 k = 0; f = x; hu = 1;
142 } /* -0.2929<x<0.41422 */
143 }
144 if (hx >= 0x7ff00000) return x + x;
145 if (k != 0)
146 {
147 if (hx < 0x43400000)
148 {
149 u = 1.0 + x;
150 hu = __HI(u); /* high word of u */
151 k = (hu >> 20) - 1023;
152 c = (k > 0) ? 1.0 - (u - x) : x - (u - 1.0);/* correction term */
153 c /= u;
154 }
155 else
156 {
157 u = x;
158 hu = __HI(u); /* high word of u */
159 k = (hu >> 20) - 1023;
160 c = 0;
161 }
162 hu &= 0x000fffff;
163 if (hu < 0x6a09e)
164 {
165 u = __HI(u, hu | 0x3ff00000); /* normalize u */
166 }
167 else
168 {
169 k += 1;
170 u = __HI(u, hu | 0x3fe00000); /* normalize u/2 */
171 hu = (0x00100000 - hu) >> 2;
172 }
173 f = u - 1.0;
174 }
175 hfsq = 0.5 * f * f;
176 if (hu == 0)
177 { /* |f| < 2**-20 */
178 if (f == zero)
179 {
180 if (k == 0) return zero;
181 else { c += k * ln2_lo; return k * ln2_hi + c; }
182 }
183 R = hfsq * (1.0 - 0.66666666666666666 * f);
184 if (k == 0) return f - R;
185 else
186 return k * ln2_hi - ((R - (k * ln2_lo + c)) - f);
187 }
188 s = f / (2.0 + f);
189 z = s * s;
190 R = z * (Lp1 + z * (Lp2 + z * (Lp3 + z * (Lp4 + z * (Lp5 + z * (Lp6 + z * Lp7))))));
191 if (k == 0) return f - (hfsq - s * (hfsq + R));
192 else
193 return k * ln2_hi - ((hfsq - (s * (hfsq + R) + (k * ln2_lo + c))) - f);
194 }
195 }
196}