/** * 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 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)); }
public void testLog2HalfEven() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { int halfEven = BigIntegerMath.log2(x, HALF_EVEN); // Now figure out what rounding mode we should behave like (it depends if FLOOR was // odd/even). boolean floorWasEven = (BigIntegerMath.log2(x, FLOOR) & 1) == 0; assertEquals(BigIntegerMath.log2(x, floorWasEven ? HALF_DOWN : HALF_UP), halfEven); } }
/** * 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 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)); }
int log2 = log2(x, FLOOR); if (log2 < Double.MAX_EXPONENT) { sqrt0 = sqrtApproxWithDoubles(x);
public void testLog2ZeroAlwaysThrows() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { BigIntegerMath.log2(ZERO, mode); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } }
public void testLog2NegativeAlwaysThrows() { for (RoundingMode mode : ALL_ROUNDING_MODES) { try { BigIntegerMath.log2(BigInteger.valueOf(-1), mode); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } }
int approxLog10 = (int) (log2(x, FLOOR) * LN_2 / LN_10); BigInteger approxPow = BigInteger.TEN.pow(approxLog10); int approxCmp = approxPow.compareTo(x);
public void testLog2MatchesBigInteger() { for (long x : POSITIVE_LONG_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // The BigInteger implementation is tested separately, use it as the reference. assertEquals(BigIntegerMath.log2(valueOf(x), mode), LongMath.log2(x, mode)); } } }
int log2 = log2(x, FLOOR); if (log2 < Double.MAX_EXPONENT) { sqrt0 = sqrtApproxWithDoubles(x);
public void testLog2MatchesBigInteger() { for (int x : POSITIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { assertEquals(BigIntegerMath.log2(valueOf(x), mode), IntMath.log2(x, mode)); } } }
int approxLog10 = (int) (log2(x, FLOOR) * LN_2 / LN_10); BigInteger approxPow = BigInteger.TEN.pow(approxLog10); int approxCmp = approxPow.compareTo(x);
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 testLog2Floor() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { for (RoundingMode mode : asList(FLOOR, DOWN)) { int result = BigIntegerMath.log2(x, mode); assertTrue(ZERO.setBit(result).compareTo(x) <= 0); assertTrue(ZERO.setBit(result + 1).compareTo(x) > 0); } } }
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 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 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); } } }