src/share/classes/java/lang/StrictMath.java

Print this page




   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package java.lang;
  27 import java.util.Random;
  28 import sun.misc.FpUtils;

  29 
  30 /**
  31  * The class {@code StrictMath} contains methods for performing basic
  32  * numeric operations such as the elementary exponential, logarithm,
  33  * square root, and trigonometric functions.
  34  *
  35  * <p>To help ensure portability of Java programs, the definitions of
  36  * some of the numeric functions in this package require that they
  37  * produce the same results as certain published algorithms. These
  38  * algorithms are available from the well-known network library
  39  * {@code netlib} as the package "Freely Distributable Math
  40  * Library," <a
  41  * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These
  42  * algorithms, which are written in the C programming language, are
  43  * then to be understood as executed with all floating-point
  44  * operations following the rules of Java floating-point arithmetic.
  45  *
  46  * <p>The Java math library is defined with respect to
  47  * {@code fdlibm} version 5.3. Where {@code fdlibm} provides
  48  * more than one definition for a function (such as


 299     public static native double IEEEremainder(double f1, double f2);
 300 
 301     /**
 302      * Returns the smallest (closest to negative infinity)
 303      * {@code double} value that is greater than or equal to the
 304      * argument and is equal to a mathematical integer. Special cases:
 305      * <ul><li>If the argument value is already equal to a
 306      * mathematical integer, then the result is the same as the
 307      * argument.  <li>If the argument is NaN or an infinity or
 308      * positive zero or negative zero, then the result is the same as
 309      * the argument.  <li>If the argument value is less than zero but
 310      * greater than -1.0, then the result is negative zero.</ul> Note
 311      * that the value of {@code StrictMath.ceil(x)} is exactly the
 312      * value of {@code -StrictMath.floor(-x)}.
 313      *
 314      * @param   a   a value.
 315      * @return  the smallest (closest to negative infinity)
 316      *          floating-point value that is greater than or equal to
 317      *          the argument and is equal to a mathematical integer.
 318      */
 319     public static native double ceil(double a);


 320 
 321     /**
 322      * Returns the largest (closest to positive infinity)
 323      * {@code double} value that is less than or equal to the
 324      * argument and is equal to a mathematical integer. Special cases:
 325      * <ul><li>If the argument value is already equal to a
 326      * mathematical integer, then the result is the same as the
 327      * argument.  <li>If the argument is NaN or an infinity or
 328      * positive zero or negative zero, then the result is the same as
 329      * the argument.</ul>
 330      *
 331      * @param   a   a value.
 332      * @return  the largest (closest to positive infinity)
 333      *          floating-point value that less than or equal to the argument
 334      *          and is equal to a mathematical integer.
 335      */
 336     public static native double floor(double a);















































 337 
 338     /**
 339      * Returns the {@code double} value that is closest in value
 340      * to the argument and is equal to a mathematical integer. If two
 341      * {@code double} values that are mathematical integers are
 342      * equally close to the value of the argument, the result is the
 343      * integer value that is even. Special cases:
 344      * <ul><li>If the argument value is already equal to a mathematical
 345      * integer, then the result is the same as the argument.
 346      * <li>If the argument is NaN or an infinity or positive zero or negative
 347      * zero, then the result is the same as the argument.</ul>
 348      *
 349      * @param   a   a value.
 350      * @return  the closest floating-point value to {@code a} that is
 351      *          equal to a mathematical integer.
 352      * @author Joseph D. Darcy
 353      */
 354     public static double rint(double a) {
 355         /*
 356          * If the absolute value of a is not less than 2^52, it




   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package java.lang;
  27 import java.util.Random;
  28 import sun.misc.FpUtils;
  29 import sun.misc.DoubleConsts;
  30 
  31 /**
  32  * The class {@code StrictMath} contains methods for performing basic
  33  * numeric operations such as the elementary exponential, logarithm,
  34  * square root, and trigonometric functions.
  35  *
  36  * <p>To help ensure portability of Java programs, the definitions of
  37  * some of the numeric functions in this package require that they
  38  * produce the same results as certain published algorithms. These
  39  * algorithms are available from the well-known network library
  40  * {@code netlib} as the package "Freely Distributable Math
  41  * Library," <a
  42  * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These
  43  * algorithms, which are written in the C programming language, are
  44  * then to be understood as executed with all floating-point
  45  * operations following the rules of Java floating-point arithmetic.
  46  *
  47  * <p>The Java math library is defined with respect to
  48  * {@code fdlibm} version 5.3. Where {@code fdlibm} provides
  49  * more than one definition for a function (such as


 300     public static native double IEEEremainder(double f1, double f2);
 301 
 302     /**
 303      * Returns the smallest (closest to negative infinity)
 304      * {@code double} value that is greater than or equal to the
 305      * argument and is equal to a mathematical integer. Special cases:
 306      * <ul><li>If the argument value is already equal to a
 307      * mathematical integer, then the result is the same as the
 308      * argument.  <li>If the argument is NaN or an infinity or
 309      * positive zero or negative zero, then the result is the same as
 310      * the argument.  <li>If the argument value is less than zero but
 311      * greater than -1.0, then the result is negative zero.</ul> Note
 312      * that the value of {@code StrictMath.ceil(x)} is exactly the
 313      * value of {@code -StrictMath.floor(-x)}.
 314      *
 315      * @param   a   a value.
 316      * @return  the smallest (closest to negative infinity)
 317      *          floating-point value that is greater than or equal to
 318      *          the argument and is equal to a mathematical integer.
 319      */
 320     public static double ceil(double a) {
 321         return floorOrCeil(a, -0.0, 1.0, 1.0);
 322     }
 323 
 324     /**
 325      * Returns the largest (closest to positive infinity)
 326      * {@code double} value that is less than or equal to the
 327      * argument and is equal to a mathematical integer. Special cases:
 328      * <ul><li>If the argument value is already equal to a
 329      * mathematical integer, then the result is the same as the
 330      * argument.  <li>If the argument is NaN or an infinity or
 331      * positive zero or negative zero, then the result is the same as
 332      * the argument.</ul>
 333      *
 334      * @param   a   a value.
 335      * @return  the largest (closest to positive infinity)
 336      *          floating-point value that less than or equal to the argument
 337      *          and is equal to a mathematical integer.
 338      */
 339     public static double floor(double a) {
 340         return floorOrCeil(a, -1.0, 0.0, -1.0);
 341     }
 342 
 343     /**
 344      * Internal method to share logic between floor and ceil.
 345      *
 346      * @param a the value to be floored or ceiled
 347      * @param negativeBoundary result for values in (-1, 0)
 348      * @param positiveBoundary result for values in (0, 1)
 349      * @param increment value to add when the argument is non-integral
 350      */
 351     private static double floorOrCeil(double a,
 352                                       double negativeBoundary,
 353                                       double positiveBoundary,
 354                                       double sign) {
 355         int exponent = Math.getExponent(a);
 356 
 357         if (exponent < 0) {
 358             /*
 359              * Absolute value of argument is less than 1.
 360              * floorOrceil(-0.0) => -0.0
 361              * floorOrceil(+0.0) => +0.0
 362              */
 363             return ((a == 0.0) ? a :
 364                     ( (a < 0.0) ?  negativeBoundary : positiveBoundary) );
 365         } else if (exponent >= 52) {
 366             /*
 367              * Infinity, NaN, or a value so large it must be integral.
 368              */
 369             return a;
 370         }
 371         // Else the argument is either an integral value already XOR it
 372         // has to be rounded to one.  
 373         assert exponent >= 0 && exponent <= 51;
 374         
 375         long doppel = Double.doubleToRawLongBits(a);
 376         long mask   = DoubleConsts.SIGNIF_BIT_MASK >> exponent;
 377                      
 378         if ( (mask & doppel) == 0L )
 379             return a; // integral value
 380         else {
 381             double result = Double.longBitsToDouble(doppel & (~mask));
 382             if (sign*a > 0.0)
 383                 result = result + sign;
 384             return result;
 385         }
 386     }
 387 
 388     /**
 389      * Returns the {@code double} value that is closest in value
 390      * to the argument and is equal to a mathematical integer. If two
 391      * {@code double} values that are mathematical integers are
 392      * equally close to the value of the argument, the result is the
 393      * integer value that is even. Special cases:
 394      * <ul><li>If the argument value is already equal to a mathematical
 395      * integer, then the result is the same as the argument.
 396      * <li>If the argument is NaN or an infinity or positive zero or negative
 397      * zero, then the result is the same as the argument.</ul>
 398      *
 399      * @param   a   a value.
 400      * @return  the closest floating-point value to {@code a} that is
 401      *          equal to a mathematical integer.
 402      * @author Joseph D. Darcy
 403      */
 404     public static double rint(double a) {
 405         /*
 406          * If the absolute value of a is not less than 2^52, it