src/share/classes/java/lang/Math.java

Print this page




   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 
  30 /**
  31  * The class {@code Math} contains methods for performing basic
  32  * numeric operations such as the elementary exponential, logarithm,
  33  * square root, and trigonometric functions.
  34  *
  35  * <p>Unlike some of the numeric methods of class
  36  * {@code StrictMath}, all implementations of the equivalent
  37  * functions of class {@code Math} are not defined to return the
  38  * bit-for-bit same results.  This relaxation permits
  39  * better-performing implementations where strict reproducibility is
  40  * not required.
  41  *
  42  * <p>By default many of the {@code Math} methods simply call
  43  * the equivalent method in {@code StrictMath} for their
  44  * implementation.  Code generators are encouraged to use
  45  * platform-specific native libraries or microprocessor instructions,
  46  * where available, to provide higher-performance implementations of
  47  * {@code Math} methods.  Such higher-performance
  48  * implementations still must conform to the specification for


 946      * double} value next larger in magnitude.  Note that for non-NaN
 947      * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
 948      *
 949      * <p>Special Cases:
 950      * <ul>
 951      * <li> If the argument is NaN, then the result is NaN.
 952      * <li> If the argument is positive or negative infinity, then the
 953      * result is positive infinity.
 954      * <li> If the argument is positive or negative zero, then the result is
 955      * {@code Double.MIN_VALUE}.
 956      * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
 957      * the result is equal to 2<sup>971</sup>.
 958      * </ul>
 959      *
 960      * @param d the floating-point value whose ulp is to be returned
 961      * @return the size of an ulp of the argument
 962      * @author Joseph D. Darcy
 963      * @since 1.5
 964      */
 965     public static double ulp(double d) {
 966         return sun.misc.FpUtils.ulp(d);
























 967     }
 968 
 969     /**
 970      * Returns the size of an ulp of the argument.  An ulp, unit in
 971      * the last place, of a {@code float} value is the positive
 972      * distance between this floating-point value and the {@code
 973      * float} value next larger in magnitude.  Note that for non-NaN
 974      * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
 975      *
 976      * <p>Special Cases:
 977      * <ul>
 978      * <li> If the argument is NaN, then the result is NaN.
 979      * <li> If the argument is positive or negative infinity, then the
 980      * result is positive infinity.
 981      * <li> If the argument is positive or negative zero, then the result is
 982      * {@code Float.MIN_VALUE}.
 983      * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
 984      * the result is equal to 2<sup>104</sup>.
 985      * </ul>
 986      *
 987      * @param f the floating-point value whose ulp is to be returned
 988      * @return the size of an ulp of the argument
 989      * @author Joseph D. Darcy
 990      * @since 1.5
 991      */
 992     public static float ulp(float f) {
 993         return sun.misc.FpUtils.ulp(f);
























 994     }
 995 
 996     /**
 997      * Returns the signum function of the argument; zero if the argument
 998      * is zero, 1.0 if the argument is greater than zero, -1.0 if the
 999      * argument is less than zero.
1000      *
1001      * <p>Special Cases:
1002      * <ul>
1003      * <li> If the argument is NaN, then the result is NaN.
1004      * <li> If the argument is positive zero or negative zero, then the
1005      *      result is the same as the argument.
1006      * </ul>
1007      *
1008      * @param d the floating-point value whose signum is to be returned
1009      * @return the signum function of the argument
1010      * @author Joseph D. Darcy
1011      * @since 1.5
1012      */
1013     public static double signum(double d) {
1014         return sun.misc.FpUtils.signum(d);
1015     }
1016 
1017     /**
1018      * Returns the signum function of the argument; zero if the argument
1019      * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1020      * argument is less than zero.
1021      *
1022      * <p>Special Cases:
1023      * <ul>
1024      * <li> If the argument is NaN, then the result is NaN.
1025      * <li> If the argument is positive zero or negative zero, then the
1026      *      result is the same as the argument.
1027      * </ul>
1028      *
1029      * @param f the floating-point value whose signum is to be returned
1030      * @return the signum function of the argument
1031      * @author Joseph D. Darcy
1032      * @since 1.5
1033      */
1034     public static float signum(float f) {
1035         return sun.misc.FpUtils.signum(f);
1036     }
1037 
1038     /**
1039      * Returns the hyperbolic sine of a {@code double} value.
1040      * The hyperbolic sine of <i>x</i> is defined to be
1041      * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/2
1042      * where <i>e</i> is {@linkplain Math#E Euler's number}.
1043      *
1044      * <p>Special cases:
1045      * <ul>
1046      *
1047      * <li>If the argument is NaN, then the result is NaN.
1048      *
1049      * <li>If the argument is infinite, then the result is an infinity
1050      * with the same sign as the argument.
1051      *
1052      * <li>If the argument is zero, then the result is a zero with the
1053      * same sign as the argument.
1054      *
1055      * </ul>


1235     public static double log1p(double x) {
1236         return StrictMath.log1p(x);
1237     }
1238 
1239     /**
1240      * Returns the first floating-point argument with the sign of the
1241      * second floating-point argument.  Note that unlike the {@link
1242      * StrictMath#copySign(double, double) StrictMath.copySign}
1243      * method, this method does not require NaN {@code sign}
1244      * arguments to be treated as positive values; implementations are
1245      * permitted to treat some NaN arguments as positive and other NaN
1246      * arguments as negative to allow greater performance.
1247      *
1248      * @param magnitude  the parameter providing the magnitude of the result
1249      * @param sign   the parameter providing the sign of the result
1250      * @return a value with the magnitude of {@code magnitude}
1251      * and the sign of {@code sign}.
1252      * @since 1.6
1253      */
1254     public static double copySign(double magnitude, double sign) {
1255         return sun.misc.FpUtils.rawCopySign(magnitude, sign);




1256     }
1257 
1258     /**
1259      * Returns the first floating-point argument with the sign of the
1260      * second floating-point argument.  Note that unlike the {@link
1261      * StrictMath#copySign(float, float) StrictMath.copySign}
1262      * method, this method does not require NaN {@code sign}
1263      * arguments to be treated as positive values; implementations are
1264      * permitted to treat some NaN arguments as positive and other NaN
1265      * arguments as negative to allow greater performance.
1266      *
1267      * @param magnitude  the parameter providing the magnitude of the result
1268      * @param sign   the parameter providing the sign of the result
1269      * @return a value with the magnitude of {@code magnitude}
1270      * and the sign of {@code sign}.
1271      * @since 1.6
1272      */
1273     public static float copySign(float magnitude, float sign) {
1274         return sun.misc.FpUtils.rawCopySign(magnitude, sign);




1275     }
1276 
1277     /**
1278      * Returns the unbiased exponent used in the representation of a
1279      * {@code float}.  Special cases:
1280      *
1281      * <ul>
1282      * <li>If the argument is NaN or infinite, then the result is
1283      * {@link Float#MAX_EXPONENT} + 1.
1284      * <li>If the argument is zero or subnormal, then the result is
1285      * {@link Float#MIN_EXPONENT} -1.
1286      * </ul>
1287      * @param f a {@code float} value
1288      * @return the unbiased exponent of the argument
1289      * @since 1.6
1290      */
1291     public static int getExponent(float f) {
1292         return sun.misc.FpUtils.getExponent(f);






1293     }
1294 
1295     /**
1296      * Returns the unbiased exponent used in the representation of a
1297      * {@code double}.  Special cases:
1298      *
1299      * <ul>
1300      * <li>If the argument is NaN or infinite, then the result is
1301      * {@link Double#MAX_EXPONENT} + 1.
1302      * <li>If the argument is zero or subnormal, then the result is
1303      * {@link Double#MIN_EXPONENT} -1.
1304      * </ul>
1305      * @param d a {@code double} value
1306      * @return the unbiased exponent of the argument
1307      * @since 1.6
1308      */
1309     public static int getExponent(double d) {
1310         return sun.misc.FpUtils.getExponent(d);






1311     }
1312 
1313     /**
1314      * Returns the floating-point number adjacent to the first
1315      * argument in the direction of the second argument.  If both
1316      * arguments compare as equal the second argument is returned.
1317      *
1318      * <p>
1319      * Special cases:
1320      * <ul>
1321      * <li> If either argument is a NaN, then NaN is returned.
1322      *
1323      * <li> If both arguments are signed zeros, {@code direction}
1324      * is returned unchanged (as implied by the requirement of
1325      * returning the second argument if the arguments compare as
1326      * equal).
1327      *
1328      * <li> If {@code start} is
1329      * &plusmn;{@link Double#MIN_VALUE} and {@code direction}
1330      * has a value such that the result should have a smaller


1334      * <li> If {@code start} is infinite and
1335      * {@code direction} has a value such that the result should
1336      * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1337      * same sign as {@code start} is returned.
1338      *
1339      * <li> If {@code start} is equal to &plusmn;
1340      * {@link Double#MAX_VALUE} and {@code direction} has a
1341      * value such that the result should have a larger magnitude, an
1342      * infinity with same sign as {@code start} is returned.
1343      * </ul>
1344      *
1345      * @param start  starting floating-point value
1346      * @param direction value indicating which of
1347      * {@code start}'s neighbors or {@code start} should
1348      * be returned
1349      * @return The floating-point number adjacent to {@code start} in the
1350      * direction of {@code direction}.
1351      * @since 1.6
1352      */
1353     public static double nextAfter(double start, double direction) {
1354         return sun.misc.FpUtils.nextAfter(start, direction);
























































1355     }
1356 
1357     /**
1358      * Returns the floating-point number adjacent to the first
1359      * argument in the direction of the second argument.  If both
1360      * arguments compare as equal a value equivalent to the second argument
1361      * is returned.
1362      *
1363      * <p>
1364      * Special cases:
1365      * <ul>
1366      * <li> If either argument is a NaN, then NaN is returned.
1367      *
1368      * <li> If both arguments are signed zeros, a value equivalent
1369      * to {@code direction} is returned.
1370      *
1371      * <li> If {@code start} is
1372      * &plusmn;{@link Float#MIN_VALUE} and {@code direction}
1373      * has a value such that the result should have a smaller
1374      * magnitude, then a zero with the same sign as {@code start}


1377      * <li> If {@code start} is infinite and
1378      * {@code direction} has a value such that the result should
1379      * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1380      * same sign as {@code start} is returned.
1381      *
1382      * <li> If {@code start} is equal to &plusmn;
1383      * {@link Float#MAX_VALUE} and {@code direction} has a
1384      * value such that the result should have a larger magnitude, an
1385      * infinity with same sign as {@code start} is returned.
1386      * </ul>
1387      *
1388      * @param start  starting floating-point value
1389      * @param direction value indicating which of
1390      * {@code start}'s neighbors or {@code start} should
1391      * be returned
1392      * @return The floating-point number adjacent to {@code start} in the
1393      * direction of {@code direction}.
1394      * @since 1.6
1395      */
1396     public static float nextAfter(float start, double direction) {
1397         return sun.misc.FpUtils.nextAfter(start, direction);
























































1398     }
1399 
1400     /**
1401      * Returns the floating-point value adjacent to {@code d} in
1402      * the direction of positive infinity.  This method is
1403      * semantically equivalent to {@code nextAfter(d,
1404      * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1405      * implementation may run faster than its equivalent
1406      * {@code nextAfter} call.
1407      *
1408      * <p>Special Cases:
1409      * <ul>
1410      * <li> If the argument is NaN, the result is NaN.
1411      *
1412      * <li> If the argument is positive infinity, the result is
1413      * positive infinity.
1414      *
1415      * <li> If the argument is zero, the result is
1416      * {@link Double#MIN_VALUE}
1417      *
1418      * </ul>
1419      *
1420      * @param d starting floating-point value
1421      * @return The adjacent floating-point value closer to positive
1422      * infinity.
1423      * @since 1.6
1424      */
1425     public static double nextUp(double d) {
1426         return sun.misc.FpUtils.nextUp(d);






1427     }
1428 
1429     /**
1430      * Returns the floating-point value adjacent to {@code f} in
1431      * the direction of positive infinity.  This method is
1432      * semantically equivalent to {@code nextAfter(f,
1433      * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1434      * implementation may run faster than its equivalent
1435      * {@code nextAfter} call.
1436      *
1437      * <p>Special Cases:
1438      * <ul>
1439      * <li> If the argument is NaN, the result is NaN.
1440      *
1441      * <li> If the argument is positive infinity, the result is
1442      * positive infinity.
1443      *
1444      * <li> If the argument is zero, the result is
1445      * {@link Float#MIN_VALUE}
1446      *
1447      * </ul>
1448      *
1449      * @param f starting floating-point value
1450      * @return The adjacent floating-point value closer to positive
1451      * infinity.
1452      * @since 1.6
1453      */
1454     public static float nextUp(float f) {
1455         return sun.misc.FpUtils.nextUp(f);






1456     }
1457 
1458 
1459     /**
1460      * Return {@code d} &times;
1461      * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1462      * by a single correctly rounded floating-point multiply to a
1463      * member of the double value set.  See the Java
1464      * Language Specification for a discussion of floating-point
1465      * value sets.  If the exponent of the result is between {@link
1466      * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1467      * answer is calculated exactly.  If the exponent of the result
1468      * would be larger than {@code Double.MAX_EXPONENT}, an
1469      * infinity is returned.  Note that if the result is subnormal,
1470      * precision may be lost; that is, when {@code scalb(x, n)}
1471      * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1472      * <i>x</i>.  When the result is non-NaN, the result has the same
1473      * sign as {@code d}.
1474      *
1475      * <p>Special cases:
1476      * <ul>
1477      * <li> If the first argument is NaN, NaN is returned.
1478      * <li> If the first argument is infinite, then an infinity of the
1479      * same sign is returned.
1480      * <li> If the first argument is zero, then a zero of the same
1481      * sign is returned.
1482      * </ul>
1483      *
1484      * @param d number to be scaled by a power of two.
1485      * @param scaleFactor power of 2 used to scale {@code d}
1486      * @return {@code d} &times; 2<sup>{@code scaleFactor}</sup>
1487      * @since 1.6
1488      */
1489     public static double scalb(double d, int scaleFactor) {
1490         return sun.misc.FpUtils.scalb(d, scaleFactor);









































































1491     }
1492 
1493     /**
1494      * Return {@code f} &times;
1495      * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1496      * by a single correctly rounded floating-point multiply to a
1497      * member of the float value set.  See the Java
1498      * Language Specification for a discussion of floating-point
1499      * value sets.  If the exponent of the result is between {@link
1500      * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1501      * answer is calculated exactly.  If the exponent of the result
1502      * would be larger than {@code Float.MAX_EXPONENT}, an
1503      * infinity is returned.  Note that if the result is subnormal,
1504      * precision may be lost; that is, when {@code scalb(x, n)}
1505      * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1506      * <i>x</i>.  When the result is non-NaN, the result has the same
1507      * sign as {@code f}.
1508      *
1509      * <p>Special cases:
1510      * <ul>
1511      * <li> If the first argument is NaN, NaN is returned.
1512      * <li> If the first argument is infinite, then an infinity of the
1513      * same sign is returned.
1514      * <li> If the first argument is zero, then a zero of the same
1515      * sign is returned.
1516      * </ul>
1517      *
1518      * @param f number to be scaled by a power of two.
1519      * @param scaleFactor power of 2 used to scale {@code f}
1520      * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
1521      * @since 1.6
1522      */
1523     public static float scalb(float f, int scaleFactor) {
1524         return sun.misc.FpUtils.scalb(f, scaleFactor);











































1525     }
1526 }


   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


 948      * double} value next larger in magnitude.  Note that for non-NaN
 949      * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
 950      *
 951      * <p>Special Cases:
 952      * <ul>
 953      * <li> If the argument is NaN, then the result is NaN.
 954      * <li> If the argument is positive or negative infinity, then the
 955      * result is positive infinity.
 956      * <li> If the argument is positive or negative zero, then the result is
 957      * {@code Double.MIN_VALUE}.
 958      * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
 959      * the result is equal to 2<sup>971</sup>.
 960      * </ul>
 961      *
 962      * @param d the floating-point value whose ulp is to be returned
 963      * @return the size of an ulp of the argument
 964      * @author Joseph D. Darcy
 965      * @since 1.5
 966      */
 967     public static double ulp(double d) {
 968         int exp = getExponent(d);
 969 
 970         switch(exp) {
 971         case DoubleConsts.MAX_EXPONENT+1:       // NaN or infinity
 972             return Math.abs(d);
 973 
 974         case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
 975             return Double.MIN_VALUE;
 976 
 977         default:
 978             assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT;
 979 
 980             // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
 981             exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1);
 982             if (exp >= DoubleConsts.MIN_EXPONENT) {
 983                 return powerOfTwoD(exp);
 984             }
 985             else {
 986                 // return a subnormal result; left shift integer
 987                 // representation of Double.MIN_VALUE appropriate
 988                 // number of positions
 989                 return Double.longBitsToDouble(1L <<
 990                 (exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
 991             }
 992         }
 993     }
 994 
 995     /**
 996      * Returns the size of an ulp of the argument.  An ulp, unit in
 997      * the last place, of a {@code float} value is the positive
 998      * distance between this floating-point value and the {@code
 999      * float} value next larger in magnitude.  Note that for non-NaN
1000      * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
1001      *
1002      * <p>Special Cases:
1003      * <ul>
1004      * <li> If the argument is NaN, then the result is NaN.
1005      * <li> If the argument is positive or negative infinity, then the
1006      * result is positive infinity.
1007      * <li> If the argument is positive or negative zero, then the result is
1008      * {@code Float.MIN_VALUE}.
1009      * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
1010      * the result is equal to 2<sup>104</sup>.
1011      * </ul>
1012      *
1013      * @param f the floating-point value whose ulp is to be returned
1014      * @return the size of an ulp of the argument
1015      * @author Joseph D. Darcy
1016      * @since 1.5
1017      */
1018     public static float ulp(float f) {
1019         int exp = getExponent(f);
1020 
1021         switch(exp) {
1022         case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
1023             return Math.abs(f);
1024 
1025         case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
1026             return FloatConsts.MIN_VALUE;
1027 
1028         default:
1029             assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT;
1030 
1031             // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
1032             exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1);
1033             if (exp >= FloatConsts.MIN_EXPONENT) {
1034                 return powerOfTwoF(exp);
1035             }
1036             else {
1037                 // return a subnormal result; left shift integer
1038                 // representation of FloatConsts.MIN_VALUE appropriate
1039                 // number of positions
1040                 return Float.intBitsToFloat(1 <<
1041                 (exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
1042             }
1043         }
1044     }
1045 
1046     /**
1047      * Returns the signum function of the argument; zero if the argument
1048      * is zero, 1.0 if the argument is greater than zero, -1.0 if the
1049      * argument is less than zero.
1050      *
1051      * <p>Special Cases:
1052      * <ul>
1053      * <li> If the argument is NaN, then the result is NaN.
1054      * <li> If the argument is positive zero or negative zero, then the
1055      *      result is the same as the argument.
1056      * </ul>
1057      *
1058      * @param d the floating-point value whose signum is to be returned
1059      * @return the signum function of the argument
1060      * @author Joseph D. Darcy
1061      * @since 1.5
1062      */
1063     public static double signum(double d) {
1064         return (d == 0.0 || Double.isNaN(d))?d:copySign(1.0, d);
1065     }
1066 
1067     /**
1068      * Returns the signum function of the argument; zero if the argument
1069      * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1070      * argument is less than zero.
1071      *
1072      * <p>Special Cases:
1073      * <ul>
1074      * <li> If the argument is NaN, then the result is NaN.
1075      * <li> If the argument is positive zero or negative zero, then the
1076      *      result is the same as the argument.
1077      * </ul>
1078      *
1079      * @param f the floating-point value whose signum is to be returned
1080      * @return the signum function of the argument
1081      * @author Joseph D. Darcy
1082      * @since 1.5
1083      */
1084     public static float signum(float f) {
1085         return (f == 0.0f || Float.isNaN(f))?f:copySign(1.0f, f);
1086     }
1087 
1088     /**
1089      * Returns the hyperbolic sine of a {@code double} value.
1090      * The hyperbolic sine of <i>x</i> is defined to be
1091      * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/2
1092      * where <i>e</i> is {@linkplain Math#E Euler's number}.
1093      *
1094      * <p>Special cases:
1095      * <ul>
1096      *
1097      * <li>If the argument is NaN, then the result is NaN.
1098      *
1099      * <li>If the argument is infinite, then the result is an infinity
1100      * with the same sign as the argument.
1101      *
1102      * <li>If the argument is zero, then the result is a zero with the
1103      * same sign as the argument.
1104      *
1105      * </ul>


1285     public static double log1p(double x) {
1286         return StrictMath.log1p(x);
1287     }
1288 
1289     /**
1290      * Returns the first floating-point argument with the sign of the
1291      * second floating-point argument.  Note that unlike the {@link
1292      * StrictMath#copySign(double, double) StrictMath.copySign}
1293      * method, this method does not require NaN {@code sign}
1294      * arguments to be treated as positive values; implementations are
1295      * permitted to treat some NaN arguments as positive and other NaN
1296      * arguments as negative to allow greater performance.
1297      *
1298      * @param magnitude  the parameter providing the magnitude of the result
1299      * @param sign   the parameter providing the sign of the result
1300      * @return a value with the magnitude of {@code magnitude}
1301      * and the sign of {@code sign}.
1302      * @since 1.6
1303      */
1304     public static double copySign(double magnitude, double sign) {
1305         return Double.longBitsToDouble((Double.doubleToRawLongBits(sign) &
1306                                         (DoubleConsts.SIGN_BIT_MASK)) |
1307                                        (Double.doubleToRawLongBits(magnitude) &
1308                                         (DoubleConsts.EXP_BIT_MASK |
1309                                          DoubleConsts.SIGNIF_BIT_MASK)));
1310     }
1311 
1312     /**
1313      * Returns the first floating-point argument with the sign of the
1314      * second floating-point argument.  Note that unlike the {@link
1315      * StrictMath#copySign(float, float) StrictMath.copySign}
1316      * method, this method does not require NaN {@code sign}
1317      * arguments to be treated as positive values; implementations are
1318      * permitted to treat some NaN arguments as positive and other NaN
1319      * arguments as negative to allow greater performance.
1320      *
1321      * @param magnitude  the parameter providing the magnitude of the result
1322      * @param sign   the parameter providing the sign of the result
1323      * @return a value with the magnitude of {@code magnitude}
1324      * and the sign of {@code sign}.
1325      * @since 1.6
1326      */
1327     public static float copySign(float magnitude, float sign) {
1328         return Float.intBitsToFloat((Float.floatToRawIntBits(sign) &
1329                                      (FloatConsts.SIGN_BIT_MASK)) |
1330                                     (Float.floatToRawIntBits(magnitude) &
1331                                      (FloatConsts.EXP_BIT_MASK |
1332                                       FloatConsts.SIGNIF_BIT_MASK)));
1333     }
1334 
1335     /**
1336      * Returns the unbiased exponent used in the representation of a
1337      * {@code float}.  Special cases:
1338      *
1339      * <ul>
1340      * <li>If the argument is NaN or infinite, then the result is
1341      * {@link Float#MAX_EXPONENT} + 1.
1342      * <li>If the argument is zero or subnormal, then the result is
1343      * {@link Float#MIN_EXPONENT} -1.
1344      * </ul>
1345      * @param f a {@code float} value
1346      * @return the unbiased exponent of the argument
1347      * @since 1.6
1348      */
1349     public static int getExponent(float f) {
1350         /*
1351          * Bitwise convert f to integer, mask out exponent bits, shift
1352          * to the right and then subtract out float's bias adjust to
1353          * get true exponent value
1354          */
1355         return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
1356                 (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
1357     }
1358 
1359     /**
1360      * Returns the unbiased exponent used in the representation of a
1361      * {@code double}.  Special cases:
1362      *
1363      * <ul>
1364      * <li>If the argument is NaN or infinite, then the result is
1365      * {@link Double#MAX_EXPONENT} + 1.
1366      * <li>If the argument is zero or subnormal, then the result is
1367      * {@link Double#MIN_EXPONENT} -1.
1368      * </ul>
1369      * @param d a {@code double} value
1370      * @return the unbiased exponent of the argument
1371      * @since 1.6
1372      */
1373     public static int getExponent(double d) {
1374         /*
1375          * Bitwise convert d to long, mask out exponent bits, shift
1376          * to the right and then subtract out double's bias adjust to
1377          * get true exponent value.
1378          */
1379         return (int)(((Double.doubleToRawLongBits(d) & DoubleConsts.EXP_BIT_MASK) >>
1380                       (DoubleConsts.SIGNIFICAND_WIDTH - 1)) - DoubleConsts.EXP_BIAS);
1381     }
1382 
1383     /**
1384      * Returns the floating-point number adjacent to the first
1385      * argument in the direction of the second argument.  If both
1386      * arguments compare as equal the second argument is returned.
1387      *
1388      * <p>
1389      * Special cases:
1390      * <ul>
1391      * <li> If either argument is a NaN, then NaN is returned.
1392      *
1393      * <li> If both arguments are signed zeros, {@code direction}
1394      * is returned unchanged (as implied by the requirement of
1395      * returning the second argument if the arguments compare as
1396      * equal).
1397      *
1398      * <li> If {@code start} is
1399      * &plusmn;{@link Double#MIN_VALUE} and {@code direction}
1400      * has a value such that the result should have a smaller


1404      * <li> If {@code start} is infinite and
1405      * {@code direction} has a value such that the result should
1406      * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1407      * same sign as {@code start} is returned.
1408      *
1409      * <li> If {@code start} is equal to &plusmn;
1410      * {@link Double#MAX_VALUE} and {@code direction} has a
1411      * value such that the result should have a larger magnitude, an
1412      * infinity with same sign as {@code start} is returned.
1413      * </ul>
1414      *
1415      * @param start  starting floating-point value
1416      * @param direction value indicating which of
1417      * {@code start}'s neighbors or {@code start} should
1418      * be returned
1419      * @return The floating-point number adjacent to {@code start} in the
1420      * direction of {@code direction}.
1421      * @since 1.6
1422      */
1423     public static double nextAfter(double start, double direction) {
1424         /*
1425          * The cases:
1426          *
1427          * nextAfter(+infinity, 0)  == MAX_VALUE
1428          * nextAfter(+infinity, +infinity)  == +infinity
1429          * nextAfter(-infinity, 0)  == -MAX_VALUE
1430          * nextAfter(-infinity, -infinity)  == -infinity
1431          *
1432          * are naturally handled without any additional testing
1433          */
1434 
1435         // First check for NaN values
1436         if (Double.isNaN(start) || Double.isNaN(direction)) {
1437             // return a NaN derived from the input NaN(s)
1438             return start + direction;
1439         } else if (start == direction) {
1440             return direction;
1441         } else {        // start > direction or start < direction
1442             // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
1443             // then bitwise convert start to integer.
1444             long transducer = Double.doubleToRawLongBits(start + 0.0d);
1445 
1446             /*
1447              * IEEE 754 floating-point numbers are lexicographically
1448              * ordered if treated as signed- magnitude integers .
1449              * Since Java's integers are two's complement,
1450              * incrementing" the two's complement representation of a
1451              * logically negative floating-point value *decrements*
1452              * the signed-magnitude representation. Therefore, when
1453              * the integer representation of a floating-point values
1454              * is less than zero, the adjustment to the representation
1455              * is in the opposite direction than would be expected at
1456              * first .
1457              */
1458             if (direction > start) { // Calculate next greater value
1459                 transducer = transducer + (transducer >= 0L ? 1L:-1L);
1460             } else  { // Calculate next lesser value
1461                 assert direction < start;
1462                 if (transducer > 0L)
1463                     --transducer;
1464                 else
1465                     if (transducer < 0L )
1466                         ++transducer;
1467                     /*
1468                      * transducer==0, the result is -MIN_VALUE
1469                      *
1470                      * The transition from zero (implicitly
1471                      * positive) to the smallest negative
1472                      * signed magnitude value must be done
1473                      * explicitly.
1474                      */
1475                     else
1476                         transducer = DoubleConsts.SIGN_BIT_MASK | 1L;
1477             }
1478 
1479             return Double.longBitsToDouble(transducer);
1480         }
1481     }
1482 
1483     /**
1484      * Returns the floating-point number adjacent to the first
1485      * argument in the direction of the second argument.  If both
1486      * arguments compare as equal a value equivalent to the second argument
1487      * is returned.
1488      *
1489      * <p>
1490      * Special cases:
1491      * <ul>
1492      * <li> If either argument is a NaN, then NaN is returned.
1493      *
1494      * <li> If both arguments are signed zeros, a value equivalent
1495      * to {@code direction} is returned.
1496      *
1497      * <li> If {@code start} is
1498      * &plusmn;{@link Float#MIN_VALUE} and {@code direction}
1499      * has a value such that the result should have a smaller
1500      * magnitude, then a zero with the same sign as {@code start}


1503      * <li> If {@code start} is infinite and
1504      * {@code direction} has a value such that the result should
1505      * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1506      * same sign as {@code start} is returned.
1507      *
1508      * <li> If {@code start} is equal to &plusmn;
1509      * {@link Float#MAX_VALUE} and {@code direction} has a
1510      * value such that the result should have a larger magnitude, an
1511      * infinity with same sign as {@code start} is returned.
1512      * </ul>
1513      *
1514      * @param start  starting floating-point value
1515      * @param direction value indicating which of
1516      * {@code start}'s neighbors or {@code start} should
1517      * be returned
1518      * @return The floating-point number adjacent to {@code start} in the
1519      * direction of {@code direction}.
1520      * @since 1.6
1521      */
1522     public static float nextAfter(float start, double direction) {
1523         /*
1524          * The cases:
1525          *
1526          * nextAfter(+infinity, 0)  == MAX_VALUE
1527          * nextAfter(+infinity, +infinity)  == +infinity
1528          * nextAfter(-infinity, 0)  == -MAX_VALUE
1529          * nextAfter(-infinity, -infinity)  == -infinity
1530          *
1531          * are naturally handled without any additional testing
1532          */
1533 
1534         // First check for NaN values
1535         if (Float.isNaN(start) || Double.isNaN(direction)) {
1536             // return a NaN derived from the input NaN(s)
1537             return start + (float)direction;
1538         } else if (start == direction) {
1539             return (float)direction;
1540         } else {        // start > direction or start < direction
1541             // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
1542             // then bitwise convert start to integer.
1543             int transducer = Float.floatToRawIntBits(start + 0.0f);
1544 
1545             /*
1546              * IEEE 754 floating-point numbers are lexicographically
1547              * ordered if treated as signed- magnitude integers .
1548              * Since Java's integers are two's complement,
1549              * incrementing" the two's complement representation of a
1550              * logically negative floating-point value *decrements*
1551              * the signed-magnitude representation. Therefore, when
1552              * the integer representation of a floating-point values
1553              * is less than zero, the adjustment to the representation
1554              * is in the opposite direction than would be expected at
1555              * first.
1556              */
1557             if (direction > start) {// Calculate next greater value
1558                 transducer = transducer + (transducer >= 0 ? 1:-1);
1559             } else  { // Calculate next lesser value
1560                 assert direction < start;
1561                 if (transducer > 0)
1562                     --transducer;
1563                 else
1564                     if (transducer < 0 )
1565                         ++transducer;
1566                     /*
1567                      * transducer==0, the result is -MIN_VALUE
1568                      *
1569                      * The transition from zero (implicitly
1570                      * positive) to the smallest negative
1571                      * signed magnitude value must be done
1572                      * explicitly.
1573                      */
1574                     else
1575                         transducer = FloatConsts.SIGN_BIT_MASK | 1;
1576             }
1577 
1578             return Float.intBitsToFloat(transducer);
1579         }
1580     }
1581 
1582     /**
1583      * Returns the floating-point value adjacent to {@code d} in
1584      * the direction of positive infinity.  This method is
1585      * semantically equivalent to {@code nextAfter(d,
1586      * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1587      * implementation may run faster than its equivalent
1588      * {@code nextAfter} call.
1589      *
1590      * <p>Special Cases:
1591      * <ul>
1592      * <li> If the argument is NaN, the result is NaN.
1593      *
1594      * <li> If the argument is positive infinity, the result is
1595      * positive infinity.
1596      *
1597      * <li> If the argument is zero, the result is
1598      * {@link Double#MIN_VALUE}
1599      *
1600      * </ul>
1601      *
1602      * @param d starting floating-point value
1603      * @return The adjacent floating-point value closer to positive
1604      * infinity.
1605      * @since 1.6
1606      */
1607     public static double nextUp(double d) {
1608         if( Double.isNaN(d) || d == Double.POSITIVE_INFINITY)
1609             return d;
1610         else {
1611             d += 0.0d;
1612             return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
1613                                            ((d >= 0.0d)?+1L:-1L));
1614         }
1615     }
1616 
1617     /**
1618      * Returns the floating-point value adjacent to {@code f} in
1619      * the direction of positive infinity.  This method is
1620      * semantically equivalent to {@code nextAfter(f,
1621      * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1622      * implementation may run faster than its equivalent
1623      * {@code nextAfter} call.
1624      *
1625      * <p>Special Cases:
1626      * <ul>
1627      * <li> If the argument is NaN, the result is NaN.
1628      *
1629      * <li> If the argument is positive infinity, the result is
1630      * positive infinity.
1631      *
1632      * <li> If the argument is zero, the result is
1633      * {@link Float#MIN_VALUE}
1634      *
1635      * </ul>
1636      *
1637      * @param f starting floating-point value
1638      * @return The adjacent floating-point value closer to positive
1639      * infinity.
1640      * @since 1.6
1641      */
1642     public static float nextUp(float f) {
1643         if( Float.isNaN(f) || f == FloatConsts.POSITIVE_INFINITY)
1644             return f;
1645         else {
1646             f += 0.0f;
1647             return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
1648                                         ((f >= 0.0f)?+1:-1));
1649         }
1650     }
1651 
1652 
1653     /**
1654      * Return {@code d} &times;
1655      * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1656      * by a single correctly rounded floating-point multiply to a
1657      * member of the double value set.  See the Java
1658      * Language Specification for a discussion of floating-point
1659      * value sets.  If the exponent of the result is between {@link
1660      * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1661      * answer is calculated exactly.  If the exponent of the result
1662      * would be larger than {@code Double.MAX_EXPONENT}, an
1663      * infinity is returned.  Note that if the result is subnormal,
1664      * precision may be lost; that is, when {@code scalb(x, n)}
1665      * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1666      * <i>x</i>.  When the result is non-NaN, the result has the same
1667      * sign as {@code d}.
1668      *
1669      * <p>Special cases:
1670      * <ul>
1671      * <li> If the first argument is NaN, NaN is returned.
1672      * <li> If the first argument is infinite, then an infinity of the
1673      * same sign is returned.
1674      * <li> If the first argument is zero, then a zero of the same
1675      * sign is returned.
1676      * </ul>
1677      *
1678      * @param d number to be scaled by a power of two.
1679      * @param scaleFactor power of 2 used to scale {@code d}
1680      * @return {@code d} &times; 2<sup>{@code scaleFactor}</sup>
1681      * @since 1.6
1682      */
1683     public static double scalb(double d, int scaleFactor) {
1684         /*
1685          * This method does not need to be declared strictfp to
1686          * compute the same correct result on all platforms.  When
1687          * scaling up, it does not matter what order the
1688          * multiply-store operations are done; the result will be
1689          * finite or overflow regardless of the operation ordering.
1690          * However, to get the correct result when scaling down, a
1691          * particular ordering must be used.
1692          *
1693          * When scaling down, the multiply-store operations are
1694          * sequenced so that it is not possible for two consecutive
1695          * multiply-stores to return subnormal results.  If one
1696          * multiply-store result is subnormal, the next multiply will
1697          * round it away to zero.  This is done by first multiplying
1698          * by 2 ^ (scaleFactor % n) and then multiplying several
1699          * times by by 2^n as needed where n is the exponent of number
1700          * that is a covenient power of two.  In this way, at most one
1701          * real rounding error occurs.  If the double value set is
1702          * being used exclusively, the rounding will occur on a
1703          * multiply.  If the double-extended-exponent value set is
1704          * being used, the products will (perhaps) be exact but the
1705          * stores to d are guaranteed to round to the double value
1706          * set.
1707          *
1708          * It is _not_ a valid implementation to first multiply d by
1709          * 2^MIN_EXPONENT and then by 2 ^ (scaleFactor %
1710          * MIN_EXPONENT) since even in a strictfp program double
1711          * rounding on underflow could occur; e.g. if the scaleFactor
1712          * argument was (MIN_EXPONENT - n) and the exponent of d was a
1713          * little less than -(MIN_EXPONENT - n), meaning the final
1714          * result would be subnormal.
1715          *
1716          * Since exact reproducibility of this method can be achieved
1717          * without any undue performance burden, there is no
1718          * compelling reason to allow double rounding on underflow in
1719          * scalb.
1720          */
1721 
1722         // magnitude of a power of two so large that scaling a finite
1723         // nonzero value by it would be guaranteed to over or
1724         // underflow; due to rounding, scaling down takes takes an
1725         // additional power of two which is reflected here
1726         final int MAX_SCALE = DoubleConsts.MAX_EXPONENT + -DoubleConsts.MIN_EXPONENT +
1727                               DoubleConsts.SIGNIFICAND_WIDTH + 1;
1728         int exp_adjust = 0;
1729         int scale_increment = 0;
1730         double exp_delta = Double.NaN;
1731 
1732         // Make sure scaling factor is in a reasonable range
1733 
1734         if(scaleFactor < 0) {
1735             scaleFactor = Math.max(scaleFactor, -MAX_SCALE);
1736             scale_increment = -512;
1737             exp_delta = twoToTheDoubleScaleDown;
1738         }
1739         else {
1740             scaleFactor = Math.min(scaleFactor, MAX_SCALE);
1741             scale_increment = 512;
1742             exp_delta = twoToTheDoubleScaleUp;
1743         }
1744 
1745         // Calculate (scaleFactor % +/-512), 512 = 2^9, using
1746         // technique from "Hacker's Delight" section 10-2.
1747         int t = (scaleFactor >> 9-1) >>> 32 - 9;
1748         exp_adjust = ((scaleFactor + t) & (512 -1)) - t;
1749 
1750         d *= powerOfTwoD(exp_adjust);
1751         scaleFactor -= exp_adjust;
1752 
1753         while(scaleFactor != 0) {
1754             d *= exp_delta;
1755             scaleFactor -= scale_increment;
1756         }
1757         return d;
1758     }
1759 
1760     /**
1761      * Return {@code f} &times;
1762      * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1763      * by a single correctly rounded floating-point multiply to a
1764      * member of the float value set.  See the Java
1765      * Language Specification for a discussion of floating-point
1766      * value sets.  If the exponent of the result is between {@link
1767      * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1768      * answer is calculated exactly.  If the exponent of the result
1769      * would be larger than {@code Float.MAX_EXPONENT}, an
1770      * infinity is returned.  Note that if the result is subnormal,
1771      * precision may be lost; that is, when {@code scalb(x, n)}
1772      * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1773      * <i>x</i>.  When the result is non-NaN, the result has the same
1774      * sign as {@code f}.
1775      *
1776      * <p>Special cases:
1777      * <ul>
1778      * <li> If the first argument is NaN, NaN is returned.
1779      * <li> If the first argument is infinite, then an infinity of the
1780      * same sign is returned.
1781      * <li> If the first argument is zero, then a zero of the same
1782      * sign is returned.
1783      * </ul>
1784      *
1785      * @param f number to be scaled by a power of two.
1786      * @param scaleFactor power of 2 used to scale {@code f}
1787      * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
1788      * @since 1.6
1789      */
1790     public static float scalb(float f, int scaleFactor) {
1791         // magnitude of a power of two so large that scaling a finite
1792         // nonzero value by it would be guaranteed to over or
1793         // underflow; due to rounding, scaling down takes takes an
1794         // additional power of two which is reflected here
1795         final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
1796                               FloatConsts.SIGNIFICAND_WIDTH + 1;
1797 
1798         // Make sure scaling factor is in a reasonable range
1799         scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);
1800 
1801         /*
1802          * Since + MAX_SCALE for float fits well within the double
1803          * exponent range and + float -> double conversion is exact
1804          * the multiplication below will be exact. Therefore, the
1805          * rounding that occurs when the double product is cast to
1806          * float will be the correctly rounded float result.  Since
1807          * all operations other than the final multiply will be exact,
1808          * it is not necessary to declare this method strictfp.
1809          */
1810         return (float)((double)f*powerOfTwoD(scaleFactor));
1811     }
1812 
1813     // Constants used in scalb
1814     static double twoToTheDoubleScaleUp = powerOfTwoD(512);
1815     static double twoToTheDoubleScaleDown = powerOfTwoD(-512);
1816 
1817     /**
1818      * Returns a floating-point power of two in the normal range.
1819      */
1820     static double powerOfTwoD(int n) {
1821         assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
1822         return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
1823                                         (DoubleConsts.SIGNIFICAND_WIDTH-1))
1824                                        & DoubleConsts.EXP_BIT_MASK);
1825     }
1826 
1827     /**
1828      * Returns a floating-point power of two in the normal range.
1829      */
1830     public static float powerOfTwoF(int n) {
1831         assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
1832         return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
1833                                      (FloatConsts.SIGNIFICAND_WIDTH-1))
1834                                     & FloatConsts.EXP_BIT_MASK);
1835     }
1836 }