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

Print this page


   1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 596      * <li>If both arguments are integers, then the result is exactly equal
 597      * to the mathematical result of raising the first argument to the power
 598      * of the second argument if that result can in fact be represented
 599      * exactly as a {@code double} value.</ul>
 600      *
 601      * <p>(In the foregoing descriptions, a floating-point value is
 602      * considered to be an integer if and only if it is finite and a
 603      * fixed point of the method {@link #ceil ceil} or,
 604      * equivalently, a fixed point of the method {@link #floor
 605      * floor}. A value is a fixed point of a one-argument
 606      * method if and only if the result of applying the method to the
 607      * value is equal to the value.)
 608      *
 609      * @param   a   base.
 610      * @param   b   the exponent.
 611      * @return  the value {@code a}<sup>{@code b}</sup>.
 612      */
 613     public static native double pow(double a, double b);
 614 
 615     /**
 616      * Returns the closest {@code int} to the argument. The
 617      * result is rounded to an integer by adding 1/2, taking the
 618      * floor of the result, and casting the result to type {@code int}.
 619      * In other words, the result is equal to the value of the expression:
 620      * <p>{@code (int)Math.floor(a + 0.5f)}
 621      *
 622      * <p>Special cases:
 623      * <ul><li>If the argument is NaN, the result is 0.
 624      * <li>If the argument is negative infinity or any value less than or
 625      * equal to the value of {@code Integer.MIN_VALUE}, the result is
 626      * equal to the value of {@code Integer.MIN_VALUE}.
 627      * <li>If the argument is positive infinity or any value greater than or
 628      * equal to the value of {@code Integer.MAX_VALUE}, the result is
 629      * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 630      *
 631      * @param   a   a floating-point value to be rounded to an integer.
 632      * @return  the value of the argument rounded to the nearest
 633      *          {@code int} value.
 634      * @see     java.lang.Integer#MAX_VALUE
 635      * @see     java.lang.Integer#MIN_VALUE
 636      */
 637     public static int round(float a) {
 638         return (int)floor(a + 0.5f);
 639     }
 640 
 641     /**
 642      * Returns the closest {@code long} to the argument. The result
 643      * is rounded to an integer by adding 1/2, taking the floor of the
 644      * result, and casting the result to type {@code long}. In other
 645      * words, the result is equal to the value of the expression:
 646      * <p>{@code (long)Math.floor(a + 0.5d)}
 647      *
 648      * <p>Special cases:
 649      * <ul><li>If the argument is NaN, the result is 0.
 650      * <li>If the argument is negative infinity or any value less than or
 651      * equal to the value of {@code Long.MIN_VALUE}, the result is
 652      * equal to the value of {@code Long.MIN_VALUE}.
 653      * <li>If the argument is positive infinity or any value greater than or
 654      * equal to the value of {@code Long.MAX_VALUE}, the result is
 655      * equal to the value of {@code Long.MAX_VALUE}.</ul>
 656      *
 657      * @param   a  a floating-point value to be rounded to a
 658      *          {@code long}.
 659      * @return  the value of the argument rounded to the nearest
 660      *          {@code long} value.
 661      * @see     java.lang.Long#MAX_VALUE
 662      * @see     java.lang.Long#MIN_VALUE
 663      */
 664     public static long round(double a) {
 665         return (long)floor(a + 0.5d);
 666     }
 667 
 668     private static Random randomNumberGenerator;
 669 
 670     private static synchronized Random initRNG() {
 671         Random rnd = randomNumberGenerator;
 672         return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
 673     }
 674 
 675     /**
 676      * Returns a {@code double} value with a positive sign, greater
 677      * than or equal to {@code 0.0} and less than {@code 1.0}.
 678      * Returned values are chosen pseudorandomly with (approximately)
 679      * uniform distribution from that range.
 680      *
 681      * <p>When this method is first called, it creates a single new
 682      * pseudorandom-number generator, exactly as if by the expression
 683      *
 684      * <blockquote>{@code new java.util.Random()}</blockquote>
 685      *


   1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 596      * <li>If both arguments are integers, then the result is exactly equal
 597      * to the mathematical result of raising the first argument to the power
 598      * of the second argument if that result can in fact be represented
 599      * exactly as a {@code double} value.</ul>
 600      *
 601      * <p>(In the foregoing descriptions, a floating-point value is
 602      * considered to be an integer if and only if it is finite and a
 603      * fixed point of the method {@link #ceil ceil} or,
 604      * equivalently, a fixed point of the method {@link #floor
 605      * floor}. A value is a fixed point of a one-argument
 606      * method if and only if the result of applying the method to the
 607      * value is equal to the value.)
 608      *
 609      * @param   a   base.
 610      * @param   b   the exponent.
 611      * @return  the value {@code a}<sup>{@code b}</sup>.
 612      */
 613     public static native double pow(double a, double b);
 614 
 615     /**
 616      * Returns the closest {@code int} to the argument, with ties
 617      * rounding up.



 618      *
 619      * <p>Special cases:
 620      * <ul><li>If the argument is NaN, the result is 0.
 621      * <li>If the argument is negative infinity or any value less than or
 622      * equal to the value of {@code Integer.MIN_VALUE}, the result is
 623      * equal to the value of {@code Integer.MIN_VALUE}.
 624      * <li>If the argument is positive infinity or any value greater than or
 625      * equal to the value of {@code Integer.MAX_VALUE}, the result is
 626      * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 627      *
 628      * @param   a   a floating-point value to be rounded to an integer.
 629      * @return  the value of the argument rounded to the nearest
 630      *          {@code int} value.
 631      * @see     java.lang.Integer#MAX_VALUE
 632      * @see     java.lang.Integer#MIN_VALUE
 633      */
 634     public static int round(float a) {
 635         return Math.round(a);
 636     }
 637 
 638     /**
 639      * Returns the closest {@code long} to the argument, with ties
 640      * rounding up.



 641      *
 642      * <p>Special cases:
 643      * <ul><li>If the argument is NaN, the result is 0.
 644      * <li>If the argument is negative infinity or any value less than or
 645      * equal to the value of {@code Long.MIN_VALUE}, the result is
 646      * equal to the value of {@code Long.MIN_VALUE}.
 647      * <li>If the argument is positive infinity or any value greater than or
 648      * equal to the value of {@code Long.MAX_VALUE}, the result is
 649      * equal to the value of {@code Long.MAX_VALUE}.</ul>
 650      *
 651      * @param   a  a floating-point value to be rounded to a
 652      *          {@code long}.
 653      * @return  the value of the argument rounded to the nearest
 654      *          {@code long} value.
 655      * @see     java.lang.Long#MAX_VALUE
 656      * @see     java.lang.Long#MIN_VALUE
 657      */
 658     public static long round(double a) {
 659         return Math.round(a);
 660     }
 661 
 662     private static Random randomNumberGenerator;
 663 
 664     private static synchronized Random initRNG() {
 665         Random rnd = randomNumberGenerator;
 666         return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
 667     }
 668 
 669     /**
 670      * Returns a {@code double} value with a positive sign, greater
 671      * than or equal to {@code 0.0} and less than {@code 1.0}.
 672      * Returned values are chosen pseudorandomly with (approximately)
 673      * uniform distribution from that range.
 674      *
 675      * <p>When this method is first called, it creates a single new
 676      * pseudorandom-number generator, exactly as if by the expression
 677      *
 678      * <blockquote>{@code new java.util.Random()}</blockquote>
 679      *