1 /*
   2  * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.math.BigDecimal;
  29 import java.util.Random;
  30 import jdk.internal.math.FloatConsts;
  31 import jdk.internal.math.DoubleConsts;
  32 import jdk.internal.HotSpotIntrinsicCandidate;
  33 
  34 /**
  35  * The class {@code Math} contains methods for performing basic
  36  * numeric operations such as the elementary exponential, logarithm,
  37  * square root, and trigonometric functions.
  38  *
  39  * <p>Unlike some of the numeric methods of class
  40  * {@code StrictMath}, all implementations of the equivalent
  41  * functions of class {@code Math} are not defined to return the
  42  * bit-for-bit same results.  This relaxation permits
  43  * better-performing implementations where strict reproducibility is
  44  * not required.
  45  *
  46  * <p>By default many of the {@code Math} methods simply call
  47  * the equivalent method in {@code StrictMath} for their
  48  * implementation.  Code generators are encouraged to use
  49  * platform-specific native libraries or microprocessor instructions,
  50  * where available, to provide higher-performance implementations of
  51  * {@code Math} methods.  Such higher-performance
  52  * implementations still must conform to the specification for
  53  * {@code Math}.
  54  *
  55  * <p>The quality of implementation specifications concern two
  56  * properties, accuracy of the returned result and monotonicity of the
  57  * method.  Accuracy of the floating-point {@code Math} methods is
  58  * measured in terms of <i>ulps</i>, units in the last place.  For a
  59  * given floating-point format, an {@linkplain #ulp(double) ulp} of a
  60  * specific real number value is the distance between the two
  61  * floating-point values bracketing that numerical value.  When
  62  * discussing the accuracy of a method as a whole rather than at a
  63  * specific argument, the number of ulps cited is for the worst-case
  64  * error at any argument.  If a method always has an error less than
  65  * 0.5 ulps, the method always returns the floating-point number
  66  * nearest the exact result; such a method is <i>correctly
  67  * rounded</i>.  A correctly rounded method is generally the best a
  68  * floating-point approximation can be; however, it is impractical for
  69  * many floating-point methods to be correctly rounded.  Instead, for
  70  * the {@code Math} class, a larger error bound of 1 or 2 ulps is
  71  * allowed for certain methods.  Informally, with a 1 ulp error bound,
  72  * when the exact result is a representable number, the exact result
  73  * should be returned as the computed result; otherwise, either of the
  74  * two floating-point values which bracket the exact result may be
  75  * returned.  For exact results large in magnitude, one of the
  76  * endpoints of the bracket may be infinite.  Besides accuracy at
  77  * individual arguments, maintaining proper relations between the
  78  * method at different arguments is also important.  Therefore, most
  79  * methods with more than 0.5 ulp errors are required to be
  80  * <i>semi-monotonic</i>: whenever the mathematical function is
  81  * non-decreasing, so is the floating-point approximation, likewise,
  82  * whenever the mathematical function is non-increasing, so is the
  83  * floating-point approximation.  Not all approximations that have 1
  84  * ulp accuracy will automatically meet the monotonicity requirements.
  85  *
  86  * <p>
  87  * The platform uses signed two's complement integer arithmetic with
  88  * int and long primitive types.  The developer should choose
  89  * the primitive type to ensure that arithmetic operations consistently
  90  * produce correct results, which in some cases means the operations
  91  * will not overflow the range of values of the computation.
  92  * The best practice is to choose the primitive type and algorithm to avoid
  93  * overflow. In cases where the size is {@code int} or {@code long} and
  94  * overflow errors need to be detected, the methods {@code addExact},
  95  * {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact}
  96  * throw an {@code ArithmeticException} when the results overflow.
  97  * For other arithmetic operations such as divide, absolute value,
  98  * increment by one, decrement by one, and negation, overflow occurs only with
  99  * a specific minimum or maximum value and should be checked against
 100  * the minimum or maximum as appropriate.
 101  *
 102  * @author  unascribed
 103  * @author  Joseph D. Darcy
 104  * @since   1.0
 105  */
 106 
 107 public final class Math {
 108 
 109     /**
 110      * Don't let anyone instantiate this class.
 111      */
 112     private Math() {}
 113 
 114     /**
 115      * The {@code double} value that is closer than any other to
 116      * <i>e</i>, the base of the natural logarithms.
 117      */
 118     public static final double E = 2.7182818284590452354;
 119 
 120     /**
 121      * The {@code double} value that is closer than any other to
 122      * <i>pi</i>, the ratio of the circumference of a circle to its
 123      * diameter.
 124      */
 125     public static final double PI = 3.14159265358979323846;
 126 
 127     /**
 128      * Constant by which to multiply an angular value in degrees to obtain an
 129      * angular value in radians.
 130      */
 131     private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
 132 
 133     /**
 134      * Constant by which to multiply an angular value in radians to obtain an
 135      * angular value in degrees.
 136      */
 137     private static final double RADIANS_TO_DEGREES = 57.29577951308232;
 138 
 139     /**
 140      * Returns the trigonometric sine of an angle.  Special cases:
 141      * <ul><li>If the argument is NaN or an infinity, then the
 142      * result is NaN.
 143      * <li>If the argument is zero, then the result is a zero with the
 144      * same sign as the argument.</ul>
 145      *
 146      * <p>The computed result must be within 1 ulp of the exact result.
 147      * Results must be semi-monotonic.
 148      *
 149      * @param   a   an angle, in radians.
 150      * @return  the sine of the argument.
 151      */
 152     @HotSpotIntrinsicCandidate
 153     public static double sin(double a) {
 154         return StrictMath.sin(a); // default impl. delegates to StrictMath
 155     }
 156 
 157     /**
 158      * Returns the trigonometric cosine of an angle. Special cases:
 159      * <ul><li>If the argument is NaN or an infinity, then the
 160      * result is NaN.</ul>
 161      *
 162      * <p>The computed result must be within 1 ulp of the exact result.
 163      * Results must be semi-monotonic.
 164      *
 165      * @param   a   an angle, in radians.
 166      * @return  the cosine of the argument.
 167      */
 168     @HotSpotIntrinsicCandidate
 169     public static double cos(double a) {
 170         return StrictMath.cos(a); // default impl. delegates to StrictMath
 171     }
 172 
 173     /**
 174      * Returns the trigonometric tangent of an angle.  Special cases:
 175      * <ul><li>If the argument is NaN or an infinity, then the result
 176      * is NaN.
 177      * <li>If the argument is zero, then the result is a zero with the
 178      * same sign as the argument.</ul>
 179      *
 180      * <p>The computed result must be within 1 ulp of the exact result.
 181      * Results must be semi-monotonic.
 182      *
 183      * @param   a   an angle, in radians.
 184      * @return  the tangent of the argument.
 185      */
 186     @HotSpotIntrinsicCandidate
 187     public static double tan(double a) {
 188         return StrictMath.tan(a); // default impl. delegates to StrictMath
 189     }
 190 
 191     /**
 192      * Returns the arc sine of a value; the returned angle is in the
 193      * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
 194      * <ul><li>If the argument is NaN or its absolute value is greater
 195      * than 1, then the result is NaN.
 196      * <li>If the argument is zero, then the result is a zero with the
 197      * same sign as the argument.</ul>
 198      *
 199      * <p>The computed result must be within 1 ulp of the exact result.
 200      * Results must be semi-monotonic.
 201      *
 202      * @param   a   the value whose arc sine is to be returned.
 203      * @return  the arc sine of the argument.
 204      */
 205     public static double asin(double a) {
 206         return StrictMath.asin(a); // default impl. delegates to StrictMath
 207     }
 208 
 209     /**
 210      * Returns the arc cosine of a value; the returned angle is in the
 211      * range 0.0 through <i>pi</i>.  Special case:
 212      * <ul><li>If the argument is NaN or its absolute value is greater
 213      * than 1, then the result is NaN.</ul>
 214      *
 215      * <p>The computed result must be within 1 ulp of the exact result.
 216      * Results must be semi-monotonic.
 217      *
 218      * @param   a   the value whose arc cosine is to be returned.
 219      * @return  the arc cosine of the argument.
 220      */
 221     public static double acos(double a) {
 222         return StrictMath.acos(a); // default impl. delegates to StrictMath
 223     }
 224 
 225     /**
 226      * Returns the arc tangent of a value; the returned angle is in the
 227      * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
 228      * <ul><li>If the argument is NaN, then the result is NaN.
 229      * <li>If the argument is zero, then the result is a zero with the
 230      * same sign as the argument.</ul>
 231      *
 232      * <p>The computed result must be within 1 ulp of the exact result.
 233      * Results must be semi-monotonic.
 234      *
 235      * @param   a   the value whose arc tangent is to be returned.
 236      * @return  the arc tangent of the argument.
 237      */
 238     public static double atan(double a) {
 239         return StrictMath.atan(a); // default impl. delegates to StrictMath
 240     }
 241 
 242     /**
 243      * Converts an angle measured in degrees to an approximately
 244      * equivalent angle measured in radians.  The conversion from
 245      * degrees to radians is generally inexact.
 246      *
 247      * @param   angdeg   an angle, in degrees
 248      * @return  the measurement of the angle {@code angdeg}
 249      *          in radians.
 250      * @since   1.2
 251      */
 252     public static double toRadians(double angdeg) {
 253         return angdeg * DEGREES_TO_RADIANS;
 254     }
 255 
 256     /**
 257      * Converts an angle measured in radians to an approximately
 258      * equivalent angle measured in degrees.  The conversion from
 259      * radians to degrees is generally inexact; users should
 260      * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
 261      * equal {@code 0.0}.
 262      *
 263      * @param   angrad   an angle, in radians
 264      * @return  the measurement of the angle {@code angrad}
 265      *          in degrees.
 266      * @since   1.2
 267      */
 268     public static double toDegrees(double angrad) {
 269         return angrad * RADIANS_TO_DEGREES;
 270     }
 271 
 272     /**
 273      * Returns Euler's number <i>e</i> raised to the power of a
 274      * {@code double} value.  Special cases:
 275      * <ul><li>If the argument is NaN, the result is NaN.
 276      * <li>If the argument is positive infinity, then the result is
 277      * positive infinity.
 278      * <li>If the argument is negative infinity, then the result is
 279      * positive zero.</ul>
 280      *
 281      * <p>The computed result must be within 1 ulp of the exact result.
 282      * Results must be semi-monotonic.
 283      *
 284      * @param   a   the exponent to raise <i>e</i> to.
 285      * @return  the value <i>e</i><sup>{@code a}</sup>,
 286      *          where <i>e</i> is the base of the natural logarithms.
 287      */
 288     @HotSpotIntrinsicCandidate
 289     public static double exp(double a) {
 290         return StrictMath.exp(a); // default impl. delegates to StrictMath
 291     }
 292 
 293     /**
 294      * Returns the natural logarithm (base <i>e</i>) of a {@code double}
 295      * value.  Special cases:
 296      * <ul><li>If the argument is NaN or less than zero, then the result
 297      * is NaN.
 298      * <li>If the argument is positive infinity, then the result is
 299      * positive infinity.
 300      * <li>If the argument is positive zero or negative zero, then the
 301      * result is negative infinity.</ul>
 302      *
 303      * <p>The computed result must be within 1 ulp of the exact result.
 304      * Results must be semi-monotonic.
 305      *
 306      * @param   a   a value
 307      * @return  the value ln&nbsp;{@code a}, the natural logarithm of
 308      *          {@code a}.
 309      */
 310     @HotSpotIntrinsicCandidate
 311     public static double log(double a) {
 312         return StrictMath.log(a); // default impl. delegates to StrictMath
 313     }
 314 
 315     /**
 316      * Returns the base 10 logarithm of a {@code double} value.
 317      * Special cases:
 318      *
 319      * <ul><li>If the argument is NaN or less than zero, then the result
 320      * is NaN.
 321      * <li>If the argument is positive infinity, then the result is
 322      * positive infinity.
 323      * <li>If the argument is positive zero or negative zero, then the
 324      * result is negative infinity.
 325      * <li> If the argument is equal to 10<sup><i>n</i></sup> for
 326      * integer <i>n</i>, then the result is <i>n</i>.
 327      * </ul>
 328      *
 329      * <p>The computed result must be within 1 ulp of the exact result.
 330      * Results must be semi-monotonic.
 331      *
 332      * @param   a   a value
 333      * @return  the base 10 logarithm of  {@code a}.
 334      * @since 1.5
 335      */
 336     @HotSpotIntrinsicCandidate
 337     public static double log10(double a) {
 338         return StrictMath.log10(a); // default impl. delegates to StrictMath
 339     }
 340 
 341     /**
 342      * Returns the correctly rounded positive square root of a
 343      * {@code double} value.
 344      * Special cases:
 345      * <ul><li>If the argument is NaN or less than zero, then the result
 346      * is NaN.
 347      * <li>If the argument is positive infinity, then the result is positive
 348      * infinity.
 349      * <li>If the argument is positive zero or negative zero, then the
 350      * result is the same as the argument.</ul>
 351      * Otherwise, the result is the {@code double} value closest to
 352      * the true mathematical square root of the argument value.
 353      *
 354      * @param   a   a value.
 355      * @return  the positive square root of {@code a}.
 356      *          If the argument is NaN or less than zero, the result is NaN.
 357      */
 358     @HotSpotIntrinsicCandidate
 359     public static double sqrt(double a) {
 360         return StrictMath.sqrt(a); // default impl. delegates to StrictMath
 361                                    // Note that hardware sqrt instructions
 362                                    // frequently can be directly used by JITs
 363                                    // and should be much faster than doing
 364                                    // Math.sqrt in software.
 365     }
 366 
 367 
 368     /**
 369      * Returns the cube root of a {@code double} value.  For
 370      * positive finite {@code x}, {@code cbrt(-x) ==
 371      * -cbrt(x)}; that is, the cube root of a negative value is
 372      * the negative of the cube root of that value's magnitude.
 373      *
 374      * Special cases:
 375      *
 376      * <ul>
 377      *
 378      * <li>If the argument is NaN, then the result is NaN.
 379      *
 380      * <li>If the argument is infinite, then the result is an infinity
 381      * with the same sign as the argument.
 382      *
 383      * <li>If the argument is zero, then the result is a zero with the
 384      * same sign as the argument.
 385      *
 386      * </ul>
 387      *
 388      * <p>The computed result must be within 1 ulp of the exact result.
 389      *
 390      * @param   a   a value.
 391      * @return  the cube root of {@code a}.
 392      * @since 1.5
 393      */
 394     public static double cbrt(double a) {
 395         return StrictMath.cbrt(a);
 396     }
 397 
 398     /**
 399      * Computes the remainder operation on two arguments as prescribed
 400      * by the IEEE 754 standard.
 401      * The remainder value is mathematically equal to
 402      * <code>f1&nbsp;-&nbsp;f2</code>&nbsp;&times;&nbsp;<i>n</i>,
 403      * where <i>n</i> is the mathematical integer closest to the exact
 404      * mathematical value of the quotient {@code f1/f2}, and if two
 405      * mathematical integers are equally close to {@code f1/f2},
 406      * then <i>n</i> is the integer that is even. If the remainder is
 407      * zero, its sign is the same as the sign of the first argument.
 408      * Special cases:
 409      * <ul><li>If either argument is NaN, or the first argument is infinite,
 410      * or the second argument is positive zero or negative zero, then the
 411      * result is NaN.
 412      * <li>If the first argument is finite and the second argument is
 413      * infinite, then the result is the same as the first argument.</ul>
 414      *
 415      * @param   f1   the dividend.
 416      * @param   f2   the divisor.
 417      * @return  the remainder when {@code f1} is divided by
 418      *          {@code f2}.
 419      */
 420     public static double IEEEremainder(double f1, double f2) {
 421         return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
 422     }
 423 
 424     /**
 425      * Returns the smallest (closest to negative infinity)
 426      * {@code double} value that is greater than or equal to the
 427      * argument and is equal to a mathematical integer. Special cases:
 428      * <ul><li>If the argument value is already equal to a
 429      * mathematical integer, then the result is the same as the
 430      * argument.  <li>If the argument is NaN or an infinity or
 431      * positive zero or negative zero, then the result is the same as
 432      * the argument.  <li>If the argument value is less than zero but
 433      * greater than -1.0, then the result is negative zero.</ul> Note
 434      * that the value of {@code Math.ceil(x)} is exactly the
 435      * value of {@code -Math.floor(-x)}.
 436      *
 437      *
 438      * @param   a   a value.
 439      * @return  the smallest (closest to negative infinity)
 440      *          floating-point value that is greater than or equal to
 441      *          the argument and is equal to a mathematical integer.
 442      */
 443     public static double ceil(double a) {
 444         return StrictMath.ceil(a); // default impl. delegates to StrictMath
 445     }
 446 
 447     /**
 448      * Returns the largest (closest to positive infinity)
 449      * {@code double} value that is less than or equal to the
 450      * argument and is equal to a mathematical integer. Special cases:
 451      * <ul><li>If the argument value is already equal to a
 452      * mathematical integer, then the result is the same as the
 453      * argument.  <li>If the argument is NaN or an infinity or
 454      * positive zero or negative zero, then the result is the same as
 455      * the argument.</ul>
 456      *
 457      * @param   a   a value.
 458      * @return  the largest (closest to positive infinity)
 459      *          floating-point value that less than or equal to the argument
 460      *          and is equal to a mathematical integer.
 461      */
 462     public static double floor(double a) {
 463         return StrictMath.floor(a); // default impl. delegates to StrictMath
 464     }
 465 
 466     /**
 467      * Returns the {@code double} value that is closest in value
 468      * to the argument and is equal to a mathematical integer. If two
 469      * {@code double} values that are mathematical integers are
 470      * equally close, the result is the integer value that is
 471      * even. Special cases:
 472      * <ul><li>If the argument value is already equal to a mathematical
 473      * integer, then the result is the same as the argument.
 474      * <li>If the argument is NaN or an infinity or positive zero or negative
 475      * zero, then the result is the same as the argument.</ul>
 476      *
 477      * @param   a   a {@code double} value.
 478      * @return  the closest floating-point value to {@code a} that is
 479      *          equal to a mathematical integer.
 480      */
 481     public static double rint(double a) {
 482         return StrictMath.rint(a); // default impl. delegates to StrictMath
 483     }
 484 
 485     /**
 486      * Returns the angle <i>theta</i> from the conversion of rectangular
 487      * coordinates ({@code x},&nbsp;{@code y}) to polar
 488      * coordinates (r,&nbsp;<i>theta</i>).
 489      * This method computes the phase <i>theta</i> by computing an arc tangent
 490      * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
 491      * cases:
 492      * <ul><li>If either argument is NaN, then the result is NaN.
 493      * <li>If the first argument is positive zero and the second argument
 494      * is positive, or the first argument is positive and finite and the
 495      * second argument is positive infinity, then the result is positive
 496      * zero.
 497      * <li>If the first argument is negative zero and the second argument
 498      * is positive, or the first argument is negative and finite and the
 499      * second argument is positive infinity, then the result is negative zero.
 500      * <li>If the first argument is positive zero and the second argument
 501      * is negative, or the first argument is positive and finite and the
 502      * second argument is negative infinity, then the result is the
 503      * {@code double} value closest to <i>pi</i>.
 504      * <li>If the first argument is negative zero and the second argument
 505      * is negative, or the first argument is negative and finite and the
 506      * second argument is negative infinity, then the result is the
 507      * {@code double} value closest to -<i>pi</i>.
 508      * <li>If the first argument is positive and the second argument is
 509      * positive zero or negative zero, or the first argument is positive
 510      * infinity and the second argument is finite, then the result is the
 511      * {@code double} value closest to <i>pi</i>/2.
 512      * <li>If the first argument is negative and the second argument is
 513      * positive zero or negative zero, or the first argument is negative
 514      * infinity and the second argument is finite, then the result is the
 515      * {@code double} value closest to -<i>pi</i>/2.
 516      * <li>If both arguments are positive infinity, then the result is the
 517      * {@code double} value closest to <i>pi</i>/4.
 518      * <li>If the first argument is positive infinity and the second argument
 519      * is negative infinity, then the result is the {@code double}
 520      * value closest to 3*<i>pi</i>/4.
 521      * <li>If the first argument is negative infinity and the second argument
 522      * is positive infinity, then the result is the {@code double} value
 523      * closest to -<i>pi</i>/4.
 524      * <li>If both arguments are negative infinity, then the result is the
 525      * {@code double} value closest to -3*<i>pi</i>/4.</ul>
 526      *
 527      * <p>The computed result must be within 2 ulps of the exact result.
 528      * Results must be semi-monotonic.
 529      *
 530      * @param   y   the ordinate coordinate
 531      * @param   x   the abscissa coordinate
 532      * @return  the <i>theta</i> component of the point
 533      *          (<i>r</i>,&nbsp;<i>theta</i>)
 534      *          in polar coordinates that corresponds to the point
 535      *          (<i>x</i>,&nbsp;<i>y</i>) in Cartesian coordinates.
 536      */
 537     @HotSpotIntrinsicCandidate
 538     public static double atan2(double y, double x) {
 539         return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
 540     }
 541 
 542     /**
 543      * Returns the value of the first argument raised to the power of the
 544      * second argument. Special cases:
 545      *
 546      * <ul><li>If the second argument is positive or negative zero, then the
 547      * result is 1.0.
 548      * <li>If the second argument is 1.0, then the result is the same as the
 549      * first argument.
 550      * <li>If the second argument is NaN, then the result is NaN.
 551      * <li>If the first argument is NaN and the second argument is nonzero,
 552      * then the result is NaN.
 553      *
 554      * <li>If
 555      * <ul>
 556      * <li>the absolute value of the first argument is greater than 1
 557      * and the second argument is positive infinity, or
 558      * <li>the absolute value of the first argument is less than 1 and
 559      * the second argument is negative infinity,
 560      * </ul>
 561      * then the result is positive infinity.
 562      *
 563      * <li>If
 564      * <ul>
 565      * <li>the absolute value of the first argument is greater than 1 and
 566      * the second argument is negative infinity, or
 567      * <li>the absolute value of the
 568      * first argument is less than 1 and the second argument is positive
 569      * infinity,
 570      * </ul>
 571      * then the result is positive zero.
 572      *
 573      * <li>If the absolute value of the first argument equals 1 and the
 574      * second argument is infinite, then the result is NaN.
 575      *
 576      * <li>If
 577      * <ul>
 578      * <li>the first argument is positive zero and the second argument
 579      * is greater than zero, or
 580      * <li>the first argument is positive infinity and the second
 581      * argument is less than zero,
 582      * </ul>
 583      * then the result is positive zero.
 584      *
 585      * <li>If
 586      * <ul>
 587      * <li>the first argument is positive zero and the second argument
 588      * is less than zero, or
 589      * <li>the first argument is positive infinity and the second
 590      * argument is greater than zero,
 591      * </ul>
 592      * then the result is positive infinity.
 593      *
 594      * <li>If
 595      * <ul>
 596      * <li>the first argument is negative zero and the second argument
 597      * is greater than zero but not a finite odd integer, or
 598      * <li>the first argument is negative infinity and the second
 599      * argument is less than zero but not a finite odd integer,
 600      * </ul>
 601      * then the result is positive zero.
 602      *
 603      * <li>If
 604      * <ul>
 605      * <li>the first argument is negative zero and the second argument
 606      * is a positive finite odd integer, or
 607      * <li>the first argument is negative infinity and the second
 608      * argument is a negative finite odd integer,
 609      * </ul>
 610      * then the result is negative zero.
 611      *
 612      * <li>If
 613      * <ul>
 614      * <li>the first argument is negative zero and the second argument
 615      * is less than zero but not a finite odd integer, or
 616      * <li>the first argument is negative infinity and the second
 617      * argument is greater than zero but not a finite odd integer,
 618      * </ul>
 619      * then the result is positive infinity.
 620      *
 621      * <li>If
 622      * <ul>
 623      * <li>the first argument is negative zero and the second argument
 624      * is a negative finite odd integer, or
 625      * <li>the first argument is negative infinity and the second
 626      * argument is a positive finite odd integer,
 627      * </ul>
 628      * then the result is negative infinity.
 629      *
 630      * <li>If the first argument is finite and less than zero
 631      * <ul>
 632      * <li> if the second argument is a finite even integer, the
 633      * result is equal to the result of raising the absolute value of
 634      * the first argument to the power of the second argument
 635      *
 636      * <li>if the second argument is a finite odd integer, the result
 637      * is equal to the negative of the result of raising the absolute
 638      * value of the first argument to the power of the second
 639      * argument
 640      *
 641      * <li>if the second argument is finite and not an integer, then
 642      * the result is NaN.
 643      * </ul>
 644      *
 645      * <li>If both arguments are integers, then the result is exactly equal
 646      * to the mathematical result of raising the first argument to the power
 647      * of the second argument if that result can in fact be represented
 648      * exactly as a {@code double} value.</ul>
 649      *
 650      * <p>(In the foregoing descriptions, a floating-point value is
 651      * considered to be an integer if and only if it is finite and a
 652      * fixed point of the method {@link #ceil ceil} or,
 653      * equivalently, a fixed point of the method {@link #floor
 654      * floor}. A value is a fixed point of a one-argument
 655      * method if and only if the result of applying the method to the
 656      * value is equal to the value.)
 657      *
 658      * <p>The computed result must be within 1 ulp of the exact result.
 659      * Results must be semi-monotonic.
 660      *
 661      * @param   a   the base.
 662      * @param   b   the exponent.
 663      * @return  the value {@code a}<sup>{@code b}</sup>.
 664      */
 665     @HotSpotIntrinsicCandidate
 666     public static double pow(double a, double b) {
 667         return StrictMath.pow(a, b); // default impl. delegates to StrictMath
 668     }
 669 
 670     /**
 671      * Returns the closest {@code int} to the argument, with ties
 672      * rounding to positive infinity.
 673      *
 674      * <p>
 675      * Special cases:
 676      * <ul><li>If the argument is NaN, the result is 0.
 677      * <li>If the argument is negative infinity or any value less than or
 678      * equal to the value of {@code Integer.MIN_VALUE}, the result is
 679      * equal to the value of {@code Integer.MIN_VALUE}.
 680      * <li>If the argument is positive infinity or any value greater than or
 681      * equal to the value of {@code Integer.MAX_VALUE}, the result is
 682      * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 683      *
 684      * @param   a   a floating-point value to be rounded to an integer.
 685      * @return  the value of the argument rounded to the nearest
 686      *          {@code int} value.
 687      * @see     java.lang.Integer#MAX_VALUE
 688      * @see     java.lang.Integer#MIN_VALUE
 689      */
 690     public static int round(float a) {
 691         int intBits = Float.floatToRawIntBits(a);
 692         int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
 693                 >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
 694         int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
 695                 + FloatConsts.EXP_BIAS) - biasedExp;
 696         if ((shift & -32) == 0) { // shift >= 0 && shift < 32
 697             // a is a finite number such that pow(2,-32) <= ulp(a) < 1
 698             int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
 699                     | (FloatConsts.SIGNIF_BIT_MASK + 1));
 700             if (intBits < 0) {
 701                 r = -r;
 702             }
 703             // In the comments below each Java expression evaluates to the value
 704             // the corresponding mathematical expression:
 705             // (r) evaluates to a / ulp(a)
 706             // (r >> shift) evaluates to floor(a * 2)
 707             // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
 708             // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
 709             return ((r >> shift) + 1) >> 1;
 710         } else {
 711             // a is either
 712             // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
 713             // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
 714             // - an infinity or NaN
 715             return (int) a;
 716         }
 717     }
 718 
 719     /**
 720      * Returns the closest {@code long} to the argument, with ties
 721      * rounding to positive infinity.
 722      *
 723      * <p>Special cases:
 724      * <ul><li>If the argument is NaN, the result is 0.
 725      * <li>If the argument is negative infinity or any value less than or
 726      * equal to the value of {@code Long.MIN_VALUE}, the result is
 727      * equal to the value of {@code Long.MIN_VALUE}.
 728      * <li>If the argument is positive infinity or any value greater than or
 729      * equal to the value of {@code Long.MAX_VALUE}, the result is
 730      * equal to the value of {@code Long.MAX_VALUE}.</ul>
 731      *
 732      * @param   a   a floating-point value to be rounded to a
 733      *          {@code long}.
 734      * @return  the value of the argument rounded to the nearest
 735      *          {@code long} value.
 736      * @see     java.lang.Long#MAX_VALUE
 737      * @see     java.lang.Long#MIN_VALUE
 738      */
 739     public static long round(double a) {
 740         long longBits = Double.doubleToRawLongBits(a);
 741         long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
 742                 >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
 743         long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
 744                 + DoubleConsts.EXP_BIAS) - biasedExp;
 745         if ((shift & -64) == 0) { // shift >= 0 && shift < 64
 746             // a is a finite number such that pow(2,-64) <= ulp(a) < 1
 747             long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
 748                     | (DoubleConsts.SIGNIF_BIT_MASK + 1));
 749             if (longBits < 0) {
 750                 r = -r;
 751             }
 752             // In the comments below each Java expression evaluates to the value
 753             // the corresponding mathematical expression:
 754             // (r) evaluates to a / ulp(a)
 755             // (r >> shift) evaluates to floor(a * 2)
 756             // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
 757             // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
 758             return ((r >> shift) + 1) >> 1;
 759         } else {
 760             // a is either
 761             // - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
 762             // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
 763             // - an infinity or NaN
 764             return (long) a;
 765         }
 766     }
 767 
 768     private static final class RandomNumberGeneratorHolder {
 769         static final Random randomNumberGenerator = new Random();
 770     }
 771 
 772     /**
 773      * Returns a {@code double} value with a positive sign, greater
 774      * than or equal to {@code 0.0} and less than {@code 1.0}.
 775      * Returned values are chosen pseudorandomly with (approximately)
 776      * uniform distribution from that range.
 777      *
 778      * <p>When this method is first called, it creates a single new
 779      * pseudorandom-number generator, exactly as if by the expression
 780      *
 781      * <blockquote>{@code new java.util.Random()}</blockquote>
 782      *
 783      * This new pseudorandom-number generator is used thereafter for
 784      * all calls to this method and is used nowhere else.
 785      *
 786      * <p>This method is properly synchronized to allow correct use by
 787      * more than one thread. However, if many threads need to generate
 788      * pseudorandom numbers at a great rate, it may reduce contention
 789      * for each thread to have its own pseudorandom-number generator.
 790      *
 791      * @apiNote
 792      * As the largest {@code double} value less than {@code 1.0}
 793      * is {@code Math.nextDown(1.0)}, a value {@code x} in the closed range
 794      * {@code [x1,x2]} where {@code x1<=x2} may be defined by the statements
 795      *
 796      * <blockquote><pre>{@code
 797      * double f = Math.random()/Math.nextDown(1.0);
 798      * double x = x1*(1.0 - f) + x2*f;
 799      * }</pre></blockquote>
 800      *
 801      * @return  a pseudorandom {@code double} greater than or equal
 802      * to {@code 0.0} and less than {@code 1.0}.
 803      * @see #nextDown(double)
 804      * @see Random#nextDouble()
 805      */
 806     public static double random() {
 807         return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
 808     }
 809 
 810     /**
 811      * Returns the sum of its arguments,
 812      * throwing an exception if the result overflows an {@code int}.
 813      *
 814      * @param x the first value
 815      * @param y the second value
 816      * @return the result
 817      * @throws ArithmeticException if the result overflows an int
 818      * @since 1.8
 819      */
 820     @HotSpotIntrinsicCandidate
 821     public static int addExact(int x, int y) {
 822         int r = x + y;
 823         // HD 2-12 Overflow iff both arguments have the opposite sign of the result
 824         if (((x ^ r) & (y ^ r)) < 0) {
 825             throw new ArithmeticException("integer overflow");
 826         }
 827         return r;
 828     }
 829 
 830     /**
 831      * Returns the sum of its arguments,
 832      * throwing an exception if the result overflows a {@code long}.
 833      *
 834      * @param x the first value
 835      * @param y the second value
 836      * @return the result
 837      * @throws ArithmeticException if the result overflows a long
 838      * @since 1.8
 839      */
 840     @HotSpotIntrinsicCandidate
 841     public static long addExact(long x, long y) {
 842         long r = x + y;
 843         // HD 2-12 Overflow iff both arguments have the opposite sign of the result
 844         if (((x ^ r) & (y ^ r)) < 0) {
 845             throw new ArithmeticException("long overflow");
 846         }
 847         return r;
 848     }
 849 
 850     /**
 851      * Returns the difference of the arguments,
 852      * throwing an exception if the result overflows an {@code int}.
 853      *
 854      * @param x the first value
 855      * @param y the second value to subtract from the first
 856      * @return the result
 857      * @throws ArithmeticException if the result overflows an int
 858      * @since 1.8
 859      */
 860     @HotSpotIntrinsicCandidate
 861     public static int subtractExact(int x, int y) {
 862         int r = x - y;
 863         // HD 2-12 Overflow iff the arguments have different signs and
 864         // the sign of the result is different from the sign of x
 865         if (((x ^ y) & (x ^ r)) < 0) {
 866             throw new ArithmeticException("integer overflow");
 867         }
 868         return r;
 869     }
 870 
 871     /**
 872      * Returns the difference of the arguments,
 873      * throwing an exception if the result overflows a {@code long}.
 874      *
 875      * @param x the first value
 876      * @param y the second value to subtract from the first
 877      * @return the result
 878      * @throws ArithmeticException if the result overflows a long
 879      * @since 1.8
 880      */
 881     @HotSpotIntrinsicCandidate
 882     public static long subtractExact(long x, long y) {
 883         long r = x - y;
 884         // HD 2-12 Overflow iff the arguments have different signs and
 885         // the sign of the result is different from the sign of x
 886         if (((x ^ y) & (x ^ r)) < 0) {
 887             throw new ArithmeticException("long overflow");
 888         }
 889         return r;
 890     }
 891 
 892     /**
 893      * Returns the product of the arguments,
 894      * throwing an exception if the result overflows an {@code int}.
 895      *
 896      * @param x the first value
 897      * @param y the second value
 898      * @return the result
 899      * @throws ArithmeticException if the result overflows an int
 900      * @since 1.8
 901      */
 902     @HotSpotIntrinsicCandidate
 903     public static int multiplyExact(int x, int y) {
 904         long r = (long)x * (long)y;
 905         if ((int)r != r) {
 906             throw new ArithmeticException("integer overflow");
 907         }
 908         return (int)r;
 909     }
 910 
 911     /**
 912      * Returns the product of the arguments, throwing an exception if the result
 913      * overflows a {@code long}.
 914      *
 915      * @param x the first value
 916      * @param y the second value
 917      * @return the result
 918      * @throws ArithmeticException if the result overflows a long
 919      * @since 9
 920      */
 921     public static long multiplyExact(long x, int y) {
 922         return multiplyExact(x, (long)y);
 923     }
 924 
 925     /**
 926      * Returns the product of the arguments,
 927      * throwing an exception if the result overflows a {@code long}.
 928      *
 929      * @param x the first value
 930      * @param y the second value
 931      * @return the result
 932      * @throws ArithmeticException if the result overflows a long
 933      * @since 1.8
 934      */
 935     @HotSpotIntrinsicCandidate
 936     public static long multiplyExact(long x, long y) {
 937         long r = x * y;
 938         long ax = Math.abs(x);
 939         long ay = Math.abs(y);
 940         if (((ax | ay) >>> 31 != 0)) {
 941             // Some bits greater than 2^31 that might cause overflow
 942             // Check the result using the divide operator
 943             // and check for the special case of Long.MIN_VALUE * -1
 944            if (((y != 0) && (r / y != x)) ||
 945                (x == Long.MIN_VALUE && y == -1)) {
 946                 throw new ArithmeticException("long overflow");
 947             }
 948         }
 949         return r;
 950     }
 951 
 952     /**
 953      * Returns the argument incremented by one, throwing an exception if the
 954      * result overflows an {@code int}.
 955      *
 956      * @param a the value to increment
 957      * @return the result
 958      * @throws ArithmeticException if the result overflows an int
 959      * @since 1.8
 960      */
 961     @HotSpotIntrinsicCandidate
 962     public static int incrementExact(int a) {
 963         if (a == Integer.MAX_VALUE) {
 964             throw new ArithmeticException("integer overflow");
 965         }
 966 
 967         return a + 1;
 968     }
 969 
 970     /**
 971      * Returns the argument incremented by one, throwing an exception if the
 972      * result overflows a {@code long}.
 973      *
 974      * @param a the value to increment
 975      * @return the result
 976      * @throws ArithmeticException if the result overflows a long
 977      * @since 1.8
 978      */
 979     @HotSpotIntrinsicCandidate
 980     public static long incrementExact(long a) {
 981         if (a == Long.MAX_VALUE) {
 982             throw new ArithmeticException("long overflow");
 983         }
 984 
 985         return a + 1L;
 986     }
 987 
 988     /**
 989      * Returns the argument decremented by one, throwing an exception if the
 990      * result overflows an {@code int}.
 991      *
 992      * @param a the value to decrement
 993      * @return the result
 994      * @throws ArithmeticException if the result overflows an int
 995      * @since 1.8
 996      */
 997     @HotSpotIntrinsicCandidate
 998     public static int decrementExact(int a) {
 999         if (a == Integer.MIN_VALUE) {
1000             throw new ArithmeticException("integer overflow");
1001         }
1002 
1003         return a - 1;
1004     }
1005 
1006     /**
1007      * Returns the argument decremented by one, throwing an exception if the
1008      * result overflows a {@code long}.
1009      *
1010      * @param a the value to decrement
1011      * @return the result
1012      * @throws ArithmeticException if the result overflows a long
1013      * @since 1.8
1014      */
1015     @HotSpotIntrinsicCandidate
1016     public static long decrementExact(long a) {
1017         if (a == Long.MIN_VALUE) {
1018             throw new ArithmeticException("long overflow");
1019         }
1020 
1021         return a - 1L;
1022     }
1023 
1024     /**
1025      * Returns the negation of the argument, throwing an exception if the
1026      * result overflows an {@code int}.
1027      *
1028      * @param a the value to negate
1029      * @return the result
1030      * @throws ArithmeticException if the result overflows an int
1031      * @since 1.8
1032      */
1033     @HotSpotIntrinsicCandidate
1034     public static int negateExact(int a) {
1035         if (a == Integer.MIN_VALUE) {
1036             throw new ArithmeticException("integer overflow");
1037         }
1038 
1039         return -a;
1040     }
1041 
1042     /**
1043      * Returns the negation of the argument, throwing an exception if the
1044      * result overflows a {@code long}.
1045      *
1046      * @param a the value to negate
1047      * @return the result
1048      * @throws ArithmeticException if the result overflows a long
1049      * @since 1.8
1050      */
1051     @HotSpotIntrinsicCandidate
1052     public static long negateExact(long a) {
1053         if (a == Long.MIN_VALUE) {
1054             throw new ArithmeticException("long overflow");
1055         }
1056 
1057         return -a;
1058     }
1059 
1060     /**
1061      * Returns the value of the {@code long} argument;
1062      * throwing an exception if the value overflows an {@code int}.
1063      *
1064      * @param value the long value
1065      * @return the argument as an int
1066      * @throws ArithmeticException if the {@code argument} overflows an int
1067      * @since 1.8
1068      */
1069     public static int toIntExact(long value) {
1070         if ((int)value != value) {
1071             throw new ArithmeticException("integer overflow");
1072         }
1073         return (int)value;
1074     }
1075 
1076     /**
1077      * Returns the exact mathematical product of the arguments.
1078      *
1079      * @param x the first value
1080      * @param y the second value
1081      * @return the result
1082      * @since 9
1083      */
1084     public static long multiplyFull(int x, int y) {
1085         return (long)x * (long)y;
1086     }
1087 
1088     /**
1089      * Returns as a {@code long} the most significant 64 bits of the 128-bit
1090      * product of two 64-bit factors.
1091      *
1092      * @param x the first value
1093      * @param y the second value
1094      * @return the result
1095      * @since 9
1096      */
1097     @HotSpotIntrinsicCandidate
1098     public static long multiplyHigh(long x, long y) {
1099         if (x < 0 || y < 0) {
1100             // Use technique from section 8-2 of Henry S. Warren, Jr.,
1101             // Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 173-174.
1102             long x1 = x >> 32;
1103             long x2 = x & 0xFFFFFFFFL;
1104             long y1 = y >> 32;
1105             long y2 = y & 0xFFFFFFFFL;
1106             long z2 = x2 * y2;
1107             long t = x1 * y2 + (z2 >>> 32);
1108             long z1 = t & 0xFFFFFFFFL;
1109             long z0 = t >> 32;
1110             z1 += x2 * y1;
1111             return x1 * y1 + z0 + (z1 >> 32);
1112         } else {
1113             // Use Karatsuba technique with two base 2^32 digits.
1114             long x1 = x >>> 32;
1115             long y1 = y >>> 32;
1116             long x2 = x & 0xFFFFFFFFL;
1117             long y2 = y & 0xFFFFFFFFL;
1118             long A = x1 * y1;
1119             long B = x2 * y2;
1120             long C = (x1 + x2) * (y1 + y2);
1121             long K = C - A - B;
1122             return (((B >>> 32) + K) >>> 32) + A;
1123         }
1124     }
1125 
1126     /**
1127      * Returns the largest (closest to positive infinity)
1128      * {@code int} value that is less than or equal to the algebraic quotient.
1129      * There is one special case, if the dividend is the
1130      * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
1131      * then integer overflow occurs and
1132      * the result is equal to {@code Integer.MIN_VALUE}.
1133      * <p>
1134      * Normal integer division operates under the round to zero rounding mode
1135      * (truncation).  This operation instead acts under the round toward
1136      * negative infinity (floor) rounding mode.
1137      * The floor rounding mode gives different results from truncation
1138      * when the exact result is negative.
1139      * <ul>
1140      *   <li>If the signs of the arguments are the same, the results of
1141      *       {@code floorDiv} and the {@code /} operator are the same.  <br>
1142      *       For example, {@code floorDiv(4, 3) == 1} and {@code (4 / 3) == 1}.</li>
1143      *   <li>If the signs of the arguments are different,  the quotient is negative and
1144      *       {@code floorDiv} returns the integer less than or equal to the quotient
1145      *       and the {@code /} operator returns the integer closest to zero.<br>
1146      *       For example, {@code floorDiv(-4, 3) == -2},
1147      *       whereas {@code (-4 / 3) == -1}.
1148      *   </li>
1149      * </ul>
1150      *
1151      * @param x the dividend
1152      * @param y the divisor
1153      * @return the largest (closest to positive infinity)
1154      * {@code int} value that is less than or equal to the algebraic quotient.
1155      * @throws ArithmeticException if the divisor {@code y} is zero
1156      * @see #floorMod(int, int)
1157      * @see #floor(double)
1158      * @since 1.8
1159      */
1160     public static int floorDiv(int x, int y) {
1161         int r = x / y;
1162         // if the signs are different and modulo not zero, round down
1163         if ((x ^ y) < 0 && (r * y != x)) {
1164             r--;
1165         }
1166         return r;
1167     }
1168 
1169     /**
1170      * Returns the largest (closest to positive infinity)
1171      * {@code long} value that is less than or equal to the algebraic quotient.
1172      * There is one special case, if the dividend is the
1173      * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
1174      * then integer overflow occurs and
1175      * the result is equal to {@code Long.MIN_VALUE}.
1176      * <p>
1177      * Normal integer division operates under the round to zero rounding mode
1178      * (truncation).  This operation instead acts under the round toward
1179      * negative infinity (floor) rounding mode.
1180      * The floor rounding mode gives different results from truncation
1181      * when the exact result is negative.
1182      * <p>
1183      * For examples, see {@link #floorDiv(int, int)}.
1184      *
1185      * @param x the dividend
1186      * @param y the divisor
1187      * @return the largest (closest to positive infinity)
1188      * {@code int} value that is less than or equal to the algebraic quotient.
1189      * @throws ArithmeticException if the divisor {@code y} is zero
1190      * @see #floorMod(long, int)
1191      * @see #floor(double)
1192      * @since 9
1193      */
1194     public static long floorDiv(long x, int y) {
1195         return floorDiv(x, (long)y);
1196     }
1197 
1198     /**
1199      * Returns the largest (closest to positive infinity)
1200      * {@code long} value that is less than or equal to the algebraic quotient.
1201      * There is one special case, if the dividend is the
1202      * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
1203      * then integer overflow occurs and
1204      * the result is equal to {@code Long.MIN_VALUE}.
1205      * <p>
1206      * Normal integer division operates under the round to zero rounding mode
1207      * (truncation).  This operation instead acts under the round toward
1208      * negative infinity (floor) rounding mode.
1209      * The floor rounding mode gives different results from truncation
1210      * when the exact result is negative.
1211      * <p>
1212      * For examples, see {@link #floorDiv(int, int)}.
1213      *
1214      * @param x the dividend
1215      * @param y the divisor
1216      * @return the largest (closest to positive infinity)
1217      * {@code long} value that is less than or equal to the algebraic quotient.
1218      * @throws ArithmeticException if the divisor {@code y} is zero
1219      * @see #floorMod(long, long)
1220      * @see #floor(double)
1221      * @since 1.8
1222      */
1223     public static long floorDiv(long x, long y) {
1224         long r = x / y;
1225         // if the signs are different and modulo not zero, round down
1226         if ((x ^ y) < 0 && (r * y != x)) {
1227             r--;
1228         }
1229         return r;
1230     }
1231 
1232     /**
1233      * Returns the floor modulus of the {@code int} arguments.
1234      * <p>
1235      * The floor modulus is {@code x - (floorDiv(x, y) * y)},
1236      * has the same sign as the divisor {@code y}, and
1237      * is in the range of {@code -abs(y) < r < +abs(y)}.
1238      *
1239      * <p>
1240      * The relationship between {@code floorDiv} and {@code floorMod} is such that:
1241      * <ul>
1242      *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1243      * </ul>
1244      * <p>
1245      * The difference in values between {@code floorMod} and
1246      * the {@code %} operator is due to the difference between
1247      * {@code floorDiv} that returns the integer less than or equal to the quotient
1248      * and the {@code /} operator that returns the integer closest to zero.
1249      * <p>
1250      * Examples:
1251      * <ul>
1252      *   <li>If the signs of the arguments are the same, the results
1253      *       of {@code floorMod} and the {@code %} operator are the same.  <br>
1254      *       <ul>
1255      *       <li>{@code floorMod(4, 3) == 1}; &nbsp; and {@code (4 % 3) == 1}</li>
1256      *       </ul>
1257      *   <li>If the signs of the arguments are different, the results differ from the {@code %} operator.<br>
1258      *      <ul>
1259      *      <li>{@code floorMod(+4, -3) == -2}; &nbsp; and {@code (+4 % -3) == +1} </li>
1260      *      <li>{@code floorMod(-4, +3) == +2}; &nbsp; and {@code (-4 % +3) == -1} </li>
1261      *      <li>{@code floorMod(-4, -3) == -1}; &nbsp; and {@code (-4 % -3) == -1 } </li>
1262      *      </ul>
1263      *   </li>
1264      * </ul>
1265      * <p>
1266      * If the signs of arguments are unknown and a positive modulus
1267      * is needed it can be computed as {@code (floorMod(x, y) + abs(y)) % abs(y)}.
1268      *
1269      * @param x the dividend
1270      * @param y the divisor
1271      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
1272      * @throws ArithmeticException if the divisor {@code y} is zero
1273      * @see #floorDiv(int, int)
1274      * @since 1.8
1275      */
1276     public static int floorMod(int x, int y) {
1277         return x - floorDiv(x, y) * y;
1278     }
1279 
1280     /**
1281      * Returns the floor modulus of the {@code long} and {@code int} arguments.
1282      * <p>
1283      * The floor modulus is {@code x - (floorDiv(x, y) * y)},
1284      * has the same sign as the divisor {@code y}, and
1285      * is in the range of {@code -abs(y) < r < +abs(y)}.
1286      *
1287      * <p>
1288      * The relationship between {@code floorDiv} and {@code floorMod} is such that:
1289      * <ul>
1290      *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1291      * </ul>
1292      * <p>
1293      * For examples, see {@link #floorMod(int, int)}.
1294      *
1295      * @param x the dividend
1296      * @param y the divisor
1297      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
1298      * @throws ArithmeticException if the divisor {@code y} is zero
1299      * @see #floorDiv(long, int)
1300      * @since 9
1301      */
1302     public static int floorMod(long x, int y) {
1303         // Result cannot overflow the range of int.
1304         return (int)(x - floorDiv(x, y) * y);
1305     }
1306 
1307     /**
1308      * Returns the floor modulus of the {@code long} arguments.
1309      * <p>
1310      * The floor modulus is {@code x - (floorDiv(x, y) * y)},
1311      * has the same sign as the divisor {@code y}, and
1312      * is in the range of {@code -abs(y) < r < +abs(y)}.
1313      *
1314      * <p>
1315      * The relationship between {@code floorDiv} and {@code floorMod} is such that:
1316      * <ul>
1317      *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
1318      * </ul>
1319      * <p>
1320      * For examples, see {@link #floorMod(int, int)}.
1321      *
1322      * @param x the dividend
1323      * @param y the divisor
1324      * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
1325      * @throws ArithmeticException if the divisor {@code y} is zero
1326      * @see #floorDiv(long, long)
1327      * @since 1.8
1328      */
1329     public static long floorMod(long x, long y) {
1330         return x - floorDiv(x, y) * y;
1331     }
1332 
1333     /**
1334      * Returns the absolute value of an {@code int} value.
1335      * If the argument is not negative, the argument is returned.
1336      * If the argument is negative, the negation of the argument is returned.
1337      *
1338      * <p>Note that if the argument is equal to the value of
1339      * {@link Integer#MIN_VALUE}, the most negative representable
1340      * {@code int} value, the result is that same value, which is
1341      * negative.
1342      *
1343      * @param   a   the argument whose absolute value is to be determined
1344      * @return  the absolute value of the argument.
1345      */
1346     @HotSpotIntrinsicCandidate
1347     public static int abs(int a) {
1348         return (a < 0) ? -a : a;
1349     }
1350 
1351     /**
1352      * Returns the absolute value of a {@code long} value.
1353      * If the argument is not negative, the argument is returned.
1354      * If the argument is negative, the negation of the argument is returned.
1355      *
1356      * <p>Note that if the argument is equal to the value of
1357      * {@link Long#MIN_VALUE}, the most negative representable
1358      * {@code long} value, the result is that same value, which
1359      * is negative.
1360      *
1361      * @param   a   the argument whose absolute value is to be determined
1362      * @return  the absolute value of the argument.
1363      */
1364     @HotSpotIntrinsicCandidate
1365     public static long abs(long a) {
1366         return (a < 0) ? -a : a;
1367     }
1368 
1369     /**
1370      * Returns the absolute value of a {@code float} value.
1371      * If the argument is not negative, the argument is returned.
1372      * If the argument is negative, the negation of the argument is returned.
1373      * Special cases:
1374      * <ul><li>If the argument is positive zero or negative zero, the
1375      * result is positive zero.
1376      * <li>If the argument is infinite, the result is positive infinity.
1377      * <li>If the argument is NaN, the result is NaN.</ul>
1378      *
1379      * @apiNote As implied by the above, one valid implementation of
1380      * this method is given by the expression below which computes a
1381      * {@code float} with the same exponent and significand as the
1382      * argument but with a guaranteed zero sign bit indicating a
1383      * positive value:<br>
1384      * {@code Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))}
1385      *
1386      * @param   a   the argument whose absolute value is to be determined
1387      * @return  the absolute value of the argument.
1388      */
1389     @HotSpotIntrinsicCandidate
1390     public static float abs(float a) {
1391         return (a <= 0.0F) ? 0.0F - a : a;
1392     }
1393 
1394     /**
1395      * Returns the absolute value of a {@code double} value.
1396      * If the argument is not negative, the argument is returned.
1397      * If the argument is negative, the negation of the argument is returned.
1398      * Special cases:
1399      * <ul><li>If the argument is positive zero or negative zero, the result
1400      * is positive zero.
1401      * <li>If the argument is infinite, the result is positive infinity.
1402      * <li>If the argument is NaN, the result is NaN.</ul>
1403      *
1404      * @apiNote As implied by the above, one valid implementation of
1405      * this method is given by the expression below which computes a
1406      * {@code double} with the same exponent and significand as the
1407      * argument but with a guaranteed zero sign bit indicating a
1408      * positive value:<br>
1409      * {@code Double.longBitsToDouble((Double.doubleToRawLongBits(a)<<1)>>>1)}
1410      *
1411      * @param   a   the argument whose absolute value is to be determined
1412      * @return  the absolute value of the argument.
1413      */
1414     @HotSpotIntrinsicCandidate
1415     public static double abs(double a) {
1416         return (a <= 0.0D) ? 0.0D - a : a;
1417     }
1418 
1419     /**
1420      * Returns the greater of two {@code int} values. That is, the
1421      * result is the argument closer to the value of
1422      * {@link Integer#MAX_VALUE}. If the arguments have the same value,
1423      * the result is that same value.
1424      *
1425      * @param   a   an argument.
1426      * @param   b   another argument.
1427      * @return  the larger of {@code a} and {@code b}.
1428      */
1429     @HotSpotIntrinsicCandidate
1430     public static int max(int a, int b) {
1431         return (a >= b) ? a : b;
1432     }
1433 
1434     /**
1435      * Returns the greater of two {@code long} values. That is, the
1436      * result is the argument closer to the value of
1437      * {@link Long#MAX_VALUE}. If the arguments have the same value,
1438      * the result is that same value.
1439      *
1440      * @param   a   an argument.
1441      * @param   b   another argument.
1442      * @return  the larger of {@code a} and {@code b}.
1443      */
1444     public static long max(long a, long b) {
1445         return (a >= b) ? a : b;
1446     }
1447 
1448     // Use raw bit-wise conversions on guaranteed non-NaN arguments.
1449     private static final long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
1450     private static final long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
1451 
1452     /**
1453      * Returns the greater of two {@code float} values.  That is,
1454      * the result is the argument closer to positive infinity. If the
1455      * arguments have the same value, the result is that same
1456      * value. If either value is NaN, then the result is NaN.  Unlike
1457      * the numerical comparison operators, this method considers
1458      * negative zero to be strictly smaller than positive zero. If one
1459      * argument is positive zero and the other negative zero, the
1460      * result is positive zero.
1461      *
1462      * @param   a   an argument.
1463      * @param   b   another argument.
1464      * @return  the larger of {@code a} and {@code b}.
1465      */
1466     @HotSpotIntrinsicCandidate
1467     public static float max(float a, float b) {
1468         if (a != a)
1469             return a;   // a is NaN
1470         if ((a == 0.0f) &&
1471             (b == 0.0f) &&
1472             (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
1473             // Raw conversion ok since NaN can't map to -0.0.
1474             return b;
1475         }
1476         return (a >= b) ? a : b;
1477     }
1478 
1479     /**
1480      * Returns the greater of two {@code double} values.  That
1481      * is, the result is the argument closer to positive infinity. If
1482      * the arguments have the same value, the result is that same
1483      * value. If either value is NaN, then the result is NaN.  Unlike
1484      * the numerical comparison operators, this method considers
1485      * negative zero to be strictly smaller than positive zero. If one
1486      * argument is positive zero and the other negative zero, the
1487      * result is positive zero.
1488      *
1489      * @param   a   an argument.
1490      * @param   b   another argument.
1491      * @return  the larger of {@code a} and {@code b}.
1492      */
1493     @HotSpotIntrinsicCandidate
1494     public static double max(double a, double b) {
1495         if (a != a)
1496             return a;   // a is NaN
1497         if ((a == 0.0d) &&
1498             (b == 0.0d) &&
1499             (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
1500             // Raw conversion ok since NaN can't map to -0.0.
1501             return b;
1502         }
1503         return (a >= b) ? a : b;
1504     }
1505 
1506     /**
1507      * Returns the smaller of two {@code int} values. That is,
1508      * the result the argument closer to the value of
1509      * {@link Integer#MIN_VALUE}.  If the arguments have the same
1510      * value, the result is that same value.
1511      *
1512      * @param   a   an argument.
1513      * @param   b   another argument.
1514      * @return  the smaller of {@code a} and {@code b}.
1515      */
1516     @HotSpotIntrinsicCandidate
1517     public static int min(int a, int b) {
1518         return (a <= b) ? a : b;
1519     }
1520 
1521     /**
1522      * Returns the smaller of two {@code long} values. That is,
1523      * the result is the argument closer to the value of
1524      * {@link Long#MIN_VALUE}. If the arguments have the same
1525      * value, the result is that same value.
1526      *
1527      * @param   a   an argument.
1528      * @param   b   another argument.
1529      * @return  the smaller of {@code a} and {@code b}.
1530      */
1531     public static long min(long a, long b) {
1532         return (a <= b) ? a : b;
1533     }
1534 
1535     /**
1536      * Returns the smaller of two {@code float} values.  That is,
1537      * the result is the value closer to negative infinity. If the
1538      * arguments have the same value, the result is that same
1539      * value. If either value is NaN, then the result is NaN.  Unlike
1540      * the numerical comparison operators, this method considers
1541      * negative zero to be strictly smaller than positive zero.  If
1542      * one argument is positive zero and the other is negative zero,
1543      * the result is negative zero.
1544      *
1545      * @param   a   an argument.
1546      * @param   b   another argument.
1547      * @return  the smaller of {@code a} and {@code b}.
1548      */
1549     @HotSpotIntrinsicCandidate
1550     public static float min(float a, float b) {
1551         if (a != a)
1552             return a;   // a is NaN
1553         if ((a == 0.0f) &&
1554             (b == 0.0f) &&
1555             (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
1556             // Raw conversion ok since NaN can't map to -0.0.
1557             return b;
1558         }
1559         return (a <= b) ? a : b;
1560     }
1561 
1562     /**
1563      * Returns the smaller of two {@code double} values.  That
1564      * is, the result is the value closer to negative infinity. If the
1565      * arguments have the same value, the result is that same
1566      * value. If either value is NaN, then the result is NaN.  Unlike
1567      * the numerical comparison operators, this method considers
1568      * negative zero to be strictly smaller than positive zero. If one
1569      * argument is positive zero and the other is negative zero, the
1570      * result is negative zero.
1571      *
1572      * @param   a   an argument.
1573      * @param   b   another argument.
1574      * @return  the smaller of {@code a} and {@code b}.
1575      */
1576     @HotSpotIntrinsicCandidate
1577     public static double min(double a, double b) {
1578         if (a != a)
1579             return a;   // a is NaN
1580         if ((a == 0.0d) &&
1581             (b == 0.0d) &&
1582             (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
1583             // Raw conversion ok since NaN can't map to -0.0.
1584             return b;
1585         }
1586         return (a <= b) ? a : b;
1587     }
1588 
1589     /**
1590      * Returns the fused multiply add of the three arguments; that is,
1591      * returns the exact product of the first two arguments summed
1592      * with the third argument and then rounded once to the nearest
1593      * {@code double}.
1594      *
1595      * The rounding is done using the {@linkplain
1596      * java.math.RoundingMode#HALF_EVEN round to nearest even
1597      * rounding mode}.
1598      *
1599      * In contrast, if {@code a * b + c} is evaluated as a regular
1600      * floating-point expression, two rounding errors are involved,
1601      * the first for the multiply operation, the second for the
1602      * addition operation.
1603      *
1604      * <p>Special cases:
1605      * <ul>
1606      * <li> If any argument is NaN, the result is NaN.
1607      *
1608      * <li> If one of the first two arguments is infinite and the
1609      * other is zero, the result is NaN.
1610      *
1611      * <li> If the exact product of the first two arguments is infinite
1612      * (in other words, at least one of the arguments is infinite and
1613      * the other is neither zero nor NaN) and the third argument is an
1614      * infinity of the opposite sign, the result is NaN.
1615      *
1616      * </ul>
1617      *
1618      * <p>Note that {@code fma(a, 1.0, c)} returns the same
1619      * result as ({@code a + c}).  However,
1620      * {@code fma(a, b, +0.0)} does <em>not</em> always return the
1621      * same result as ({@code a * b}) since
1622      * {@code fma(-0.0, +0.0, +0.0)} is {@code +0.0} while
1623      * ({@code -0.0 * +0.0}) is {@code -0.0}; {@code fma(a, b, -0.0)} is
1624      * equivalent to ({@code a * b}) however.
1625      *
1626      * @apiNote This method corresponds to the fusedMultiplyAdd
1627      * operation defined in IEEE 754-2008.
1628      *
1629      * @param a a value
1630      * @param b a value
1631      * @param c a value
1632      *
1633      * @return (<i>a</i>&nbsp;&times;&nbsp;<i>b</i>&nbsp;+&nbsp;<i>c</i>)
1634      * computed, as if with unlimited range and precision, and rounded
1635      * once to the nearest {@code double} value
1636      *
1637      * @since 9
1638      */
1639     @HotSpotIntrinsicCandidate
1640     public static double fma(double a, double b, double c) {
1641         /*
1642          * Infinity and NaN arithmetic is not quite the same with two
1643          * roundings as opposed to just one so the simple expression
1644          * "a * b + c" cannot always be used to compute the correct
1645          * result.  With two roundings, the product can overflow and
1646          * if the addend is infinite, a spurious NaN can be produced
1647          * if the infinity from the overflow and the infinite addend
1648          * have opposite signs.
1649          */
1650 
1651         // First, screen for and handle non-finite input values whose
1652         // arithmetic is not supported by BigDecimal.
1653         if (Double.isNaN(a) || Double.isNaN(b) || Double.isNaN(c)) {
1654             return Double.NaN;
1655         } else { // All inputs non-NaN
1656             boolean infiniteA = Double.isInfinite(a);
1657             boolean infiniteB = Double.isInfinite(b);
1658             boolean infiniteC = Double.isInfinite(c);
1659             double result;
1660 
1661             if (infiniteA || infiniteB || infiniteC) {
1662                 if (infiniteA && b == 0.0 ||
1663                     infiniteB && a == 0.0 ) {
1664                     return Double.NaN;
1665                 }
1666                 // Store product in a double field to cause an
1667                 // overflow even if non-strictfp evaluation is being
1668                 // used.
1669                 double product = a * b;
1670                 if (Double.isInfinite(product) && !infiniteA && !infiniteB) {
1671                     // Intermediate overflow; might cause a
1672                     // spurious NaN if added to infinite c.
1673                     assert Double.isInfinite(c);
1674                     return c;
1675                 } else {
1676                     result = product + c;
1677                     assert !Double.isFinite(result);
1678                     return result;
1679                 }
1680             } else { // All inputs finite
1681                 BigDecimal product = (new BigDecimal(a)).multiply(new BigDecimal(b));
1682                 if (c == 0.0) { // Positive or negative zero
1683                     // If the product is an exact zero, use a
1684                     // floating-point expression to compute the sign
1685                     // of the zero final result. The product is an
1686                     // exact zero if and only if at least one of a and
1687                     // b is zero.
1688                     if (a == 0.0 || b == 0.0) {
1689                         return a * b + c;
1690                     } else {
1691                         // The sign of a zero addend doesn't matter if
1692                         // the product is nonzero. The sign of a zero
1693                         // addend is not factored in the result if the
1694                         // exact product is nonzero but underflows to
1695                         // zero; see IEEE-754 2008 section 6.3 "The
1696                         // sign bit".
1697                         return product.doubleValue();
1698                     }
1699                 } else {
1700                     return product.add(new BigDecimal(c)).doubleValue();
1701                 }
1702             }
1703         }
1704     }
1705 
1706     /**
1707      * Returns the fused multiply add of the three arguments; that is,
1708      * returns the exact product of the first two arguments summed
1709      * with the third argument and then rounded once to the nearest
1710      * {@code float}.
1711      *
1712      * The rounding is done using the {@linkplain
1713      * java.math.RoundingMode#HALF_EVEN round to nearest even
1714      * rounding mode}.
1715      *
1716      * In contrast, if {@code a * b + c} is evaluated as a regular
1717      * floating-point expression, two rounding errors are involved,
1718      * the first for the multiply operation, the second for the
1719      * addition operation.
1720      *
1721      * <p>Special cases:
1722      * <ul>
1723      * <li> If any argument is NaN, the result is NaN.
1724      *
1725      * <li> If one of the first two arguments is infinite and the
1726      * other is zero, the result is NaN.
1727      *
1728      * <li> If the exact product of the first two arguments is infinite
1729      * (in other words, at least one of the arguments is infinite and
1730      * the other is neither zero nor NaN) and the third argument is an
1731      * infinity of the opposite sign, the result is NaN.
1732      *
1733      * </ul>
1734      *
1735      * <p>Note that {@code fma(a, 1.0f, c)} returns the same
1736      * result as ({@code a + c}).  However,
1737      * {@code fma(a, b, +0.0f)} does <em>not</em> always return the
1738      * same result as ({@code a * b}) since
1739      * {@code fma(-0.0f, +0.0f, +0.0f)} is {@code +0.0f} while
1740      * ({@code -0.0f * +0.0f}) is {@code -0.0f}; {@code fma(a, b, -0.0f)} is
1741      * equivalent to ({@code a * b}) however.
1742      *
1743      * @apiNote This method corresponds to the fusedMultiplyAdd
1744      * operation defined in IEEE 754-2008.
1745      *
1746      * @param a a value
1747      * @param b a value
1748      * @param c a value
1749      *
1750      * @return (<i>a</i>&nbsp;&times;&nbsp;<i>b</i>&nbsp;+&nbsp;<i>c</i>)
1751      * computed, as if with unlimited range and precision, and rounded
1752      * once to the nearest {@code float} value
1753      *
1754      * @since 9
1755      */
1756     @HotSpotIntrinsicCandidate
1757     public static float fma(float a, float b, float c) {
1758         if (Float.isFinite(a) && Float.isFinite(b) && Float.isFinite(c)) {
1759             if (a == 0.0 || b == 0.0) {
1760                 return a * b + c; // Handled signed zero cases
1761             } else {
1762                 return (new BigDecimal((double)a * (double)b) // Exact multiply
1763                         .add(new BigDecimal((double)c)))      // Exact sum
1764                     .floatValue();                            // One rounding
1765                                                               // to a float value
1766             }
1767         } else {
1768             // At least one of a,b, and c is non-finite. The result
1769             // will be non-finite as well and will be the same
1770             // non-finite value under double as float arithmetic.
1771             return (float)fma((double)a, (double)b, (double)c);
1772         }
1773     }
1774 
1775     /**
1776      * Returns the size of an ulp of the argument.  An ulp, unit in
1777      * the last place, of a {@code double} value is the positive
1778      * distance between this floating-point value and the {@code
1779      * double} value next larger in magnitude.  Note that for non-NaN
1780      * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
1781      *
1782      * <p>Special Cases:
1783      * <ul>
1784      * <li> If the argument is NaN, then the result is NaN.
1785      * <li> If the argument is positive or negative infinity, then the
1786      * result is positive infinity.
1787      * <li> If the argument is positive or negative zero, then the result is
1788      * {@code Double.MIN_VALUE}.
1789      * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
1790      * the result is equal to 2<sup>971</sup>.
1791      * </ul>
1792      *
1793      * @param d the floating-point value whose ulp is to be returned
1794      * @return the size of an ulp of the argument
1795      * @author Joseph D. Darcy
1796      * @since 1.5
1797      */
1798     public static double ulp(double d) {
1799         int exp = getExponent(d);
1800 
1801         switch(exp) {
1802         case Double.MAX_EXPONENT + 1:       // NaN or infinity
1803             return Math.abs(d);
1804 
1805         case Double.MIN_EXPONENT - 1:       // zero or subnormal
1806             return Double.MIN_VALUE;
1807 
1808         default:
1809             assert exp <= Double.MAX_EXPONENT && exp >= Double.MIN_EXPONENT;
1810 
1811             // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
1812             exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1);
1813             if (exp >= Double.MIN_EXPONENT) {
1814                 return powerOfTwoD(exp);
1815             }
1816             else {
1817                 // return a subnormal result; left shift integer
1818                 // representation of Double.MIN_VALUE appropriate
1819                 // number of positions
1820                 return Double.longBitsToDouble(1L <<
1821                 (exp - (Double.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
1822             }
1823         }
1824     }
1825 
1826     /**
1827      * Returns the size of an ulp of the argument.  An ulp, unit in
1828      * the last place, of a {@code float} value is the positive
1829      * distance between this floating-point value and the {@code
1830      * float} value next larger in magnitude.  Note that for non-NaN
1831      * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
1832      *
1833      * <p>Special Cases:
1834      * <ul>
1835      * <li> If the argument is NaN, then the result is NaN.
1836      * <li> If the argument is positive or negative infinity, then the
1837      * result is positive infinity.
1838      * <li> If the argument is positive or negative zero, then the result is
1839      * {@code Float.MIN_VALUE}.
1840      * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
1841      * the result is equal to 2<sup>104</sup>.
1842      * </ul>
1843      *
1844      * @param f the floating-point value whose ulp is to be returned
1845      * @return the size of an ulp of the argument
1846      * @author Joseph D. Darcy
1847      * @since 1.5
1848      */
1849     public static float ulp(float f) {
1850         int exp = getExponent(f);
1851 
1852         switch(exp) {
1853         case Float.MAX_EXPONENT+1:        // NaN or infinity
1854             return Math.abs(f);
1855 
1856         case Float.MIN_EXPONENT-1:        // zero or subnormal
1857             return Float.MIN_VALUE;
1858 
1859         default:
1860             assert exp <= Float.MAX_EXPONENT && exp >= Float.MIN_EXPONENT;
1861 
1862             // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
1863             exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1);
1864             if (exp >= Float.MIN_EXPONENT) {
1865                 return powerOfTwoF(exp);
1866             } else {
1867                 // return a subnormal result; left shift integer
1868                 // representation of FloatConsts.MIN_VALUE appropriate
1869                 // number of positions
1870                 return Float.intBitsToFloat(1 <<
1871                 (exp - (Float.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
1872             }
1873         }
1874     }
1875 
1876     /**
1877      * Returns the signum function of the argument; zero if the argument
1878      * is zero, 1.0 if the argument is greater than zero, -1.0 if the
1879      * argument is less than zero.
1880      *
1881      * <p>Special Cases:
1882      * <ul>
1883      * <li> If the argument is NaN, then the result is NaN.
1884      * <li> If the argument is positive zero or negative zero, then the
1885      *      result is the same as the argument.
1886      * </ul>
1887      *
1888      * @param d the floating-point value whose signum is to be returned
1889      * @return the signum function of the argument
1890      * @author Joseph D. Darcy
1891      * @since 1.5
1892      */
1893     public static double signum(double d) {
1894         return (d == 0.0 || Double.isNaN(d))?d:copySign(1.0, d);
1895     }
1896 
1897     /**
1898      * Returns the signum function of the argument; zero if the argument
1899      * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1900      * argument is less than zero.
1901      *
1902      * <p>Special Cases:
1903      * <ul>
1904      * <li> If the argument is NaN, then the result is NaN.
1905      * <li> If the argument is positive zero or negative zero, then the
1906      *      result is the same as the argument.
1907      * </ul>
1908      *
1909      * @param f the floating-point value whose signum is to be returned
1910      * @return the signum function of the argument
1911      * @author Joseph D. Darcy
1912      * @since 1.5
1913      */
1914     public static float signum(float f) {
1915         return (f == 0.0f || Float.isNaN(f))?f:copySign(1.0f, f);
1916     }
1917 
1918     /**
1919      * Returns the hyperbolic sine of a {@code double} value.
1920      * The hyperbolic sine of <i>x</i> is defined to be
1921      * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/2
1922      * where <i>e</i> is {@linkplain Math#E Euler's number}.
1923      *
1924      * <p>Special cases:
1925      * <ul>
1926      *
1927      * <li>If the argument is NaN, then the result is NaN.
1928      *
1929      * <li>If the argument is infinite, then the result is an infinity
1930      * with the same sign as the argument.
1931      *
1932      * <li>If the argument is zero, then the result is a zero with the
1933      * same sign as the argument.
1934      *
1935      * </ul>
1936      *
1937      * <p>The computed result must be within 2.5 ulps of the exact result.
1938      *
1939      * @param   x The number whose hyperbolic sine is to be returned.
1940      * @return  The hyperbolic sine of {@code x}.
1941      * @since 1.5
1942      */
1943     public static double sinh(double x) {
1944         return StrictMath.sinh(x);
1945     }
1946 
1947     /**
1948      * Returns the hyperbolic cosine of a {@code double} value.
1949      * The hyperbolic cosine of <i>x</i> is defined to be
1950      * (<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>)/2
1951      * where <i>e</i> is {@linkplain Math#E Euler's number}.
1952      *
1953      * <p>Special cases:
1954      * <ul>
1955      *
1956      * <li>If the argument is NaN, then the result is NaN.
1957      *
1958      * <li>If the argument is infinite, then the result is positive
1959      * infinity.
1960      *
1961      * <li>If the argument is zero, then the result is {@code 1.0}.
1962      *
1963      * </ul>
1964      *
1965      * <p>The computed result must be within 2.5 ulps of the exact result.
1966      *
1967      * @param   x The number whose hyperbolic cosine is to be returned.
1968      * @return  The hyperbolic cosine of {@code x}.
1969      * @since 1.5
1970      */
1971     public static double cosh(double x) {
1972         return StrictMath.cosh(x);
1973     }
1974 
1975     /**
1976      * Returns the hyperbolic tangent of a {@code double} value.
1977      * The hyperbolic tangent of <i>x</i> is defined to be
1978      * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/(<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>),
1979      * in other words, {@linkplain Math#sinh
1980      * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}.  Note
1981      * that the absolute value of the exact tanh is always less than
1982      * 1.
1983      *
1984      * <p>Special cases:
1985      * <ul>
1986      *
1987      * <li>If the argument is NaN, then the result is NaN.
1988      *
1989      * <li>If the argument is zero, then the result is a zero with the
1990      * same sign as the argument.
1991      *
1992      * <li>If the argument is positive infinity, then the result is
1993      * {@code +1.0}.
1994      *
1995      * <li>If the argument is negative infinity, then the result is
1996      * {@code -1.0}.
1997      *
1998      * </ul>
1999      *
2000      * <p>The computed result must be within 2.5 ulps of the exact result.
2001      * The result of {@code tanh} for any finite input must have
2002      * an absolute value less than or equal to 1.  Note that once the
2003      * exact result of tanh is within 1/2 of an ulp of the limit value
2004      * of &plusmn;1, correctly signed &plusmn;{@code 1.0} should
2005      * be returned.
2006      *
2007      * @param   x The number whose hyperbolic tangent is to be returned.
2008      * @return  The hyperbolic tangent of {@code x}.
2009      * @since 1.5
2010      */
2011     public static double tanh(double x) {
2012         return StrictMath.tanh(x);
2013     }
2014 
2015     /**
2016      * Returns sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
2017      * without intermediate overflow or underflow.
2018      *
2019      * <p>Special cases:
2020      * <ul>
2021      *
2022      * <li> If either argument is infinite, then the result
2023      * is positive infinity.
2024      *
2025      * <li> If either argument is NaN and neither argument is infinite,
2026      * then the result is NaN.
2027      *
2028      * </ul>
2029      *
2030      * <p>The computed result must be within 1 ulp of the exact
2031      * result.  If one parameter is held constant, the results must be
2032      * semi-monotonic in the other parameter.
2033      *
2034      * @param x a value
2035      * @param y a value
2036      * @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
2037      * without intermediate overflow or underflow
2038      * @since 1.5
2039      */
2040     public static double hypot(double x, double y) {
2041         return StrictMath.hypot(x, y);
2042     }
2043 
2044     /**
2045      * Returns <i>e</i><sup>x</sup>&nbsp;-1.  Note that for values of
2046      * <i>x</i> near 0, the exact sum of
2047      * {@code expm1(x)}&nbsp;+&nbsp;1 is much closer to the true
2048      * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
2049      *
2050      * <p>Special cases:
2051      * <ul>
2052      * <li>If the argument is NaN, the result is NaN.
2053      *
2054      * <li>If the argument is positive infinity, then the result is
2055      * positive infinity.
2056      *
2057      * <li>If the argument is negative infinity, then the result is
2058      * -1.0.
2059      *
2060      * <li>If the argument is zero, then the result is a zero with the
2061      * same sign as the argument.
2062      *
2063      * </ul>
2064      *
2065      * <p>The computed result must be within 1 ulp of the exact result.
2066      * Results must be semi-monotonic.  The result of
2067      * {@code expm1} for any finite input must be greater than or
2068      * equal to {@code -1.0}.  Note that once the exact result of
2069      * <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1 is within 1/2
2070      * ulp of the limit value -1, {@code -1.0} should be
2071      * returned.
2072      *
2073      * @param   x   the exponent to raise <i>e</i> to in the computation of
2074      *              <i>e</i><sup>{@code x}</sup>&nbsp;-1.
2075      * @return  the value <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1.
2076      * @since 1.5
2077      */
2078     public static double expm1(double x) {
2079         return StrictMath.expm1(x);
2080     }
2081 
2082     /**
2083      * Returns the natural logarithm of the sum of the argument and 1.
2084      * Note that for small values {@code x}, the result of
2085      * {@code log1p(x)} is much closer to the true result of ln(1
2086      * + {@code x}) than the floating-point evaluation of
2087      * {@code log(1.0+x)}.
2088      *
2089      * <p>Special cases:
2090      *
2091      * <ul>
2092      *
2093      * <li>If the argument is NaN or less than -1, then the result is
2094      * NaN.
2095      *
2096      * <li>If the argument is positive infinity, then the result is
2097      * positive infinity.
2098      *
2099      * <li>If the argument is negative one, then the result is
2100      * negative infinity.
2101      *
2102      * <li>If the argument is zero, then the result is a zero with the
2103      * same sign as the argument.
2104      *
2105      * </ul>
2106      *
2107      * <p>The computed result must be within 1 ulp of the exact result.
2108      * Results must be semi-monotonic.
2109      *
2110      * @param   x   a value
2111      * @return the value ln({@code x}&nbsp;+&nbsp;1), the natural
2112      * log of {@code x}&nbsp;+&nbsp;1
2113      * @since 1.5
2114      */
2115     public static double log1p(double x) {
2116         return StrictMath.log1p(x);
2117     }
2118 
2119     /**
2120      * Returns the first floating-point argument with the sign of the
2121      * second floating-point argument.  Note that unlike the {@link
2122      * StrictMath#copySign(double, double) StrictMath.copySign}
2123      * method, this method does not require NaN {@code sign}
2124      * arguments to be treated as positive values; implementations are
2125      * permitted to treat some NaN arguments as positive and other NaN
2126      * arguments as negative to allow greater performance.
2127      *
2128      * @param magnitude  the parameter providing the magnitude of the result
2129      * @param sign   the parameter providing the sign of the result
2130      * @return a value with the magnitude of {@code magnitude}
2131      * and the sign of {@code sign}.
2132      * @since 1.6
2133      */
2134     public static double copySign(double magnitude, double sign) {
2135         return Double.longBitsToDouble((Double.doubleToRawLongBits(sign) &
2136                                         (DoubleConsts.SIGN_BIT_MASK)) |
2137                                        (Double.doubleToRawLongBits(magnitude) &
2138                                         (DoubleConsts.EXP_BIT_MASK |
2139                                          DoubleConsts.SIGNIF_BIT_MASK)));
2140     }
2141 
2142     /**
2143      * Returns the first floating-point argument with the sign of the
2144      * second floating-point argument.  Note that unlike the {@link
2145      * StrictMath#copySign(float, float) StrictMath.copySign}
2146      * method, this method does not require NaN {@code sign}
2147      * arguments to be treated as positive values; implementations are
2148      * permitted to treat some NaN arguments as positive and other NaN
2149      * arguments as negative to allow greater performance.
2150      *
2151      * @param magnitude  the parameter providing the magnitude of the result
2152      * @param sign   the parameter providing the sign of the result
2153      * @return a value with the magnitude of {@code magnitude}
2154      * and the sign of {@code sign}.
2155      * @since 1.6
2156      */
2157     public static float copySign(float magnitude, float sign) {
2158         return Float.intBitsToFloat((Float.floatToRawIntBits(sign) &
2159                                      (FloatConsts.SIGN_BIT_MASK)) |
2160                                     (Float.floatToRawIntBits(magnitude) &
2161                                      (FloatConsts.EXP_BIT_MASK |
2162                                       FloatConsts.SIGNIF_BIT_MASK)));
2163     }
2164 
2165     /**
2166      * Returns the unbiased exponent used in the representation of a
2167      * {@code float}.  Special cases:
2168      *
2169      * <ul>
2170      * <li>If the argument is NaN or infinite, then the result is
2171      * {@link Float#MAX_EXPONENT} + 1.
2172      * <li>If the argument is zero or subnormal, then the result is
2173      * {@link Float#MIN_EXPONENT} -1.
2174      * </ul>
2175      * @param f a {@code float} value
2176      * @return the unbiased exponent of the argument
2177      * @since 1.6
2178      */
2179     public static int getExponent(float f) {
2180         /*
2181          * Bitwise convert f to integer, mask out exponent bits, shift
2182          * to the right and then subtract out float's bias adjust to
2183          * get true exponent value
2184          */
2185         return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
2186                 (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
2187     }
2188 
2189     /**
2190      * Returns the unbiased exponent used in the representation of a
2191      * {@code double}.  Special cases:
2192      *
2193      * <ul>
2194      * <li>If the argument is NaN or infinite, then the result is
2195      * {@link Double#MAX_EXPONENT} + 1.
2196      * <li>If the argument is zero or subnormal, then the result is
2197      * {@link Double#MIN_EXPONENT} -1.
2198      * </ul>
2199      * @param d a {@code double} value
2200      * @return the unbiased exponent of the argument
2201      * @since 1.6
2202      */
2203     public static int getExponent(double d) {
2204         /*
2205          * Bitwise convert d to long, mask out exponent bits, shift
2206          * to the right and then subtract out double's bias adjust to
2207          * get true exponent value.
2208          */
2209         return (int)(((Double.doubleToRawLongBits(d) & DoubleConsts.EXP_BIT_MASK) >>
2210                       (DoubleConsts.SIGNIFICAND_WIDTH - 1)) - DoubleConsts.EXP_BIAS);
2211     }
2212 
2213     /**
2214      * Returns the floating-point number adjacent to the first
2215      * argument in the direction of the second argument.  If both
2216      * arguments compare as equal the second argument is returned.
2217      *
2218      * <p>
2219      * Special cases:
2220      * <ul>
2221      * <li> If either argument is a NaN, then NaN is returned.
2222      *
2223      * <li> If both arguments are signed zeros, {@code direction}
2224      * is returned unchanged (as implied by the requirement of
2225      * returning the second argument if the arguments compare as
2226      * equal).
2227      *
2228      * <li> If {@code start} is
2229      * &plusmn;{@link Double#MIN_VALUE} and {@code direction}
2230      * has a value such that the result should have a smaller
2231      * magnitude, then a zero with the same sign as {@code start}
2232      * is returned.
2233      *
2234      * <li> If {@code start} is infinite and
2235      * {@code direction} has a value such that the result should
2236      * have a smaller magnitude, {@link Double#MAX_VALUE} with the
2237      * same sign as {@code start} is returned.
2238      *
2239      * <li> If {@code start} is equal to &plusmn;
2240      * {@link Double#MAX_VALUE} and {@code direction} has a
2241      * value such that the result should have a larger magnitude, an
2242      * infinity with same sign as {@code start} is returned.
2243      * </ul>
2244      *
2245      * @param start  starting floating-point value
2246      * @param direction value indicating which of
2247      * {@code start}'s neighbors or {@code start} should
2248      * be returned
2249      * @return The floating-point number adjacent to {@code start} in the
2250      * direction of {@code direction}.
2251      * @since 1.6
2252      */
2253     public static double nextAfter(double start, double direction) {
2254         /*
2255          * The cases:
2256          *
2257          * nextAfter(+infinity, 0)  == MAX_VALUE
2258          * nextAfter(+infinity, +infinity)  == +infinity
2259          * nextAfter(-infinity, 0)  == -MAX_VALUE
2260          * nextAfter(-infinity, -infinity)  == -infinity
2261          *
2262          * are naturally handled without any additional testing
2263          */
2264 
2265         /*
2266          * IEEE 754 floating-point numbers are lexicographically
2267          * ordered if treated as signed-magnitude integers.
2268          * Since Java's integers are two's complement,
2269          * incrementing the two's complement representation of a
2270          * logically negative floating-point value *decrements*
2271          * the signed-magnitude representation. Therefore, when
2272          * the integer representation of a floating-point value
2273          * is negative, the adjustment to the representation is in
2274          * the opposite direction from what would initially be expected.
2275          */
2276 
2277         // Branch to descending case first as it is more costly than ascending
2278         // case due to start != 0.0d conditional.
2279         if (start > direction) { // descending
2280             if (start != 0.0d) {
2281                 final long transducer = Double.doubleToRawLongBits(start);
2282                 return Double.longBitsToDouble(transducer + ((transducer > 0L) ? -1L : 1L));
2283             } else { // start == 0.0d && direction < 0.0d
2284                 return -Double.MIN_VALUE;
2285             }
2286         } else if (start < direction) { // ascending
2287             // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
2288             // then bitwise convert start to integer.
2289             final long transducer = Double.doubleToRawLongBits(start + 0.0d);
2290             return Double.longBitsToDouble(transducer + ((transducer >= 0L) ? 1L : -1L));
2291         } else if (start == direction) {
2292             return direction;
2293         } else { // isNaN(start) || isNaN(direction)
2294             return start + direction;
2295         }
2296     }
2297 
2298     /**
2299      * Returns the floating-point number adjacent to the first
2300      * argument in the direction of the second argument.  If both
2301      * arguments compare as equal a value equivalent to the second argument
2302      * is returned.
2303      *
2304      * <p>
2305      * Special cases:
2306      * <ul>
2307      * <li> If either argument is a NaN, then NaN is returned.
2308      *
2309      * <li> If both arguments are signed zeros, a value equivalent
2310      * to {@code direction} is returned.
2311      *
2312      * <li> If {@code start} is
2313      * &plusmn;{@link Float#MIN_VALUE} and {@code direction}
2314      * has a value such that the result should have a smaller
2315      * magnitude, then a zero with the same sign as {@code start}
2316      * is returned.
2317      *
2318      * <li> If {@code start} is infinite and
2319      * {@code direction} has a value such that the result should
2320      * have a smaller magnitude, {@link Float#MAX_VALUE} with the
2321      * same sign as {@code start} is returned.
2322      *
2323      * <li> If {@code start} is equal to &plusmn;
2324      * {@link Float#MAX_VALUE} and {@code direction} has a
2325      * value such that the result should have a larger magnitude, an
2326      * infinity with same sign as {@code start} is returned.
2327      * </ul>
2328      *
2329      * @param start  starting floating-point value
2330      * @param direction value indicating which of
2331      * {@code start}'s neighbors or {@code start} should
2332      * be returned
2333      * @return The floating-point number adjacent to {@code start} in the
2334      * direction of {@code direction}.
2335      * @since 1.6
2336      */
2337     public static float nextAfter(float start, double direction) {
2338         /*
2339          * The cases:
2340          *
2341          * nextAfter(+infinity, 0)  == MAX_VALUE
2342          * nextAfter(+infinity, +infinity)  == +infinity
2343          * nextAfter(-infinity, 0)  == -MAX_VALUE
2344          * nextAfter(-infinity, -infinity)  == -infinity
2345          *
2346          * are naturally handled without any additional testing
2347          */
2348 
2349         /*
2350          * IEEE 754 floating-point numbers are lexicographically
2351          * ordered if treated as signed-magnitude integers.
2352          * Since Java's integers are two's complement,
2353          * incrementing the two's complement representation of a
2354          * logically negative floating-point value *decrements*
2355          * the signed-magnitude representation. Therefore, when
2356          * the integer representation of a floating-point value
2357          * is negative, the adjustment to the representation is in
2358          * the opposite direction from what would initially be expected.
2359          */
2360 
2361         // Branch to descending case first as it is more costly than ascending
2362         // case due to start != 0.0f conditional.
2363         if (start > direction) { // descending
2364             if (start != 0.0f) {
2365                 final int transducer = Float.floatToRawIntBits(start);
2366                 return Float.intBitsToFloat(transducer + ((transducer > 0) ? -1 : 1));
2367             } else { // start == 0.0f && direction < 0.0f
2368                 return -Float.MIN_VALUE;
2369             }
2370         } else if (start < direction) { // ascending
2371             // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
2372             // then bitwise convert start to integer.
2373             final int transducer = Float.floatToRawIntBits(start + 0.0f);
2374             return Float.intBitsToFloat(transducer + ((transducer >= 0) ? 1 : -1));
2375         } else if (start == direction) {
2376             return (float)direction;
2377         } else { // isNaN(start) || isNaN(direction)
2378             return start + (float)direction;
2379         }
2380     }
2381 
2382     /**
2383      * Returns the floating-point value adjacent to {@code d} in
2384      * the direction of positive infinity.  This method is
2385      * semantically equivalent to {@code nextAfter(d,
2386      * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
2387      * implementation may run faster than its equivalent
2388      * {@code nextAfter} call.
2389      *
2390      * <p>Special Cases:
2391      * <ul>
2392      * <li> If the argument is NaN, the result is NaN.
2393      *
2394      * <li> If the argument is positive infinity, the result is
2395      * positive infinity.
2396      *
2397      * <li> If the argument is zero, the result is
2398      * {@link Double#MIN_VALUE}
2399      *
2400      * </ul>
2401      *
2402      * @param d starting floating-point value
2403      * @return The adjacent floating-point value closer to positive
2404      * infinity.
2405      * @since 1.6
2406      */
2407     public static double nextUp(double d) {
2408         // Use a single conditional and handle the likely cases first.
2409         if (d < Double.POSITIVE_INFINITY) {
2410             // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0).
2411             final long transducer = Double.doubleToRawLongBits(d + 0.0D);
2412             return Double.longBitsToDouble(transducer + ((transducer >= 0L) ? 1L : -1L));
2413         } else { // d is NaN or +Infinity
2414             return d;
2415         }
2416     }
2417 
2418     /**
2419      * Returns the floating-point value adjacent to {@code f} in
2420      * the direction of positive infinity.  This method is
2421      * semantically equivalent to {@code nextAfter(f,
2422      * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
2423      * implementation may run faster than its equivalent
2424      * {@code nextAfter} call.
2425      *
2426      * <p>Special Cases:
2427      * <ul>
2428      * <li> If the argument is NaN, the result is NaN.
2429      *
2430      * <li> If the argument is positive infinity, the result is
2431      * positive infinity.
2432      *
2433      * <li> If the argument is zero, the result is
2434      * {@link Float#MIN_VALUE}
2435      *
2436      * </ul>
2437      *
2438      * @param f starting floating-point value
2439      * @return The adjacent floating-point value closer to positive
2440      * infinity.
2441      * @since 1.6
2442      */
2443     public static float nextUp(float f) {
2444         // Use a single conditional and handle the likely cases first.
2445         if (f < Float.POSITIVE_INFINITY) {
2446             // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0).
2447             final int transducer = Float.floatToRawIntBits(f + 0.0F);
2448             return Float.intBitsToFloat(transducer + ((transducer >= 0) ? 1 : -1));
2449         } else { // f is NaN or +Infinity
2450             return f;
2451         }
2452     }
2453 
2454     /**
2455      * Returns the floating-point value adjacent to {@code d} in
2456      * the direction of negative infinity.  This method is
2457      * semantically equivalent to {@code nextAfter(d,
2458      * Double.NEGATIVE_INFINITY)}; however, a
2459      * {@code nextDown} implementation may run faster than its
2460      * equivalent {@code nextAfter} call.
2461      *
2462      * <p>Special Cases:
2463      * <ul>
2464      * <li> If the argument is NaN, the result is NaN.
2465      *
2466      * <li> If the argument is negative infinity, the result is
2467      * negative infinity.
2468      *
2469      * <li> If the argument is zero, the result is
2470      * {@code -Double.MIN_VALUE}
2471      *
2472      * </ul>
2473      *
2474      * @param d  starting floating-point value
2475      * @return The adjacent floating-point value closer to negative
2476      * infinity.
2477      * @since 1.8
2478      */
2479     public static double nextDown(double d) {
2480         if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY)
2481             return d;
2482         else {
2483             if (d == 0.0)
2484                 return -Double.MIN_VALUE;
2485             else
2486                 return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
2487                                                ((d > 0.0d)?-1L:+1L));
2488         }
2489     }
2490 
2491     /**
2492      * Returns the floating-point value adjacent to {@code f} in
2493      * the direction of negative infinity.  This method is
2494      * semantically equivalent to {@code nextAfter(f,
2495      * Float.NEGATIVE_INFINITY)}; however, a
2496      * {@code nextDown} implementation may run faster than its
2497      * equivalent {@code nextAfter} call.
2498      *
2499      * <p>Special Cases:
2500      * <ul>
2501      * <li> If the argument is NaN, the result is NaN.
2502      *
2503      * <li> If the argument is negative infinity, the result is
2504      * negative infinity.
2505      *
2506      * <li> If the argument is zero, the result is
2507      * {@code -Float.MIN_VALUE}
2508      *
2509      * </ul>
2510      *
2511      * @param f  starting floating-point value
2512      * @return The adjacent floating-point value closer to negative
2513      * infinity.
2514      * @since 1.8
2515      */
2516     public static float nextDown(float f) {
2517         if (Float.isNaN(f) || f == Float.NEGATIVE_INFINITY)
2518             return f;
2519         else {
2520             if (f == 0.0f)
2521                 return -Float.MIN_VALUE;
2522             else
2523                 return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
2524                                             ((f > 0.0f)?-1:+1));
2525         }
2526     }
2527 
2528     /**
2529      * Returns {@code d} &times;
2530      * 2<sup>{@code scaleFactor}</sup> rounded as if performed
2531      * by a single correctly rounded floating-point multiply to a
2532      * member of the double value set.  See the Java
2533      * Language Specification for a discussion of floating-point
2534      * value sets.  If the exponent of the result is between {@link
2535      * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
2536      * answer is calculated exactly.  If the exponent of the result
2537      * would be larger than {@code Double.MAX_EXPONENT}, an
2538      * infinity is returned.  Note that if the result is subnormal,
2539      * precision may be lost; that is, when {@code scalb(x, n)}
2540      * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
2541      * <i>x</i>.  When the result is non-NaN, the result has the same
2542      * sign as {@code d}.
2543      *
2544      * <p>Special cases:
2545      * <ul>
2546      * <li> If the first argument is NaN, NaN is returned.
2547      * <li> If the first argument is infinite, then an infinity of the
2548      * same sign is returned.
2549      * <li> If the first argument is zero, then a zero of the same
2550      * sign is returned.
2551      * </ul>
2552      *
2553      * @param d number to be scaled by a power of two.
2554      * @param scaleFactor power of 2 used to scale {@code d}
2555      * @return {@code d} &times; 2<sup>{@code scaleFactor}</sup>
2556      * @since 1.6
2557      */
2558     public static double scalb(double d, int scaleFactor) {
2559         /*
2560          * This method does not need to be declared strictfp to
2561          * compute the same correct result on all platforms.  When
2562          * scaling up, it does not matter what order the
2563          * multiply-store operations are done; the result will be
2564          * finite or overflow regardless of the operation ordering.
2565          * However, to get the correct result when scaling down, a
2566          * particular ordering must be used.
2567          *
2568          * When scaling down, the multiply-store operations are
2569          * sequenced so that it is not possible for two consecutive
2570          * multiply-stores to return subnormal results.  If one
2571          * multiply-store result is subnormal, the next multiply will
2572          * round it away to zero.  This is done by first multiplying
2573          * by 2 ^ (scaleFactor % n) and then multiplying several
2574          * times by 2^n as needed where n is the exponent of number
2575          * that is a covenient power of two.  In this way, at most one
2576          * real rounding error occurs.  If the double value set is
2577          * being used exclusively, the rounding will occur on a
2578          * multiply.  If the double-extended-exponent value set is
2579          * being used, the products will (perhaps) be exact but the
2580          * stores to d are guaranteed to round to the double value
2581          * set.
2582          *
2583          * It is _not_ a valid implementation to first multiply d by
2584          * 2^MIN_EXPONENT and then by 2 ^ (scaleFactor %
2585          * MIN_EXPONENT) since even in a strictfp program double
2586          * rounding on underflow could occur; e.g. if the scaleFactor
2587          * argument was (MIN_EXPONENT - n) and the exponent of d was a
2588          * little less than -(MIN_EXPONENT - n), meaning the final
2589          * result would be subnormal.
2590          *
2591          * Since exact reproducibility of this method can be achieved
2592          * without any undue performance burden, there is no
2593          * compelling reason to allow double rounding on underflow in
2594          * scalb.
2595          */
2596 
2597         // magnitude of a power of two so large that scaling a finite
2598         // nonzero value by it would be guaranteed to over or
2599         // underflow; due to rounding, scaling down takes an
2600         // additional power of two which is reflected here
2601         final int MAX_SCALE = Double.MAX_EXPONENT + -Double.MIN_EXPONENT +
2602                               DoubleConsts.SIGNIFICAND_WIDTH + 1;
2603         int exp_adjust = 0;
2604         int scale_increment = 0;
2605         double exp_delta = Double.NaN;
2606 
2607         // Make sure scaling factor is in a reasonable range
2608 
2609         if(scaleFactor < 0) {
2610             scaleFactor = Math.max(scaleFactor, -MAX_SCALE);
2611             scale_increment = -512;
2612             exp_delta = twoToTheDoubleScaleDown;
2613         }
2614         else {
2615             scaleFactor = Math.min(scaleFactor, MAX_SCALE);
2616             scale_increment = 512;
2617             exp_delta = twoToTheDoubleScaleUp;
2618         }
2619 
2620         // Calculate (scaleFactor % +/-512), 512 = 2^9, using
2621         // technique from "Hacker's Delight" section 10-2.
2622         int t = (scaleFactor >> 9-1) >>> 32 - 9;
2623         exp_adjust = ((scaleFactor + t) & (512 -1)) - t;
2624 
2625         d *= powerOfTwoD(exp_adjust);
2626         scaleFactor -= exp_adjust;
2627 
2628         while(scaleFactor != 0) {
2629             d *= exp_delta;
2630             scaleFactor -= scale_increment;
2631         }
2632         return d;
2633     }
2634 
2635     /**
2636      * Returns {@code f} &times;
2637      * 2<sup>{@code scaleFactor}</sup> rounded as if performed
2638      * by a single correctly rounded floating-point multiply to a
2639      * member of the float value set.  See the Java
2640      * Language Specification for a discussion of floating-point
2641      * value sets.  If the exponent of the result is between {@link
2642      * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
2643      * answer is calculated exactly.  If the exponent of the result
2644      * would be larger than {@code Float.MAX_EXPONENT}, an
2645      * infinity is returned.  Note that if the result is subnormal,
2646      * precision may be lost; that is, when {@code scalb(x, n)}
2647      * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
2648      * <i>x</i>.  When the result is non-NaN, the result has the same
2649      * sign as {@code f}.
2650      *
2651      * <p>Special cases:
2652      * <ul>
2653      * <li> If the first argument is NaN, NaN is returned.
2654      * <li> If the first argument is infinite, then an infinity of the
2655      * same sign is returned.
2656      * <li> If the first argument is zero, then a zero of the same
2657      * sign is returned.
2658      * </ul>
2659      *
2660      * @param f number to be scaled by a power of two.
2661      * @param scaleFactor power of 2 used to scale {@code f}
2662      * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
2663      * @since 1.6
2664      */
2665     public static float scalb(float f, int scaleFactor) {
2666         // magnitude of a power of two so large that scaling a finite
2667         // nonzero value by it would be guaranteed to over or
2668         // underflow; due to rounding, scaling down takes an
2669         // additional power of two which is reflected here
2670         final int MAX_SCALE = Float.MAX_EXPONENT + -Float.MIN_EXPONENT +
2671                               FloatConsts.SIGNIFICAND_WIDTH + 1;
2672 
2673         // Make sure scaling factor is in a reasonable range
2674         scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);
2675 
2676         /*
2677          * Since + MAX_SCALE for float fits well within the double
2678          * exponent range and + float -> double conversion is exact
2679          * the multiplication below will be exact. Therefore, the
2680          * rounding that occurs when the double product is cast to
2681          * float will be the correctly rounded float result.  Since
2682          * all operations other than the final multiply will be exact,
2683          * it is not necessary to declare this method strictfp.
2684          */
2685         return (float)((double)f*powerOfTwoD(scaleFactor));
2686     }
2687 
2688     // Constants used in scalb
2689     static double twoToTheDoubleScaleUp = powerOfTwoD(512);
2690     static double twoToTheDoubleScaleDown = powerOfTwoD(-512);
2691 
2692     /**
2693      * Returns a floating-point power of two in the normal range.
2694      */
2695     static double powerOfTwoD(int n) {
2696         assert(n >= Double.MIN_EXPONENT && n <= Double.MAX_EXPONENT);
2697         return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
2698                                         (DoubleConsts.SIGNIFICAND_WIDTH-1))
2699                                        & DoubleConsts.EXP_BIT_MASK);
2700     }
2701 
2702     /**
2703      * Returns a floating-point power of two in the normal range.
2704      */
2705     static float powerOfTwoF(int n) {
2706         assert(n >= Float.MIN_EXPONENT && n <= Float.MAX_EXPONENT);
2707         return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
2708                                      (FloatConsts.SIGNIFICAND_WIDTH-1))
2709                                     & FloatConsts.EXP_BIT_MASK);
2710     }
2711 }