1 /*
2 * Copyright (c) 1994, 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
610 * <p>(In the foregoing descriptions, a floating-point value is
611 * considered to be an integer if and only if it is finite and a
612 * fixed point of the method {@link #ceil ceil} or,
613 * equivalently, a fixed point of the method {@link #floor
614 * floor}. A value is a fixed point of a one-argument
615 * method if and only if the result of applying the method to the
616 * value is equal to the value.)
617 *
618 * <p>The computed result must be within 1 ulp of the exact result.
619 * Results must be semi-monotonic.
620 *
621 * @param a the base.
622 * @param b the exponent.
623 * @return the value {@code a}<sup>{@code b}</sup>.
624 */
625 public static double pow(double a, double b) {
626 return StrictMath.pow(a, b); // default impl. delegates to StrictMath
627 }
628
629 /**
630 * Returns the closest {@code int} to the argument. The
631 * result is rounded to an integer by adding 1/2, taking the
632 * floor of the result, and casting the result to type {@code int}.
633 * In other words, the result is equal to the value of the expression:
634 * <p>{@code (int)Math.floor(a + 0.5f)}
635 * <p>
636 * Special cases:
637 * <ul><li>If the argument is NaN, the result is 0.
638 * <li>If the argument is negative infinity or any value less than or
639 * equal to the value of {@code Integer.MIN_VALUE}, the result is
640 * equal to the value of {@code Integer.MIN_VALUE}.
641 * <li>If the argument is positive infinity or any value greater than or
642 * equal to the value of {@code Integer.MAX_VALUE}, the result is
643 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
644 *
645 * @param a a floating-point value to be rounded to an integer.
646 * @return the value of the argument rounded to the nearest
647 * {@code int} value.
648 * @see java.lang.Integer#MAX_VALUE
649 * @see java.lang.Integer#MIN_VALUE
650 */
651 public static int round(float a) {
652 return (int)floor(a + 0.5f);
653 }
654
655 /**
656 * Returns the closest {@code long} to the argument. The result
657 * is rounded to an integer by adding 1/2, taking the floor of the
658 * result, and casting the result to type {@code long}. In other
659 * words, the result is equal to the value of the expression:
660 * <p>{@code (long)Math.floor(a + 0.5d)}
661 * <p>
662 * Special cases:
663 * <ul><li>If the argument is NaN, the result is 0.
664 * <li>If the argument is negative infinity or any value less than or
665 * equal to the value of {@code Long.MIN_VALUE}, the result is
666 * equal to the value of {@code Long.MIN_VALUE}.
667 * <li>If the argument is positive infinity or any value greater than or
668 * equal to the value of {@code Long.MAX_VALUE}, the result is
669 * equal to the value of {@code Long.MAX_VALUE}.</ul>
670 *
671 * @param a a floating-point value to be rounded to a
672 * {@code long}.
673 * @return the value of the argument rounded to the nearest
674 * {@code long} value.
675 * @see java.lang.Long#MAX_VALUE
676 * @see java.lang.Long#MIN_VALUE
677 */
678 public static long round(double a) {
679 return (long)floor(a + 0.5d);
680 }
681
682 private static Random randomNumberGenerator;
683
684 private static synchronized Random initRNG() {
685 Random rnd = randomNumberGenerator;
686 return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
687 }
688
689 /**
690 * Returns a {@code double} value with a positive sign, greater
691 * than or equal to {@code 0.0} and less than {@code 1.0}.
692 * Returned values are chosen pseudorandomly with (approximately)
693 * uniform distribution from that range.
694 *
695 * <p>When this method is first called, it creates a single new
696 * pseudorandom-number generator, exactly as if by the expression
697 *
698 * <blockquote>{@code new java.util.Random()}</blockquote>
699 *
|
1 /*
2 * Copyright (c) 1994, 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
610 * <p>(In the foregoing descriptions, a floating-point value is
611 * considered to be an integer if and only if it is finite and a
612 * fixed point of the method {@link #ceil ceil} or,
613 * equivalently, a fixed point of the method {@link #floor
614 * floor}. A value is a fixed point of a one-argument
615 * method if and only if the result of applying the method to the
616 * value is equal to the value.)
617 *
618 * <p>The computed result must be within 1 ulp of the exact result.
619 * Results must be semi-monotonic.
620 *
621 * @param a the base.
622 * @param b the exponent.
623 * @return the value {@code a}<sup>{@code b}</sup>.
624 */
625 public static double pow(double a, double b) {
626 return StrictMath.pow(a, b); // default impl. delegates to StrictMath
627 }
628
629 /**
630 * Returns the closest {@code int} to the argument, with ties
631 * rounding up.
632 *
633 * <p>
634 * Special cases:
635 * <ul><li>If the argument is NaN, the result is 0.
636 * <li>If the argument is negative infinity or any value less than or
637 * equal to the value of {@code Integer.MIN_VALUE}, the result is
638 * equal to the value of {@code Integer.MIN_VALUE}.
639 * <li>If the argument is positive infinity or any value greater than or
640 * equal to the value of {@code Integer.MAX_VALUE}, the result is
641 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
642 *
643 * @param a a floating-point value to be rounded to an integer.
644 * @return the value of the argument rounded to the nearest
645 * {@code int} value.
646 * @see java.lang.Integer#MAX_VALUE
647 * @see java.lang.Integer#MIN_VALUE
648 */
649 public static int round(float a) {
650 if (a != 0x1.fffffep-2f) // greatest float value less than 0.5
651 return (int)floor(a + 0.5f);
652 else
653 return 0;
654 }
655
656 /**
657 * Returns the closest {@code long} to the argument, with ties
658 * rounding up.
659 *
660 * <p>Special cases:
661 * <ul><li>If the argument is NaN, the result is 0.
662 * <li>If the argument is negative infinity or any value less than or
663 * equal to the value of {@code Long.MIN_VALUE}, the result is
664 * equal to the value of {@code Long.MIN_VALUE}.
665 * <li>If the argument is positive infinity or any value greater than or
666 * equal to the value of {@code Long.MAX_VALUE}, the result is
667 * equal to the value of {@code Long.MAX_VALUE}.</ul>
668 *
669 * @param a a floating-point value to be rounded to a
670 * {@code long}.
671 * @return the value of the argument rounded to the nearest
672 * {@code long} value.
673 * @see java.lang.Long#MAX_VALUE
674 * @see java.lang.Long#MIN_VALUE
675 */
676 public static long round(double a) {
677 if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
678 return (long)floor(a + 0.5d);
679 else
680 return 0;
681 }
682
683 private static Random randomNumberGenerator;
684
685 private static synchronized Random initRNG() {
686 Random rnd = randomNumberGenerator;
687 return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
688 }
689
690 /**
691 * Returns a {@code double} value with a positive sign, greater
692 * than or equal to {@code 0.0} and less than {@code 1.0}.
693 * Returned values are chosen pseudorandomly with (approximately)
694 * uniform distribution from that range.
695 *
696 * <p>When this method is first called, it creates a single new
697 * pseudorandom-number generator, exactly as if by the expression
698 *
699 * <blockquote>{@code new java.util.Random()}</blockquote>
700 *
|