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