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

Print this page




 348      * mathematical integer, then the result is the same as the
 349      * argument.  <li>If the argument is NaN or an infinity or
 350      * positive zero or negative zero, then the result is the same as
 351      * the argument.</ul>
 352      *
 353      * @param   a   a value.
 354      * @return  the largest (closest to positive infinity)
 355      *          floating-point value that less than or equal to the argument
 356      *          and is equal to a mathematical integer.
 357      */
 358     public static double floor(double a) {
 359         return floorOrCeil(a, -1.0, 0.0, -1.0);
 360     }
 361 
 362     /**
 363      * Internal method to share logic between floor and ceil.
 364      *
 365      * @param a the value to be floored or ceiled
 366      * @param negativeBoundary result for values in (-1, 0)
 367      * @param positiveBoundary result for values in (0, 1)
 368      * @param increment value to add when the argument is non-integral
 369      */
 370     private static double floorOrCeil(double a,
 371                                       double negativeBoundary,
 372                                       double positiveBoundary,
 373                                       double sign) {
 374         int exponent = Math.getExponent(a);
 375 
 376         if (exponent < 0) {
 377             /*
 378              * Absolute value of argument is less than 1.
 379              * floorOrceil(-0.0) => -0.0
 380              * floorOrceil(+0.0) => +0.0
 381              */
 382             return ((a == 0.0) ? a :
 383                     ( (a < 0.0) ?  negativeBoundary : positiveBoundary) );
 384         } else if (exponent >= 52) {
 385             /*
 386              * Infinity, NaN, or a value so large it must be integral.
 387              */
 388             return a;


 794      * throwing an exception if the result overflows a {@code long}.
 795      *
 796      * @param x the first value
 797      * @param y the second value
 798      * @return the result
 799      * @throws ArithmeticException if the result overflows a long
 800      * @see Math#multiplyExact(long,long)
 801      * @since 1.8
 802      */
 803     public static long multiplyExact(long x, long y) {
 804         return Math.multiplyExact(x, y);
 805     }
 806 
 807     /**
 808      * Return the value of the {@code long} argument;
 809      * throwing an exception if the value overflows an {@code int}.
 810      *
 811      * @param value the long value
 812      * @return the argument as an int
 813      * @throws ArithmeticException if the {@code argument} overflows an int
 814      * @see Math#toIntExact(int)
 815      * @since 1.8
 816      */
 817     public static int toIntExact(long value) {
 818         return Math.toIntExact(value);
 819     }
 820 
 821     /**
 822      * Returns the absolute value of an {@code int} value.
 823      * If the argument is not negative, the argument is returned.
 824      * If the argument is negative, the negation of the argument is returned.
 825      *
 826      * <p>Note that if the argument is equal to the value of
 827      * {@link Integer#MIN_VALUE}, the most negative representable
 828      * {@code int} value, the result is that same value, which is
 829      * negative.
 830      *
 831      * @param   a   the  argument whose absolute value is to be determined.
 832      * @return  the absolute value of the argument.
 833      */
 834     public static int abs(int a) {




 348      * mathematical integer, then the result is the same as the
 349      * argument.  <li>If the argument is NaN or an infinity or
 350      * positive zero or negative zero, then the result is the same as
 351      * the argument.</ul>
 352      *
 353      * @param   a   a value.
 354      * @return  the largest (closest to positive infinity)
 355      *          floating-point value that less than or equal to the argument
 356      *          and is equal to a mathematical integer.
 357      */
 358     public static double floor(double a) {
 359         return floorOrCeil(a, -1.0, 0.0, -1.0);
 360     }
 361 
 362     /**
 363      * Internal method to share logic between floor and ceil.
 364      *
 365      * @param a the value to be floored or ceiled
 366      * @param negativeBoundary result for values in (-1, 0)
 367      * @param positiveBoundary result for values in (0, 1)
 368      * @param sign the sign of the result
 369      */
 370     private static double floorOrCeil(double a,
 371                                       double negativeBoundary,
 372                                       double positiveBoundary,
 373                                       double sign) {
 374         int exponent = Math.getExponent(a);
 375 
 376         if (exponent < 0) {
 377             /*
 378              * Absolute value of argument is less than 1.
 379              * floorOrceil(-0.0) => -0.0
 380              * floorOrceil(+0.0) => +0.0
 381              */
 382             return ((a == 0.0) ? a :
 383                     ( (a < 0.0) ?  negativeBoundary : positiveBoundary) );
 384         } else if (exponent >= 52) {
 385             /*
 386              * Infinity, NaN, or a value so large it must be integral.
 387              */
 388             return a;


 794      * throwing an exception if the result overflows a {@code long}.
 795      *
 796      * @param x the first value
 797      * @param y the second value
 798      * @return the result
 799      * @throws ArithmeticException if the result overflows a long
 800      * @see Math#multiplyExact(long,long)
 801      * @since 1.8
 802      */
 803     public static long multiplyExact(long x, long y) {
 804         return Math.multiplyExact(x, y);
 805     }
 806 
 807     /**
 808      * Return the value of the {@code long} argument;
 809      * throwing an exception if the value overflows an {@code int}.
 810      *
 811      * @param value the long value
 812      * @return the argument as an int
 813      * @throws ArithmeticException if the {@code argument} overflows an int
 814      * @see Math#toIntExact(long)
 815      * @since 1.8
 816      */
 817     public static int toIntExact(long value) {
 818         return Math.toIntExact(value);
 819     }
 820 
 821     /**
 822      * Returns the absolute value of an {@code int} value.
 823      * If the argument is not negative, the argument is returned.
 824      * If the argument is negative, the negation of the argument is returned.
 825      *
 826      * <p>Note that if the argument is equal to the value of
 827      * {@link Integer#MIN_VALUE}, the most negative representable
 828      * {@code int} value, the result is that same value, which is
 829      * negative.
 830      *
 831      * @param   a   the  argument whose absolute value is to be determined.
 832      * @return  the absolute value of the argument.
 833      */
 834     public static int abs(int a) {