1 /* |
1 /* |
2 * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
644 return StrictMath.pow(a, b); // default impl. delegates to StrictMath |
644 return StrictMath.pow(a, b); // default impl. delegates to StrictMath |
645 } |
645 } |
646 |
646 |
647 /** |
647 /** |
648 * Returns the closest {@code int} to the argument, with ties |
648 * Returns the closest {@code int} to the argument, with ties |
649 * rounding up. |
649 * rounding to positive infinity. |
650 * |
650 * |
651 * <p> |
651 * <p> |
652 * Special cases: |
652 * Special cases: |
653 * <ul><li>If the argument is NaN, the result is 0. |
653 * <ul><li>If the argument is NaN, the result is 0. |
654 * <li>If the argument is negative infinity or any value less than or |
654 * <li>If the argument is negative infinity or any value less than or |
663 * {@code int} value. |
663 * {@code int} value. |
664 * @see java.lang.Integer#MAX_VALUE |
664 * @see java.lang.Integer#MAX_VALUE |
665 * @see java.lang.Integer#MIN_VALUE |
665 * @see java.lang.Integer#MIN_VALUE |
666 */ |
666 */ |
667 public static int round(float a) { |
667 public static int round(float a) { |
668 if (a != 0x1.fffffep-2f) // greatest float value less than 0.5 |
668 int intBits = Float.floatToRawIntBits(a); |
669 return (int)floor(a + 0.5f); |
669 int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK) |
670 else |
670 >> (FloatConsts.SIGNIFICAND_WIDTH - 1); |
671 return 0; |
671 int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2 |
|
672 + FloatConsts.EXP_BIAS) - biasedExp; |
|
673 if ((shift & -32) == 0) { // shift >= 0 && shift < 32 |
|
674 // a is a finite number such that pow(2,-32) <= ulp(a) < 1 |
|
675 int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK) |
|
676 | (FloatConsts.SIGNIF_BIT_MASK + 1)); |
|
677 if (intBits < 0) { |
|
678 r = -r; |
|
679 } |
|
680 // In the comments below each Java expression evaluates to the value |
|
681 // the corresponding mathematical expression: |
|
682 // (r) evaluates to a / ulp(a) |
|
683 // (r >> shift) evaluates to floor(a * 2) |
|
684 // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2) |
|
685 // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2) |
|
686 return ((r >> shift) + 1) >> 1; |
|
687 } else { |
|
688 // a is either |
|
689 // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2 |
|
690 // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer |
|
691 // - an infinity or NaN |
|
692 return (int) a; |
|
693 } |
672 } |
694 } |
673 |
695 |
674 /** |
696 /** |
675 * Returns the closest {@code long} to the argument, with ties |
697 * Returns the closest {@code long} to the argument, with ties |
676 * rounding up. |
698 * rounding to positive infinity. |
677 * |
699 * |
678 * <p>Special cases: |
700 * <p>Special cases: |
679 * <ul><li>If the argument is NaN, the result is 0. |
701 * <ul><li>If the argument is NaN, the result is 0. |
680 * <li>If the argument is negative infinity or any value less than or |
702 * <li>If the argument is negative infinity or any value less than or |
681 * equal to the value of {@code Long.MIN_VALUE}, the result is |
703 * equal to the value of {@code Long.MIN_VALUE}, the result is |
690 * {@code long} value. |
712 * {@code long} value. |
691 * @see java.lang.Long#MAX_VALUE |
713 * @see java.lang.Long#MAX_VALUE |
692 * @see java.lang.Long#MIN_VALUE |
714 * @see java.lang.Long#MIN_VALUE |
693 */ |
715 */ |
694 public static long round(double a) { |
716 public static long round(double a) { |
695 if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5 |
717 long longBits = Double.doubleToRawLongBits(a); |
696 return (long)floor(a + 0.5d); |
718 long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK) |
697 else |
719 >> (DoubleConsts.SIGNIFICAND_WIDTH - 1); |
698 return 0; |
720 long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2 |
|
721 + DoubleConsts.EXP_BIAS) - biasedExp; |
|
722 if ((shift & -64) == 0) { // shift >= 0 && shift < 64 |
|
723 // a is a finite number such that pow(2,-64) <= ulp(a) < 1 |
|
724 long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK) |
|
725 | (DoubleConsts.SIGNIF_BIT_MASK + 1)); |
|
726 if (longBits < 0) { |
|
727 r = -r; |
|
728 } |
|
729 // In the comments below each Java expression evaluates to the value |
|
730 // the corresponding mathematical expression: |
|
731 // (r) evaluates to a / ulp(a) |
|
732 // (r >> shift) evaluates to floor(a * 2) |
|
733 // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2) |
|
734 // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2) |
|
735 return ((r >> shift) + 1) >> 1; |
|
736 } else { |
|
737 // a is either |
|
738 // - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2 |
|
739 // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer |
|
740 // - an infinity or NaN |
|
741 return (long) a; |
|
742 } |
699 } |
743 } |
700 |
744 |
701 private static final class RandomNumberGeneratorHolder { |
745 private static final class RandomNumberGeneratorHolder { |
702 static final Random randomNumberGenerator = new Random(); |
746 static final Random randomNumberGenerator = new Random(); |
703 } |
747 } |