@Override public BigInteger next(BigInteger value) { return value.add(BigInteger.ONE); }
@Override protected Object instantiate() throws Exception { return new BigInteger("0"); } }
/** * Computes the mean in a way that is obvious and resilient to overflow by using BigInteger * arithmetic. */ private static int computeMeanSafely(int x, int y) { BigInteger bigX = BigInteger.valueOf(x); BigInteger bigY = BigInteger.valueOf(y); BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR); // parseInt blows up on overflow as opposed to intValue() which does not. return Integer.parseInt(bigMean.toString()); }
private static long bigIntegerDecimalToGenericIntegerType(BigInteger bigInteger, int sourceScale, long minValue, long maxValue) { BigDecimal bigDecimal = new BigDecimal(bigInteger, sourceScale); BigInteger unscaledValue = bigDecimal.setScale(0, FLOOR).unscaledValue(); if (unscaledValue.compareTo(BigInteger.valueOf(maxValue)) > 0) { return maxValue; } if (unscaledValue.compareTo(BigInteger.valueOf(minValue)) < 0) { return minValue; } return unscaledValue.longValueExact(); }
import java.security.SecureRandom; import java.math.BigInteger; public final class SessionIdentifierGenerator { private SecureRandom random = new SecureRandom(); public String nextSessionId() { return new BigInteger(130, random).toString(32); } }
public Number getNumber() { if (chars != null && number == null) { this.number = new BigDecimal(chars); } return this.number; }
public void testConstantsMaxFactorial() { BigInteger maxDoubleValue = BigDecimal.valueOf(Double.MAX_VALUE).toBigInteger(); assertTrue(BigIntegerMath.factorial(DoubleMath.MAX_FACTORIAL).compareTo(maxDoubleValue) <= 0); assertTrue( BigIntegerMath.factorial(DoubleMath.MAX_FACTORIAL + 1).compareTo(maxDoubleValue) > 0); }
@Override public BigInteger apply(Long n) { return BigInteger.valueOf(n); } };
/** Returns {@code true} if {@code x} represents a power of two. */ public static boolean isPowerOfTwo(BigInteger x) { checkNotNull(x); return x.signum() > 0 && x.getLowestSetBit() == x.bitLength() - 1; }
@Override public BigInteger previous(BigInteger value) { return value.subtract(BigInteger.ONE); }
private static boolean fitsInInt(BigInteger big) { return big.bitLength() <= 31; }
@Override public BigInteger apply(BigInteger x) { return x.negate(); } };
/** * Generates values in a distribution equivalent to randomNonNegativeBigInteger but omitting zero. */ static BigInteger randomPositiveBigInteger(int numBits) { BigInteger result; do { result = randomNonNegativeBigInteger(numBits); } while (result.signum() == 0); return result; }
/** * Computes the mean in a way that is obvious and resilient to overflow by using BigInteger * arithmetic. */ private static long computeMeanSafely(long x, long y) { BigInteger bigX = BigInteger.valueOf(x); BigInteger bigY = BigInteger.valueOf(y); BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR); // parseInt blows up on overflow as opposed to intValue() which does not. return Long.parseLong(bigMean.toString()); }
private static boolean fitsInLong(BigInteger big) { return big.bitLength() <= 63; }