001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM.
004 *
005 * All rights reserved.
006 *
007 * Redistribution and use in source and binary forms, with or without modification,
008 * are permitted provided that the following conditions are met:
009 *
010 * 1. Redistributions of source code must retain the above copyright notice,
011 *    this list of conditions and the following disclaimer.
012 *
013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
014 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
015 *
016 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products
017 *    derived from this software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package tec.units.ri.internal;
031
032/*
033 * Ported from the Sun Microsystems FDLIBM C-library.
034 * (Freely Distributable Library for Math)
035 * ====================================================
036 * Portions Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
037 *
038 * Permission to use, copy, modify, and distribute this
039 * software is freely granted, provided that this notice
040 * is preserved.
041 * ====================================================
042 */
043
044/**
045 * MathUtil for Java ME. This fills the gap in Java ME Math with a port of Sun's public FDLIBM C-library for IEEE-754.
046 *
047 * @author kmashint
048 *
049 * @see http://www.netlib.org/fdlibm/readme For the Freely Distributable C-library conforming to IEEE-754 floating point math.
050 * @see http://web.mit.edu/source/third/gcc/libjava/java/lang/ For the GNU C variant of the same IEEE-754 routines.
051 * @see http://www.dclausen.net/projects/microfloat/ Another take on the IEEE-754 routines.
052 * @see http://real-java.sourceforge.net/Real.html Yet another take on the IEEE-754 routines.
053 * @see http ://today.java.net/pub/a/today/2007/11/06/creating-java-me-math-pow-method .html For other approximations.
054 * @see http ://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java- and-c-c/ For fast but rough approximations.
055 * @see http ://martin.ankerl.com/2007/02/11/optimized-exponential-functions-for-java / For more fast but rough approximations.
056 */
057public abstract class MathUtil {
058
059  /* Common constants. */
060
061  private static final double zero = 0.0, one = 1.0, two = 2.0, tiny = 1.0e-300, huge = 1.0e+300, two53 = 9007199254740992.0, /*
062                                                                                                                              * 0x43400000                                                                                                                              */
063  two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
064  twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
065  P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
066  P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
067  P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
068  P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
069  P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
070
071  // private static final double pio2_hi = 1.57079632679489655800e+00;
072  /*
073                                                                    * 0x3FF921FB,
074                                                                    * 0x54442D18
075                                                                    */
076  // pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
077  // pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */
078  /* coefficient for R(x^2) */
079  // pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
080  // pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
081  // pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
082  // pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
083  // pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
084  // pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
085  // qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
086  // qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
087  // qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
088  // qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
089
090  private static final double pi_o_4 = 7.8539816339744827900E-01, /*
091                                                                  * 0x3FE921FB,
092                                                                  * 0x54442D18
093                                                                  */
094  pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
095  pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
096  pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
097
098  private static final long HI_MASK = 0xffffffff00000000L, LO_MASK = 0x00000000ffffffffL;
099
100  private static final int HI_SHIFT = 32;
101
102  /**
103   * Return Math.E to the exponent a. This in turn uses ieee7854_exp(double).
104   */
105  public static final double exp(double a) {
106    return ieee754_exp(a);
107  }
108
109  /**
110   * Return the natural logarithm, ln(a), as it relates to Math.E. This in turn uses ieee7854_log(double).
111   */
112  public static final double log(double a) {
113    return ieee754_log(a);
114  }
115
116  /**
117   * Return a to the power of b, sometimes written as a ** b but not to be confused with the bitwise ^ operator. This in turn uses
118   * ieee7854_log(double).
119   */
120  public static final double pow(double a, double b) {
121    return ieee754_pow(a, b);
122  }
123
124  /**
125   * Return the arcsine of a.
126   */
127  /*  public static final double asin(double a) {
128      return ieee754_asin(a);
129    }
130  */
131  /**
132   * Return the arccosine of a.
133   */
134  /*private static final double acos(double a) {
135    return ieee754_acos(a);
136  }*/
137
138  /**
139   * Return the arctangent of a, call it b, where a = tan(b).
140   */
141  public static final double atan(double a) {
142    return ieee754_atan(a);
143  }
144
145  /**
146   * For any real arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point
147   * given by the coordinates (x, y) on it. The angle is positive for counter-clockwise angles (upper half-plane, y > 0), and negative for clockwise
148   * angles (lower half-plane, y < 0). This in turn uses ieee7854_arctan2(double).
149   */
150  public static final double atan2(double b, double a) {
151    return ieee754_atan2(a, b);
152  }
153
154  /*
155   * __ieee754_exp(x) Returns the exponential of x.
156   * 
157   * Method 1. Argument reduction: Reduce x to an r so that |r| <= 0.5*ln2 ~
158   * 0.34658. Given x, find r and integer k such that
159   * 
160   * x = k*ln2 + r, |r| <= 0.5*ln2.
161   * 
162   * Here r will be represented as r = hi-lo for better accuracy.
163   * 
164   * 2. Approximation of exp(r) by a special rational function on the interval
165   * [0,0.34658]: Write R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 -
166   * r**4/360 + ... We use a special Remes algorithm on [0,0.34658] to
167   * generate a polynomial of degree 5 to approximate R. The maximum error of
168   * this polynomial approximation is bounded by 2**-59. In other words, R(z)
169   * ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 (where z=r*r, and
170   * the values of P1 to P5 are listed below) and | 5 | -59 |
171   * 2.0+P1*z+...+P5*z - R(z) | <= 2 | | The computation of exp(r) thus
172   * becomes 2*r exp(r) = 1 + ------- R - r r*R1(r) = 1 + r + ----------- (for
173   * better accuracy) 2 - R1(r) where 2 4 10 R1(r) = r - (P1*r + P2*r + ... +
174   * P5*r ).
175   * 
176   * 3. Scale back to obtain exp(x): From step 1, we have exp(x) = 2^k *
177   * exp(r)
178   * 
179   * Special cases: exp(INF) is INF, exp(NaN) is NaN; exp(-INF) is 0, and for
180   * finite argument, only exp(0)=1 is exact.
181   * 
182   * Accuracy: according to an error analysis, the error is always less than 1
183   * ulp (unit in the last place).
184   * 
185   * Misc. info. For IEEE double if x > 7.09782712893383973096e+02 then exp(x)
186   * overflow if x < -7.45133219101941108420e+02 then exp(x) underflow
187   * 
188   * Constants: The hexadecimal values are the intended ones for the following
189   * constants. The decimal values may be used, provided that the compiler
190   * will convert from decimal to binary accurately enough to produce the
191   * hexadecimal values shown.
192   */
193
194  private static final double twom1000 = 9.33263618503218878990e-302, /*
195                                                                      * 2**-1000
196                                                                      * =
197                                                                      * 0x01700000
198                                                                      * ,0
199                                                                      */
200  o_threshold = 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */
201  u_threshold = -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */
202  invln2 = 1.44269504088896338700e+00; /* 0x3ff71547, 0x652b82fe */
203
204  private static final double[] halF = new double[] { 0.5, -0.5 }, ln2HI = new double[] { 6.93147180369123816490e-01, /*
205                                                                                                                      * 0x3fe62e42,
206                                                                                                                      * 0xfee00000
207                                                                                                                      */
208  -6.93147180369123816490e-01 }, /* 0xbfe62e42, 0xfee00000 */
209  ln2LO = new double[] { 1.90821492927058770002e-10, /*
210                                                     * 0x3dea39ef,
211                                                     * 0x35793c76
212                                                     */
213  -1.90821492927058770002e-10 }; /* 0xbdea39ef, 0x35793c76 */
214
215  private static final double ieee754_exp(double x) {
216    double y, c, t;
217    double hi = 0, lo = 0;
218    int k = 0;
219    int xsb, hx, lx;
220    long yl;
221    long xl = Double.doubleToLongBits(x);
222
223    hx = (int) ((long) xl >>> HI_SHIFT); /* high word of x */
224    xsb = (hx >> 31) & 1; /* sign bit of x */
225    hx &= 0x7fffffff; /* high word of |x| */
226
227    /* filter out non-finite argument */
228    if (hx >= 0x40862E42) { /* if |x|>=709.78... */
229      if (hx >= 0x7ff00000) {
230        lx = (int) ((long) xl & LO_MASK); /* low word of x */
231        if (((hx & 0xfffff) | lx) != 0)
232          return x + x; /* NaN */
233        else
234          return (xsb == 0) ? x : 0.0; /* exp(+-inf)={inf,0} */
235      }
236      if (x > o_threshold)
237        return huge * huge; /* overflow */
238      if (x < u_threshold)
239        return twom1000 * twom1000; /* underflow */
240    }
241
242    /* argument reduction */
243    if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
244      if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
245        hi = x - ln2HI[xsb];
246        lo = ln2LO[xsb];
247        k = 1 - xsb - xsb;
248      } else {
249        k = (int) (invln2 * x + halF[xsb]);
250        t = k;
251        hi = x - t * ln2HI[0]; /* t*ln2HI is exact here */
252        lo = t * ln2LO[0];
253      }
254      x = hi - lo;
255    } else if (hx < 0x3e300000) { /* when |x|<2**-28 */
256      if (huge + x > one)
257        return one + x;/* trigger inexact */
258    }
259    // else k = 0; // handled at declaration
260
261    /* x is now in primary range */
262    t = x * x;
263    c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
264    if (k == 0)
265      return one - ((x * c) / (c - 2.0) - x);
266    else
267      y = one - ((lo - (x * c) / (2.0 - c)) - hi);
268    yl = Double.doubleToLongBits(y);
269    if (k >= -1021) {
270      yl += ((long) k << (20 + HI_SHIFT)); /* add k to y's exponent */
271      return Double.longBitsToDouble(yl);
272    } else {
273      yl += ((long) (k + 1000) << (20 + HI_SHIFT));/* add k to y's exponent */
274      return Double.longBitsToDouble(yl) * twom1000;
275    }
276  }
277
278  /*
279   * __ieee754_log(x) Return the logrithm of x
280   * 
281   * Method : 1. Argument Reduction: find k and f such that x = 2^k * (1+f),
282   * where sqrt(2)/2 < 1+f < sqrt(2) .
283   * 
284   * 2. Approximation of log(1+f). Let s = f/(2+f) ; based on log(1+f) =
285   * log(1+s) - log(1-s) = 2s + 2/3 s**3 + 2/5 s**5 + ....., = 2s + s*R We use
286   * a special Reme algorithm on [0,0.1716] to generate a polynomial of degree
287   * 14 to approximate R The maximum error of this polynomial approximation is
288   * bounded by 2**-58.45. In other words, 2 4 6 8 10 12 14 R(z) ~ Lg1*s
289   * +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s (the values of Lg1 to Lg7 are
290   * listed in the program) and | 2 14 | -58.45 | Lg1*s +...+Lg7*s - R(z) | <=
291   * 2 | | Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. In
292   * order to guarantee error in log below 1ulp, we compute log by log(1+f) =
293   * f - s*(f - R) (if f is not too large) log(1+f) = f - (hfsq - s*(hfsq+R)).
294   * (better accuracy)
295   * 
296   * 3. Finally, log(x) = k*ln2 + log(1+f). =
297   * k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) Here ln2 is split into two
298   * floating point number: ln2_hi + ln2_lo, where n*ln2_hi is always exact
299   * for |n| < 2000.
300   * 
301   * Special cases: log(x) is NaN with signal if x < 0 (including -INF) ;
302   * log(+INF) is +INF; log(0) is -INF with signal; log(NaN) is that NaN with
303   * no signal.
304   * 
305   * Accuracy: according to an error analysis, the error is always less than 1
306   * ulp (unit in the last place).
307   * 
308   * Constants: The hexadecimal values are the intended ones for the following
309   * constants. The decimal values may be used, provided that the compiler
310   * will convert from decimal to binary accurately enough to produce the
311   * hexadecimal values shown.
312   */
313
314  private static final double ln2_hi = 6.93147180369123816490e-01, /*
315                                                                   * 3fe62e42
316                                                                   * fee00000
317                                                                   */
318  ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
319  Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
320  Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
321  Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
322  Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
323  Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
324  Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
325  Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
326
327  private static final double ieee754_log(double x) {
328    double hfsq, f, s, z, R, w, t1, t2, dk;
329    int k, hx, lx, i, j;
330    long xl = Double.doubleToLongBits(x);
331
332    hx = (int) (xl >> HI_SHIFT); /* high word of x */
333    lx = (int) (xl & LO_MASK); /* low word of x */
334
335    k = 0;
336    if (hx < 0x00100000) { /* x < 2**-1022 */
337      if (((hx & 0x7fffffff) | lx) == 0)
338        return -two54 / zero; /* log(+-0)=-inf */
339      if (hx < 0)
340        return (x - x) / zero; /* log(-#) = NaN */
341      k -= 54;
342      x *= two54; /* subnormal number, scale up x */
343      hx = (int) (Double.doubleToLongBits(x) >>> HI_SHIFT); /*
344                                                            * high word of
345                                                            * x
346                                                            */
347    }
348    if (hx >= 0x7ff00000)
349      return x + x;
350    k += (hx >> 20) - 1023;
351    hx &= 0x000fffff;
352    i = (hx + 0x95f64) & 0x100000;
353    // __HI(x) = hx|(i^0x3ff00000); /* normalize x or x/2 */
354    x = Double.longBitsToDouble(((long) (hx | (i ^ 0x3ff00000)) << HI_SHIFT) | (Double.doubleToLongBits(x) & LO_MASK));
355    k += (i >> 20);
356    f = x - 1.0;
357    if ((0x000fffff & (2 + hx)) < 3) { /* |f| < 2**-20 */
358      if (f == zero)
359        if (k == 0)
360          return zero;
361        else {
362          dk = (double) k;
363          return dk * ln2_hi + dk * ln2_lo;
364        }
365      R = f * f * (0.5 - 0.33333333333333333 * f);
366      if (k == 0)
367        return f - R;
368      else {
369        dk = (double) k;
370        return dk * ln2_hi - ((R - dk * ln2_lo) - f);
371      }
372    }
373    s = f / (2.0 + f);
374    dk = (double) k;
375    z = s * s;
376    i = hx - 0x6147a;
377    w = z * z;
378    j = 0x6b851 - hx;
379    t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
380    t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
381    i |= j;
382    R = t2 + t1;
383    if (i > 0) {
384      hfsq = 0.5 * f * f;
385      if (k == 0)
386        return f - (hfsq - s * (hfsq + R));
387      else
388        return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) - f);
389    } else {
390      if (k == 0)
391        return f - s * (f - R);
392      else
393        return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f);
394    }
395  }
396
397  /*
398   * __ieee754_pow(x,y) return x**y
399   * 
400   * n Method: Let x = 2 * (1+f) 1. Compute and return log2(x) in two pieces:
401   * log2(x) = w1 + w2, where w1 has 53-24 = 29 bit trailing zeros. 2. Perform
402   * y*log2(x) = n+y' by simulating muti-precision arithmetic, where
403   * |y'|<=0.5. 3. Return x**y = 2**n*exp(y'*log2)
404   * 
405   * Special cases: 1. (anything) ** 0 is 1 2. (anything) ** 1 is itself 3.
406   * (anything) ** NAN is NAN 4. NAN ** (anything except 0) is NAN 5. +-(|x| >
407   * 1) ** +INF is +INF 6. +-(|x| > 1) ** -INF is +0 7. +-(|x| < 1) ** +INF is
408   * +0 8. +-(|x| < 1) ** -INF is +INF 9. +-1 ** +-INF is NAN 10. +0 **
409   * (+anything except 0, NAN) is +0 11. -0 ** (+anything except 0, NAN, odd
410   * integer) is +0 12. +0 ** (-anything except 0, NAN) is +INF 13. -0 **
411   * (-anything except 0, NAN, odd integer) is +INF 14. -0 ** (odd integer) =
412   * -( +0 ** (odd integer) ) 15. +INF ** (+anything except 0,NAN) is +INF 16.
413   * +INF ** (-anything except 0,NAN) is +0 17. -INF ** (anything) = -0 **
414   * (-anything) 18. (-anything) ** (integer) is
415   * (-1)**(integer)*(+anything**integer) 19. (-anything except 0 and inf) **
416   * (non-integer) is NAN
417   * 
418   * Accuracy: pow(x,y) returns x**y nearly rounded. In particular
419   * pow(integer,integer) always returns the correct integer provided it is
420   * representable.
421   * 
422   * Constants : The hexadecimal values are the intended ones for the
423   * following constants. The decimal values may be used, provided that the
424   * compiler will convert from decimal to binary accurately enough to produce
425   * the hexadecimal values shown.
426   */
427
428  private static final double bp[] = { 1.0, 1.5, }, dp_h[] = { 0.0, 5.84962487220764160156e-01, }, /* 0x3FE2B803, 0x40000000 */
429  dp_l[] = { 0.0, 1.35003920212974897128e-08, }, /* 0x3E4CFDEB, 0x43CFD006 */
430  /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
431  L1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */
432  L2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */
433  L3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */
434  L4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */
435  L5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */
436  L6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */
437  lg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
438  lg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */
439  lg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */
440  ovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */
441  cp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */
442  cp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */
443  cp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h */
444  ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */
445  ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2 */
446  ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail */
447
448  private static final double ieee754_pow(double x, double y) {
449    double z, ax, z_h, z_l, p_h, p_l;
450    double y1, t1, t2, r, s, t, u, v, w;
451    // int i0,i1;
452    int i, j, k, yisint, n;
453    int hx, hy, ix, iy;
454    int lx, ly;
455
456    // i0 = (int)((Double.doubleToLongBits(one)) >>> (29+HI_SHIFT))^1;
457    // i1 = 1-i0;
458    hx = (int) (Double.doubleToLongBits(x) >>> HI_SHIFT);
459    lx = (int) (Double.doubleToLongBits(x) & LO_MASK);
460    hy = (int) (Double.doubleToLongBits(y) >>> HI_SHIFT);
461    ly = (int) (Double.doubleToLongBits(y) & LO_MASK);
462    ix = hx & 0x7fffffff;
463    iy = hy & 0x7fffffff;
464
465    /* y==zero: x**0 = 1 */
466    if ((iy | ly) == 0)
467      return one;
468
469    /* +-NaN return x+y */
470    if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || iy > 0x7ff00000 || ((iy == 0x7ff00000) && (ly != 0)))
471      return x + y;
472
473    /*
474     * determine if y is an odd int when x < 0 yisint = 0 ... y is not an
475     * integer yisint = 1 ... y is an odd int yisint = 2 ... y is an even
476     * int
477     */
478    yisint = 0;
479    if (hx < 0) {
480      if (iy >= 0x43400000)
481        yisint = 2; /* even integer y */
482      else if (iy >= 0x3ff00000) {
483        k = (iy >> 20) - 0x3ff; /* exponent */
484        if (k > 20) {
485          j = ly >> (52 - k);
486          if ((j << (52 - k)) == ly)
487            yisint = 2 - (j & 1);
488        } else if (ly == 0) {
489          j = iy >> (20 - k);
490          if ((j << (20 - k)) == iy)
491            yisint = 2 - (j & 1);
492        }
493      }
494    }
495
496    /* special value of y */
497    if (ly == 0) {
498      if (iy == 0x7ff00000) { /* y is +-inf */
499        if (((ix - 0x3ff00000) | lx) == 0)
500          return y - y; /* inf**+-1 is NaN */
501        else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */
502          return (hy >= 0) ? y : zero;
503        else
504          /* (|x|<1)**-,+inf = inf,0 */
505          return (hy < 0) ? -y : zero;
506      }
507      if (iy == 0x3ff00000) { /* y is +-1 */
508        if (hy < 0)
509          return one / x;
510        else
511          return x;
512      }
513      if (hy == 0x40000000)
514        return x * x; /* y is 2 */
515      if (hy == 0x3fe00000) { /* y is 0.5 */
516        if (hx >= 0) /* x >= +0 */
517          return Math.sqrt(x);
518      }
519    }
520
521    ax = Math.abs(x);
522    /* special value of x */
523    if (lx == 0) {
524      if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000) {
525        z = ax; /* x is +-0,+-inf,+-1 */
526        if (hy < 0)
527          z = one / z; /* z = (1/|x|) */
528        if (hx < 0) {
529          if (((ix - 0x3ff00000) | yisint) == 0) {
530            z = (z - z) / (z - z); /* (-1)**non-int is NaN */
531          } else if (yisint == 1)
532            z = -z; /* (x<0)**odd = -(|x|**odd) */
533        }
534        return z;
535      }
536    }
537
538    n = (hx >>> 31) + 1;
539
540    /* (x<0)**(non-int) is NaN */
541    if ((n | yisint) == 0)
542      return (x - x) / (x - x);
543
544    s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
545    if ((n | (yisint - 1)) == 0)
546      s = -one;/* (-ve)**(odd int) */
547
548    /* |y| is huge */
549    if (iy > 0x41e00000) { /* if |y| > 2**31 */
550      if (iy > 0x43f00000) { /* if |y| > 2**64, must o/uflow */
551        if (ix <= 0x3fefffff)
552          return (hy < 0) ? huge * huge : tiny * tiny;
553        if (ix >= 0x3ff00000)
554          return (hy > 0) ? huge * huge : tiny * tiny;
555      }
556      /* over/underflow if x is not close to one */
557      if (ix < 0x3fefffff)
558        return (hy < 0) ? s * huge * huge : s * tiny * tiny;
559      if (ix > 0x3ff00000)
560        return (hy > 0) ? s * huge * huge : s * tiny * tiny;
561      /*
562       * now |1-x| is tiny <= 2**-20, suffice to compute log(x) by
563       * x-x^2/2+x^3/3-x^4/4
564       */
565      t = x - one; /* t has 20 trailing zeros */
566      w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
567      u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
568      v = t * ivln2_l - w * ivln2;
569      t1 = u + v;
570      // __LO(t1) = 0; // keep high word
571      t1 = Double.longBitsToDouble(Double.doubleToLongBits(t1) & HI_MASK);
572      t2 = v - (t1 - u);
573    } else {
574      double ss, s2, s_h, s_l, t_h, t_l;
575      n = 0;
576      /* take care subnormal number */
577      if (ix < 0x00100000) {
578        ax *= two53;
579        n -= 53;
580        ix = (int) (Double.doubleToLongBits(ax) >>> HI_SHIFT);
581      }
582      n += ((ix) >> 20) - 0x3ff;
583      j = ix & 0x000fffff;
584      /* determine interval */
585      ix = j | 0x3ff00000; /* normalize ix */
586      if (j <= 0x3988E)
587        k = 0; /* |x|<sqrt(3/2) */
588      else if (j < 0xBB67A)
589        k = 1; /* |x|<sqrt(3) */
590      else {
591        k = 0;
592        n += 1;
593        ix -= 0x00100000;
594      }
595      // __HI(ax) = ix;
596      ax = Double.longBitsToDouble(((long) ix << HI_SHIFT) | (Double.doubleToLongBits(ax) & LO_MASK));
597
598      /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
599      u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
600      v = one / (ax + bp[k]);
601      ss = u * v;
602      s_h = ss;
603      // __LO(s_h) = 0; // keep high word
604      s_h = Double.longBitsToDouble(Double.doubleToLongBits(s_h) & HI_MASK);
605      /* t_h=ax+bp[k] High */
606      t_h = zero;
607      // __HI(t_h)=((ix>>1)|0x20000000)+0x00080000+(k<<18);
608      t_h = Double.longBitsToDouble(((long) ((int) ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18)) << HI_SHIFT)
609          | (Double.doubleToLongBits(t_h) & LO_MASK));
610      t_l = ax - (t_h - bp[k]);
611      s_l = v * ((u - s_h * t_h) - s_h * t_l);
612      /* compute log(ax) */
613      s2 = ss * ss;
614      r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
615      r += s_l * (s_h + ss);
616      s2 = s_h * s_h;
617      t_h = 3.0 + s2 + r;
618      // __LO(t_h) = 0; // keep high word
619      t_h = Double.longBitsToDouble(Double.doubleToLongBits(t_h) & HI_MASK);
620      t_l = r - ((t_h - 3.0) - s2);
621      /* u+v = ss*(1+...) */
622      u = s_h * t_h;
623      v = s_l * t_h + t_l * ss;
624      /* 2/(3log2)*(ss+...) */
625      p_h = u + v;
626      // __LO(p_h) = 0; // keep high word
627      p_h = Double.longBitsToDouble(Double.doubleToLongBits(p_h) & HI_MASK);
628      p_l = v - (p_h - u);
629      z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
630      z_l = cp_l * p_h + p_l * cp + dp_l[k];
631      /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
632      t = (double) n;
633      t1 = (((z_h + z_l) + dp_h[k]) + t);
634      // __LO(t1) = 0; // keep high word
635      t1 = Double.longBitsToDouble(Double.doubleToLongBits(t1) & HI_MASK);
636      t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
637    }
638
639    /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
640    y1 = y;
641    // __LO(y1) = 0; // keep high word
642    y1 = Double.longBitsToDouble(Double.doubleToLongBits(y1) & HI_MASK);
643    p_l = (y - y1) * t1 + y * t2;
644    p_h = y1 * t1;
645    z = p_l + p_h;
646    j = (int) (Double.doubleToLongBits(z) >>> HI_SHIFT);
647    i = (int) (Double.doubleToLongBits(z) & LO_MASK);
648    if (j >= 0x40900000) { /* z >= 1024 */
649      if (((j - 0x40900000) | i) != 0) /* if z > 1024 */
650        return s * huge * huge; /* overflow */
651      else {
652        if (p_l + ovt > z - p_h)
653          return s * huge * huge; /* overflow */
654      }
655    } else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */
656      if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */
657        return s * tiny * tiny; /* underflow */
658      else {
659        if (p_l <= z - p_h)
660          return s * tiny * tiny; /* underflow */
661      }
662    }
663    /*
664     * compute 2**(p_h+p_l)
665     */
666    i = j & 0x7fffffff;
667    k = (i >> 20) - 0x3ff;
668    n = 0;
669    if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
670      n = j + (0x00100000 >> (k + 1));
671      k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
672      t = zero;
673      // __HI(t) = (n&~(0x000fffff>>k));
674      t = Double.longBitsToDouble(((long) (n & ~(0x000fffff >> k)) << HI_SHIFT) | (Double.doubleToLongBits(t) & LO_MASK));
675      n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
676      if (j < 0)
677        n = -n;
678      p_h -= t;
679    }
680    t = p_l + p_h;
681    // __LO(t) = 0; // keep high word
682    t = Double.longBitsToDouble(Double.doubleToLongBits(t) & HI_MASK);
683    u = t * lg2_h;
684    v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
685    z = u + v;
686    w = v - (z - u);
687    t = z * z;
688    t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
689    r = (z * t1) / (t1 - two) - (w + z * w);
690    z = one - (r - z);
691    j = (int) ((long) Double.doubleToLongBits(z) >>> HI_SHIFT);
692    j += (n << 20);
693    if ((j >> 20) <= 0)
694      z = scalbn(z, n); /* subnormal output */
695    else
696      // __HI(z) = j;
697      z = Double.longBitsToDouble(((long) j << HI_SHIFT) | (Double.doubleToLongBits(z) & LO_MASK));
698    return s * z;
699  }
700
701  /*
702   * __ieee754_acos(x) Method : acos(x) = pi/2 - asin(x) acos(-x) = pi/2 +
703   * asin(x) For |x|<=0.5 acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c) For
704   * x>0.5 acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2))) =
705   * 2asin(sqrt((1-x)/2)) = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z) = 2f + (2c
706   * + 2s*z*R(z)) where f=hi part of s, and c = (z-f*f)/(s+f) is the
707   * correction term for f so that f+c ~ sqrt(z). For x<-0.5 acos(x) = pi -
708   * 2asin(sqrt((1-|x|)/2)) = pi - 0.5*(s+s*z*R(z)), where
709   * z=(1-|x|)/2,s=sqrt(z)
710   * 
711   * Special cases: if x is NaN, return x itself; if |x|>1, return NaN with
712   * invalid signal.
713   * 
714   * Function needed: sqrt
715   */
716
717  // private static final double ieee754_acos(double x) {
718  // double z, p, q, r, w, s, c, df;
719  // int hx, ix;
720  // hx = (int) (Double.doubleToLongBits(x) >>> HI_SHIFT);
721  // ix = hx & 0x7fffffff;
722  // if (ix >= 0x3ff00000) { /* |x| >= 1 */
723  // if (((ix - 0x3ff00000) | (int) (Double.doubleToLongBits(x) & LO_MASK)) == 0) { /*
724  // * |
725  // * x
726  // * |=
727  // * =
728  // * 1
729  // */
730  // if (hx > 0)
731  // return 0.0; /* acos(1) = 0 */
732  // else
733  // return pi + 2.0 * pio2_lo; /* acos(-1)= pi */
734  // }
735  // return (x - x) / (x - x); /* acos(|x|>1) is NaN */
736  // }
737  // if (ix < 0x3fe00000) { /* |x| < 0.5 */
738  // if (ix <= 0x3c600000)
739  // return pio2_hi + pio2_lo;/* if|x|<2**-57 */
740  // z = x * x;
741  // p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
742  // q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
743  // r = p / q;
744  // return pio2_hi - (x - (pio2_lo - x * r));
745  // } else if (hx < 0) { /* x < -0.5 */
746  // z = (one + x) * 0.5;
747  // p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
748  // q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
749  // s = Math.sqrt(z);
750  // r = p / q;
751  // w = r * s - pio2_lo;
752  // return pi - 2.0 * (s + w);
753  // } else { /* x > 0.5 */
754  // z = (one - x) * 0.5;
755  // s = Math.sqrt(z);
756  // df = s;
757  // // __LO(df) = 0; // keep high word
758  // df = Double.longBitsToDouble(Double.doubleToLongBits(df) & HI_MASK);
759  // c = (z - df * df) / (s + df);
760  // p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
761  // q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
762  // r = p / q;
763  // w = r * s + c;
764  // return 2.0 * (df + w);
765  // }
766  // }
767
768  /*
769   * __ieee754_asin(x) Method : Since asin(x) = x + x^3/6 + x^5*3/40 +
770   * x^7*15/336 + ... we approximate asin(x) on [0,0.5] by asin(x) = x +
771   * x*x^2*R(x^2) where R(x^2) is a rational approximation of (asin(x)-x)/x^3
772   * and its remez error is bounded by |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
773   * 
774   * For x in [0.5,1] asin(x) = pi/2-2*asin(sqrt((1-x)/2)) Let y = (1-x), z =
775   * y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2; then for x>0.98 asin(x) =
776   * pi/2 - 2*(s+s*z*R(z)) = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo) For x<=0.98,
777   * let pio4_hi = pio2_hi/2, then f = hi part of s; c = sqrt(z) - f =
778   * (z-f*f)/(s+f) ...f+c=sqrt(z) and asin(x) = pi/2 - 2*(s+s*z*R(z)) =
779   * pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo) =
780   * pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
781   * 
782   * Special cases: if x is NaN, return x itself; if |x|>1, return NaN with
783   * invalid signal.
784   */
785
786  // private static final double ieee754_asin(double x) {
787  // double t, w, p, q, c, r, s;
788  // int hx, ix;
789  // hx = (int) (Double.doubleToLongBits(x) >>> HI_SHIFT);
790  // ix = hx & 0x7fffffff;
791  // if (ix >= 0x3ff00000) { /* |x|>= 1 */
792  // if (((ix - 0x3ff00000) | (int) (Double.doubleToLongBits(x) & LO_MASK)) == 0)
793  // /* asin(1)=+-pi/2 with inexact */
794  // return x * pio2_hi + x * pio2_lo;
795  // return (x - x) / (x - x); /* asin(|x|>1) is NaN */
796  // } else if (ix < 0x3fe00000) { /* |x|<0.5 */
797  // if (ix < 0x3e400000) { /* if |x| < 2**-27 */
798  // if (huge + x > one)
799  // return x;/* return x with inexact if x!=0 */
800  // } else {
801  // t = x * x;
802  // p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
803  // q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
804  // w = p / q;
805  // return x + x * w;
806  // }
807  // }
808  // /* 1> |x|>= 0.5 */
809  // w = one - Math.abs(x);
810  // t = w * 0.5;
811  // p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
812  // q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
813  // s = Math.sqrt(t);
814  // if (ix >= 0x3FEF3333) { /* if |x| > 0.975 */
815  // w = p / q;
816  // t = pio2_hi - (2.0 * (s + s * w) - pio2_lo);
817  // } else {
818  // w = s;
819  // // __LO(w) = 0; // keep the high word
820  // w = Double.longBitsToDouble(Double.doubleToLongBits(w) & HI_MASK);
821  // c = (t - w * w) / (s + w);
822  // r = p / q;
823  // p = 2.0 * s * r - (pio2_lo - 2.0 * c);
824  // q = pio4_hi - 2.0 * w;
825  // t = pio4_hi - (p - q);
826  // }
827  // if (hx > 0)
828  // return t;
829  // else
830  // return -t;
831  // }
832
833  /*
834   * atan(x) Method 1. Reduce x to positive by atan(x) = -atan(-x). 2.
835   * According to the integer k=4t+0.25 chopped, t=x, the argument is further
836   * reduced to one of the following intervals and the arctangent of t is
837   * evaluated by the corresponding formula:
838   * 
839   * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) [7/16,11/16]
840   * atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) [11/16.19/16] atan(x) =
841   * atan( 1 ) + atan( (t-1)/(1+t) ) [19/16,39/16] atan(x) = atan(3/2) + atan(
842   * (t-1.5)/(1+1.5t) ) [39/16,INF] atan(x) = atan(INF) + atan( -1/t )
843   * 
844   * Constants: The hexadecimal values are the intended ones for the following
845   * constants. The decimal values may be used, provided that the compiler
846   * will convert from decimal to binary accurately enough to produce the
847   * hexadecimal values shown.
848   */
849
850  private static final double atanhi[] = { 4.63647609000806093515e-01, /*
851                                                                       * atan(0.5
852                                                                       * )hi
853                                                                       * 0x3FDDAC67
854                                                                       * ,
855                                                                       * 0x0561BB4F
856                                                                       */
857  7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */
858  9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */
859  1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */
860  };
861
862  private static final double atanlo[] = { 2.26987774529616870924e-17, /*
863                                                                       * atan(0.5
864                                                                       * )lo
865                                                                       * 0x3C7A2B7F
866                                                                       * ,
867                                                                       * 0x222F65E2
868                                                                       */
869  3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */
870  1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */
871  6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */
872  };
873
874  private static final double aT[] = { 3.33333333333329318027e-01, /*
875                                                                   * 0x3FD55555,
876                                                                   * 0x5555550D
877                                                                   */
878  -1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */
879  1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */
880  -1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */
881  9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */
882  -7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */
883  6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */
884  -5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */
885  4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */
886  -3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */
887  1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */
888  };
889
890  private static final double ieee754_atan(double x) {
891    double w, s1, s2, z;
892    int ix, hx, id;
893
894    hx = (int) (Double.doubleToLongBits(x) >>> HI_SHIFT);
895    ix = hx & 0x7fffffff;
896    if (ix >= 0x44100000) { /* if |x| >= 2^66 */
897      if (ix > 0x7ff00000 || (ix == 0x7ff00000 && ((int) (Double.doubleToLongBits(x) & LO_MASK) != 0)))
898        return x + x; /* NaN */
899      if (hx > 0)
900        return atanhi[3] + atanlo[3];
901      else
902        return -atanhi[3] - atanlo[3];
903    }
904    if (ix < 0x3fdc0000) { /* |x| < 0.4375 */
905      if (ix < 0x3e200000) { /* |x| < 2^-29 */
906        if (huge + x > one)
907          return x; /* raise inexact */
908      }
909      id = -1;
910    } else {
911      x = Math.abs(x);
912      if (ix < 0x3ff30000) { /* |x| < 1.1875 */
913        if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */
914          id = 0;
915          x = (2.0 * x - one) / (2.0 + x);
916        } else { /* 11/16<=|x|< 19/16 */
917          id = 1;
918          x = (x - one) / (x + one);
919        }
920      } else {
921        if (ix < 0x40038000) { /* |x| < 2.4375 */
922          id = 2;
923          x = (x - 1.5) / (one + 1.5 * x);
924        } else { /* 2.4375 <= |x| < 2^66 */
925          id = 3;
926          x = -1.0 / x;
927        }
928      }
929    }
930    /* end of argument reduction */
931    z = x * x;
932    w = z * z;
933    /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
934    s1 = z * (aT[0] + w * (aT[2] + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10])))));
935    s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9]))));
936    if (id < 0)
937      return x - x * (s1 + s2);
938    else {
939      z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
940      return (hx < 0) ? -z : z;
941    }
942  }
943
944  /*
945   * __ieee754_atan2(y,x) Method : 1. Reduce y to positive by
946   * atan2(y,x)=-atan2(-y,x). 2. Reduce x to positive by (if x and y are
947   * unexceptional): ARG (x+iy) = arctan(y/x) ... if x > 0, ARG (x+iy) = pi -
948   * arctan[y/(-x)] ... if x < 0,
949   * 
950   * Special cases:
951   * 
952   * ATAN2((anything), NaN ) is NaN; ATAN2(NAN , (anything) ) is NaN;
953   * ATAN2(+-0, +(anything but NaN)) is +-0 ; ATAN2(+-0, -(anything but NaN))
954   * is +-pi ; ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
955   * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ; ATAN2(+-(anything but
956   * INF and NaN), -INF) is +-pi; ATAN2(+-INF,+INF ) is +-pi/4 ;
957   * ATAN2(+-INF,-INF ) is +-3pi/4; ATAN2(+-INF, (anything but,0,NaN, and
958   * INF)) is +-pi/2;
959   * 
960   * Constants: The hexadecimal values are the intended ones for the following
961   * constants. The decimal values may be used, provided that the compiler
962   * will convert from decimal to binary accurately enough to produce the
963   * hexadecimal values shown.
964   */
965
966  private static final double ieee754_atan2(double x, double y) {
967    double z;
968    int k, m;
969    int hx, hy, ix, iy;
970    int lx, ly;
971
972    // i0 = (int)((Double.doubleToLongBits(one)) >> (29+HI_SHIFT))^1;
973    // i1 = 1-i0;
974    hx = (int) (Double.doubleToLongBits(x) >>> HI_SHIFT);
975    lx = (int) (Double.doubleToLongBits(x) & LO_MASK);
976    hy = (int) (Double.doubleToLongBits(y) >>> HI_SHIFT);
977    ly = (int) (Double.doubleToLongBits(y) & LO_MASK);
978    ix = hx & 0x7fffffff;
979    iy = hy & 0x7fffffff;
980
981    if (((ix | ((lx | -lx) >> 31)) > 0x7ff00000) || ((iy | ((ly | -ly) >> 31)) > 0x7ff00000)) /* x or y is NaN */
982      return x + y;
983    if ((hx - 0x3ff00000 | lx) == 0)
984      return ieee754_atan(y); /* x=1.0 */
985    m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */
986
987    /* when y = 0 */
988    if ((iy | ly) == 0) {
989      switch (m) {
990        case 0:
991        case 1:
992          return y; /* atan(+-0,+anything)=+-0 */
993        case 2:
994          return pi + tiny;/* atan(+0,-anything) = pi */
995        case 3:
996          return -pi - tiny;/* atan(-0,-anything) =-pi */
997      }
998    }
999    /* when x = 0 */
1000    if ((ix | lx) == 0)
1001      return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
1002
1003    /* when x is INF */
1004    if (ix == 0x7ff00000) {
1005      if (iy == 0x7ff00000) {
1006        switch (m) {
1007          case 0:
1008            return pi_o_4 + tiny;/* atan(+INF,+INF) */
1009          case 1:
1010            return -pi_o_4 - tiny;/* atan(-INF,+INF) */
1011          case 2:
1012            return 3.0 * pi_o_4 + tiny;/* atan(+INF,-INF) */
1013          case 3:
1014            return -3.0 * pi_o_4 - tiny;/* atan(-INF,-INF) */
1015        }
1016      } else {
1017        switch (m) {
1018          case 0:
1019            return zero; /* atan(+...,+INF) */
1020          case 1:
1021            return -zero; /* atan(-...,+INF) */
1022          case 2:
1023            return pi + tiny; /* atan(+...,-INF) */
1024          case 3:
1025            return -pi - tiny; /* atan(-...,-INF) */
1026        }
1027      }
1028    }
1029    /* when y is INF */
1030    if (iy == 0x7ff00000)
1031      return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
1032
1033    /* compute y/x */
1034    k = (iy - ix) >> 20;
1035    if (k > 60)
1036      z = pi_o_2 + 0.5 * pi_lo; /* |y/x| > 2**60 */
1037    else if (hx < 0 && k < -60)
1038      z = 0.0; /* |y|/x < -2**60 */
1039    else
1040      z = ieee754_atan(Math.abs(y / x)); /* safe to do y/x */
1041    switch (m) {
1042      case 0:
1043        return z; /* atan(+,+) */
1044      case 1:
1045        z = Double.longBitsToDouble(Double.doubleToLongBits(z) ^ 0x80000000); // __HI(z)
1046        // ^=
1047        // 0x80000000;
1048        return z; /* atan(-,+) */
1049      case 2:
1050        return pi - (z - pi_lo);/* atan(+,-) */
1051      default: /* case 3 */
1052        return (z - pi_lo) - pi;/* atan(-,-) */
1053    }
1054  }
1055
1056  /**
1057   * scalbn (double x, int n) scalbn(x,n) returns x* 2**n computed by exponent manipulation rather than by actually performing an exponentiation or a
1058   * multiplication.
1059   */
1060  public static final double scalbn(double x, int n) {
1061    int k, hx, lx;
1062    hx = (int) (Double.doubleToLongBits(x) >>> HI_SHIFT);
1063    lx = (int) (Double.doubleToLongBits(x) & LO_MASK);
1064    k = (hx & 0x7ff00000) >> 20; /* extract exponent */
1065    if (k == 0) { /* 0 or subnormal x */
1066      if ((lx | (hx & 0x7fffffff)) == 0)
1067        return x; /* +-0 */
1068      x *= two54;
1069      hx = (int) (Double.doubleToLongBits(x) >>> HI_SHIFT);
1070      k = ((hx & 0x7ff00000) >> 20) - 54;
1071      if (n < -50000)
1072        return tiny * x; /* underflow */
1073    }
1074    if (k == 0x7ff)
1075      return x + x; /* NaN or Inf */
1076    k = k + n;
1077    if (k > 0x7fe)
1078      return huge * copysign(huge, x); /* overflow */
1079    if (k > 0) /* normal result */
1080    {
1081      // __HI(x) = (hx&0x800fffff)|(k<<20);
1082      x = Double.longBitsToDouble(((long) ((int) (hx & 0x800fffff) | (k << 20)) << HI_SHIFT) | (Double.doubleToLongBits(x) & LO_MASK));
1083      return x;
1084    }
1085    if (k <= -54)
1086      if (n > 50000) /* in case integer overflow in n+k */
1087        return huge * copysign(huge, x); /* overflow */
1088      else
1089        return tiny * copysign(tiny, x); /* underflow */
1090    k += 54; /* subnormal result */
1091    // __HI(x) = (hx&0x800fffff)|(k<<20);
1092    x = Double.longBitsToDouble(((long) ((int) (hx & 0x800fffff) | (k << 20)) << HI_SHIFT) | (Double.doubleToLongBits(x) & LO_MASK));
1093    return x * twom54;
1094  }
1095
1096  /*
1097   * copysign(double x, double y) copysign(x,y) returns a value with the
1098   * magnitude of x and with the sign bit of y.
1099   */
1100  public static final double copysign(final double x, final double y) {
1101    // __HI(x) = (__HI(x)&0x7fffffff)|(__HI(y)&0x80000000);
1102    // The below is actually about 30% faster than doing greater/less
1103    // comparisons.
1104    return Double.longBitsToDouble((Double.doubleToLongBits(x) & 0x7fffffffffffffffL) | (Double.doubleToLongBits(y) & 0x8000000000000000L));
1105  }
1106
1107  /*
1108   * fabs(x) returns the absolute value of x. This is already handled by Java
1109   * ME. public static final double fabs(double x) { //__HI(x) &= 0x7fffffff;
1110   * //return Double.longBitsToDouble(Double.doubleToLongBits(x) &
1111   * 0x7fffffffffffffffL); }
1112   */
1113
1114  /**
1115   * Returns the negation of the argument, throwing an exception if the result exceeds a {@code double}.
1116   *
1117   * @param a
1118   *          the value to negate
1119   * @return the result
1120   * @throws ArithmeticException
1121   *           if the result overflows a double
1122   */
1123  public static double negateExact(double a) {
1124    if (a == Double.MAX_VALUE || a == Double.MIN_VALUE) {
1125      throw new ArithmeticException("double overflow");
1126    }
1127
1128    return -a;
1129  }
1130
1131  public static double gcd(double a, double b) {
1132    if (b == 0)
1133      return a;
1134    return gcd(b, a % b);
1135  }
1136  /*
1137    private static final double powSqrt(double x, double y) {
1138      int den = 1024, num = (int) (y * den), iterations = 10;
1139      double n = Double.MAX_VALUE;
1140
1141      while (n >= Double.MAX_VALUE && iterations > 1) {
1142        n = x;
1143
1144        for (int i = 1; i < num; i++)
1145          n *= x;
1146
1147        if (n >= Double.MAX_VALUE) {
1148          iterations--;
1149          den = (int) (den / 2);
1150          num = (int) (y * den);
1151        }
1152      }
1153
1154      for (int i = 0; i < iterations; i++)
1155        n = Math.sqrt(n);
1156
1157      return n;
1158    }
1159  */
1160
1161  /*
1162   * 
1163   * private static final double powDecay(double x, double y) { int num, den =
1164   * 1001, s = 0; double n = x, z = Double.MAX_VALUE;
1165   * 
1166   * for( int i = 1; i < s; i++)n *= x;
1167   * 
1168   * while( z >= Double.MAX_VALUE ) { den -=1; num = (int)(y*den); s =
1169   * (num/den)+1;
1170   * 
1171   * z = x; for( int i = 1; i < num; i++ )z *= x; }
1172   * 
1173   * while( n > 0 ) { double a = n;
1174   * 
1175   * for( int i = 1; i < den; i++ )a *= n;
1176   * 
1177   * if( (a-z) < .00001 || (z-a) > .00001 ) return n;
1178   * 
1179   * n *= .9999; }
1180   * 
1181   * return -1.0; }
1182   * 
1183   * private static final double powTaylor(double a, double b) { boolean gt1 =
1184   * (Math.sqrt((a-1)*(a-1)) <= 1)? false:true; int oc = -1,iter = 30; double
1185   * p = a, x, x2, sumX, sumY;
1186   * 
1187   * if( (b-Math.floor(b)) == 0 ) { for( int i = 1; i < b; i++ )p *= a; return
1188   * p; }
1189   * 
1190   * x = (gt1)?(a /(a-1)):(a-1); sumX = (gt1)?(1/x):x;
1191   * 
1192   * for( int i = 2; i < iter; i++ ) { p = x; for( int j = 1; j < i; j++)p *=
1193   * x;
1194   * 
1195   * double xTemp = (gt1)?(1/(i*p)):(p/i);
1196   * 
1197   * sumX = (gt1)?(sumX+xTemp):(sumX+(xTemp*oc));
1198   * 
1199   * oc *= -1; }
1200   * 
1201   * x2 = b * sumX; sumY = 1+x2;
1202   * 
1203   * for( int i = 2; i <= iter; i++ ) { p = x2; for( int j = 1; j < i; j++)p
1204   * *= x2;
1205   * 
1206   * int yTemp = 2; for( int j = i; j > 2; j-- )yTemp *= j;
1207   * 
1208   * sumY += p/yTemp; }
1209   * 
1210   * return sumY; }
1211   */
1212}