Source for java.lang.StrictMath

   1: /* java.lang.StrictMath -- common mathematical functions, strict Java
   2:    Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: /*
  39:  * Some of the algorithms in this class are in the public domain, as part
  40:  * of fdlibm (freely-distributable math library), available at
  41:  * http://www.netlib.org/fdlibm/, and carry the following copyright:
  42:  * ====================================================
  43:  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  44:  *
  45:  * Developed at SunSoft, a Sun Microsystems, Inc. business.
  46:  * Permission to use, copy, modify, and distribute this
  47:  * software is freely granted, provided that this notice
  48:  * is preserved.
  49:  * ====================================================
  50:  */
  51: 
  52: package java.lang;
  53: 
  54: import gnu.classpath.Configuration;
  55: 
  56: import java.util.Random;
  57: 
  58: /**
  59:  * Helper class containing useful mathematical functions and constants.
  60:  * This class mirrors {@link Math}, but is 100% portable, because it uses
  61:  * no native methods whatsoever.  Also, these algorithms are all accurate
  62:  * to less than 1 ulp, and execute in <code>strictfp</code> mode, while
  63:  * Math is allowed to vary in its results for some functions. Unfortunately,
  64:  * this usually means StrictMath has less efficiency and speed, as Math can
  65:  * use native methods.
  66:  *
  67:  * <p>The source of the various algorithms used is the fdlibm library, at:<br>
  68:  * <a href="http://www.netlib.org/fdlibm/">http://www.netlib.org/fdlibm/</a>
  69:  *
  70:  * Note that angles are specified in radians.  Conversion functions are
  71:  * provided for your convenience.
  72:  *
  73:  * @author Eric Blake (ebb9@email.byu.edu)
  74:  * @since 1.3
  75:  */
  76: public final strictfp class StrictMath
  77: {
  78:   /**
  79:    * StrictMath is non-instantiable.
  80:    */
  81:   private StrictMath()
  82:   {
  83:   }
  84: 
  85:   /**
  86:    * A random number generator, initialized on first use.
  87:    *
  88:    * @see #random()
  89:    */
  90:   private static Random rand;
  91: 
  92:   /**
  93:    * The most accurate approximation to the mathematical constant <em>e</em>:
  94:    * <code>2.718281828459045</code>. Used in natural log and exp.
  95:    *
  96:    * @see #log(double)
  97:    * @see #exp(double)
  98:    */
  99:   public static final double E
 100:     = 2.718281828459045; // Long bits 0x4005bf0z8b145769L.
 101: 
 102:   /**
 103:    * The most accurate approximation to the mathematical constant <em>pi</em>:
 104:    * <code>3.141592653589793</code>. This is the ratio of a circle's diameter
 105:    * to its circumference.
 106:    */
 107:   public static final double PI
 108:     = 3.141592653589793; // Long bits 0x400921fb54442d18L.
 109: 
 110:   /**
 111:    * Take the absolute value of the argument. (Absolute value means make
 112:    * it positive.)
 113:    *
 114:    * <p>Note that the the largest negative value (Integer.MIN_VALUE) cannot
 115:    * be made positive.  In this case, because of the rules of negation in
 116:    * a computer, MIN_VALUE is what will be returned.
 117:    * This is a <em>negative</em> value.  You have been warned.
 118:    *
 119:    * @param i the number to take the absolute value of
 120:    * @return the absolute value
 121:    * @see Integer#MIN_VALUE
 122:    */
 123:   public static int abs(int i)
 124:   {
 125:     return (i < 0) ? -i : i;
 126:   }
 127: 
 128:   /**
 129:    * Take the absolute value of the argument. (Absolute value means make
 130:    * it positive.)
 131:    *
 132:    * <p>Note that the the largest negative value (Long.MIN_VALUE) cannot
 133:    * be made positive.  In this case, because of the rules of negation in
 134:    * a computer, MIN_VALUE is what will be returned.
 135:    * This is a <em>negative</em> value.  You have been warned.
 136:    *
 137:    * @param l the number to take the absolute value of
 138:    * @return the absolute value
 139:    * @see Long#MIN_VALUE
 140:    */
 141:   public static long abs(long l)
 142:   {
 143:     return (l < 0) ? -l : l;
 144:   }
 145: 
 146:   /**
 147:    * Take the absolute value of the argument. (Absolute value means make
 148:    * it positive.)
 149:    *
 150:    * @param f the number to take the absolute value of
 151:    * @return the absolute value
 152:    */
 153:   public static float abs(float f)
 154:   {
 155:     return (f <= 0) ? 0 - f : f;
 156:   }
 157: 
 158:   /**
 159:    * Take the absolute value of the argument. (Absolute value means make
 160:    * it positive.)
 161:    *
 162:    * @param d the number to take the absolute value of
 163:    * @return the absolute value
 164:    */
 165:   public static double abs(double d)
 166:   {
 167:     return (d <= 0) ? 0 - d : d;
 168:   }
 169: 
 170:   /**
 171:    * Return whichever argument is smaller.
 172:    *
 173:    * @param a the first number
 174:    * @param b a second number
 175:    * @return the smaller of the two numbers
 176:    */
 177:   public static int min(int a, int b)
 178:   {
 179:     return (a < b) ? a : b;
 180:   }
 181: 
 182:   /**
 183:    * Return whichever argument is smaller.
 184:    *
 185:    * @param a the first number
 186:    * @param b a second number
 187:    * @return the smaller of the two numbers
 188:    */
 189:   public static long min(long a, long b)
 190:   {
 191:     return (a < b) ? a : b;
 192:   }
 193: 
 194:   /**
 195:    * Return whichever argument is smaller. If either argument is NaN, the
 196:    * result is NaN, and when comparing 0 and -0, -0 is always smaller.
 197:    *
 198:    * @param a the first number
 199:    * @param b a second number
 200:    * @return the smaller of the two numbers
 201:    */
 202:   public static float min(float a, float b)
 203:   {
 204:     // this check for NaN, from JLS 15.21.1, saves a method call
 205:     if (a != a)
 206:       return a;
 207:     // no need to check if b is NaN; < will work correctly
 208:     // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
 209:     if (a == 0 && b == 0)
 210:       return -(-a - b);
 211:     return (a < b) ? a : b;
 212:   }
 213: 
 214:   /**
 215:    * Return whichever argument is smaller. If either argument is NaN, the
 216:    * result is NaN, and when comparing 0 and -0, -0 is always smaller.
 217:    *
 218:    * @param a the first number
 219:    * @param b a second number
 220:    * @return the smaller of the two numbers
 221:    */
 222:   public static double min(double a, double b)
 223:   {
 224:     // this check for NaN, from JLS 15.21.1, saves a method call
 225:     if (a != a)
 226:       return a;
 227:     // no need to check if b is NaN; < will work correctly
 228:     // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
 229:     if (a == 0 && b == 0)
 230:       return -(-a - b);
 231:     return (a < b) ? a : b;
 232:   }
 233: 
 234:   /**
 235:    * Return whichever argument is larger.
 236:    *
 237:    * @param a the first number
 238:    * @param b a second number
 239:    * @return the larger of the two numbers
 240:    */
 241:   public static int max(int a, int b)
 242:   {
 243:     return (a > b) ? a : b;
 244:   }
 245: 
 246:   /**
 247:    * Return whichever argument is larger.
 248:    *
 249:    * @param a the first number
 250:    * @param b a second number
 251:    * @return the larger of the two numbers
 252:    */
 253:   public static long max(long a, long b)
 254:   {
 255:     return (a > b) ? a : b;
 256:   }
 257: 
 258:   /**
 259:    * Return whichever argument is larger. If either argument is NaN, the
 260:    * result is NaN, and when comparing 0 and -0, 0 is always larger.
 261:    *
 262:    * @param a the first number
 263:    * @param b a second number
 264:    * @return the larger of the two numbers
 265:    */
 266:   public static float max(float a, float b)
 267:   {
 268:     // this check for NaN, from JLS 15.21.1, saves a method call
 269:     if (a != a)
 270:       return a;
 271:     // no need to check if b is NaN; > will work correctly
 272:     // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
 273:     if (a == 0 && b == 0)
 274:       return a - -b;
 275:     return (a > b) ? a : b;
 276:   }
 277: 
 278:   /**
 279:    * Return whichever argument is larger. If either argument is NaN, the
 280:    * result is NaN, and when comparing 0 and -0, 0 is always larger.
 281:    *
 282:    * @param a the first number
 283:    * @param b a second number
 284:    * @return the larger of the two numbers
 285:    */
 286:   public static double max(double a, double b)
 287:   {
 288:     // this check for NaN, from JLS 15.21.1, saves a method call
 289:     if (a != a)
 290:       return a;
 291:     // no need to check if b is NaN; > will work correctly
 292:     // recall that -0.0 == 0.0, but [+-]0.0 - [+-]0.0 behaves special
 293:     if (a == 0 && b == 0)
 294:       return a - -b;
 295:     return (a > b) ? a : b;
 296:   }
 297: 
 298:   /**
 299:    * The trigonometric function <em>sin</em>. The sine of NaN or infinity is
 300:    * NaN, and the sine of 0 retains its sign.
 301:    *
 302:    * @param a the angle (in radians)
 303:    * @return sin(a)
 304:    */
 305:   public static double sin(double a)
 306:   {
 307:     if (a == Double.NEGATIVE_INFINITY || ! (a < Double.POSITIVE_INFINITY))
 308:       return Double.NaN;
 309: 
 310:     if (abs(a) <= PI / 4)
 311:       return sin(a, 0);
 312: 
 313:     // Argument reduction needed.
 314:     double[] y = new double[2];
 315:     int n = remPiOver2(a, y);
 316:     switch (n & 3)
 317:       {
 318:       case 0:
 319:         return sin(y[0], y[1]);
 320:       case 1:
 321:         return cos(y[0], y[1]);
 322:       case 2:
 323:         return -sin(y[0], y[1]);
 324:       default:
 325:         return -cos(y[0], y[1]);
 326:       }
 327:   }
 328: 
 329:   /**
 330:    * The trigonometric function <em>cos</em>. The cosine of NaN or infinity is
 331:    * NaN.
 332:    *
 333:    * @param a the angle (in radians).
 334:    * @return cos(a).
 335:    */
 336:   public static double cos(double a)
 337:   {
 338:     if (a == Double.NEGATIVE_INFINITY || ! (a < Double.POSITIVE_INFINITY))
 339:       return Double.NaN;
 340: 
 341:     if (abs(a) <= PI / 4)
 342:       return cos(a, 0);
 343: 
 344:     // Argument reduction needed.
 345:     double[] y = new double[2];
 346:     int n = remPiOver2(a, y);
 347:     switch (n & 3)
 348:       {
 349:       case 0:
 350:         return cos(y[0], y[1]);
 351:       case 1:
 352:         return -sin(y[0], y[1]);
 353:       case 2:
 354:         return -cos(y[0], y[1]);
 355:       default:
 356:         return sin(y[0], y[1]);
 357:       }
 358:   }
 359: 
 360:   /**
 361:    * The trigonometric function <em>tan</em>. The tangent of NaN or infinity
 362:    * is NaN, and the tangent of 0 retains its sign.
 363:    *
 364:    * @param a the angle (in radians)
 365:    * @return tan(a)
 366:    */
 367:   public static double tan(double a)
 368:   {
 369:     if (a == Double.NEGATIVE_INFINITY || ! (a < Double.POSITIVE_INFINITY))
 370:       return Double.NaN;
 371: 
 372:     if (abs(a) <= PI / 4)
 373:       return tan(a, 0, false);
 374: 
 375:     // Argument reduction needed.
 376:     double[] y = new double[2];
 377:     int n = remPiOver2(a, y);
 378:     return tan(y[0], y[1], (n & 1) == 1);
 379:   }
 380: 
 381:   /**
 382:    * The trigonometric function <em>arcsin</em>. The range of angles returned
 383:    * is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN or
 384:    * its absolute value is beyond 1, the result is NaN; and the arcsine of
 385:    * 0 retains its sign.
 386:    *
 387:    * @param x the sin to turn back into an angle
 388:    * @return arcsin(x)
 389:    */
 390:   public static double asin(double x)
 391:   {
 392:     boolean negative = x < 0;
 393:     if (negative)
 394:       x = -x;
 395:     if (! (x <= 1))
 396:       return Double.NaN;
 397:     if (x == 1)
 398:       return negative ? -PI / 2 : PI / 2;
 399:     if (x < 0.5)
 400:       {
 401:         if (x < 1 / TWO_27)
 402:           return negative ? -x : x;
 403:         double t = x * x;
 404:         double p = t * (PS0 + t * (PS1 + t * (PS2 + t * (PS3 + t
 405:                                                          * (PS4 + t * PS5)))));
 406:         double q = 1 + t * (QS1 + t * (QS2 + t * (QS3 + t * QS4)));
 407:         return negative ? -x - x * (p / q) : x + x * (p / q);
 408:       }
 409:     double w = 1 - x; // 1>|x|>=0.5.
 410:     double t = w * 0.5;
 411:     double p = t * (PS0 + t * (PS1 + t * (PS2 + t * (PS3 + t
 412:                                                      * (PS4 + t * PS5)))));
 413:     double q = 1 + t * (QS1 + t * (QS2 + t * (QS3 + t * QS4)));
 414:     double s = sqrt(t);
 415:     if (x >= 0.975)
 416:       {
 417:         w = p / q;
 418:         t = PI / 2 - (2 * (s + s * w) - PI_L / 2);
 419:       }
 420:     else
 421:       {
 422:         w = (float) s;
 423:         double c = (t - w * w) / (s + w);
 424:         p = 2 * s * (p / q) - (PI_L / 2 - 2 * c);
 425:         q = PI / 4 - 2 * w;
 426:         t = PI / 4 - (p - q);
 427:       }
 428:     return negative ? -t : t;
 429:   }
 430: 
 431:   /**
 432:    * The trigonometric function <em>arccos</em>. The range of angles returned
 433:    * is 0 to pi radians (0 to 180 degrees). If the argument is NaN or
 434:    * its absolute value is beyond 1, the result is NaN.
 435:    *
 436:    * @param x the cos to turn back into an angle
 437:    * @return arccos(x)
 438:    */
 439:   public static double acos(double x)
 440:   {
 441:     boolean negative = x < 0;
 442:     if (negative)
 443:       x = -x;
 444:     if (! (x <= 1))
 445:       return Double.NaN;
 446:     if (x == 1)
 447:       return negative ? PI : 0;
 448:     if (x < 0.5)
 449:       {
 450:         if (x < 1 / TWO_57)
 451:           return PI / 2;
 452:         double z = x * x;
 453:         double p = z * (PS0 + z * (PS1 + z * (PS2 + z * (PS3 + z
 454:                                                          * (PS4 + z * PS5)))));
 455:         double q = 1 + z * (QS1 + z * (QS2 + z * (QS3 + z * QS4)));
 456:         double r = x - (PI_L / 2 - x * (p / q));
 457:         return negative ? PI / 2 + r : PI / 2 - r;
 458:       }
 459:     if (negative) // x<=-0.5.
 460:       {
 461:         double z = (1 + x) * 0.5;
 462:         double p = z * (PS0 + z * (PS1 + z * (PS2 + z * (PS3 + z
 463:                                                          * (PS4 + z * PS5)))));
 464:         double q = 1 + z * (QS1 + z * (QS2 + z * (QS3 + z * QS4)));
 465:         double s = sqrt(z);
 466:         double w = p / q * s - PI_L / 2;
 467:         return PI - 2 * (s + w);
 468:       }
 469:     double z = (1 - x) * 0.5; // x>0.5.
 470:     double s = sqrt(z);
 471:     double df = (float) s;
 472:     double c = (z - df * df) / (s + df);
 473:     double p = z * (PS0 + z * (PS1 + z * (PS2 + z * (PS3 + z
 474:                                                      * (PS4 + z * PS5)))));
 475:     double q = 1 + z * (QS1 + z * (QS2 + z * (QS3 + z * QS4)));
 476:     double w = p / q * s + c;
 477:     return 2 * (df + w);
 478:   }
 479: 
 480:   /**
 481:    * The trigonometric function <em>arcsin</em>. The range of angles returned
 482:    * is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN, the
 483:    * result is NaN; and the arctangent of 0 retains its sign.
 484:    *
 485:    * @param x the tan to turn back into an angle
 486:    * @return arcsin(x)
 487:    * @see #atan2(double, double)
 488:    */
 489:   public static double atan(double x)
 490:   {
 491:     double lo;
 492:     double hi;
 493:     boolean negative = x < 0;
 494:     if (negative)
 495:       x = -x;
 496:     if (x >= TWO_66)
 497:       return negative ? -PI / 2 : PI / 2;
 498:     if (! (x >= 0.4375)) // |x|<7/16, or NaN.
 499:       {
 500:         if (! (x >= 1 / TWO_29)) // Small, or NaN.
 501:           return negative ? -x : x;
 502:         lo = hi = 0;
 503:       }
 504:     else if (x < 1.1875)
 505:       {
 506:         if (x < 0.6875) // 7/16<=|x|<11/16.
 507:           {
 508:             x = (2 * x - 1) / (2 + x);
 509:             hi = ATAN_0_5H;
 510:             lo = ATAN_0_5L;
 511:           }
 512:         else // 11/16<=|x|<19/16.
 513:           {
 514:             x = (x - 1) / (x + 1);
 515:             hi = PI / 4;
 516:             lo = PI_L / 4;
 517:           }
 518:       }
 519:     else if (x < 2.4375) // 19/16<=|x|<39/16.
 520:       {
 521:         x = (x - 1.5) / (1 + 1.5 * x);
 522:         hi = ATAN_1_5H;
 523:         lo = ATAN_1_5L;
 524:       }
 525:     else // 39/16<=|x|<2**66.
 526:       {
 527:         x = -1 / x;
 528:         hi = PI / 2;
 529:         lo = PI_L / 2;
 530:       }
 531: 
 532:     // Break sum from i=0 to 10 ATi*z**(i+1) into odd and even poly.
 533:     double z = x * x;
 534:     double w = z * z;
 535:     double s1 = z * (AT0 + w * (AT2 + w * (AT4 + w * (AT6 + w
 536:                                                       * (AT8 + w * AT10)))));
 537:     double s2 = w * (AT1 + w * (AT3 + w * (AT5 + w * (AT7 + w * AT9))));
 538:     if (hi == 0)
 539:       return negative ? x * (s1 + s2) - x : x - x * (s1 + s2);
 540:     z = hi - ((x * (s1 + s2) - lo) - x);
 541:     return negative ? -z : z;
 542:   }
 543: 
 544:   /**
 545:    * A special version of the trigonometric function <em>arctan</em>, for
 546:    * converting rectangular coordinates <em>(x, y)</em> to polar
 547:    * <em>(r, theta)</em>. This computes the arctangent of x/y in the range
 548:    * of -pi to pi radians (-180 to 180 degrees). Special cases:<ul>
 549:    * <li>If either argument is NaN, the result is NaN.</li>
 550:    * <li>If the first argument is positive zero and the second argument is
 551:    * positive, or the first argument is positive and finite and the second
 552:    * argument is positive infinity, then the result is positive zero.</li>
 553:    * <li>If the first argument is negative zero and the second argument is
 554:    * positive, or the first argument is negative and finite and the second
 555:    * argument is positive infinity, then the result is negative zero.</li>
 556:    * <li>If the first argument is positive zero and the second argument is
 557:    * negative, or the first argument is positive and finite and the second
 558:    * argument is negative infinity, then the result is the double value
 559:    * closest to pi.</li>
 560:    * <li>If the first argument is negative zero and the second argument is
 561:    * negative, or the first argument is negative and finite and the second
 562:    * argument is negative infinity, then the result is the double value
 563:    * closest to -pi.</li>
 564:    * <li>If the first argument is positive and the second argument is
 565:    * positive zero or negative zero, or the first argument is positive
 566:    * infinity and the second argument is finite, then the result is the
 567:    * double value closest to pi/2.</li>
 568:    * <li>If the first argument is negative and the second argument is
 569:    * positive zero or negative zero, or the first argument is negative
 570:    * infinity and the second argument is finite, then the result is the
 571:    * double value closest to -pi/2.</li>
 572:    * <li>If both arguments are positive infinity, then the result is the
 573:    * double value closest to pi/4.</li>
 574:    * <li>If the first argument is positive infinity and the second argument
 575:    * is negative infinity, then the result is the double value closest to
 576:    * 3*pi/4.</li>
 577:    * <li>If the first argument is negative infinity and the second argument
 578:    * is positive infinity, then the result is the double value closest to
 579:    * -pi/4.</li>
 580:    * <li>If both arguments are negative infinity, then the result is the
 581:    * double value closest to -3*pi/4.</li>
 582:    *
 583:    * </ul><p>This returns theta, the angle of the point. To get r, albeit
 584:    * slightly inaccurately, use sqrt(x*x+y*y).
 585:    *
 586:    * @param y the y position
 587:    * @param x the x position
 588:    * @return <em>theta</em> in the conversion of (x, y) to (r, theta)
 589:    * @see #atan(double)
 590:    */
 591:   public static double atan2(double y, double x)
 592:   {
 593:     if (x != x || y != y)
 594:       return Double.NaN;
 595:     if (x == 1)
 596:       return atan(y);
 597:     if (x == Double.POSITIVE_INFINITY)
 598:       {
 599:         if (y == Double.POSITIVE_INFINITY)
 600:           return PI / 4;
 601:         if (y == Double.NEGATIVE_INFINITY)
 602:           return -PI / 4;
 603:         return 0 * y;
 604:       }
 605:     if (x == Double.NEGATIVE_INFINITY)
 606:       {
 607:         if (y == Double.POSITIVE_INFINITY)
 608:           return 3 * PI / 4;
 609:         if (y == Double.NEGATIVE_INFINITY)
 610:           return -3 * PI / 4;
 611:         return (1 / (0 * y) == Double.POSITIVE_INFINITY) ? PI : -PI;
 612:       }
 613:     if (y == 0)
 614:       {
 615:         if (1 / (0 * x) == Double.POSITIVE_INFINITY)
 616:           return y;
 617:         return (1 / y == Double.POSITIVE_INFINITY) ? PI : -PI;
 618:       }
 619:     if (y == Double.POSITIVE_INFINITY || y == Double.NEGATIVE_INFINITY
 620:         || x == 0)
 621:       return y < 0 ? -PI / 2 : PI / 2;
 622: 
 623:     double z = abs(y / x); // Safe to do y/x.
 624:     if (z > TWO_60)
 625:       z = PI / 2 + 0.5 * PI_L;
 626:     else if (x < 0 && z < 1 / TWO_60)
 627:       z = 0;
 628:     else
 629:       z = atan(z);
 630:     if (x > 0)
 631:       return y > 0 ? z : -z;
 632:     return y > 0 ? PI - (z - PI_L) : z - PI_L - PI;
 633:   }
 634: 
 635:   /**
 636:    * Take <em>e</em><sup>a</sup>.  The opposite of <code>log()</code>. If the
 637:    * argument is NaN, the result is NaN; if the argument is positive infinity,
 638:    * the result is positive infinity; and if the argument is negative
 639:    * infinity, the result is positive zero.
 640:    *
 641:    * @param x the number to raise to the power
 642:    * @return the number raised to the power of <em>e</em>
 643:    * @see #log(double)
 644:    * @see #pow(double, double)
 645:    */
 646:   public static double exp(double x)
 647:   {
 648:     if (x != x)
 649:       return x;
 650:     if (x > EXP_LIMIT_H)
 651:       return Double.POSITIVE_INFINITY;
 652:     if (x < EXP_LIMIT_L)
 653:       return 0;
 654: 
 655:     // Argument reduction.
 656:     double hi;
 657:     double lo;
 658:     int k;
 659:     double t = abs(x);
 660:     if (t > 0.5 * LN2)
 661:       {
 662:         if (t < 1.5 * LN2)
 663:           {
 664:             hi = t - LN2_H;
 665:             lo = LN2_L;
 666:             k = 1;
 667:           }
 668:         else
 669:           {
 670:             k = (int) (INV_LN2 * t + 0.5);
 671:             hi = t - k * LN2_H;
 672:             lo = k * LN2_L;
 673:           }
 674:         if (x < 0)
 675:           {
 676:             hi = -hi;
 677:             lo = -lo;
 678:             k = -k;
 679:           }
 680:         x = hi - lo;
 681:       }
 682:     else if (t < 1 / TWO_28)
 683:       return 1;
 684:     else
 685:       lo = hi = k = 0;
 686: 
 687:     // Now x is in primary range.
 688:     t = x * x;
 689:     double c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
 690:     if (k == 0)
 691:       return 1 - (x * c / (c - 2) - x);
 692:     double y = 1 - (lo - x * c / (2 - c) - hi);
 693:     return scale(y, k);
 694:   }
 695: 
 696:   /**
 697:    * Take ln(a) (the natural log).  The opposite of <code>exp()</code>. If the
 698:    * argument is NaN or negative, the result is NaN; if the argument is
 699:    * positive infinity, the result is positive infinity; and if the argument
 700:    * is either zero, the result is negative infinity.
 701:    *
 702:    * <p>Note that the way to get log<sub>b</sub>(a) is to do this:
 703:    * <code>ln(a) / ln(b)</code>.
 704:    *
 705:    * @param x the number to take the natural log of
 706:    * @return the natural log of <code>a</code>
 707:    * @see #exp(double)
 708:    */
 709:   public static double log(double x)
 710:   {
 711:     if (x == 0)
 712:       return Double.NEGATIVE_INFINITY;
 713:     if (x < 0)
 714:       return Double.NaN;
 715:     if (! (x < Double.POSITIVE_INFINITY))
 716:       return x;
 717: 
 718:     // Normalize x.
 719:     long bits = Double.doubleToLongBits(x);
 720:     int exp = (int) (bits >> 52);
 721:     if (exp == 0) // Subnormal x.
 722:       {
 723:         x *= TWO_54;
 724:         bits = Double.doubleToLongBits(x);
 725:         exp = (int) (bits >> 52) - 54;
 726:       }
 727:     exp -= 1023; // Unbias exponent.
 728:     bits = (bits & 0x000fffffffffffffL) | 0x3ff0000000000000L;
 729:     x = Double.longBitsToDouble(bits);
 730:     if (x >= SQRT_2)
 731:       {
 732:         x *= 0.5;
 733:         exp++;
 734:       }
 735:     x--;
 736:     if (abs(x) < 1 / TWO_20)
 737:       {
 738:         if (x == 0)
 739:           return exp * LN2_H + exp * LN2_L;
 740:         double r = x * x * (0.5 - 1 / 3.0 * x);
 741:         if (exp == 0)
 742:           return x - r;
 743:         return exp * LN2_H - ((r - exp * LN2_L) - x);
 744:       }
 745:     double s = x / (2 + x);
 746:     double z = s * s;
 747:     double w = z * z;
 748:     double t1 = w * (LG2 + w * (LG4 + w * LG6));
 749:     double t2 = z * (LG1 + w * (LG3 + w * (LG5 + w * LG7)));
 750:     double r = t2 + t1;
 751:     if (bits >= 0x3ff6174a00000000L && bits < 0x3ff6b85200000000L)
 752:       {
 753:         double h = 0.5 * x * x; // Need more accuracy for x near sqrt(2).
 754:         if (exp == 0)
 755:           return x - (h - s * (h + r));
 756:         return exp * LN2_H - ((h - (s * (h + r) + exp * LN2_L)) - x);
 757:       }
 758:     if (exp == 0)
 759:       return x - s * (x - r);
 760:     return exp * LN2_H - ((s * (x - r) - exp * LN2_L) - x);
 761:   }
 762: 
 763:   /**
 764:    * Take a square root. If the argument is NaN or negative, the result is
 765:    * NaN; if the argument is positive infinity, the result is positive
 766:    * infinity; and if the result is either zero, the result is the same.
 767:    *
 768:    * <p>For other roots, use pow(x, 1/rootNumber).
 769:    *
 770:    * @param x the numeric argument
 771:    * @return the square root of the argument
 772:    * @see #pow(double, double)
 773:    */
 774:   public static double sqrt(double x)
 775:   {
 776:     if (x < 0)
 777:       return Double.NaN;
 778:     if (x == 0 || ! (x < Double.POSITIVE_INFINITY))
 779:       return x;
 780: 
 781:     // Normalize x.
 782:     long bits = Double.doubleToLongBits(x);
 783:     int exp = (int) (bits >> 52);
 784:     if (exp == 0) // Subnormal x.
 785:       {
 786:         x *= TWO_54;
 787:         bits = Double.doubleToLongBits(x);
 788:         exp = (int) (bits >> 52) - 54;
 789:       }
 790:     exp -= 1023; // Unbias exponent.
 791:     bits = (bits & 0x000fffffffffffffL) | 0x0010000000000000L;
 792:     if ((exp & 1) == 1) // Odd exp, double x to make it even.
 793:       bits <<= 1;
 794:     exp >>= 1;
 795: 
 796:     // Generate sqrt(x) bit by bit.
 797:     bits <<= 1;
 798:     long q = 0;
 799:     long s = 0;
 800:     long r = 0x0020000000000000L; // Move r right to left.
 801:     while (r != 0)
 802:       {
 803:         long t = s + r;
 804:         if (t <= bits)
 805:           {
 806:             s = t + r;
 807:             bits -= t;
 808:             q += r;
 809:           }
 810:         bits <<= 1;
 811:         r >>= 1;
 812:       }
 813: 
 814:     // Use floating add to round correctly.
 815:     if (bits != 0)
 816:       q += q & 1;
 817:     return Double.longBitsToDouble((q >> 1) + ((exp + 1022L) << 52));
 818:   }
 819: 
 820:   /**
 821:    * Raise a number to a power. Special cases:<ul>
 822:    * <li>If the second argument is positive or negative zero, then the result
 823:    * is 1.0.</li>
 824:    * <li>If the second argument is 1.0, then the result is the same as the
 825:    * first argument.</li>
 826:    * <li>If the second argument is NaN, then the result is NaN.</li>
 827:    * <li>If the first argument is NaN and the second argument is nonzero,
 828:    * then the result is NaN.</li>
 829:    * <li>If the absolute value of the first argument is greater than 1 and
 830:    * the second argument is positive infinity, or the absolute value of the
 831:    * first argument is less than 1 and the second argument is negative
 832:    * infinity, then the result is positive infinity.</li>
 833:    * <li>If the absolute value of the first argument is greater than 1 and
 834:    * the second argument is negative infinity, or the absolute value of the
 835:    * first argument is less than 1 and the second argument is positive
 836:    * infinity, then the result is positive zero.</li>
 837:    * <li>If the absolute value of the first argument equals 1 and the second
 838:    * argument is infinite, then the result is NaN.</li>
 839:    * <li>If the first argument is positive zero and the second argument is
 840:    * greater than zero, or the first argument is positive infinity and the
 841:    * second argument is less than zero, then the result is positive zero.</li>
 842:    * <li>If the first argument is positive zero and the second argument is
 843:    * less than zero, or the first argument is positive infinity and the
 844:    * second argument is greater than zero, then the result is positive
 845:    * infinity.</li>
 846:    * <li>If the first argument is negative zero and the second argument is
 847:    * greater than zero but not a finite odd integer, or the first argument is
 848:    * negative infinity and the second argument is less than zero but not a
 849:    * finite odd integer, then the result is positive zero.</li>
 850:    * <li>If the first argument is negative zero and the second argument is a
 851:    * positive finite odd integer, or the first argument is negative infinity
 852:    * and the second argument is a negative finite odd integer, then the result
 853:    * is negative zero.</li>
 854:    * <li>If the first argument is negative zero and the second argument is
 855:    * less than zero but not a finite odd integer, or the first argument is
 856:    * negative infinity and the second argument is greater than zero but not a
 857:    * finite odd integer, then the result is positive infinity.</li>
 858:    * <li>If the first argument is negative zero and the second argument is a
 859:    * negative finite odd integer, or the first argument is negative infinity
 860:    * and the second argument is a positive finite odd integer, then the result
 861:    * is negative infinity.</li>
 862:    * <li>If the first argument is less than zero and the second argument is a
 863:    * finite even integer, then the result is equal to the result of raising
 864:    * the absolute value of the first argument to the power of the second
 865:    * argument.</li>
 866:    * <li>If the first argument is less than zero and the second argument is a
 867:    * finite odd integer, then the result is equal to the negative of the
 868:    * result of raising the absolute value of the first argument to the power
 869:    * of the second argument.</li>
 870:    * <li>If the first argument is finite and less than zero and the second
 871:    * argument is finite and not an integer, then the result is NaN.</li>
 872:    * <li>If both arguments are integers, then the result is exactly equal to
 873:    * the mathematical result of raising the first argument to the power of
 874:    * the second argument if that result can in fact be represented exactly as
 875:    * a double value.</li>
 876:    *
 877:    * </ul><p>(In the foregoing descriptions, a floating-point value is
 878:    * considered to be an integer if and only if it is a fixed point of the
 879:    * method {@link #ceil(double)} or, equivalently, a fixed point of the
 880:    * method {@link #floor(double)}. A value is a fixed point of a one-argument
 881:    * method if and only if the result of applying the method to the value is
 882:    * equal to the value.)
 883:    *
 884:    * @param x the number to raise
 885:    * @param y the power to raise it to
 886:    * @return x<sup>y</sup>
 887:    */
 888:   public static double pow(double x, double y)
 889:   {
 890:     // Special cases first.
 891:     if (y == 0)
 892:       return 1;
 893:     if (y == 1)
 894:       return x;
 895:     if (y == -1)
 896:       return 1 / x;
 897:     if (x != x || y != y)
 898:       return Double.NaN;
 899: 
 900:     // When x < 0, yisint tells if y is not an integer (0), even(1),
 901:     // or odd (2).
 902:     int yisint = 0;
 903:     if (x < 0 && floor(y) == y)
 904:       yisint = (y % 2 == 0) ? 2 : 1;
 905:     double ax = abs(x);
 906:     double ay = abs(y);
 907: 
 908:     // More special cases, of y.
 909:     if (ay == Double.POSITIVE_INFINITY)
 910:       {
 911:         if (ax == 1)
 912:           return Double.NaN;
 913:         if (ax > 1)
 914:           return y > 0 ? y : 0;
 915:         return y < 0 ? -y : 0;
 916:       }
 917:     if (y == 2)
 918:       return x * x;
 919:     if (y == 0.5)
 920:       return sqrt(x);
 921: 
 922:     // More special cases, of x.
 923:     if (x == 0 || ax == Double.POSITIVE_INFINITY || ax == 1)
 924:       {
 925:         if (y < 0)
 926:           ax = 1 / ax;
 927:         if (x < 0)
 928:           {
 929:             if (x == -1 && yisint == 0)
 930:               ax = Double.NaN;
 931:             else if (yisint == 1)
 932:               ax = -ax;
 933:           }
 934:         return ax;
 935:       }
 936:     if (x < 0 && yisint == 0)
 937:       return Double.NaN;
 938: 
 939:     // Now we can start!
 940:     double t;
 941:     double t1;
 942:     double t2;
 943:     double u;
 944:     double v;
 945:     double w;
 946:     if (ay > TWO_31)
 947:       {
 948:         if (ay > TWO_64) // Automatic over/underflow.
 949:           return ((ax < 1) ? y < 0 : y > 0) ? Double.POSITIVE_INFINITY : 0;
 950:         // Over/underflow if x is not close to one.
 951:         if (ax < 0.9999995231628418)
 952:           return y < 0 ? Double.POSITIVE_INFINITY : 0;
 953:         if (ax >= 1.0000009536743164)
 954:           return y > 0 ? Double.POSITIVE_INFINITY : 0;
 955:         // Now |1-x| is <= 2**-20, sufficient to compute
 956:         // log(x) by x-x^2/2+x^3/3-x^4/4.
 957:         t = x - 1;
 958:         w = t * t * (0.5 - t * (1 / 3.0 - t * 0.25));
 959:         u = INV_LN2_H * t;
 960:         v = t * INV_LN2_L - w * INV_LN2;
 961:         t1 = (float) (u + v);
 962:         t2 = v - (t1 - u);
 963:       }
 964:     else
 965:     {
 966:       long bits = Double.doubleToLongBits(ax);
 967:       int exp = (int) (bits >> 52);
 968:       if (exp == 0) // Subnormal x.
 969:         {
 970:           ax *= TWO_54;
 971:           bits = Double.doubleToLongBits(ax);
 972:           exp = (int) (bits >> 52) - 54;
 973:         }
 974:       exp -= 1023; // Unbias exponent.
 975:       ax = Double.longBitsToDouble((bits & 0x000fffffffffffffL)
 976:                                    | 0x3ff0000000000000L);
 977:       boolean k;
 978:       if (ax < SQRT_1_5)  // |x|<sqrt(3/2).
 979:         k = false;
 980:       else if (ax < SQRT_3) // |x|<sqrt(3).
 981:         k = true;
 982:       else
 983:         {
 984:           k = false;
 985:           ax *= 0.5;
 986:           exp++;
 987:         }
 988: 
 989:       // Compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5).
 990:       u = ax - (k ? 1.5 : 1);
 991:       v = 1 / (ax + (k ? 1.5 : 1));
 992:       double s = u * v;
 993:       double s_h = (float) s;
 994:       double t_h = (float) (ax + (k ? 1.5 : 1));
 995:       double t_l = ax - (t_h - (k ? 1.5 : 1));
 996:       double s_l = v * ((u - s_h * t_h) - s_h * t_l);
 997:       // Compute log(ax).
 998:       double s2 = s * s;
 999:       double r = s_l * (s_h + s) + s2 * s2
1000:         * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
1001:       s2 = s_h * s_h;
1002:       t_h = (float) (3.0 + s2 + r);
1003:       t_l = r - (t_h - 3.0 - s2);
1004:       // u+v = s*(1+...).
1005:       u = s_h * t_h;
1006:       v = s_l * t_h + t_l * s;
1007:       // 2/(3log2)*(s+...).
1008:       double p_h = (float) (u + v);
1009:       double p_l = v - (p_h - u);
1010:       double z_h = CP_H * p_h;
1011:       double z_l = CP_L * p_h + p_l * CP + (k ? DP_L : 0);
1012:       // log2(ax) = (s+..)*2/(3*log2) = exp + dp_h + z_h + z_l.
1013:       t = exp;
1014:       t1 = (float) (z_h + z_l + (k ? DP_H : 0) + t);
1015:       t2 = z_l - (t1 - t - (k ? DP_H : 0) - z_h);
1016:     }
1017: 
1018:     // Split up y into y1+y2 and compute (y1+y2)*(t1+t2).
1019:     boolean negative = x < 0 && yisint == 1;
1020:     double y1 = (float) y;
1021:     double p_l = (y - y1) * t1 + y * t2;
1022:     double p_h = y1 * t1;
1023:     double z = p_l + p_h;
1024:     if (z >= 1024) // Detect overflow.
1025:       {
1026:         if (z > 1024 || p_l + OVT > z - p_h)
1027:           return negative ? Double.NEGATIVE_INFINITY
1028:             : Double.POSITIVE_INFINITY;
1029:       }
1030:     else if (z <= -1075) // Detect underflow.
1031:       {
1032:         if (z < -1075 || p_l <= z - p_h)
1033:           return negative ? -0.0 : 0;
1034:       }
1035: 
1036:     // Compute 2**(p_h+p_l).
1037:     int n = round((float) z);
1038:     p_h -= n;
1039:     t = (float) (p_l + p_h);
1040:     u = t * LN2_H;
1041:     v = (p_l - (t - p_h)) * LN2 + t * LN2_L;
1042:     z = u + v;
1043:     w = v - (z - u);
1044:     t = z * z;
1045:     t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
1046:     double r = (z * t1) / (t1 - 2) - (w + z * w);
1047:     z = scale(1 - (r - z), n);
1048:     return negative ? -z : z;
1049:   }
1050: 
1051:   /**
1052:    * Get the IEEE 754 floating point remainder on two numbers. This is the
1053:    * value of <code>x - y * <em>n</em></code>, where <em>n</em> is the closest
1054:    * double to <code>x / y</code> (ties go to the even n); for a zero
1055:    * remainder, the sign is that of <code>x</code>. If either argument is NaN,
1056:    * the first argument is infinite, or the second argument is zero, the result
1057:    * is NaN; if x is finite but y is infinite, the result is x.
1058:    *
1059:    * @param x the dividend (the top half)
1060:    * @param y the divisor (the bottom half)
1061:    * @return the IEEE 754-defined floating point remainder of x/y
1062:    * @see #rint(double)
1063:    */
1064:   public static double IEEEremainder(double x, double y)
1065:   {
1066:     // Purge off exception values.
1067:     if (x == Double.NEGATIVE_INFINITY || ! (x < Double.POSITIVE_INFINITY)
1068:         || y == 0 || y != y)
1069:       return Double.NaN;
1070: 
1071:     boolean negative = x < 0;
1072:     x = abs(x);
1073:     y = abs(y);
1074:     if (x == y || x == 0)
1075:       return 0 * x; // Get correct sign.
1076: 
1077:     // Achieve x < 2y, then take first shot at remainder.
1078:     if (y < TWO_1023)
1079:       x %= y + y;
1080: 
1081:     // Now adjust x to get correct precision.
1082:     if (y < 4 / TWO_1023)
1083:       {
1084:         if (x + x > y)
1085:           {
1086:             x -= y;
1087:             if (x + x >= y)
1088:               x -= y;
1089:           }
1090:       }
1091:     else
1092:       {
1093:         y *= 0.5;
1094:         if (x > y)
1095:           {
1096:             x -= y;
1097:             if (x >= y)
1098:               x -= y;
1099:           }
1100:       }
1101:     return negative ? -x : x;
1102:   }
1103: 
1104:   /**
1105:    * Take the nearest integer that is that is greater than or equal to the
1106:    * argument. If the argument is NaN, infinite, or zero, the result is the
1107:    * same; if the argument is between -1 and 0, the result is negative zero.
1108:    * Note that <code>Math.ceil(x) == -Math.floor(-x)</code>.
1109:    *
1110:    * @param a the value to act upon
1111:    * @return the nearest integer &gt;= <code>a</code>
1112:    */
1113:   public static double ceil(double a)
1114:   {
1115:     return -floor(-a);
1116:   }
1117: 
1118:   /**
1119:    * Take the nearest integer that is that is less than or equal to the
1120:    * argument. If the argument is NaN, infinite, or zero, the result is the
1121:    * same. Note that <code>Math.ceil(x) == -Math.floor(-x)</code>.
1122:    *
1123:    * @param a the value to act upon
1124:    * @return the nearest integer &lt;= <code>a</code>
1125:    */
1126:   public static double floor(double a)
1127:   {
1128:     double x = abs(a);
1129:     if (! (x < TWO_52) || (long) a == a)
1130:       return a; // No fraction bits; includes NaN and infinity.
1131:     if (x < 1)
1132:       return a >= 0 ? 0 * a : -1; // Worry about signed zero.
1133:     return a < 0 ? (long) a - 1.0 : (long) a; // Cast to long truncates.
1134:   }
1135: 
1136:   /**
1137:    * Take the nearest integer to the argument.  If it is exactly between
1138:    * two integers, the even integer is taken. If the argument is NaN,
1139:    * infinite, or zero, the result is the same.
1140:    *
1141:    * @param a the value to act upon
1142:    * @return the nearest integer to <code>a</code>
1143:    */
1144:   public static double rint(double a)
1145:   {
1146:     double x = abs(a);
1147:     if (! (x < TWO_52))
1148:       return a; // No fraction bits; includes NaN and infinity.
1149:     if (x <= 0.5)
1150:       return 0 * a; // Worry about signed zero.
1151:     if (x % 2 <= 0.5)
1152:       return (long) a; // Catch round down to even.
1153:     return (long) (a + (a < 0 ? -0.5 : 0.5)); // Cast to long truncates.
1154:   }
1155: 
1156:   /**
1157:    * Take the nearest integer to the argument.  This is equivalent to
1158:    * <code>(int) Math.floor(f + 0.5f)</code>. If the argument is NaN, the
1159:    * result is 0; otherwise if the argument is outside the range of int, the
1160:    * result will be Integer.MIN_VALUE or Integer.MAX_VALUE, as appropriate.
1161:    *
1162:    * @param f the argument to round
1163:    * @return the nearest integer to the argument
1164:    * @see Integer#MIN_VALUE
1165:    * @see Integer#MAX_VALUE
1166:    */
1167:   public static int round(float f)
1168:   {
1169:     return (int) floor(f + 0.5f);
1170:   }
1171: 
1172:   /**
1173:    * Take the nearest long to the argument.  This is equivalent to
1174:    * <code>(long) Math.floor(d + 0.5)</code>. If the argument is NaN, the
1175:    * result is 0; otherwise if the argument is outside the range of long, the
1176:    * result will be Long.MIN_VALUE or Long.MAX_VALUE, as appropriate.
1177:    *
1178:    * @param d the argument to round
1179:    * @return the nearest long to the argument
1180:    * @see Long#MIN_VALUE
1181:    * @see Long#MAX_VALUE
1182:    */
1183:   public static long round(double d)
1184:   {
1185:     return (long) floor(d + 0.5);
1186:   }
1187: 
1188:   /**
1189:    * Get a random number.  This behaves like Random.nextDouble(), seeded by
1190:    * System.currentTimeMillis() when first called. In other words, the number
1191:    * is from a pseudorandom sequence, and lies in the range [+0.0, 1.0).
1192:    * This random sequence is only used by this method, and is threadsafe,
1193:    * although you may want your own random number generator if it is shared
1194:    * among threads.
1195:    *
1196:    * @return a random number
1197:    * @see Random#nextDouble()
1198:    * @see System#currentTimeMillis()
1199:    */
1200:   public static synchronized double random()
1201:   {
1202:     if (rand == null)
1203:       rand = new Random();
1204:     return rand.nextDouble();
1205:   }
1206: 
1207:   /**
1208:    * Convert from degrees to radians. The formula for this is
1209:    * radians = degrees * (pi/180); however it is not always exact given the
1210:    * limitations of floating point numbers.
1211:    *
1212:    * @param degrees an angle in degrees
1213:    * @return the angle in radians
1214:    */
1215:   public static double toRadians(double degrees)
1216:   {
1217:     return (degrees * PI) / 180;
1218:   }
1219: 
1220:   /**
1221:    * Convert from radians to degrees. The formula for this is
1222:    * degrees = radians * (180/pi); however it is not always exact given the
1223:    * limitations of floating point numbers.
1224:    *
1225:    * @param rads an angle in radians
1226:    * @return the angle in degrees
1227:    */
1228:   public static double toDegrees(double rads)
1229:   {
1230:     return (rads * 180) / PI;
1231:   }
1232: 
1233:   /**
1234:    * Constants for scaling and comparing doubles by powers of 2. The compiler
1235:    * must automatically inline constructs like (1/TWO_54), so we don't list
1236:    * negative powers of two here.
1237:    */
1238:   private static final double
1239:     TWO_16 = 0x10000, // Long bits 0x40f0000000000000L.
1240:     TWO_20 = 0x100000, // Long bits 0x4130000000000000L.
1241:     TWO_24 = 0x1000000, // Long bits 0x4170000000000000L.
1242:     TWO_27 = 0x8000000, // Long bits 0x41a0000000000000L.
1243:     TWO_28 = 0x10000000, // Long bits 0x41b0000000000000L.
1244:     TWO_29 = 0x20000000, // Long bits 0x41c0000000000000L.
1245:     TWO_31 = 0x80000000L, // Long bits 0x41e0000000000000L.
1246:     TWO_49 = 0x2000000000000L, // Long bits 0x4300000000000000L.
1247:     TWO_52 = 0x10000000000000L, // Long bits 0x4330000000000000L.
1248:     TWO_54 = 0x40000000000000L, // Long bits 0x4350000000000000L.
1249:     TWO_57 = 0x200000000000000L, // Long bits 0x4380000000000000L.
1250:     TWO_60 = 0x1000000000000000L, // Long bits 0x43b0000000000000L.
1251:     TWO_64 = 1.8446744073709552e19, // Long bits 0x43f0000000000000L.
1252:     TWO_66 = 7.378697629483821e19, // Long bits 0x4410000000000000L.
1253:     TWO_1023 = 8.98846567431158e307; // Long bits 0x7fe0000000000000L.
1254: 
1255:   /**
1256:    * Super precision for 2/pi in 24-bit chunks, for use in
1257:    * {@link #remPiOver2(double, double[])}.
1258:    */
1259:   private static final int TWO_OVER_PI[] = {
1260:     0xa2f983, 0x6e4e44, 0x1529fc, 0x2757d1, 0xf534dd, 0xc0db62,
1261:     0x95993c, 0x439041, 0xfe5163, 0xabdebb, 0xc561b7, 0x246e3a,
1262:     0x424dd2, 0xe00649, 0x2eea09, 0xd1921c, 0xfe1deb, 0x1cb129,
1263:     0xa73ee8, 0x8235f5, 0x2ebb44, 0x84e99c, 0x7026b4, 0x5f7e41,
1264:     0x3991d6, 0x398353, 0x39f49c, 0x845f8b, 0xbdf928, 0x3b1ff8,
1265:     0x97ffde, 0x05980f, 0xef2f11, 0x8b5a0a, 0x6d1f6d, 0x367ecf,
1266:     0x27cb09, 0xb74f46, 0x3f669e, 0x5fea2d, 0x7527ba, 0xc7ebe5,
1267:     0xf17b3d, 0x0739f7, 0x8a5292, 0xea6bfb, 0x5fb11f, 0x8d5d08,
1268:     0x560330, 0x46fc7b, 0x6babf0, 0xcfbc20, 0x9af436, 0x1da9e3,
1269:     0x91615e, 0xe61b08, 0x659985, 0x5f14a0, 0x68408d, 0xffd880,
1270:     0x4d7327, 0x310606, 0x1556ca, 0x73a8c9, 0x60e27b, 0xc08c6b,
1271:   };
1272: 
1273:   /**
1274:    * Super precision for pi/2 in 24-bit chunks, for use in
1275:    * {@link #remPiOver2(double, double[])}.
1276:    */
1277:   private static final double PI_OVER_TWO[] = {
1278:     1.570796251296997, // Long bits 0x3ff921fb40000000L.
1279:     7.549789415861596e-8, // Long bits 0x3e74442d00000000L.
1280:     5.390302529957765e-15, // Long bits 0x3cf8469880000000L.
1281:     3.282003415807913e-22, // Long bits 0x3b78cc5160000000L.
1282:     1.270655753080676e-29, // Long bits 0x39f01b8380000000L.
1283:     1.2293330898111133e-36, // Long bits 0x387a252040000000L.
1284:     2.7337005381646456e-44, // Long bits 0x36e3822280000000L.
1285:     2.1674168387780482e-51, // Long bits 0x3569f31d00000000L.
1286:   };
1287: 
1288:   /**
1289:    * More constants related to pi, used in
1290:    * {@link #remPiOver2(double, double[])} and elsewhere.
1291:    */
1292:   private static final double
1293:     PI_L = 1.2246467991473532e-16, // Long bits 0x3ca1a62633145c07L.
1294:     PIO2_1 = 1.5707963267341256, // Long bits 0x3ff921fb54400000L.
1295:     PIO2_1L = 6.077100506506192e-11, // Long bits 0x3dd0b4611a626331L.
1296:     PIO2_2 = 6.077100506303966e-11, // Long bits 0x3dd0b4611a600000L.
1297:     PIO2_2L = 2.0222662487959506e-21, // Long bits 0x3ba3198a2e037073L.
1298:     PIO2_3 = 2.0222662487111665e-21, // Long bits 0x3ba3198a2e000000L.
1299:     PIO2_3L = 8.4784276603689e-32; // Long bits 0x397b839a252049c1L.
1300: 
1301:   /**
1302:    * Natural log and square root constants, for calculation of
1303:    * {@link #exp(double)}, {@link #log(double)} and
1304:    * {@link #pow(double, double)}. CP is 2/(3*ln(2)).
1305:    */
1306:   private static final double
1307:     SQRT_1_5 = 1.224744871391589, // Long bits 0x3ff3988e1409212eL.
1308:     SQRT_2 = 1.4142135623730951, // Long bits 0x3ff6a09e667f3bcdL.
1309:     SQRT_3 = 1.7320508075688772, // Long bits 0x3ffbb67ae8584caaL.
1310:     EXP_LIMIT_H = 709.782712893384, // Long bits 0x40862e42fefa39efL.
1311:     EXP_LIMIT_L = -745.1332191019411, // Long bits 0xc0874910d52d3051L.
1312:     CP = 0.9617966939259756, // Long bits 0x3feec709dc3a03fdL.
1313:     CP_H = 0.9617967009544373, // Long bits 0x3feec709e0000000L.
1314:     CP_L = -7.028461650952758e-9, // Long bits 0xbe3e2fe0145b01f5L.
1315:     LN2 = 0.6931471805599453, // Long bits 0x3fe62e42fefa39efL.
1316:     LN2_H = 0.6931471803691238, // Long bits 0x3fe62e42fee00000L.
1317:     LN2_L = 1.9082149292705877e-10, // Long bits 0x3dea39ef35793c76L.
1318:     INV_LN2 = 1.4426950408889634, // Long bits 0x3ff71547652b82feL.
1319:     INV_LN2_H = 1.4426950216293335, // Long bits 0x3ff7154760000000L.
1320:     INV_LN2_L = 1.9259629911266175e-8; // Long bits 0x3e54ae0bf85ddf44L.
1321: 
1322:   /**
1323:    * Constants for computing {@link #log(double)}.
1324:    */
1325:   private static final double
1326:     LG1 = 0.6666666666666735, // Long bits 0x3fe5555555555593L.
1327:     LG2 = 0.3999999999940942, // Long bits 0x3fd999999997fa04L.
1328:     LG3 = 0.2857142874366239, // Long bits 0x3fd2492494229359L.
1329:     LG4 = 0.22222198432149784, // Long bits 0x3fcc71c51d8e78afL.
1330:     LG5 = 0.1818357216161805, // Long bits 0x3fc7466496cb03deL.
1331:     LG6 = 0.15313837699209373, // Long bits 0x3fc39a09d078c69fL.
1332:     LG7 = 0.14798198605116586; // Long bits 0x3fc2f112df3e5244L.
1333: 
1334:   /**
1335:    * Constants for computing {@link #pow(double, double)}. L and P are
1336:    * coefficients for series; OVT is -(1024-log2(ovfl+.5ulp)); and DP is ???.
1337:    * The P coefficients also calculate {@link #exp(double)}.
1338:    */
1339:   private static final double
1340:     L1 = 0.5999999999999946, // Long bits 0x3fe3333333333303L.
1341:     L2 = 0.4285714285785502, // Long bits 0x3fdb6db6db6fabffL.
1342:     L3 = 0.33333332981837743, // Long bits 0x3fd55555518f264dL.
1343:     L4 = 0.272728123808534, // Long bits 0x3fd17460a91d4101L.
1344:     L5 = 0.23066074577556175, // Long bits 0x3fcd864a93c9db65L.
1345:     L6 = 0.20697501780033842, // Long bits 0x3fca7e284a454eefL.
1346:     P1 = 0.16666666666666602, // Long bits 0x3fc555555555553eL.
1347:     P2 = -2.7777777777015593e-3, // Long bits 0xbf66c16c16bebd93L.
1348:     P3 = 6.613756321437934e-5, // Long bits 0x3f11566aaf25de2cL.
1349:     P4 = -1.6533902205465252e-6, // Long bits 0xbebbbd41c5d26bf1L.
1350:     P5 = 4.1381367970572385e-8, // Long bits 0x3e66376972bea4d0L.
1351:     DP_H = 0.5849624872207642, // Long bits 0x3fe2b80340000000L.
1352:     DP_L = 1.350039202129749e-8, // Long bits 0x3e4cfdeb43cfd006L.
1353:     OVT = 8.008566259537294e-17; // Long bits 0x3c971547652b82feL.
1354: 
1355:   /**
1356:    * Coefficients for computing {@link #sin(double)}.
1357:    */
1358:   private static final double
1359:     S1 = -0.16666666666666632, // Long bits 0xbfc5555555555549L.
1360:     S2 = 8.33333333332249e-3, // Long bits 0x3f8111111110f8a6L.
1361:     S3 = -1.984126982985795e-4, // Long bits 0xbf2a01a019c161d5L.
1362:     S4 = 2.7557313707070068e-6, // Long bits 0x3ec71de357b1fe7dL.
1363:     S5 = -2.5050760253406863e-8, // Long bits 0xbe5ae5e68a2b9cebL.
1364:     S6 = 1.58969099521155e-10; // Long bits 0x3de5d93a5acfd57cL.
1365: 
1366:   /**
1367:    * Coefficients for computing {@link #cos(double)}.
1368:    */
1369:   private static final double
1370:     C1 = 0.0416666666666666, // Long bits 0x3fa555555555554cL.
1371:     C2 = -1.388888888887411e-3, // Long bits 0xbf56c16c16c15177L.
1372:     C3 = 2.480158728947673e-5, // Long bits 0x3efa01a019cb1590L.
1373:     C4 = -2.7557314351390663e-7, // Long bits 0xbe927e4f809c52adL.
1374:     C5 = 2.087572321298175e-9, // Long bits 0x3e21ee9ebdb4b1c4L.
1375:     C6 = -1.1359647557788195e-11; // Long bits 0xbda8fae9be8838d4L.
1376: 
1377:   /**
1378:    * Coefficients for computing {@link #tan(double)}.
1379:    */
1380:   private static final double
1381:     T0 = 0.3333333333333341, // Long bits 0x3fd5555555555563L.
1382:     T1 = 0.13333333333320124, // Long bits 0x3fc111111110fe7aL.
1383:     T2 = 0.05396825397622605, // Long bits 0x3faba1ba1bb341feL.
1384:     T3 = 0.021869488294859542, // Long bits 0x3f9664f48406d637L.
1385:     T4 = 8.8632398235993e-3, // Long bits 0x3f8226e3e96e8493L.
1386:     T5 = 3.5920791075913124e-3, // Long bits 0x3f6d6d22c9560328L.
1387:     T6 = 1.4562094543252903e-3, // Long bits 0x3f57dbc8fee08315L.
1388:     T7 = 5.880412408202641e-4, // Long bits 0x3f4344d8f2f26501L.
1389:     T8 = 2.464631348184699e-4, // Long bits 0x3f3026f71a8d1068L.
1390:     T9 = 7.817944429395571e-5, // Long bits 0x3f147e88a03792a6L.
1391:     T10 = 7.140724913826082e-5, // Long bits 0x3f12b80f32f0a7e9L.
1392:     T11 = -1.8558637485527546e-5, // Long bits 0xbef375cbdb605373L.
1393:     T12 = 2.590730518636337e-5; // Long bits 0x3efb2a7074bf7ad4L.
1394: 
1395:   /**
1396:    * Coefficients for computing {@link #asin(double)} and
1397:    * {@link #acos(double)}.
1398:    */
1399:   private static final double
1400:     PS0 = 0.16666666666666666, // Long bits 0x3fc5555555555555L.
1401:     PS1 = -0.3255658186224009, // Long bits 0xbfd4d61203eb6f7dL.
1402:     PS2 = 0.20121253213486293, // Long bits 0x3fc9c1550e884455L.
1403:     PS3 = -0.04005553450067941, // Long bits 0xbfa48228b5688f3bL.
1404:     PS4 = 7.915349942898145e-4, // Long bits 0x3f49efe07501b288L.
1405:     PS5 = 3.479331075960212e-5, // Long bits 0x3f023de10dfdf709L.
1406:     QS1 = -2.403394911734414, // Long bits 0xc0033a271c8a2d4bL.
1407:     QS2 = 2.0209457602335057, // Long bits 0x40002ae59c598ac8L.
1408:     QS3 = -0.6882839716054533, // Long bits 0xbfe6066c1b8d0159L.
1409:     QS4 = 0.07703815055590194; // Long bits 0x3fb3b8c5b12e9282L.
1410: 
1411:   /**
1412:    * Coefficients for computing {@link #atan(double)}.
1413:    */
1414:   private static final double
1415:     ATAN_0_5H = 0.4636476090008061, // Long bits 0x3fddac670561bb4fL.
1416:     ATAN_0_5L = 2.2698777452961687e-17, // Long bits 0x3c7a2b7f222f65e2L.
1417:     ATAN_1_5H = 0.982793723247329, // Long bits 0x3fef730bd281f69bL.
1418:     ATAN_1_5L = 1.3903311031230998e-17, // Long bits 0x3c7007887af0cbbdL.
1419:     AT0 = 0.3333333333333293, // Long bits 0x3fd555555555550dL.
1420:     AT1 = -0.19999999999876483, // Long bits 0xbfc999999998ebc4L.
1421:     AT2 = 0.14285714272503466, // Long bits 0x3fc24924920083ffL.
1422:     AT3 = -0.11111110405462356, // Long bits 0xbfbc71c6fe231671L.
1423:     AT4 = 0.09090887133436507, // Long bits 0x3fb745cdc54c206eL.
1424:     AT5 = -0.0769187620504483, // Long bits 0xbfb3b0f2af749a6dL.
1425:     AT6 = 0.06661073137387531, // Long bits 0x3fb10d66a0d03d51L.
1426:     AT7 = -0.058335701337905735, // Long bits 0xbfadde2d52defd9aL.
1427:     AT8 = 0.049768779946159324, // Long bits 0x3fa97b4b24760debL.
1428:     AT9 = -0.036531572744216916, // Long bits 0xbfa2b4442c6a6c2fL.
1429:     AT10 = 0.016285820115365782; // Long bits 0x3f90ad3ae322da11L.
1430: 
1431:   /**
1432:    * Helper function for reducing an angle to a multiple of pi/2 within
1433:    * [-pi/4, pi/4].
1434:    *
1435:    * @param x the angle; not infinity or NaN, and outside pi/4
1436:    * @param y an array of 2 doubles modified to hold the remander x % pi/2
1437:    * @return the quadrant of the result, mod 4: 0: [-pi/4, pi/4],
1438:    *         1: [pi/4, 3*pi/4], 2: [3*pi/4, 5*pi/4], 3: [-3*pi/4, -pi/4]
1439:    */
1440:   private static int remPiOver2(double x, double[] y)
1441:   {
1442:     boolean negative = x < 0;
1443:     x = abs(x);
1444:     double z;
1445:     int n;
1446:     if (Configuration.DEBUG && (x <= PI / 4 || x != x
1447:                                 || x == Double.POSITIVE_INFINITY))
1448:       throw new InternalError("Assertion failure");
1449:     if (x < 3 * PI / 4) // If |x| is small.
1450:       {
1451:         z = x - PIO2_1;
1452:         if ((float) x != (float) (PI / 2)) // 33+53 bit pi is good enough.
1453:           {
1454:             y[0] = z - PIO2_1L;
1455:             y[1] = z - y[0] - PIO2_1L;
1456:           }
1457:         else // Near pi/2, use 33+33+53 bit pi.
1458:           {
1459:             z -= PIO2_2;
1460:             y[0] = z - PIO2_2L;
1461:             y[1] = z - y[0] - PIO2_2L;
1462:           }
1463:         n = 1;
1464:       }
1465:     else if (x <= TWO_20 * PI / 2) // Medium size.
1466:       {
1467:         n = (int) (2 / PI * x + 0.5);
1468:         z = x - n * PIO2_1;
1469:         double w = n * PIO2_1L; // First round good to 85 bits.
1470:         y[0] = z - w;
1471:         if (n >= 32 || (float) x == (float) (w))
1472:           {
1473:             if (x / y[0] >= TWO_16) // Second iteration, good to 118 bits.
1474:               {
1475:                 double t = z;
1476:                 w = n * PIO2_2;
1477:                 z = t - w;
1478:                 w = n * PIO2_2L - (t - z - w);
1479:                 y[0] = z - w;
1480:                 if (x / y[0] >= TWO_49) // Third iteration, 151 bits accuracy.
1481:                   {
1482:                     t = z;
1483:                     w = n * PIO2_3;
1484:                     z = t - w;
1485:                     w = n * PIO2_3L - (t - z - w);
1486:                     y[0] = z - w;
1487:                   }
1488:               }
1489:           }
1490:         y[1] = z - y[0] - w;
1491:       }
1492:     else
1493:       {
1494:         // All other (large) arguments.
1495:         int e0 = (int) (Double.doubleToLongBits(x) >> 52) - 1046;
1496:         z = scale(x, -e0); // e0 = ilogb(z) - 23.
1497:         double[] tx = new double[3];
1498:         for (int i = 0; i < 2; i++)
1499:           {
1500:             tx[i] = (int) z;
1501:             z = (z - tx[i]) * TWO_24;
1502:           }
1503:         tx[2] = z;
1504:         int nx = 2;
1505:         while (tx[nx] == 0)
1506:           nx--;
1507:         n = remPiOver2(tx, y, e0, nx);
1508:       }
1509:     if (negative)
1510:       {
1511:         y[0] = -y[0];
1512:         y[1] = -y[1];
1513:         return -n;
1514:       }
1515:     return n;
1516:   }
1517: 
1518:   /**
1519:    * Helper function for reducing an angle to a multiple of pi/2 within
1520:    * [-pi/4, pi/4].
1521:    *
1522:    * @param x the positive angle, broken into 24-bit chunks
1523:    * @param y an array of 2 doubles modified to hold the remander x % pi/2
1524:    * @param e0 the exponent of x[0]
1525:    * @param nx the last index used in x
1526:    * @return the quadrant of the result, mod 4: 0: [-pi/4, pi/4],
1527:    *         1: [pi/4, 3*pi/4], 2: [3*pi/4, 5*pi/4], 3: [-3*pi/4, -pi/4]
1528:    */
1529:   private static int remPiOver2(double[] x, double[] y, int e0, int nx)
1530:   {
1531:     int i;
1532:     int ih;
1533:     int n;
1534:     double fw;
1535:     double z;
1536:     int[] iq = new int[20];
1537:     double[] f = new double[20];
1538:     double[] q = new double[20];
1539:     boolean recompute = false;
1540: 
1541:     // Initialize jk, jz, jv, q0; note that 3>q0.
1542:     int jk = 4;
1543:     int jz = jk;
1544:     int jv = max((e0 - 3) / 24, 0);
1545:     int q0 = e0 - 24 * (jv + 1);
1546: 
1547:     // Set up f[0] to f[nx+jk] where f[nx+jk] = TWO_OVER_PI[jv+jk].
1548:     int j = jv - nx;
1549:     int m = nx + jk;
1550:     for (i = 0; i <= m; i++, j++)
1551:       f[i] = (j < 0) ? 0 : TWO_OVER_PI[j];
1552: 
1553:     // Compute q[0],q[1],...q[jk].
1554:     for (i = 0; i <= jk; i++)
1555:       {
1556:         for (j = 0, fw = 0; j <= nx; j++)
1557:           fw += x[j] * f[nx + i - j];
1558:         q[i] = fw;
1559:       }
1560: 
1561:     do
1562:       {
1563:         // Distill q[] into iq[] reversingly.
1564:         for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--)
1565:           {
1566:             fw = (int) (1 / TWO_24 * z);
1567:             iq[i] = (int) (z - TWO_24 * fw);
1568:             z = q[j - 1] + fw;
1569:           }
1570: 
1571:         // Compute n.
1572:         z = scale(z, q0);
1573:         z -= 8 * floor(z * 0.125); // Trim off integer >= 8.
1574:         n = (int) z;
1575:         z -= n;
1576:         ih = 0;
1577:         if (q0 > 0) // Need iq[jz-1] to determine n.
1578:           {
1579:             i = iq[jz - 1] >> (24 - q0);
1580:             n += i;
1581:             iq[jz - 1] -= i << (24 - q0);
1582:             ih = iq[jz - 1] >> (23 - q0);
1583:           }
1584:         else if (q0 == 0)
1585:           ih = iq[jz - 1] >> 23;
1586:         else if (z >= 0.5)
1587:           ih = 2;
1588: 
1589:         if (ih > 0) // If q > 0.5.
1590:           {
1591:             n += 1;
1592:             int carry = 0;
1593:             for (i = 0; i < jz; i++) // Compute 1-q.
1594:               {
1595:                 j = iq[i];
1596:                 if (carry == 0)
1597:                   {
1598:                     if (j != 0)
1599:                       {
1600:                         carry = 1;
1601:                         iq[i] = 0x1000000 - j;
1602:                       }
1603:                   }
1604:                 else
1605:                   iq[i] = 0xffffff - j;
1606:               }
1607:             switch (q0)
1608:               {
1609:               case 1: // Rare case: chance is 1 in 12 for non-default.
1610:                 iq[jz - 1] &= 0x7fffff;
1611:                 break;
1612:               case 2:
1613:                 iq[jz - 1] &= 0x3fffff;
1614:               }
1615:             if (ih == 2)
1616:               {
1617:                 z = 1 - z;
1618:                 if (carry != 0)
1619:                   z -= scale(1, q0);
1620:               }
1621:           }
1622: 
1623:         // Check if recomputation is needed.
1624:         if (z == 0)
1625:           {
1626:             j = 0;
1627:             for (i = jz - 1; i >= jk; i--)
1628:               j |= iq[i];
1629:             if (j == 0) // Need recomputation.
1630:               {
1631:                 int k;
1632:                 for (k = 1; iq[jk - k] == 0; k++); // k = no. of terms needed.
1633: 
1634:                 for (i = jz + 1; i <= jz + k; i++) // Add q[jz+1] to q[jz+k].
1635:                   {
1636:                     f[nx + i] = TWO_OVER_PI[jv + i];
1637:                     for (j = 0, fw = 0; j <= nx; j++)
1638:                       fw += x[j] * f[nx + i - j];
1639:                     q[i] = fw;
1640:                   }
1641:                 jz += k;
1642:                 recompute = true;
1643:               }
1644:           }
1645:       }
1646:     while (recompute);
1647: 
1648:     // Chop off zero terms.
1649:     if (z == 0)
1650:       {
1651:         jz--;
1652:         q0 -= 24;
1653:         while (iq[jz] == 0)
1654:           {
1655:             jz--;
1656:             q0 -= 24;
1657:           }
1658:       }
1659:     else // Break z into 24-bit if necessary.
1660:       {
1661:         z = scale(z, -q0);
1662:         if (z >= TWO_24)
1663:           {
1664:             fw = (int) (1 / TWO_24 * z);
1665:             iq[jz] = (int) (z - TWO_24 * fw);
1666:             jz++;
1667:             q0 += 24;
1668:             iq[jz] = (int) fw;
1669:           }
1670:         else
1671:           iq[jz] = (int) z;
1672:       }
1673: 
1674:     // Convert integer "bit" chunk to floating-point value.
1675:     fw = scale(1, q0);
1676:     for (i = jz; i >= 0; i--)
1677:       {
1678:         q[i] = fw * iq[i];
1679:         fw *= 1 / TWO_24;
1680:       }
1681: 
1682:     // Compute PI_OVER_TWO[0,...,jk]*q[jz,...,0].
1683:     double[] fq = new double[20];
1684:     for (i = jz; i >= 0; i--)
1685:       {
1686:         fw = 0;
1687:         for (int k = 0; k <= jk && k <= jz - i; k++)
1688:           fw += PI_OVER_TWO[k] * q[i + k];
1689:         fq[jz - i] = fw;
1690:       }
1691: 
1692:     // Compress fq[] into y[].
1693:     fw = 0;
1694:     for (i = jz; i >= 0; i--)
1695:       fw += fq[i];
1696:     y[0] = (ih == 0) ? fw : -fw;
1697:     fw = fq[0] - fw;
1698:     for (i = 1; i <= jz; i++)
1699:       fw += fq[i];
1700:     y[1] = (ih == 0) ? fw : -fw;
1701:     return n;
1702:   }
1703: 
1704:   /**
1705:    * Helper method for scaling a double by a power of 2.
1706:    *
1707:    * @param x the double
1708:    * @param n the scale; |n| < 2048
1709:    * @return x * 2**n
1710:    */
1711:   private static double scale(double x, int n)
1712:   {
1713:     if (Configuration.DEBUG && abs(n) >= 2048)
1714:       throw new InternalError("Assertion failure");
1715:     if (x == 0 || x == Double.NEGATIVE_INFINITY
1716:         || ! (x < Double.POSITIVE_INFINITY) || n == 0)
1717:       return x;
1718:     long bits = Double.doubleToLongBits(x);
1719:     int exp = (int) (bits >> 52) & 0x7ff;
1720:     if (exp == 0) // Subnormal x.
1721:       {
1722:         x *= TWO_54;
1723:         exp = ((int) (Double.doubleToLongBits(x) >> 52) & 0x7ff) - 54;
1724:       }
1725:     exp += n;
1726:     if (exp > 0x7fe) // Overflow.
1727:       return Double.POSITIVE_INFINITY * x;
1728:     if (exp > 0) // Normal.
1729:       return Double.longBitsToDouble((bits & 0x800fffffffffffffL)
1730:                                      | ((long) exp << 52));
1731:     if (exp <= -54)
1732:       return 0 * x; // Underflow.
1733:     exp += 54; // Subnormal result.
1734:     x = Double.longBitsToDouble((bits & 0x800fffffffffffffL)
1735:                                 | ((long) exp << 52));
1736:     return x * (1 / TWO_54);
1737:   }
1738: 
1739:   /**
1740:    * Helper trig function; computes sin in range [-pi/4, pi/4].
1741:    *
1742:    * @param x angle within about pi/4
1743:    * @param y tail of x, created by remPiOver2
1744:    * @return sin(x+y)
1745:    */
1746:   private static double sin(double x, double y)
1747:   {
1748:     if (Configuration.DEBUG && abs(x + y) > 0.7854)
1749:       throw new InternalError("Assertion failure");
1750:     if (abs(x) < 1 / TWO_27)
1751:       return x;  // If |x| ~< 2**-27, already know answer.
1752: 
1753:     double z = x * x;
1754:     double v = z * x;
1755:     double r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
1756:     if (y == 0)
1757:       return x + v * (S1 + z * r);
1758:     return x - ((z * (0.5 * y - v * r) - y) - v * S1);
1759:   }
1760: 
1761:   /**
1762:    * Helper trig function; computes cos in range [-pi/4, pi/4].
1763:    *
1764:    * @param x angle within about pi/4
1765:    * @param y tail of x, created by remPiOver2
1766:    * @return cos(x+y)
1767:    */
1768:   private static double cos(double x, double y)
1769:   {
1770:     if (Configuration.DEBUG && abs(x + y) > 0.7854)
1771:       throw new InternalError("Assertion failure");
1772:     x = abs(x);
1773:     if (x < 1 / TWO_27)
1774:       return 1;  // If |x| ~< 2**-27, already know answer.
1775: 
1776:     double z = x * x;
1777:     double r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
1778: 
1779:     if (x < 0.3)
1780:       return 1 - (0.5 * z - (z * r - x * y));
1781: 
1782:     double qx = (x > 0.78125) ? 0.28125 : (x * 0.25);
1783:     return 1 - qx - ((0.5 * z - qx) - (z * r - x * y));
1784:   }
1785: 
1786:   /**
1787:    * Helper trig function; computes tan in range [-pi/4, pi/4].
1788:    *
1789:    * @param x angle within about pi/4
1790:    * @param y tail of x, created by remPiOver2
1791:    * @param invert true iff -1/tan should be returned instead
1792:    * @return tan(x+y)
1793:    */
1794:   private static double tan(double x, double y, boolean invert)
1795:   {
1796:     // PI/2 is irrational, so no double is a perfect multiple of it.
1797:     if (Configuration.DEBUG && (abs(x + y) > 0.7854 || (x == 0 && invert)))
1798:       throw new InternalError("Assertion failure");
1799:     boolean negative = x < 0;
1800:     if (negative)
1801:       {
1802:         x = -x;
1803:         y = -y;
1804:       }
1805:     if (x < 1 / TWO_28) // If |x| ~< 2**-28, already know answer.
1806:       return (negative ? -1 : 1) * (invert ? -1 / x : x);
1807: 
1808:     double z;
1809:     double w;
1810:     boolean large = x >= 0.6744;
1811:     if (large)
1812:       {
1813:         z = PI / 4 - x;
1814:         w = PI_L / 4 - y;
1815:         x = z + w;
1816:         y = 0;
1817:       }
1818:     z = x * x;
1819:     w = z * z;
1820:     // Break x**5*(T1+x**2*T2+...) into
1821:     //   x**5(T1+x**4*T3+...+x**20*T11)
1822:     // + x**5(x**2*(T2+x**4*T4+...+x**22*T12)).
1823:     double r = T1 + w * (T3 + w * (T5 + w * (T7 + w * (T9 + w * T11))));
1824:     double v = z * (T2 + w * (T4 + w * (T6 + w * (T8 + w * (T10 + w * T12)))));
1825:     double s = z * x;
1826:     r = y + z * (s * (r + v) + y);
1827:     r += T0 * s;
1828:     w = x + r;
1829:     if (large)
1830:       {
1831:         v = invert ? -1 : 1;
1832:         return (negative ? -1 : 1) * (v - 2 * (x - (w * w / (w + v) - r)));
1833:       }
1834:     if (! invert)
1835:       return w;
1836: 
1837:     // Compute -1.0/(x+r) accurately.
1838:     z = (float) w;
1839:     v = r - (z - x);
1840:     double a = -1 / w;
1841:     double t = (float) a;
1842:     return t + a * (1 + t * z + t * v);
1843:   }
1844: }