0001: /*
0002: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
0003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0004: *
0005: * This code is free software; you can redistribute it and/or modify it
0006: * under the terms of the GNU General Public License version 2 only, as
0007: * published by the Free Software Foundation. Sun designates this
0008: * particular file as subject to the "Classpath" exception as provided
0009: * by Sun in the LICENSE file that accompanied this code.
0010: *
0011: * This code is distributed in the hope that it will be useful, but WITHOUT
0012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0014: * version 2 for more details (a copy is included in the LICENSE file that
0015: * accompanied this code).
0016: *
0017: * You should have received a copy of the GNU General Public License version
0018: * 2 along with this work; if not, write to the Free Software Foundation,
0019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0020: *
0021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0022: * CA 95054 USA or visit www.sun.com if you need additional information or
0023: * have any questions.
0024: */
0025:
0026: package java.lang;
0027:
0028: import java.util.Random;
0029: import sun.misc.FpUtils;
0030:
0031: /**
0032: * The class {@code StrictMath} contains methods for performing basic
0033: * numeric operations such as the elementary exponential, logarithm,
0034: * square root, and trigonometric functions.
0035: *
0036: * <p>To help ensure portability of Java programs, the definitions of
0037: * some of the numeric functions in this package require that they
0038: * produce the same results as certain published algorithms. These
0039: * algorithms are available from the well-known network library
0040: * {@code netlib} as the package "Freely Distributable Math
0041: * Library," <a
0042: * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These
0043: * algorithms, which are written in the C programming language, are
0044: * then to be understood as executed with all floating-point
0045: * operations following the rules of Java floating-point arithmetic.
0046: *
0047: * <p>The Java math library is defined with respect to
0048: * {@code fdlibm} version 5.3. Where {@code fdlibm} provides
0049: * more than one definition for a function (such as
0050: * {@code acos}), use the "IEEE 754 core function" version
0051: * (residing in a file whose name begins with the letter
0052: * {@code e}). The methods which require {@code fdlibm}
0053: * semantics are {@code sin}, {@code cos}, {@code tan},
0054: * {@code asin}, {@code acos}, {@code atan},
0055: * {@code exp}, {@code log}, {@code log10},
0056: * {@code cbrt}, {@code atan2}, {@code pow},
0057: * {@code sinh}, {@code cosh}, {@code tanh},
0058: * {@code hypot}, {@code expm1}, and {@code log1p}.
0059: *
0060: * @author unascribed
0061: * @author Joseph D. Darcy
0062: * @version 1.37, 06/20/07
0063: * @since 1.3
0064: */
0065:
0066: public final class StrictMath {
0067:
0068: /**
0069: * Don't let anyone instantiate this class.
0070: */
0071: private StrictMath() {
0072: }
0073:
0074: /**
0075: * The {@code double} value that is closer than any other to
0076: * <i>e</i>, the base of the natural logarithms.
0077: */
0078: public static final double E = 2.7182818284590452354;
0079:
0080: /**
0081: * The {@code double} value that is closer than any other to
0082: * <i>pi</i>, the ratio of the circumference of a circle to its
0083: * diameter.
0084: */
0085: public static final double PI = 3.14159265358979323846;
0086:
0087: /**
0088: * Returns the trigonometric sine of an angle. Special cases:
0089: * <ul><li>If the argument is NaN or an infinity, then the
0090: * result is NaN.
0091: * <li>If the argument is zero, then the result is a zero with the
0092: * same sign as the argument.</ul>
0093: *
0094: * @param a an angle, in radians.
0095: * @return the sine of the argument.
0096: */
0097: public static native double sin(double a);
0098:
0099: /**
0100: * Returns the trigonometric cosine of an angle. Special cases:
0101: * <ul><li>If the argument is NaN or an infinity, then the
0102: * result is NaN.</ul>
0103: *
0104: * @param a an angle, in radians.
0105: * @return the cosine of the argument.
0106: */
0107: public static native double cos(double a);
0108:
0109: /**
0110: * Returns the trigonometric tangent of an angle. Special cases:
0111: * <ul><li>If the argument is NaN or an infinity, then the result
0112: * is NaN.
0113: * <li>If the argument is zero, then the result is a zero with the
0114: * same sign as the argument.</ul>
0115: *
0116: * @param a an angle, in radians.
0117: * @return the tangent of the argument.
0118: */
0119: public static native double tan(double a);
0120:
0121: /**
0122: * Returns the arc sine of a value; the returned angle is in the
0123: * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
0124: * <ul><li>If the argument is NaN or its absolute value is greater
0125: * than 1, then the result is NaN.
0126: * <li>If the argument is zero, then the result is a zero with the
0127: * same sign as the argument.</ul>
0128: *
0129: * @param a the value whose arc sine is to be returned.
0130: * @return the arc sine of the argument.
0131: */
0132: public static native double asin(double a);
0133:
0134: /**
0135: * Returns the arc cosine of a value; the returned angle is in the
0136: * range 0.0 through <i>pi</i>. Special case:
0137: * <ul><li>If the argument is NaN or its absolute value is greater
0138: * than 1, then the result is NaN.</ul>
0139: *
0140: * @param a the value whose arc cosine is to be returned.
0141: * @return the arc cosine of the argument.
0142: */
0143: public static native double acos(double a);
0144:
0145: /**
0146: * Returns the arc tangent of a value; the returned angle is in the
0147: * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
0148: * <ul><li>If the argument is NaN, then the result is NaN.
0149: * <li>If the argument is zero, then the result is a zero with the
0150: * same sign as the argument.</ul>
0151: *
0152: * @param a the value whose arc tangent is to be returned.
0153: * @return the arc tangent of the argument.
0154: */
0155: public static native double atan(double a);
0156:
0157: /**
0158: * Converts an angle measured in degrees to an approximately
0159: * equivalent angle measured in radians. The conversion from
0160: * degrees to radians is generally inexact.
0161: *
0162: * @param angdeg an angle, in degrees
0163: * @return the measurement of the angle {@code angdeg}
0164: * in radians.
0165: */
0166: public static strictfp double toRadians(double angdeg) {
0167: return angdeg / 180.0 * PI;
0168: }
0169:
0170: /**
0171: * Converts an angle measured in radians to an approximately
0172: * equivalent angle measured in degrees. The conversion from
0173: * radians to degrees is generally inexact; users should
0174: * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
0175: * equal {@code 0.0}.
0176: *
0177: * @param angrad an angle, in radians
0178: * @return the measurement of the angle {@code angrad}
0179: * in degrees.
0180: */
0181: public static strictfp double toDegrees(double angrad) {
0182: return angrad * 180.0 / PI;
0183: }
0184:
0185: /**
0186: * Returns Euler's number <i>e</i> raised to the power of a
0187: * {@code double} value. Special cases:
0188: * <ul><li>If the argument is NaN, the result is NaN.
0189: * <li>If the argument is positive infinity, then the result is
0190: * positive infinity.
0191: * <li>If the argument is negative infinity, then the result is
0192: * positive zero.</ul>
0193: *
0194: * @param a the exponent to raise <i>e</i> to.
0195: * @return the value <i>e</i><sup>{@code a}</sup>,
0196: * where <i>e</i> is the base of the natural logarithms.
0197: */
0198: public static native double exp(double a);
0199:
0200: /**
0201: * Returns the natural logarithm (base <i>e</i>) of a {@code double}
0202: * value. Special cases:
0203: * <ul><li>If the argument is NaN or less than zero, then the result
0204: * is NaN.
0205: * <li>If the argument is positive infinity, then the result is
0206: * positive infinity.
0207: * <li>If the argument is positive zero or negative zero, then the
0208: * result is negative infinity.</ul>
0209: *
0210: * @param a a value
0211: * @return the value ln {@code a}, the natural logarithm of
0212: * {@code a}.
0213: */
0214: public static native double log(double a);
0215:
0216: /**
0217: * Returns the base 10 logarithm of a {@code double} value.
0218: * Special cases:
0219: *
0220: * <ul><li>If the argument is NaN or less than zero, then the result
0221: * is NaN.
0222: * <li>If the argument is positive infinity, then the result is
0223: * positive infinity.
0224: * <li>If the argument is positive zero or negative zero, then the
0225: * result is negative infinity.
0226: * <li> If the argument is equal to 10<sup><i>n</i></sup> for
0227: * integer <i>n</i>, then the result is <i>n</i>.
0228: * </ul>
0229: *
0230: * @param a a value
0231: * @return the base 10 logarithm of {@code a}.
0232: * @since 1.5
0233: */
0234: public static native double log10(double a);
0235:
0236: /**
0237: * Returns the correctly rounded positive square root of a
0238: * {@code double} value.
0239: * Special cases:
0240: * <ul><li>If the argument is NaN or less than zero, then the result
0241: * is NaN.
0242: * <li>If the argument is positive infinity, then the result is positive
0243: * infinity.
0244: * <li>If the argument is positive zero or negative zero, then the
0245: * result is the same as the argument.</ul>
0246: * Otherwise, the result is the {@code double} value closest to
0247: * the true mathematical square root of the argument value.
0248: *
0249: * @param a a value.
0250: * @return the positive square root of {@code a}.
0251: */
0252: public static native double sqrt(double a);
0253:
0254: /**
0255: * Returns the cube root of a {@code double} value. For
0256: * positive finite {@code x}, {@code cbrt(-x) ==
0257: * -cbrt(x)}; that is, the cube root of a negative value is
0258: * the negative of the cube root of that value's magnitude.
0259: * Special cases:
0260: *
0261: * <ul>
0262: *
0263: * <li>If the argument is NaN, then the result is NaN.
0264: *
0265: * <li>If the argument is infinite, then the result is an infinity
0266: * with the same sign as the argument.
0267: *
0268: * <li>If the argument is zero, then the result is a zero with the
0269: * same sign as the argument.
0270: *
0271: * </ul>
0272: *
0273: * @param a a value.
0274: * @return the cube root of {@code a}.
0275: * @since 1.5
0276: */
0277: public static native double cbrt(double a);
0278:
0279: /**
0280: * Computes the remainder operation on two arguments as prescribed
0281: * by the IEEE 754 standard.
0282: * The remainder value is mathematically equal to
0283: * <code>f1 - f2</code> × <i>n</i>,
0284: * where <i>n</i> is the mathematical integer closest to the exact
0285: * mathematical value of the quotient {@code f1/f2}, and if two
0286: * mathematical integers are equally close to {@code f1/f2},
0287: * then <i>n</i> is the integer that is even. If the remainder is
0288: * zero, its sign is the same as the sign of the first argument.
0289: * Special cases:
0290: * <ul><li>If either argument is NaN, or the first argument is infinite,
0291: * or the second argument is positive zero or negative zero, then the
0292: * result is NaN.
0293: * <li>If the first argument is finite and the second argument is
0294: * infinite, then the result is the same as the first argument.</ul>
0295: *
0296: * @param f1 the dividend.
0297: * @param f2 the divisor.
0298: * @return the remainder when {@code f1} is divided by
0299: * {@code f2}.
0300: */
0301: public static native double IEEEremainder(double f1, double f2);
0302:
0303: /**
0304: * Returns the smallest (closest to negative infinity)
0305: * {@code double} value that is greater than or equal to the
0306: * argument and is equal to a mathematical integer. Special cases:
0307: * <ul><li>If the argument value is already equal to a
0308: * mathematical integer, then the result is the same as the
0309: * argument. <li>If the argument is NaN or an infinity or
0310: * positive zero or negative zero, then the result is the same as
0311: * the argument. <li>If the argument value is less than zero but
0312: * greater than -1.0, then the result is negative zero.</ul> Note
0313: * that the value of {@code StrictMath.ceil(x)} is exactly the
0314: * value of {@code -StrictMath.floor(-x)}.
0315: *
0316: * @param a a value.
0317: * @return the smallest (closest to negative infinity)
0318: * floating-point value that is greater than or equal to
0319: * the argument and is equal to a mathematical integer.
0320: */
0321: public static native double ceil(double a);
0322:
0323: /**
0324: * Returns the largest (closest to positive infinity)
0325: * {@code double} value that is less than or equal to the
0326: * argument and is equal to a mathematical integer. Special cases:
0327: * <ul><li>If the argument value is already equal to a
0328: * mathematical integer, then the result is the same as the
0329: * argument. <li>If the argument is NaN or an infinity or
0330: * positive zero or negative zero, then the result is the same as
0331: * the argument.</ul>
0332: *
0333: * @param a a value.
0334: * @return the largest (closest to positive infinity)
0335: * floating-point value that less than or equal to the argument
0336: * and is equal to a mathematical integer.
0337: */
0338: public static native double floor(double a);
0339:
0340: /**
0341: * Returns the {@code double} value that is closest in value
0342: * to the argument and is equal to a mathematical integer. If two
0343: * {@code double} values that are mathematical integers are
0344: * equally close to the value of the argument, the result is the
0345: * integer value that is even. Special cases:
0346: * <ul><li>If the argument value is already equal to a mathematical
0347: * integer, then the result is the same as the argument.
0348: * <li>If the argument is NaN or an infinity or positive zero or negative
0349: * zero, then the result is the same as the argument.</ul>
0350: *
0351: * @param a a value.
0352: * @return the closest floating-point value to {@code a} that is
0353: * equal to a mathematical integer.
0354: * @author Joseph D. Darcy
0355: */
0356: public static double rint(double a) {
0357: /*
0358: * If the absolute value of a is not less than 2^52, it
0359: * is either a finite integer (the double format does not have
0360: * enough significand bits for a number that large to have any
0361: * fractional portion), an infinity, or a NaN. In any of
0362: * these cases, rint of the argument is the argument.
0363: *
0364: * Otherwise, the sum (twoToThe52 + a ) will properly round
0365: * away any fractional portion of a since ulp(twoToThe52) ==
0366: * 1.0; subtracting out twoToThe52 from this sum will then be
0367: * exact and leave the rounded integer portion of a.
0368: *
0369: * This method does *not* need to be declared strictfp to get
0370: * fully reproducible results. Whether or not a method is
0371: * declared strictfp can only make a difference in the
0372: * returned result if some operation would overflow or
0373: * underflow with strictfp semantics. The operation
0374: * (twoToThe52 + a ) cannot overflow since large values of a
0375: * are screened out; the add cannot underflow since twoToThe52
0376: * is too large. The subtraction ((twoToThe52 + a ) -
0377: * twoToThe52) will be exact as discussed above and thus
0378: * cannot overflow or meaningfully underflow. Finally, the
0379: * last multiply in the return statement is by plus or minus
0380: * 1.0, which is exact too.
0381: */
0382: double twoToThe52 = (double) (1L << 52); // 2^52
0383: double sign = FpUtils.rawCopySign(1.0, a); // preserve sign info
0384: a = Math.abs(a);
0385:
0386: if (a < twoToThe52) { // E_min <= ilogb(a) <= 51
0387: a = ((twoToThe52 + a) - twoToThe52);
0388: }
0389:
0390: return sign * a; // restore original sign
0391: }
0392:
0393: /**
0394: * Returns the angle <i>theta</i> from the conversion of rectangular
0395: * coordinates ({@code x}, {@code y}) to polar
0396: * coordinates (r, <i>theta</i>).
0397: * This method computes the phase <i>theta</i> by computing an arc tangent
0398: * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
0399: * cases:
0400: * <ul><li>If either argument is NaN, then the result is NaN.
0401: * <li>If the first argument is positive zero and the second argument
0402: * is positive, or the first argument is positive and finite and the
0403: * second argument is positive infinity, then the result is positive
0404: * zero.
0405: * <li>If the first argument is negative zero and the second argument
0406: * is positive, or the first argument is negative and finite and the
0407: * second argument is positive infinity, then the result is negative zero.
0408: * <li>If the first argument is positive zero and the second argument
0409: * is negative, or the first argument is positive and finite and the
0410: * second argument is negative infinity, then the result is the
0411: * {@code double} value closest to <i>pi</i>.
0412: * <li>If the first argument is negative zero and the second argument
0413: * is negative, or the first argument is negative and finite and the
0414: * second argument is negative infinity, then the result is the
0415: * {@code double} value closest to -<i>pi</i>.
0416: * <li>If the first argument is positive and the second argument is
0417: * positive zero or negative zero, or the first argument is positive
0418: * infinity and the second argument is finite, then the result is the
0419: * {@code double} value closest to <i>pi</i>/2.
0420: * <li>If the first argument is negative and the second argument is
0421: * positive zero or negative zero, or the first argument is negative
0422: * infinity and the second argument is finite, then the result is the
0423: * {@code double} value closest to -<i>pi</i>/2.
0424: * <li>If both arguments are positive infinity, then the result is the
0425: * {@code double} value closest to <i>pi</i>/4.
0426: * <li>If the first argument is positive infinity and the second argument
0427: * is negative infinity, then the result is the {@code double}
0428: * value closest to 3*<i>pi</i>/4.
0429: * <li>If the first argument is negative infinity and the second argument
0430: * is positive infinity, then the result is the {@code double} value
0431: * closest to -<i>pi</i>/4.
0432: * <li>If both arguments are negative infinity, then the result is the
0433: * {@code double} value closest to -3*<i>pi</i>/4.</ul>
0434: *
0435: * @param y the ordinate coordinate
0436: * @param x the abscissa coordinate
0437: * @return the <i>theta</i> component of the point
0438: * (<i>r</i>, <i>theta</i>)
0439: * in polar coordinates that corresponds to the point
0440: * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
0441: */
0442: public static native double atan2(double y, double x);
0443:
0444: /**
0445: * Returns the value of the first argument raised to the power of the
0446: * second argument. Special cases:
0447: *
0448: * <ul><li>If the second argument is positive or negative zero, then the
0449: * result is 1.0.
0450: * <li>If the second argument is 1.0, then the result is the same as the
0451: * first argument.
0452: * <li>If the second argument is NaN, then the result is NaN.
0453: * <li>If the first argument is NaN and the second argument is nonzero,
0454: * then the result is NaN.
0455: *
0456: * <li>If
0457: * <ul>
0458: * <li>the absolute value of the first argument is greater than 1
0459: * and the second argument is positive infinity, or
0460: * <li>the absolute value of the first argument is less than 1 and
0461: * the second argument is negative infinity,
0462: * </ul>
0463: * then the result is positive infinity.
0464: *
0465: * <li>If
0466: * <ul>
0467: * <li>the absolute value of the first argument is greater than 1 and
0468: * the second argument is negative infinity, or
0469: * <li>the absolute value of the
0470: * first argument is less than 1 and the second argument is positive
0471: * infinity,
0472: * </ul>
0473: * then the result is positive zero.
0474: *
0475: * <li>If the absolute value of the first argument equals 1 and the
0476: * second argument is infinite, then the result is NaN.
0477: *
0478: * <li>If
0479: * <ul>
0480: * <li>the first argument is positive zero and the second argument
0481: * is greater than zero, or
0482: * <li>the first argument is positive infinity and the second
0483: * argument is less than zero,
0484: * </ul>
0485: * then the result is positive zero.
0486: *
0487: * <li>If
0488: * <ul>
0489: * <li>the first argument is positive zero and the second argument
0490: * is less than zero, or
0491: * <li>the first argument is positive infinity and the second
0492: * argument is greater than zero,
0493: * </ul>
0494: * then the result is positive infinity.
0495: *
0496: * <li>If
0497: * <ul>
0498: * <li>the first argument is negative zero and the second argument
0499: * is greater than zero but not a finite odd integer, or
0500: * <li>the first argument is negative infinity and the second
0501: * argument is less than zero but not a finite odd integer,
0502: * </ul>
0503: * then the result is positive zero.
0504: *
0505: * <li>If
0506: * <ul>
0507: * <li>the first argument is negative zero and the second argument
0508: * is a positive finite odd integer, or
0509: * <li>the first argument is negative infinity and the second
0510: * argument is a negative finite odd integer,
0511: * </ul>
0512: * then the result is negative zero.
0513: *
0514: * <li>If
0515: * <ul>
0516: * <li>the first argument is negative zero and the second argument
0517: * is less than zero but not a finite odd integer, or
0518: * <li>the first argument is negative infinity and the second
0519: * argument is greater than zero but not a finite odd integer,
0520: * </ul>
0521: * then the result is positive infinity.
0522: *
0523: * <li>If
0524: * <ul>
0525: * <li>the first argument is negative zero and the second argument
0526: * is a negative finite odd integer, or
0527: * <li>the first argument is negative infinity and the second
0528: * argument is a positive finite odd integer,
0529: * </ul>
0530: * then the result is negative infinity.
0531: *
0532: * <li>If the first argument is finite and less than zero
0533: * <ul>
0534: * <li> if the second argument is a finite even integer, the
0535: * result is equal to the result of raising the absolute value of
0536: * the first argument to the power of the second argument
0537: *
0538: * <li>if the second argument is a finite odd integer, the result
0539: * is equal to the negative of the result of raising the absolute
0540: * value of the first argument to the power of the second
0541: * argument
0542: *
0543: * <li>if the second argument is finite and not an integer, then
0544: * the result is NaN.
0545: * </ul>
0546: *
0547: * <li>If both arguments are integers, then the result is exactly equal
0548: * to the mathematical result of raising the first argument to the power
0549: * of the second argument if that result can in fact be represented
0550: * exactly as a {@code double} value.</ul>
0551: *
0552: * <p>(In the foregoing descriptions, a floating-point value is
0553: * considered to be an integer if and only if it is finite and a
0554: * fixed point of the method {@link #ceil ceil} or,
0555: * equivalently, a fixed point of the method {@link #floor
0556: * floor}. A value is a fixed point of a one-argument
0557: * method if and only if the result of applying the method to the
0558: * value is equal to the value.)
0559: *
0560: * @param a base.
0561: * @param b the exponent.
0562: * @return the value {@code a}<sup>{@code b}</sup>.
0563: */
0564: public static native double pow(double a, double b);
0565:
0566: /**
0567: * Returns the closest {@code int} to the argument. The
0568: * result is rounded to an integer by adding 1/2, taking the
0569: * floor of the result, and casting the result to type {@code int}.
0570: * In other words, the result is equal to the value of the expression:
0571: * <p>{@code (int)Math.floor(a + 0.5f)}
0572: *
0573: * <p>Special cases:
0574: * <ul><li>If the argument is NaN, the result is 0.
0575: * <li>If the argument is negative infinity or any value less than or
0576: * equal to the value of {@code Integer.MIN_VALUE}, the result is
0577: * equal to the value of {@code Integer.MIN_VALUE}.
0578: * <li>If the argument is positive infinity or any value greater than or
0579: * equal to the value of {@code Integer.MAX_VALUE}, the result is
0580: * equal to the value of {@code Integer.MAX_VALUE}.</ul>
0581: *
0582: * @param a a floating-point value to be rounded to an integer.
0583: * @return the value of the argument rounded to the nearest
0584: * {@code int} value.
0585: * @see java.lang.Integer#MAX_VALUE
0586: * @see java.lang.Integer#MIN_VALUE
0587: */
0588: public static int round(float a) {
0589: return (int) floor(a + 0.5f);
0590: }
0591:
0592: /**
0593: * Returns the closest {@code long} to the argument. The result
0594: * is rounded to an integer by adding 1/2, taking the floor of the
0595: * result, and casting the result to type {@code long}. In other
0596: * words, the result is equal to the value of the expression:
0597: * <p>{@code (long)Math.floor(a + 0.5d)}
0598: *
0599: * <p>Special cases:
0600: * <ul><li>If the argument is NaN, the result is 0.
0601: * <li>If the argument is negative infinity or any value less than or
0602: * equal to the value of {@code Long.MIN_VALUE}, the result is
0603: * equal to the value of {@code Long.MIN_VALUE}.
0604: * <li>If the argument is positive infinity or any value greater than or
0605: * equal to the value of {@code Long.MAX_VALUE}, the result is
0606: * equal to the value of {@code Long.MAX_VALUE}.</ul>
0607: *
0608: * @param a a floating-point value to be rounded to a
0609: * {@code long}.
0610: * @return the value of the argument rounded to the nearest
0611: * {@code long} value.
0612: * @see java.lang.Long#MAX_VALUE
0613: * @see java.lang.Long#MIN_VALUE
0614: */
0615: public static long round(double a) {
0616: return (long) floor(a + 0.5d);
0617: }
0618:
0619: private static Random randomNumberGenerator;
0620:
0621: private static synchronized void initRNG() {
0622: if (randomNumberGenerator == null)
0623: randomNumberGenerator = new Random();
0624: }
0625:
0626: /**
0627: * Returns a {@code double} value with a positive sign, greater
0628: * than or equal to {@code 0.0} and less than {@code 1.0}.
0629: * Returned values are chosen pseudorandomly with (approximately)
0630: * uniform distribution from that range.
0631: *
0632: * <p>When this method is first called, it creates a single new
0633: * pseudorandom-number generator, exactly as if by the expression
0634: * <blockquote>{@code new java.util.Random}</blockquote> This
0635: * new pseudorandom-number generator is used thereafter for all
0636: * calls to this method and is used nowhere else.
0637: *
0638: * <p>This method is properly synchronized to allow correct use by
0639: * more than one thread. However, if many threads need to generate
0640: * pseudorandom numbers at a great rate, it may reduce contention
0641: * for each thread to have its own pseudorandom number generator.
0642: *
0643: * @return a pseudorandom {@code double} greater than or equal
0644: * to {@code 0.0} and less than {@code 1.0}.
0645: * @see java.util.Random#nextDouble()
0646: */
0647: public static double random() {
0648: if (randomNumberGenerator == null)
0649: initRNG();
0650: return randomNumberGenerator.nextDouble();
0651: }
0652:
0653: /**
0654: * Returns the absolute value of an {@code int} value..
0655: * If the argument is not negative, the argument is returned.
0656: * If the argument is negative, the negation of the argument is returned.
0657: *
0658: * <p>Note that if the argument is equal to the value of
0659: * {@link Integer#MIN_VALUE}, the most negative representable
0660: * {@code int} value, the result is that same value, which is
0661: * negative.
0662: *
0663: * @param a the argument whose absolute value is to be determined.
0664: * @return the absolute value of the argument.
0665: */
0666: public static int abs(int a) {
0667: return (a < 0) ? -a : a;
0668: }
0669:
0670: /**
0671: * Returns the absolute value of a {@code long} value.
0672: * If the argument is not negative, the argument is returned.
0673: * If the argument is negative, the negation of the argument is returned.
0674: *
0675: * <p>Note that if the argument is equal to the value of
0676: * {@link Long#MIN_VALUE}, the most negative representable
0677: * {@code long} value, the result is that same value, which
0678: * is negative.
0679: *
0680: * @param a the argument whose absolute value is to be determined.
0681: * @return the absolute value of the argument.
0682: */
0683: public static long abs(long a) {
0684: return (a < 0) ? -a : a;
0685: }
0686:
0687: /**
0688: * Returns the absolute value of a {@code float} value.
0689: * If the argument is not negative, the argument is returned.
0690: * If the argument is negative, the negation of the argument is returned.
0691: * Special cases:
0692: * <ul><li>If the argument is positive zero or negative zero, the
0693: * result is positive zero.
0694: * <li>If the argument is infinite, the result is positive infinity.
0695: * <li>If the argument is NaN, the result is NaN.</ul>
0696: * In other words, the result is the same as the value of the expression:
0697: * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
0698: *
0699: * @param a the argument whose absolute value is to be determined
0700: * @return the absolute value of the argument.
0701: */
0702: public static float abs(float a) {
0703: return (a <= 0.0F) ? 0.0F - a : a;
0704: }
0705:
0706: /**
0707: * Returns the absolute value of a {@code double} value.
0708: * If the argument is not negative, the argument is returned.
0709: * If the argument is negative, the negation of the argument is returned.
0710: * Special cases:
0711: * <ul><li>If the argument is positive zero or negative zero, the result
0712: * is positive zero.
0713: * <li>If the argument is infinite, the result is positive infinity.
0714: * <li>If the argument is NaN, the result is NaN.</ul>
0715: * In other words, the result is the same as the value of the expression:
0716: * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
0717: *
0718: * @param a the argument whose absolute value is to be determined
0719: * @return the absolute value of the argument.
0720: */
0721: public static double abs(double a) {
0722: return (a <= 0.0D) ? 0.0D - a : a;
0723: }
0724:
0725: /**
0726: * Returns the greater of two {@code int} values. That is, the
0727: * result is the argument closer to the value of
0728: * {@link Integer#MAX_VALUE}. If the arguments have the same value,
0729: * the result is that same value.
0730: *
0731: * @param a an argument.
0732: * @param b another argument.
0733: * @return the larger of {@code a} and {@code b}.
0734: */
0735: public static int max(int a, int b) {
0736: return (a >= b) ? a : b;
0737: }
0738:
0739: /**
0740: * Returns the greater of two {@code long} values. That is, the
0741: * result is the argument closer to the value of
0742: * {@link Long#MAX_VALUE}. If the arguments have the same value,
0743: * the result is that same value.
0744: *
0745: * @param a an argument.
0746: * @param b another argument.
0747: * @return the larger of {@code a} and {@code b}.
0748: */
0749: public static long max(long a, long b) {
0750: return (a >= b) ? a : b;
0751: }
0752:
0753: private static long negativeZeroFloatBits = Float
0754: .floatToIntBits(-0.0f);
0755: private static long negativeZeroDoubleBits = Double
0756: .doubleToLongBits(-0.0d);
0757:
0758: /**
0759: * Returns the greater of two {@code float} values. That is,
0760: * the result is the argument closer to positive infinity. If the
0761: * arguments have the same value, the result is that same
0762: * value. If either value is NaN, then the result is NaN. Unlike
0763: * the numerical comparison operators, this method considers
0764: * negative zero to be strictly smaller than positive zero. If one
0765: * argument is positive zero and the other negative zero, the
0766: * result is positive zero.
0767: *
0768: * @param a an argument.
0769: * @param b another argument.
0770: * @return the larger of {@code a} and {@code b}.
0771: */
0772: public static float max(float a, float b) {
0773: if (a != a)
0774: return a; // a is NaN
0775: if ((a == 0.0f) && (b == 0.0f)
0776: && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
0777: return b;
0778: }
0779: return (a >= b) ? a : b;
0780: }
0781:
0782: /**
0783: * Returns the greater of two {@code double} values. That
0784: * is, the result is the argument closer to positive infinity. If
0785: * the arguments have the same value, the result is that same
0786: * value. If either value is NaN, then the result is NaN. Unlike
0787: * the numerical comparison operators, this method considers
0788: * negative zero to be strictly smaller than positive zero. If one
0789: * argument is positive zero and the other negative zero, the
0790: * result is positive zero.
0791: *
0792: * @param a an argument.
0793: * @param b another argument.
0794: * @return the larger of {@code a} and {@code b}.
0795: */
0796: public static double max(double a, double b) {
0797: if (a != a)
0798: return a; // a is NaN
0799: if ((a == 0.0d)
0800: && (b == 0.0d)
0801: && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
0802: return b;
0803: }
0804: return (a >= b) ? a : b;
0805: }
0806:
0807: /**
0808: * Returns the smaller of two {@code int} values. That is,
0809: * the result the argument closer to the value of
0810: * {@link Integer#MIN_VALUE}. If the arguments have the same
0811: * value, the result is that same value.
0812: *
0813: * @param a an argument.
0814: * @param b another argument.
0815: * @return the smaller of {@code a} and {@code b}.
0816: */
0817: public static int min(int a, int b) {
0818: return (a <= b) ? a : b;
0819: }
0820:
0821: /**
0822: * Returns the smaller of two {@code long} values. That is,
0823: * the result is the argument closer to the value of
0824: * {@link Long#MIN_VALUE}. If the arguments have the same
0825: * value, the result is that same value.
0826: *
0827: * @param a an argument.
0828: * @param b another argument.
0829: * @return the smaller of {@code a} and {@code b}.
0830: */
0831: public static long min(long a, long b) {
0832: return (a <= b) ? a : b;
0833: }
0834:
0835: /**
0836: * Returns the smaller of two {@code float} values. That is,
0837: * the result is the value closer to negative infinity. If the
0838: * arguments have the same value, the result is that same
0839: * value. If either value is NaN, then the result is NaN. Unlike
0840: * the numerical comparison operators, this method considers
0841: * negative zero to be strictly smaller than positive zero. If
0842: * one argument is positive zero and the other is negative zero,
0843: * the result is negative zero.
0844: *
0845: * @param a an argument.
0846: * @param b another argument.
0847: * @return the smaller of {@code a} and {@code b.}
0848: */
0849: public static float min(float a, float b) {
0850: if (a != a)
0851: return a; // a is NaN
0852: if ((a == 0.0f) && (b == 0.0f)
0853: && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
0854: return b;
0855: }
0856: return (a <= b) ? a : b;
0857: }
0858:
0859: /**
0860: * Returns the smaller of two {@code double} values. That
0861: * is, the result is the value closer to negative infinity. If the
0862: * arguments have the same value, the result is that same
0863: * value. If either value is NaN, then the result is NaN. Unlike
0864: * the numerical comparison operators, this method considers
0865: * negative zero to be strictly smaller than positive zero. If one
0866: * argument is positive zero and the other is negative zero, the
0867: * result is negative zero.
0868: *
0869: * @param a an argument.
0870: * @param b another argument.
0871: * @return the smaller of {@code a} and {@code b}.
0872: */
0873: public static double min(double a, double b) {
0874: if (a != a)
0875: return a; // a is NaN
0876: if ((a == 0.0d)
0877: && (b == 0.0d)
0878: && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
0879: return b;
0880: }
0881: return (a <= b) ? a : b;
0882: }
0883:
0884: /**
0885: * Returns the size of an ulp of the argument. An ulp of a
0886: * {@code double} value is the positive distance between this
0887: * floating-point value and the {@code double} value next
0888: * larger in magnitude. Note that for non-NaN <i>x</i>,
0889: * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
0890: *
0891: * <p>Special Cases:
0892: * <ul>
0893: * <li> If the argument is NaN, then the result is NaN.
0894: * <li> If the argument is positive or negative infinity, then the
0895: * result is positive infinity.
0896: * <li> If the argument is positive or negative zero, then the result is
0897: * {@code Double.MIN_VALUE}.
0898: * <li> If the argument is ±{@code Double.MAX_VALUE}, then
0899: * the result is equal to 2<sup>971</sup>.
0900: * </ul>
0901: *
0902: * @param d the floating-point value whose ulp is to be returned
0903: * @return the size of an ulp of the argument
0904: * @author Joseph D. Darcy
0905: * @since 1.5
0906: */
0907: public static double ulp(double d) {
0908: return sun.misc.FpUtils.ulp(d);
0909: }
0910:
0911: /**
0912: * Returns the size of an ulp of the argument. An ulp of a
0913: * {@code float} value is the positive distance between this
0914: * floating-point value and the {@code float} value next
0915: * larger in magnitude. Note that for non-NaN <i>x</i>,
0916: * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
0917: *
0918: * <p>Special Cases:
0919: * <ul>
0920: * <li> If the argument is NaN, then the result is NaN.
0921: * <li> If the argument is positive or negative infinity, then the
0922: * result is positive infinity.
0923: * <li> If the argument is positive or negative zero, then the result is
0924: * {@code Float.MIN_VALUE}.
0925: * <li> If the argument is ±{@code Float.MAX_VALUE}, then
0926: * the result is equal to 2<sup>104</sup>.
0927: * </ul>
0928: *
0929: * @param f the floating-point value whose ulp is to be returned
0930: * @return the size of an ulp of the argument
0931: * @author Joseph D. Darcy
0932: * @since 1.5
0933: */
0934: public static float ulp(float f) {
0935: return sun.misc.FpUtils.ulp(f);
0936: }
0937:
0938: /**
0939: * Returns the signum function of the argument; zero if the argument
0940: * is zero, 1.0 if the argument is greater than zero, -1.0 if the
0941: * argument is less than zero.
0942: *
0943: * <p>Special Cases:
0944: * <ul>
0945: * <li> If the argument is NaN, then the result is NaN.
0946: * <li> If the argument is positive zero or negative zero, then the
0947: * result is the same as the argument.
0948: * </ul>
0949: *
0950: * @param d the floating-point value whose signum is to be returned
0951: * @return the signum function of the argument
0952: * @author Joseph D. Darcy
0953: * @since 1.5
0954: */
0955: public static double signum(double d) {
0956: return sun.misc.FpUtils.signum(d);
0957: }
0958:
0959: /**
0960: * Returns the signum function of the argument; zero if the argument
0961: * is zero, 1.0f if the argument is greater than zero, -1.0f if the
0962: * argument is less than zero.
0963: *
0964: * <p>Special Cases:
0965: * <ul>
0966: * <li> If the argument is NaN, then the result is NaN.
0967: * <li> If the argument is positive zero or negative zero, then the
0968: * result is the same as the argument.
0969: * </ul>
0970: *
0971: * @param f the floating-point value whose signum is to be returned
0972: * @return the signum function of the argument
0973: * @author Joseph D. Darcy
0974: * @since 1.5
0975: */
0976: public static float signum(float f) {
0977: return sun.misc.FpUtils.signum(f);
0978: }
0979:
0980: /**
0981: * Returns the hyperbolic sine of a {@code double} value.
0982: * The hyperbolic sine of <i>x</i> is defined to be
0983: * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2
0984: * where <i>e</i> is {@linkplain Math#E Euler's number}.
0985: *
0986: * <p>Special cases:
0987: * <ul>
0988: *
0989: * <li>If the argument is NaN, then the result is NaN.
0990: *
0991: * <li>If the argument is infinite, then the result is an infinity
0992: * with the same sign as the argument.
0993: *
0994: * <li>If the argument is zero, then the result is a zero with the
0995: * same sign as the argument.
0996: *
0997: * </ul>
0998: *
0999: * @param x The number whose hyperbolic sine is to be returned.
1000: * @return The hyperbolic sine of {@code x}.
1001: * @since 1.5
1002: */
1003: public static native double sinh(double x);
1004:
1005: /**
1006: * Returns the hyperbolic cosine of a {@code double} value.
1007: * The hyperbolic cosine of <i>x</i> is defined to be
1008: * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2
1009: * where <i>e</i> is {@linkplain Math#E Euler's number}.
1010: *
1011: * <p>Special cases:
1012: * <ul>
1013: *
1014: * <li>If the argument is NaN, then the result is NaN.
1015: *
1016: * <li>If the argument is infinite, then the result is positive
1017: * infinity.
1018: *
1019: * <li>If the argument is zero, then the result is {@code 1.0}.
1020: *
1021: * </ul>
1022: *
1023: * @param x The number whose hyperbolic cosine is to be returned.
1024: * @return The hyperbolic cosine of {@code x}.
1025: * @since 1.5
1026: */
1027: public static native double cosh(double x);
1028:
1029: /**
1030: * Returns the hyperbolic tangent of a {@code double} value.
1031: * The hyperbolic tangent of <i>x</i> is defined to be
1032: * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>),
1033: * in other words, {@linkplain Math#sinh
1034: * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note
1035: * that the absolute value of the exact tanh is always less than
1036: * 1.
1037: *
1038: * <p>Special cases:
1039: * <ul>
1040: *
1041: * <li>If the argument is NaN, then the result is NaN.
1042: *
1043: * <li>If the argument is zero, then the result is a zero with the
1044: * same sign as the argument.
1045: *
1046: * <li>If the argument is positive infinity, then the result is
1047: * {@code +1.0}.
1048: *
1049: * <li>If the argument is negative infinity, then the result is
1050: * {@code -1.0}.
1051: *
1052: * </ul>
1053: *
1054: * @param x The number whose hyperbolic tangent is to be returned.
1055: * @return The hyperbolic tangent of {@code x}.
1056: * @since 1.5
1057: */
1058: public static native double tanh(double x);
1059:
1060: /**
1061: * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1062: * without intermediate overflow or underflow.
1063: *
1064: * <p>Special cases:
1065: * <ul>
1066: *
1067: * <li> If either argument is infinite, then the result
1068: * is positive infinity.
1069: *
1070: * <li> If either argument is NaN and neither argument is infinite,
1071: * then the result is NaN.
1072: *
1073: * </ul>
1074: *
1075: * @param x a value
1076: * @param y a value
1077: * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1078: * without intermediate overflow or underflow
1079: * @since 1.5
1080: */
1081: public static native double hypot(double x, double y);
1082:
1083: /**
1084: * Returns <i>e</i><sup>x</sup> -1. Note that for values of
1085: * <i>x</i> near 0, the exact sum of
1086: * {@code expm1(x)} + 1 is much closer to the true
1087: * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
1088: *
1089: * <p>Special cases:
1090: * <ul>
1091: * <li>If the argument is NaN, the result is NaN.
1092: *
1093: * <li>If the argument is positive infinity, then the result is
1094: * positive infinity.
1095: *
1096: * <li>If the argument is negative infinity, then the result is
1097: * -1.0.
1098: *
1099: * <li>If the argument is zero, then the result is a zero with the
1100: * same sign as the argument.
1101: *
1102: * </ul>
1103: *
1104: * @param x the exponent to raise <i>e</i> to in the computation of
1105: * <i>e</i><sup>{@code x}</sup> -1.
1106: * @return the value <i>e</i><sup>{@code x}</sup> - 1.
1107: * @since 1.5
1108: */
1109: public static native double expm1(double x);
1110:
1111: /**
1112: * Returns the natural logarithm of the sum of the argument and 1.
1113: * Note that for small values {@code x}, the result of
1114: * {@code log1p(x)} is much closer to the true result of ln(1
1115: * + {@code x}) than the floating-point evaluation of
1116: * {@code log(1.0+x)}.
1117: *
1118: * <p>Special cases:
1119: * <ul>
1120: *
1121: * <li>If the argument is NaN or less than -1, then the result is
1122: * NaN.
1123: *
1124: * <li>If the argument is positive infinity, then the result is
1125: * positive infinity.
1126: *
1127: * <li>If the argument is negative one, then the result is
1128: * negative infinity.
1129: *
1130: * <li>If the argument is zero, then the result is a zero with the
1131: * same sign as the argument.
1132: *
1133: * </ul>
1134: *
1135: * @param x a value
1136: * @return the value ln({@code x} + 1), the natural
1137: * log of {@code x} + 1
1138: * @since 1.5
1139: */
1140: public static native double log1p(double x);
1141:
1142: /**
1143: * Returns the first floating-point argument with the sign of the
1144: * second floating-point argument. For this method, a NaN
1145: * {@code sign} argument is always treated as if it were
1146: * positive.
1147: *
1148: * @param magnitude the parameter providing the magnitude of the result
1149: * @param sign the parameter providing the sign of the result
1150: * @return a value with the magnitude of {@code magnitude}
1151: * and the sign of {@code sign}.
1152: * @since 1.6
1153: */
1154: public static double copySign(double magnitude, double sign) {
1155: return sun.misc.FpUtils.copySign(magnitude, sign);
1156: }
1157:
1158: /**
1159: * Returns the first floating-point argument with the sign of the
1160: * second floating-point argument. For this method, a NaN
1161: * {@code sign} argument is always treated as if it were
1162: * positive.
1163: *
1164: * @param magnitude the parameter providing the magnitude of the result
1165: * @param sign the parameter providing the sign of the result
1166: * @return a value with the magnitude of {@code magnitude}
1167: * and the sign of {@code sign}.
1168: * @since 1.6
1169: */
1170: public static float copySign(float magnitude, float sign) {
1171: return sun.misc.FpUtils.copySign(magnitude, sign);
1172: }
1173:
1174: /**
1175: * Returns the unbiased exponent used in the representation of a
1176: * {@code float}. Special cases:
1177: *
1178: * <ul>
1179: * <li>If the argument is NaN or infinite, then the result is
1180: * {@link Float#MAX_EXPONENT} + 1.
1181: * <li>If the argument is zero or subnormal, then the result is
1182: * {@link Float#MIN_EXPONENT} -1.
1183: * </ul>
1184: * @param f a {@code float} value
1185: * @since 1.6
1186: */
1187: public static int getExponent(float f) {
1188: return sun.misc.FpUtils.getExponent(f);
1189: }
1190:
1191: /**
1192: * Returns the unbiased exponent used in the representation of a
1193: * {@code double}. Special cases:
1194: *
1195: * <ul>
1196: * <li>If the argument is NaN or infinite, then the result is
1197: * {@link Double#MAX_EXPONENT} + 1.
1198: * <li>If the argument is zero or subnormal, then the result is
1199: * {@link Double#MIN_EXPONENT} -1.
1200: * </ul>
1201: * @param d a {@code double} value
1202: * @since 1.6
1203: */
1204: public static int getExponent(double d) {
1205: return sun.misc.FpUtils.getExponent(d);
1206: }
1207:
1208: /**
1209: * Returns the floating-point number adjacent to the first
1210: * argument in the direction of the second argument. If both
1211: * arguments compare as equal the second argument is returned.
1212: *
1213: * <p>Special cases:
1214: * <ul>
1215: * <li> If either argument is a NaN, then NaN is returned.
1216: *
1217: * <li> If both arguments are signed zeros, {@code direction}
1218: * is returned unchanged (as implied by the requirement of
1219: * returning the second argument if the arguments compare as
1220: * equal).
1221: *
1222: * <li> If {@code start} is
1223: * ±{@link Double#MIN_VALUE} and {@code direction}
1224: * has a value such that the result should have a smaller
1225: * magnitude, then a zero with the same sign as {@code start}
1226: * is returned.
1227: *
1228: * <li> If {@code start} is infinite and
1229: * {@code direction} has a value such that the result should
1230: * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1231: * same sign as {@code start} is returned.
1232: *
1233: * <li> If {@code start} is equal to ±
1234: * {@link Double#MAX_VALUE} and {@code direction} has a
1235: * value such that the result should have a larger magnitude, an
1236: * infinity with same sign as {@code start} is returned.
1237: * </ul>
1238: *
1239: * @param start starting floating-point value
1240: * @param direction value indicating which of
1241: * {@code start}'s neighbors or {@code start} should
1242: * be returned
1243: * @return The floating-point number adjacent to {@code start} in the
1244: * direction of {@code direction}.
1245: * @since 1.6
1246: */
1247: public static double nextAfter(double start, double direction) {
1248: return sun.misc.FpUtils.nextAfter(start, direction);
1249: }
1250:
1251: /**
1252: * Returns the floating-point number adjacent to the first
1253: * argument in the direction of the second argument. If both
1254: * arguments compare as equal a value equivalent to the second argument
1255: * is returned.
1256: *
1257: * <p>Special cases:
1258: * <ul>
1259: * <li> If either argument is a NaN, then NaN is returned.
1260: *
1261: * <li> If both arguments are signed zeros, a value equivalent
1262: * to {@code direction} is returned.
1263: *
1264: * <li> If {@code start} is
1265: * ±{@link Float#MIN_VALUE} and {@code direction}
1266: * has a value such that the result should have a smaller
1267: * magnitude, then a zero with the same sign as {@code start}
1268: * is returned.
1269: *
1270: * <li> If {@code start} is infinite and
1271: * {@code direction} has a value such that the result should
1272: * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1273: * same sign as {@code start} is returned.
1274: *
1275: * <li> If {@code start} is equal to ±
1276: * {@link Float#MAX_VALUE} and {@code direction} has a
1277: * value such that the result should have a larger magnitude, an
1278: * infinity with same sign as {@code start} is returned.
1279: * </ul>
1280: *
1281: * @param start starting floating-point value
1282: * @param direction value indicating which of
1283: * {@code start}'s neighbors or {@code start} should
1284: * be returned
1285: * @return The floating-point number adjacent to {@code start} in the
1286: * direction of {@code direction}.
1287: * @since 1.6
1288: */
1289: public static float nextAfter(float start, double direction) {
1290: return sun.misc.FpUtils.nextAfter(start, direction);
1291: }
1292:
1293: /**
1294: * Returns the floating-point value adjacent to {@code d} in
1295: * the direction of positive infinity. This method is
1296: * semantically equivalent to {@code nextAfter(d,
1297: * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1298: * implementation may run faster than its equivalent
1299: * {@code nextAfter} call.
1300: *
1301: * <p>Special Cases:
1302: * <ul>
1303: * <li> If the argument is NaN, the result is NaN.
1304: *
1305: * <li> If the argument is positive infinity, the result is
1306: * positive infinity.
1307: *
1308: * <li> If the argument is zero, the result is
1309: * {@link Double#MIN_VALUE}
1310: *
1311: * </ul>
1312: *
1313: * @param d starting floating-point value
1314: * @return The adjacent floating-point value closer to positive
1315: * infinity.
1316: * @since 1.6
1317: */
1318: public static double nextUp(double d) {
1319: return sun.misc.FpUtils.nextUp(d);
1320: }
1321:
1322: /**
1323: * Returns the floating-point value adjacent to {@code f} in
1324: * the direction of positive infinity. This method is
1325: * semantically equivalent to {@code nextAfter(f,
1326: * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1327: * implementation may run faster than its equivalent
1328: * {@code nextAfter} call.
1329: *
1330: * <p>Special Cases:
1331: * <ul>
1332: * <li> If the argument is NaN, the result is NaN.
1333: *
1334: * <li> If the argument is positive infinity, the result is
1335: * positive infinity.
1336: *
1337: * <li> If the argument is zero, the result is
1338: * {@link Float#MIN_VALUE}
1339: *
1340: * </ul>
1341: *
1342: * @param f starting floating-point value
1343: * @return The adjacent floating-point value closer to positive
1344: * infinity.
1345: * @since 1.6
1346: */
1347: public static float nextUp(float f) {
1348: return sun.misc.FpUtils.nextUp(f);
1349: }
1350:
1351: /**
1352: * Return {@code d} ×
1353: * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1354: * by a single correctly rounded floating-point multiply to a
1355: * member of the double value set. See the Java
1356: * Language Specification for a discussion of floating-point
1357: * value sets. If the exponent of the result is between {@link
1358: * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1359: * answer is calculated exactly. If the exponent of the result
1360: * would be larger than {@code Double.MAX_EXPONENT}, an
1361: * infinity is returned. Note that if the result is subnormal,
1362: * precision may be lost; that is, when {@code scalb(x, n)}
1363: * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1364: * <i>x</i>. When the result is non-NaN, the result has the same
1365: * sign as {@code d}.
1366: *
1367: * <p>Special cases:
1368: * <ul>
1369: * <li> If the first argument is NaN, NaN is returned.
1370: * <li> If the first argument is infinite, then an infinity of the
1371: * same sign is returned.
1372: * <li> If the first argument is zero, then a zero of the same
1373: * sign is returned.
1374: * </ul>
1375: *
1376: * @param d number to be scaled by a power of two.
1377: * @param scaleFactor power of 2 used to scale {@code d}
1378: * @return {@code d} × 2<sup>{@code scaleFactor}</sup>
1379: * @since 1.6
1380: */
1381: public static double scalb(double d, int scaleFactor) {
1382: return sun.misc.FpUtils.scalb(d, scaleFactor);
1383: }
1384:
1385: /**
1386: * Return {@code f} ×
1387: * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1388: * by a single correctly rounded floating-point multiply to a
1389: * member of the float value set. See the Java
1390: * Language Specification for a discussion of floating-point
1391: * value sets. If the exponent of the result is between {@link
1392: * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1393: * answer is calculated exactly. If the exponent of the result
1394: * would be larger than {@code Float.MAX_EXPONENT}, an
1395: * infinity is returned. Note that if the result is subnormal,
1396: * precision may be lost; that is, when {@code scalb(x, n)}
1397: * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1398: * <i>x</i>. When the result is non-NaN, the result has the same
1399: * sign as {@code f}.
1400: *
1401: * <p>Special cases:
1402: * <ul>
1403: * <li> If the first argument is NaN, NaN is returned.
1404: * <li> If the first argument is infinite, then an infinity of the
1405: * same sign is returned.
1406: * <li> If the first argument is zero, then a zero of the same
1407: * sign is returned.
1408: * </ul>
1409: *
1410: * @param f number to be scaled by a power of two.
1411: * @param scaleFactor power of 2 used to scale {@code f}
1412: * @return {@code f} × 2<sup>{@code scaleFactor}</sup>
1413: * @since 1.6
1414: */
1415: public static float scalb(float f, int scaleFactor) {
1416: return sun.misc.FpUtils.scalb(f, scaleFactor);
1417: }
1418: }
|