src/share/classes/java/math/BigInteger.java
Print this page
*** 2917,2926 ****
--- 2917,2927 ----
* Note that this conversion can lose information about the
* overall magnitude of the BigInteger value as well as return a
* result with the opposite sign.
*
* @return this BigInteger converted to an {@code int}.
+ * @see #intValueExact()
*/
public int intValue() {
int result = 0;
result = getInt(0);
return result;
*** 2937,2946 ****
--- 2938,2948 ----
* Note that this conversion can lose information about the
* overall magnitude of the BigInteger value as well as return a
* result with the opposite sign.
*
* @return this BigInteger converted to a {@code long}.
+ * @see #longValueExact()
*/
public long longValue() {
long result = 0;
for (int i=1; i>=0; i--)
*** 3380,3385 ****
--- 3382,3467 ----
}
result[i] = (byte)nextInt;
}
return result;
}
+
+ /**
+ * Converts this {@code BigInteger} to a {@code long}, checking
+ * for lost information. If the value of this {@code BigInteger}
+ * is out of the range of the {@code long} type, then an
+ * {@code ArithmeticException} is thrown.
+ *
+ * @return this {@code BigInteger} converted to a {@code long}.
+ * @throws ArithmeticException if the value of {@code this} will
+ * not exactly fit in a {@code long}.
+ * @see BigInteger#longValue
+ * @since 1.8
+ */
+ public long longValueExact() {
+ if (mag.length <= 2 && bitLength() <= 63)
+ return longValue();
+ else
+ throw new ArithmeticException("BigInteger out of long range");
+ }
+
+ /**
+ * Converts this {@code BigInteger} to an {@code int}, checking
+ * for lost information. If the value of this {@code BigInteger}
+ * is out of the range of the {@code int} type, then an
+ * {@code ArithmeticException} is thrown.
+ *
+ * @return this {@code BigInteger} converted to an {@code int}.
+ * @throws ArithmeticException if the value of {@code this} will
+ * not exactly fit in a {@code int}.
+ * @see BigInteger#intValue
+ * @since 1.8
+ */
+ public int intValueExact() {
+ if (mag.length <= 1 && bitLength() <= 31)
+ return intValue();
+ else
+ throw new ArithmeticException("BigInteger out of int range");
+ }
+
+ /**
+ * Converts this {@code BigInteger} to a {@code short}, checking
+ * for lost information. If the value of this {@code BigInteger}
+ * is out of the range of the {@code short} type, then an
+ * {@code ArithmeticException} is thrown.
+ *
+ * @return this {@code BigInteger} converted to a {@code short}.
+ * @throws ArithmeticException if the value of {@code this} will
+ * not exactly fit in a {@code short}.
+ * @see BigInteger#shortValue
+ * @since 1.8
+ */
+ public short shortValueExact() {
+ if (mag.length <= 1 && bitLength() <= 31) {
+ int value = intValue();
+ if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE)
+ return shortValue();
+ }
+ throw new ArithmeticException("BigInteger out of short range");
+ }
+
+ /**
+ * Converts this {@code BigInteger} to a {@code byte}, checking
+ * for lost information. If the value of this {@code BigInteger}
+ * is out of the range of the {@code byte} type, then an
+ * {@code ArithmeticException} is thrown.
+ *
+ * @return this {@code BigInteger} converted to a {@code byte}.
+ * @throws ArithmeticException if the value of {@code this} will
+ * not exactly fit in a {@code byte}.
+ * @see BigInteger#byteValue
+ * @since 1.8
+ */
+ public byte byteValueExact() {
+ if (mag.length <= 1 && bitLength() <= 31) {
+ int value = intValue();
+ if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE)
+ return byteValue();
+ }
+ throw new ArithmeticException("BigInteger out of byte range");
+ }
}