48 */
49 private static double __LO(double x, int low) {
50 long transX = Double.doubleToRawLongBits(x);
51 return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L)|low );
52 }
53
54 /**
55 * Return the high-order 32 bits of the double argument as an int.
56 */
57 private static int __HI(double x) {
58 long transducer = Double.doubleToRawLongBits(x);
59 return (int)(transducer >> 32);
60 }
61
62 /**
63 * Return a double with its high-order bits of the second argument
64 * and the low-order bits of the first argument..
65 */
66 private static double __HI(double x, int high) {
67 long transX = Double.doubleToRawLongBits(x);
68 return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL)|( ((long)high)) << 32 );
69 }
70
71 public static double hypot(double x, double y) {
72 return Hypot.compute(x, y);
73 }
74
75 /**
76 * cbrt(x)
77 * Return cube root of x
78 */
79 public static class Cbrt {
80 // unsigned
81 private static final int B1 = 715094163; /* B1 = (682-0.03306235651)*2**20 */
82 private static final int B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
83
84 private static final double C = 5.42857142857142815906e-01; /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
85 private static final double D = -7.05306122448979611050e-01; /* -864/1225 = 0xBFE691DE, 0x2532C834 */
86 private static final double E = 1.41428571428571436819e+00; /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
87 private static final double F = 1.60714285714285720630e+00; /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
88 private static final double G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
231 t2 = a - t1;
232 w = Math.sqrt(t1*t1 - (b*(-b) - t2 * (a + t1)));
233 } else {
234 a = a + a;
235 y1 = 0;
236 y1 = __HI(y1, hb);
237 y2 = b - y1;
238 t1 = 0;
239 t1 = __HI(t1, ha + 0x00100000);
240 t2 = a - t1;
241 w = Math.sqrt(t1*y1 - (w*(-w) - (t1*y2 + t2*b)));
242 }
243 if (k != 0) {
244 t1 = 1.0;
245 int t1_hi = __HI(t1);
246 t1_hi += (k << 20);
247 t1 = __HI(t1, t1_hi);
248 return t1 * w;
249 } else
250 return w;
251 }
252 }
253 }
|
48 */
49 private static double __LO(double x, int low) {
50 long transX = Double.doubleToRawLongBits(x);
51 return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L)|low );
52 }
53
54 /**
55 * Return the high-order 32 bits of the double argument as an int.
56 */
57 private static int __HI(double x) {
58 long transducer = Double.doubleToRawLongBits(x);
59 return (int)(transducer >> 32);
60 }
61
62 /**
63 * Return a double with its high-order bits of the second argument
64 * and the low-order bits of the first argument..
65 */
66 private static double __HI(double x, int high) {
67 long transX = Double.doubleToRawLongBits(x);
68 return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL) |
69 ( ((long)high)) << 32 );
70 }
71
72 public static double hypot(double x, double y) {
73 return Hypot.compute(x, y);
74 }
75
76 /**
77 * cbrt(x)
78 * Return cube root of x
79 */
80 public static class Cbrt {
81 // unsigned
82 private static final int B1 = 715094163; /* B1 = (682-0.03306235651)*2**20 */
83 private static final int B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
84
85 private static final double C = 5.42857142857142815906e-01; /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
86 private static final double D = -7.05306122448979611050e-01; /* -864/1225 = 0xBFE691DE, 0x2532C834 */
87 private static final double E = 1.41428571428571436819e+00; /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
88 private static final double F = 1.60714285714285720630e+00; /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
89 private static final double G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
232 t2 = a - t1;
233 w = Math.sqrt(t1*t1 - (b*(-b) - t2 * (a + t1)));
234 } else {
235 a = a + a;
236 y1 = 0;
237 y1 = __HI(y1, hb);
238 y2 = b - y1;
239 t1 = 0;
240 t1 = __HI(t1, ha + 0x00100000);
241 t2 = a - t1;
242 w = Math.sqrt(t1*y1 - (w*(-w) - (t1*y2 + t2*b)));
243 }
244 if (k != 0) {
245 t1 = 1.0;
246 int t1_hi = __HI(t1);
247 t1_hi += (k << 20);
248 t1 = __HI(t1, t1_hi);
249 return t1 * w;
250 } else
251 return w;
252 }
253 }
254
255 /**
256 * Returns the exponential of x.
257 *
258 * Method
259 * 1. Argument reduction:
260 * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
261 * Given x, find r and integer k such that
262 *
263 * x = k*ln2 + r, |r| <= 0.5*ln2.
264 *
265 * Here r will be represented as r = hi-lo for better
266 * accuracy.
267 *
268 * 2. Approximation of exp(r) by a special rational function on
269 * the interval [0,0.34658]:
270 * Write
271 * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
272 * We use a special Reme algorithm on [0,0.34658] to generate
273 * a polynomial of degree 5 to approximate R. The maximum error
274 * of this polynomial approximation is bounded by 2**-59. In
275 * other words,
276 * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
277 * (where z=r*r, and the values of P1 to P5 are listed below)
278 * and
279 * | 5 | -59
280 * | 2.0+P1*z+...+P5*z - R(z) | <= 2
281 * | |
282 * The computation of exp(r) thus becomes
283 * 2*r
284 * exp(r) = 1 + -------
285 * R - r
286 * r*R1(r)
287 * = 1 + r + ----------- (for better accuracy)
288 * 2 - R1(r)
289 * where
290 * 2 4 10
291 * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
292 *
293 * 3. Scale back to obtain exp(x):
294 * From step 1, we have
295 * exp(x) = 2^k * exp(r)
296 *
297 * Special cases:
298 * exp(INF) is INF, exp(NaN) is NaN;
299 * exp(-INF) is 0, and
300 * for finite argument, only exp(0)=1 is exact.
301 *
302 * Accuracy:
303 * according to an error analysis, the error is always less than
304 * 1 ulp (unit in the last place).
305 *
306 * Misc. info.
307 * For IEEE double
308 * if x > 7.09782712893383973096e+02 then exp(x) overflow
309 * if x < -7.45133219101941108420e+02 then exp(x) underflow
310 *
311 * Constants:
312 * The hexadecimal values are the intended ones for the following
313 * constants. The decimal values may be used, provided that the
314 * compiler will convert from decimal to binary accurately enough
315 * to produce the hexadecimal values shown.
316 */
317 static class Exp {
318 private static final double one = 1.0;
319 private static final double[] halF = {0.5,-0.5,};
320 private static final double huge = 1.0e+300;
321 private static final double twom1000= 9.33263618503218878990e-302; /* 2**-1000=0x01700000,0*/
322 private static final double o_threshold= 7.09782712893383973096e+02; /* 0x40862E42, 0xFEFA39EF */
323 private static final double u_threshold= -7.45133219101941108420e+02; /* 0xc0874910, 0xD52D3051 */
324 private static final double[] ln2HI ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */
325 -6.93147180369123816490e-01}; /* 0xbfe62e42, 0xfee00000 */
326 private static final double[] ln2LO ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */
327 -1.90821492927058770002e-10,}; /* 0xbdea39ef, 0x35793c76 */
328 private static final double invln2 = 1.44269504088896338700e+00; /* 0x3ff71547, 0x652b82fe */
329 private static final double P1 = 1.66666666666666019037e-01; /* 0x3FC55555, 0x5555553E */
330 private static final double P2 = -2.77777777770155933842e-03; /* 0xBF66C16C, 0x16BEBD93 */
331 private static final double P3 = 6.61375632143793436117e-05; /* 0x3F11566A, 0xAF25DE2C */
332 private static final double P4 = -1.65339022054652515390e-06; /* 0xBEBBBD41, 0xC5D26BF1 */
333 private static final double P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
334
335 public static strictfp double compute(double x) {
336 double y,hi=0,lo=0,c,t;
337 int k=0,xsb;
338 /*unsigned*/ int hx;
339
340 hx = __HI(x); /* high word of x */
341 xsb = (hx>>31)&1; /* sign bit of x */
342 hx &= 0x7fffffff; /* high word of |x| */
343
344 /* filter out non-finite argument */
345 if(hx >= 0x40862E42) { /* if |x|>=709.78... */
346 if(hx>=0x7ff00000) {
347 if(((hx&0xfffff)|__LO(x))!=0)
348 return x+x; /* NaN */
349 else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */
350 }
351 if(x > o_threshold) return huge*huge; /* overflow */
352 if(x < u_threshold) return twom1000*twom1000; /* underflow */
353 }
354
355 /* argument reduction */
356 if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
357 if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
358 hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
359 } else {
360 k = (int)(invln2*x+halF[xsb]);
361 t = k;
362 hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */
363 lo = t*ln2LO[0];
364 }
365 x = hi - lo;
366 }
367 else if(hx < 0x3e300000) { /* when |x|<2**-28 */
368 if(huge+x>one) return one+x;/* trigger inexact */
369 }
370 else k = 0;
371
372 /* x is now in primary range */
373 t = x*x;
374 c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
375 if(k==0) return one-((x*c)/(c-2.0)-x);
376 else y = one-((lo-(x*c)/(2.0-c))-hi);
377 if(k >= -1021) {
378 y = __HI(y, __HI(y) + (k<<20)); /* add k to y's exponent */
379 return y;
380 } else {
381 y = __HI(y, __HI(y) + ((k+1000)<<20));/* add k to y's exponent */
382 return y*twom1000;
383 }
384 }
385 }
386 }
|