@GwtIncompatible // TODO public static long checkedPow(long b, int k) { checkNonNegative("exponent", k); if (b >= -2 & b <= 2) { switch ((int) b) { return ((k & 1) == 0) ? 1 : -1; case 2: checkNoOverflow(k < Long.SIZE - 1, "checkedPow", b, k); return 1L << k; case (-2): checkNoOverflow(k < Long.SIZE, "checkedPow", b, k); return ((k & 1) == 0) ? (1L << k) : (-1L << k); default: return accum; case 1: return checkedMultiply(accum, b); default: if ((k & 1) != 0) { accum = checkedMultiply(accum, b); checkNoOverflow( -FLOOR_SQRT_MAX_LONG <= b && b <= FLOOR_SQRT_MAX_LONG, "checkedPow", b, k); b *= b;
@GwtIncompatible // TODO @SuppressWarnings("fallthrough") public static int log10(BigInteger x, RoundingMode mode) { checkPositive("x", x); if (fitsInLong(x)) { return LongMath.log10(x.longValue(), mode); int approxLog10 = (int) (log2(x, FLOOR) * LN_2 / LN_10); BigInteger approxPow = BigInteger.TEN.pow(approxLog10); int approxCmp = approxPow.compareTo(x); checkRoundingUnnecessary(floorCmp == 0);
/** * Returns the {@code long} value that is equal to {@code x} rounded with the specified rounding * mode, if possible. * * @throws ArithmeticException if * <ul> * <li>{@code x} is infinite or NaN * <li>{@code x}, after being rounded to a mathematical integer using the specified rounding * mode, is either less than {@code Long.MIN_VALUE} or greater than {@code * Long.MAX_VALUE} * <li>{@code x} is not a mathematical integer and {@code mode} is {@link * RoundingMode#UNNECESSARY} * </ul> */ @GwtIncompatible // #roundIntermediate public static long roundToLong(double x, RoundingMode mode) { double z = roundIntermediate(x, mode); checkInRange(MIN_LONG_AS_DOUBLE - z < 1.0 & z < MAX_LONG_AS_DOUBLE_PLUS_ONE); return (long) z; }
checkNonNegative("exponent", k); switch (b) { case 0: return ((k & 1) == 0) ? 1 : -1; case 2: checkNoOverflow(k < Integer.SIZE - 1, "checkedPow", b, k); return 1 << k; case (-2): checkNoOverflow(k < Integer.SIZE, "checkedPow", b, k); return ((k & 1) == 0) ? 1 << k : -1 << k; default: return accum; case 1: return checkedMultiply(accum, b); default: if ((k & 1) != 0) { accum = checkedMultiply(accum, b); checkNoOverflow(-FLOOR_SQRT_MAX_INT <= b & b <= FLOOR_SQRT_MAX_INT, "checkedPow", b, k); b *= b;
@GwtIncompatible // need BigIntegerMath to adequately test @SuppressWarnings("fallthrough") public static int log10(int x, RoundingMode mode) { checkPositive("x", x); int logFloor = log10Floor(x); int floorPow = powersOf10[logFloor]; switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(x == floorPow); case CEILING: case UP: return logFloor + lessThanBranchFree(floorPow, x); case HALF_DOWN: case HALF_UP: case HALF_EVEN: return logFloor + lessThanBranchFree(halfPowersOf10[logFloor], x); default: throw new AssertionError();
@GwtIncompatible // TODO @SuppressWarnings("fallthrough") public static BigInteger sqrt(BigInteger x, RoundingMode mode) { checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode)); BigInteger sqrtFloor = sqrtFloor(x); switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(sqrtFloor.pow(2).equals(x)); // fall through case FLOOR: case DOWN:
checkNonNegative("n", n); checkNonNegative("k", k); checkArgument(k <= n, "k (%s) > n (%s)", k, n); if (k > (n >> 1)) { return BigInteger.valueOf(LongMath.binomial(n, k)); long denominatorAccum = 1; int bits = LongMath.log2(n, RoundingMode.CEILING); .multiply(BigInteger.valueOf(numeratorAccum)) .divide(BigInteger.valueOf(denominatorAccum)); numeratorAccum = p;
checkNonNegative("n", n); return BigInteger.valueOf(LongMath.factorials[n]); int approxSize = IntMath.divide(n * IntMath.log2(n, CEILING), Long.SIZE, CEILING); ArrayList<BigInteger> bignums = new ArrayList<>(approxSize); int productBits = LongMath.log2(product, FLOOR) + 1; int bits = LongMath.log2(startingNumber, FLOOR) + 1; bignums.add(BigInteger.valueOf(product)); product = 1; productBits = 0; productBits = LongMath.log2(product, FLOOR) + 1; bignums.add(BigInteger.valueOf(product)); return listProduct(bignums).shiftLeft(shift);
checkPositive("x", checkNotNull(x)); int logFloor = x.bitLength() - 1; switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(isPowerOfTwo(x)); // fall through case DOWN: case FLOOR: return isPowerOfTwo(x) ? logFloor : logFloor + 1; if (logFloor < SQRT2_PRECOMPUTE_THRESHOLD) { BigInteger halfPower = SQRT2_PRECOMPUTED_BITS.shiftRight(SQRT2_PRECOMPUTE_THRESHOLD - logFloor); if (x.compareTo(halfPower) <= 0) { return logFloor; } else {
public void testCheckPositive_negativeBigInteger() { try { MathPreconditions.checkPositive("BigInteger", BigInteger.ZERO.negate()); fail(); } catch (IllegalArgumentException expected) { } }
public void testCheckNonNegative_negativeBigInteger() { try { MathPreconditions.checkNonNegative("int", BigInteger.ONE.negate()); fail(); } catch (IllegalArgumentException expected) { } }
@GwtIncompatible // TODO @SuppressWarnings("fallthrough") public static long sqrt(long x, RoundingMode mode) { checkNonNegative("x", x); if (fitsInInt(x)) { return IntMath.sqrt((int) x, mode); checkRoundingUnnecessary(guessSquared == x); return guess; case FLOOR: return sqrtFloor + lessThanBranchFree(halfSquare, x); default: throw new AssertionError();
@GwtIncompatible // TODO @SuppressWarnings("fallthrough") checkPositive("x", x); int logFloor = log10Floor(x); long floorPow = powersOf10[logFloor]; switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(x == floorPow); case CEILING: case UP: return logFloor + lessThanBranchFree(floorPow, x); case HALF_DOWN: case HALF_UP: case HALF_EVEN: return logFloor + lessThanBranchFree(halfPowersOf10[logFloor], x); default: throw new AssertionError();
@GwtIncompatible // need BigIntegerMath to adequately test @SuppressWarnings("fallthrough") public static int sqrt(int x, RoundingMode mode) { checkNonNegative("x", x); int sqrtFloor = sqrtFloor(x); switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(sqrtFloor * sqrtFloor == x); // fall through case FLOOR: case DOWN: case CEILING: case UP: return sqrtFloor + lessThanBranchFree(sqrtFloor * sqrtFloor, x); case HALF_DOWN: case HALF_UP: return sqrtFloor + lessThanBranchFree(halfSquare, x); default: throw new AssertionError();
@GwtIncompatible // java.lang.Math.getExponent, com.google.common.math.DoubleUtils @SuppressWarnings("fallthrough") public static int log2(double x, RoundingMode mode) { int exponent = getExponent(x); if (!isNormal(x)) { return log2(x * IMPLICIT_BIT, mode) - SIGNIFICAND_BITS; switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(isPowerOfTwo(x)); break; case CEILING: increment = !isPowerOfTwo(x); break; case DOWN:
@GwtIncompatible("TODO") @SuppressWarnings("fallthrough") public static long sqrt(long x, RoundingMode mode) { checkNonNegative("x", x); if (fitsInInt(x)) { return IntMath.sqrt((int) x, mode); long sqrtFloor = sqrtFloor(x); switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(sqrtFloor * sqrtFloor == x); // fall through case FLOOR: case DOWN:
@GwtIncompatible // #isMathematicalInteger, com.google.common.math.DoubleUtils static double roundIntermediate(double x, RoundingMode mode) { if (!isFinite(x)) { checkRoundingUnnecessary(isMathematicalInteger(x)); return x; if (x >= 0.0 || isMathematicalInteger(x)) { return x; } else { if (x <= 0.0 || isMathematicalInteger(x)) { return x; } else {
checkPositive("x", x); if (fitsInInt(x)) { return IntMath.log10((int) x, mode); int logFloor = log10Floor(x); long floorPow = POWERS_OF_10[logFloor]; switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(x == floorPow);
/** * Returns the difference of {@code a} and {@code b}, provided it does not overflow. * * @throws ArithmeticException if {@code a - b} overflows in signed {@code long} arithmetic */ @GwtIncompatible // TODO public static long checkedSubtract(long a, long b) { long result = a - b; checkNoOverflow((a ^ b) >= 0 | (a ^ result) >= 0, "checkedSubtract", a, b); return result; }
/** * Returns {@code n!}, that is, the product of the first {@code n} positive integers, {@code 1} if * {@code n == 0}, or {@link Long#MAX_VALUE} if the result does not fit in a {@code long}. * * @throws IllegalArgumentException if {@code n < 0} */ @GwtIncompatible // TODO public static long factorial(int n) { checkNonNegative("n", n); return (n < factorials.length) ? factorials[n] : Long.MAX_VALUE; }