< prev index next >

## src/java.base/share/classes/java/math/BigDecimal.java

```8247782: typos in java.math
 ``` `````` 917 * @throws ArithmeticException if the result is inexact but the 918 * RoundingMode is UNNECESSARY. 919 * @throws NumberFormatException if {@code val} is infinite or NaN. 920 * @since 1.5 921 */ 922 public BigDecimal(double val, MathContext mc) { 923 if (Double.isInfinite(val) || Double.isNaN(val)) 924 throw new NumberFormatException("Infinite or NaN"); 925 // Translate the double into sign, exponent and significand, according 926 // to the formulae in JLS, Section 20.10.22. 927 long valBits = Double.doubleToLongBits(val); 928 int sign = ((valBits >> 63) == 0 ? 1 : -1); 929 int exponent = (int) ((valBits >> 52) & 0x7ffL); 930 long significand = (exponent == 0 931 ? (valBits & ((1L << 52) - 1)) << 1 932 : (valBits & ((1L << 52) - 1)) | (1L << 52)); 933 exponent -= 1075; 934 // At this point, val == sign * significand * 2**exponent. 935 936 /* 937 * Special case zero to supress nonterminating normalization and bogus 938 * scale calculation. 939 */ 940 if (significand == 0) { 941 this.intVal = BigInteger.ZERO; 942 this.scale = 0; 943 this.intCompact = 0; 944 this.precision = 1; 945 return; 946 } 947 // Normalize 948 while ((significand & 1) == 0) { // i.e., significand is even 949 significand >>= 1; 950 exponent++; 951 } 952 int scl = 0; 953 // Calculate intVal and scale 954 BigInteger rb; 955 long compactVal = sign * significand; 956 if (exponent == 0) { 957 rb = (compactVal == INFLATED) ? INFLATED_BIGINT : null; ``````4035 * @return a {@code BigDecimal} with the value (10n) and 4036 * in the meantime, the BIG_TEN_POWERS_TABLE array gets 4037 * expanded to the size greater than n. 4038 */ 4039 private static BigInteger expandBigIntegerTenPowers(int n) { 4040 synchronized(BigDecimal.class) { 4041 BigInteger[] pows = BIG_TEN_POWERS_TABLE; 4042 int curLen = pows.length; 4043 // The following comparison and the above synchronized statement is 4044 // to prevent multiple threads from expanding the same array. 4045 if (curLen <= n) { 4046 int newLen = curLen << 1; 4047 while (newLen <= n) { 4048 newLen <<= 1; 4049 } 4050 pows = Arrays.copyOf(pows, newLen); 4051 for (int i = curLen; i < newLen; i++) { 4052 pows[i] = pows[i - 1].multiply(BigInteger.TEN); 4053 } 4054 // Based on the following facts: 4055 // 1. pows is a private local varible; 4056 // 2. the following store is a volatile store. 4057 // the newly created array elements can be safely published. 4058 BIG_TEN_POWERS_TABLE = pows; 4059 } 4060 return pows[n]; 4061 } 4062 } 4063 4064 private static final long[] LONG_TEN_POWERS_TABLE = { 4065 1, // 0 / 10^0 4066 10, // 1 / 10^1 4067 100, // 2 / 10^2 4068 1000, // 3 / 10^3 4069 10000, // 4 / 10^4 4070 100000, // 5 / 10^5 4071 1000000, // 6 / 10^6 4072 10000000, // 7 / 10^7 4073 100000000, // 8 / 10^8 4074 1000000000, // 9 / 10^9 4075 10000000000L, // 10 / 10^10 ``` ``` `````` 917 * @throws ArithmeticException if the result is inexact but the 918 * RoundingMode is UNNECESSARY. 919 * @throws NumberFormatException if {@code val} is infinite or NaN. 920 * @since 1.5 921 */ 922 public BigDecimal(double val, MathContext mc) { 923 if (Double.isInfinite(val) || Double.isNaN(val)) 924 throw new NumberFormatException("Infinite or NaN"); 925 // Translate the double into sign, exponent and significand, according 926 // to the formulae in JLS, Section 20.10.22. 927 long valBits = Double.doubleToLongBits(val); 928 int sign = ((valBits >> 63) == 0 ? 1 : -1); 929 int exponent = (int) ((valBits >> 52) & 0x7ffL); 930 long significand = (exponent == 0 931 ? (valBits & ((1L << 52) - 1)) << 1 932 : (valBits & ((1L << 52) - 1)) | (1L << 52)); 933 exponent -= 1075; 934 // At this point, val == sign * significand * 2**exponent. 935 936 /* 937 * Special case zero to suppress nonterminating normalization and bogus 938 * scale calculation. 939 */ 940 if (significand == 0) { 941 this.intVal = BigInteger.ZERO; 942 this.scale = 0; 943 this.intCompact = 0; 944 this.precision = 1; 945 return; 946 } 947 // Normalize 948 while ((significand & 1) == 0) { // i.e., significand is even 949 significand >>= 1; 950 exponent++; 951 } 952 int scl = 0; 953 // Calculate intVal and scale 954 BigInteger rb; 955 long compactVal = sign * significand; 956 if (exponent == 0) { 957 rb = (compactVal == INFLATED) ? INFLATED_BIGINT : null; ``````4035 * @return a {@code BigDecimal} with the value (10n) and 4036 * in the meantime, the BIG_TEN_POWERS_TABLE array gets 4037 * expanded to the size greater than n. 4038 */ 4039 private static BigInteger expandBigIntegerTenPowers(int n) { 4040 synchronized(BigDecimal.class) { 4041 BigInteger[] pows = BIG_TEN_POWERS_TABLE; 4042 int curLen = pows.length; 4043 // The following comparison and the above synchronized statement is 4044 // to prevent multiple threads from expanding the same array. 4045 if (curLen <= n) { 4046 int newLen = curLen << 1; 4047 while (newLen <= n) { 4048 newLen <<= 1; 4049 } 4050 pows = Arrays.copyOf(pows, newLen); 4051 for (int i = curLen; i < newLen; i++) { 4052 pows[i] = pows[i - 1].multiply(BigInteger.TEN); 4053 } 4054 // Based on the following facts: 4055 // 1. pows is a private local variable; 4056 // 2. the following store is a volatile store. 4057 // the newly created array elements can be safely published. 4058 BIG_TEN_POWERS_TABLE = pows; 4059 } 4060 return pows[n]; 4061 } 4062 } 4063 4064 private static final long[] LONG_TEN_POWERS_TABLE = { 4065 1, // 0 / 10^0 4066 10, // 1 / 10^1 4067 100, // 2 / 10^2 4068 1000, // 3 / 10^3 4069 10000, // 4 / 10^4 4070 100000, // 5 / 10^5 4071 1000000, // 6 / 10^6 4072 10000000, // 7 / 10^7 4073 100000000, // 8 / 10^8 4074 1000000000, // 9 / 10^9 4075 10000000000L, // 10 / 10^10 ```