/** Returns the value of this {@code UnsignedLong} as a {@link BigInteger}. */ public BigInteger bigIntegerValue() { BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK); if (value < 0) { bigInt = bigInt.setBit(Long.SIZE - 1); } return bigInt; }
/** Returns the value of this {@code UnsignedLong} as a {@link BigInteger}. */ public BigInteger bigIntegerValue() { BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK); if (value < 0) { bigInt = bigInt.setBit(Long.SIZE - 1); } return bigInt; }
/** * Returns the smallest power of two greater than or equal to {@code x}. This is equivalent to * {@code BigInteger.valueOf(2).pow(log2(x, CEILING))}. * * @throws IllegalArgumentException if {@code x <= 0} * @since 20.0 */ @Beta public static BigInteger ceilingPowerOfTwo(BigInteger x) { return BigInteger.ZERO.setBit(log2(x, RoundingMode.CEILING)); }
/** * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code * BigInteger.valueOf(2).pow(log2(x, FLOOR))}. * * @throws IllegalArgumentException if {@code x <= 0} * @since 20.0 */ @Beta public static BigInteger floorPowerOfTwo(BigInteger x) { return BigInteger.ZERO.setBit(log2(x, RoundingMode.FLOOR)); }
/** * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code * BigInteger.valueOf(2).pow(log2(x, FLOOR))}. * * @throws IllegalArgumentException if {@code x <= 0} * @since 20.0 */ @Beta public static BigInteger floorPowerOfTwo(BigInteger x) { return BigInteger.ZERO.setBit(log2(x, RoundingMode.FLOOR)); }
/** * Generates a number in [0, 2^numBits) with an exponential distribution. The floor of the log2 of * the result is chosen uniformly at random in [0, numBits), and then the result is chosen in that * range uniformly at random. Zero is treated as having log2 == 0. */ static BigInteger randomNonNegativeBigInteger(int numBits) { int digits = RANDOM_SOURCE.nextInt(numBits); if (digits == 0) { return new BigInteger(1, RANDOM_SOURCE); } else { return new BigInteger(digits, RANDOM_SOURCE).setBit(digits); } }
/** * Returns the smallest power of two greater than or equal to {@code x}. This is equivalent to * {@code BigInteger.valueOf(2).pow(log2(x, CEILING))}. * * @throws IllegalArgumentException if {@code x <= 0} * @since 20.0 */ @Beta public static BigInteger ceilingPowerOfTwo(BigInteger x) { return BigInteger.ZERO.setBit(log2(x, RoundingMode.CEILING)); }
/** * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code * BigInteger.valueOf(2).pow(log2(x, FLOOR))}. * * @throws IllegalArgumentException if {@code x <= 0} * @since 20.0 */ @Beta public static BigInteger floorPowerOfTwo(BigInteger x) { return BigInteger.ZERO.setBit(log2(x, RoundingMode.FLOOR)); }
public void testLog2HalfDown() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { int result = BigIntegerMath.log2(x, HALF_DOWN); BigInteger x2 = x.pow(2); // x^2 <= 2^(2 * result + 1), or else we would have rounded up assertTrue(ZERO.setBit(2 * result + 1).compareTo(x2) >= 0); // x^2 > 2^(2 * result - 1), or else we would have rounded down assertTrue(result == 0 || ZERO.setBit(2 * result - 1).compareTo(x2) < 0); } }
public void testLog2HalfUp() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { int result = BigIntegerMath.log2(x, HALF_UP); BigInteger x2 = x.pow(2); // x^2 < 2^(2 * result + 1), or else we would have rounded up assertTrue(ZERO.setBit(2 * result + 1).compareTo(x2) > 0); // x^2 >= 2^(2 * result - 1), or else we would have rounded down assertTrue(result == 0 || ZERO.setBit(2 * result - 1).compareTo(x2) <= 0); } }
public void testLog2Ceiling() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { for (RoundingMode mode : asList(CEILING, UP)) { int result = BigIntegerMath.log2(x, mode); assertTrue(ZERO.setBit(result).compareTo(x) >= 0); assertTrue(result == 0 || ZERO.setBit(result - 1).compareTo(x) < 0); } } }
public void testAsUnsignedBigIntegerValue() { for (long value : TEST_LONGS) { BigInteger expected = (value >= 0) ? BigInteger.valueOf(value) : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64)); assertEquals( UnsignedLongs.toString(value), expected, UnsignedLong.fromLongBits(value).bigIntegerValue()); } }
@GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testConstantMaxPowerOfSqrt2Unsigned() { assertEquals( /*expected=*/ BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Integer.SIZE - 1), FLOOR) .intValue(), /*actual=*/ IntMath.MAX_POWER_OF_SQRT2_UNSIGNED); }
@GwtIncompatible // TODO public void testConstantMaxPowerOfSqrt2Unsigned() { assertEquals( /*expected=*/ BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Long.SIZE - 1), FLOOR) .longValue(), /*actual=*/ LongMath.MAX_POWER_OF_SQRT2_UNSIGNED); }
@Test public void testUnscaledBigIntegerToDecimalOverflow() { assertUnscaledBigIntegerToDecimalOverflows(MAX_DECIMAL_UNSCALED_VALUE.add(BigInteger.ONE)); assertUnscaledBigIntegerToDecimalOverflows(MAX_DECIMAL_UNSCALED_VALUE.setBit(95)); assertUnscaledBigIntegerToDecimalOverflows(MAX_DECIMAL_UNSCALED_VALUE.setBit(127)); assertUnscaledBigIntegerToDecimalOverflows(MIN_DECIMAL_UNSCALED_VALUE.subtract(BigInteger.ONE)); }
@GwtIncompatible // TODO public void testConstantSqrt2PrecomputedBits() { assertEquals( BigIntegerMath.sqrt( BigInteger.ZERO.setBit(2 * BigIntegerMath.SQRT2_PRECOMPUTE_THRESHOLD + 1), FLOOR), BigIntegerMath.SQRT2_PRECOMPUTED_BITS); }
@GwtIncompatible // too slow public void testEquals() { EqualsTester equalsTester = new EqualsTester(); for (long a : TEST_LONGS) { BigInteger big = (a >= 0) ? BigInteger.valueOf(a) : BigInteger.valueOf(a).add(BigInteger.ZERO.setBit(64)); equalsTester.addEqualityGroup( UnsignedLong.fromLongBits(a), UnsignedLong.valueOf(big), UnsignedLong.valueOf(big.toString()), UnsignedLong.valueOf(big.toString(16), 16)); } equalsTester.testEquals(); }
public void testParseLongWithRadixLimits() { BigInteger max = BigInteger.ZERO.setBit(64).subtract(ONE); // loops through all legal radix values. for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { // tests can successfully parse a number string with this radix. String maxAsString = max.toString(radix); assertEquals(max.longValue(), UnsignedLongs.parseUnsignedLong(maxAsString, radix)); try { // tests that we get exception whre an overflow would occur. BigInteger overflow = max.add(ONE); String overflowAsString = overflow.toString(radix); UnsignedLongs.parseUnsignedLong(overflowAsString, radix); fail(); } catch (NumberFormatException expected) { } } try { UnsignedLongs.parseUnsignedLong("1234567890abcdef1", 16); fail(); } catch (NumberFormatException expected) { } }
public void testLog2Exact() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { // We only expect an exception if x was not a power of 2. boolean isPowerOf2 = BigIntegerMath.isPowerOfTwo(x); try { assertEquals(x, ZERO.setBit(BigIntegerMath.log2(x, UNNECESSARY))); assertTrue(isPowerOf2); } catch (ArithmeticException e) { assertFalse(isPowerOf2); } } }