/** Bounded multiplication - overflows become MAX_VALUE */ public static long safeMult(long a, long b) { try { return LongMath.checkedMultiply(a, b); } catch (ArithmeticException ex) { return Long.MAX_VALUE; } }
@GwtIncompatible // TODO private long simpleBinomial(int n, int k) { long accum = 1; for (int i = 0; i < k; i++) { accum = LongMath.checkedMultiply(accum, n - i); accum /= i + 1; } return accum; }
multiplier = LongMath.checkedMultiply(multiplier, 100);
return accum; case 1: return checkedMultiply(accum, b); default: if ((k & 1) != 0) { accum = checkedMultiply(accum, b);
for (String[] ss : selectRule.hierarchyDims) { hierarchyDims.addAll(Arrays.asList(ss)); combination = LongMath.checkedMultiply(combination, (ss.length + 1)); jointDims.addAll(Arrays.asList(ss)); combination = LongMath.checkedMultiply(combination, (1L << selectRule.jointDims.length)); normalDims.removeAll(jointDims); combination = LongMath.checkedMultiply(combination, (1L << normalDims.size()));
static Long binop(Kind kind, long lhs, long rhs) { switch (kind) { case MULTIPLY: return LongMath.checkedMultiply(lhs, rhs); case DIVIDE: return lhs / rhs; case REMAINDER: return lhs % rhs; case PLUS: return LongMath.checkedAdd(lhs, rhs); case MINUS: return LongMath.checkedSubtract(lhs, rhs); case LEFT_SHIFT: return lhs << rhs; case RIGHT_SHIFT: return lhs >> rhs; case UNSIGNED_RIGHT_SHIFT: return lhs >>> rhs; case AND: return lhs & rhs; case XOR: return lhs ^ rhs; case OR: return lhs | rhs; default: return null; } }
return accum; case 1: return checkedMultiply(accum, b); default: if ((k & 1) != 0) { accum = checkedMultiply(accum, b);
@GwtIncompatible // TODO public void testConstantsFactorials() { long expected = 1; for (int i = 0; i < LongMath.factorials.length; i++, expected *= i) { assertEquals(expected, LongMath.factorials[i]); } try { LongMath.checkedMultiply( LongMath.factorials[LongMath.factorials.length - 1], LongMath.factorials.length); fail("Expected ArithmeticException"); } catch (ArithmeticException expect) { } }
return accum; case 1: return checkedMultiply(accum, b); default: if ((k & 1) != 0) { accum = checkedMultiply(accum, b);
@AndroidIncompatible // slow public void testCheckedMultiply() { boolean isAndroid = TestPlatform.isAndroid(); for (long a : ALL_LONG_CANDIDATES) { for (long b : ALL_LONG_CANDIDATES) { if (isAndroid && a == -4294967296L && b == 2147483648L) { /* * Bug in older versions of Android we test against, since fixed: -9223372036854775808L / * -4294967296L = -9223372036854775808L! * * To be clear, this bug affects not the test's computation of the expected result but the * _actual prod code_. But it probably affects only unusual cases. */ continue; } BigInteger expectedResult = valueOf(a).multiply(valueOf(b)); boolean expectedSuccess = fitsInLong(expectedResult); try { assertEquals(a * b, LongMath.checkedMultiply(a, b)); assertTrue(expectedSuccess); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( "expected checkedMultiply(%s, %s) = %s; got ArithmeticException", a, b, expectedResult); } } } } }
/** * @return true if we limit of a sequence has been reached. */ public static boolean checkIfLimitReached(long currentValue, long minValue, long maxValue, long incrementBy, long cacheSize, long numToAllocate) { long nextValue = 0; boolean increasingSeq = incrementBy > 0 ? true : false; // advance currentValue while checking for overflow try { long incrementValue; if (isBulkAllocation(numToAllocate)) { // For bulk allocation we increment independent of cache size incrementValue = LongMath.checkedMultiply(incrementBy, numToAllocate); } else { incrementValue = LongMath.checkedMultiply(incrementBy, cacheSize); } nextValue = LongMath.checkedAdd(currentValue, incrementValue); } catch (ArithmeticException e) { return true; } // check if limit was reached if ((increasingSeq && nextValue > maxValue) || (!increasingSeq && nextValue < minValue)) { return true; } return false; }
multiplier = LongMath.checkedMultiply(multiplier, 100);
multiplier = LongMath.checkedMultiply(multiplier, 100);
public static long cartesianProductSize(List<? extends Set<?>> listOfSets) { if (listOfSets.isEmpty()) { return 0L; } else { return listOfSets.stream().mapToLong(s -> s.size()).reduce(1, (l, r) -> LongMath.checkedMultiply(l, r)); } }
public static long cartesianProductSize(List<? extends Set<?>> listOfSets) { if (listOfSets.isEmpty()) { return 0L; } else { return listOfSets.stream().mapToLong(s -> s.size()).reduce(1, (l, r) -> LongMath.checkedMultiply(l, r)); } }
@Override public boolean noMulOverflow(long a, long b) { try { long unused = LongMath.checkedMultiply(a, b); return true; } catch (ArithmeticException e) { return false; } } };
private static long getNanos(Timestamp time) { return LongMath.checkedAdd( LongMath.checkedMultiply(time.getSeconds(), NUM_NANOS_PER_SECOND), time.getNanos()); } }
@GwtIncompatible // TODO private long simpleBinomial(int n, int k) { long accum = 1; for (int i = 0; i < k; i++) { accum = LongMath.checkedMultiply(accum, n - i); accum /= i + 1; } return accum; }
/** * Convert a Timestamp to the number of microseconds elapsed from the epoch. * * <p>The result will be rounded down to the nearest microsecond. E.g., if the timestamp * represents "1969-12-31T23:59:59.999999999Z", it will be rounded to -1 microsecond. */ public static long toMicros(Timestamp timestamp) { checkValid(timestamp); return checkedAdd( checkedMultiply(timestamp.getSeconds(), MICROS_PER_SECOND), timestamp.getNanos() / NANOS_PER_MICROSECOND); }
/** Convert a Duration to the number of nanoseconds. */ public static long toNanos(Duration duration) { checkValid(duration); return checkedAdd( checkedMultiply(duration.getSeconds(), NANOS_PER_SECOND), duration.getNanos()); }