Refine search
@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()); }
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 AlternativeJdkIdGenerator() { SecureRandom secureRandom = new SecureRandom(); byte[] seed = new byte[8]; secureRandom.nextBytes(seed); this.random = new Random(new BigInteger(seed).longValue()); }
@VisibleForTesting public static BigDecimal average(LongDecimalWithOverflowAndLongState state, DecimalType type) { BigDecimal sum = new BigDecimal(Decimals.decodeUnscaledValue(state.getLongDecimal()), type.getScale()); BigDecimal count = BigDecimal.valueOf(state.getLong()); if (state.getOverflow() != 0) { BigInteger overflowMultiplier = TWO.shiftLeft(UNSCALED_DECIMAL_128_SLICE_LENGTH * 8 - 2); BigInteger overflow = overflowMultiplier.multiply(BigInteger.valueOf(state.getOverflow())); sum = sum.add(new BigDecimal(overflow)); } return sum.divide(count, type.getScale(), ROUND_HALF_UP); }
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(); }
@Test public void convertBigDecimalToBigInteger() { String number = "987459837583750387355346"; BigDecimal decimal = new BigDecimal(number); assertEquals(new BigInteger(number), NumberUtils.convertNumberToTargetClass(decimal, BigInteger.class)); }
@GwtIncompatible // TODO @AndroidIncompatible // TODO(cpovirk): File BigDecimal.divide() rounding bug. public void testDivNonZero() { for (long p : NONZERO_LONG_CANDIDATES) { for (long q : NONZERO_LONG_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { long expected = new BigDecimal(valueOf(p)).divide(new BigDecimal(valueOf(q)), 0, mode).longValue(); long actual = LongMath.divide(p, q, mode); if (expected != actual) { failFormat("expected divide(%s, %s, %s) = %s; got %s", p, q, mode, expected, actual); } } } } }
@AndroidIncompatible // slow public void testDivNonZero() { for (int p : NONZERO_INTEGER_CANDIDATES) { for (int q : NONZERO_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // Skip some tests that fail due to GWT's non-compliant int implementation. // TODO(cpovirk): does this test fail for only some rounding modes or for all? if (p == -2147483648 && q == -1 && intsCanGoOutOfRange()) { continue; } int expected = new BigDecimal(valueOf(p)).divide(new BigDecimal(valueOf(q)), 0, mode).intValue(); assertEquals(p + "/" + q, force32(expected), IntMath.divide(p, q, mode)); } } } }
public void testToString() { String[] tests = { "0", "ffffffffffffffff", "7fffffffffffffff", "ff1a618b7f65ea12", "5a4316b8c153ac4d", "6cf78a4b139a4e2a" }; int[] bases = {2, 5, 7, 8, 10, 16}; for (int base : bases) { for (String x : tests) { BigInteger xValue = new BigInteger(x, 16); long xLong = xValue.longValue(); // signed assertEquals(xValue.toString(base), UnsignedLongs.toString(xLong, base)); } } }
if (new BigInteger("1111000011110001", 2).toByteArray() == array)
public void testTryParse_radix() { for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { radixEncodeParseAndAssertEquals((long) 0, radix); radixEncodeParseAndAssertEquals((long) 8000, radix); radixEncodeParseAndAssertEquals((long) -8000, radix); radixEncodeParseAndAssertEquals(MAX_VALUE, radix); radixEncodeParseAndAssertEquals(MIN_VALUE, radix); assertNull("Radix: " + radix, Longs.tryParse("999999999999999999999999", radix)); assertNull( "Radix: " + radix, Longs.tryParse(BigInteger.valueOf(MAX_VALUE).add(BigInteger.ONE).toString(), radix)); assertNull( "Radix: " + radix, Longs.tryParse(BigInteger.valueOf(MIN_VALUE).subtract(BigInteger.ONE).toString(), radix)); } assertNull("Hex string and dec parm", Longs.tryParse("FFFF", 10)); assertEquals("Mixed hex case", 65535, Longs.tryParse("ffFF", 16).longValue()); }
@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(); }
String generateRSAPublicKey() { RSAPublicKey pub = (RSAPublicKey) cert.getPublicKey(); StringBuilder builder = new StringBuilder(); append(builder, NLS.str("certificate.serialPubKeyType"), pub.getAlgorithm()); append(builder, NLS.str("certificate.serialPubKeyExponent"), pub.getPublicExponent().toString(10)); append(builder, NLS.str("certificate.serialPubKeyModulusSize"), Integer.toString( pub.getModulus().toString(2).length())); append(builder, NLS.str("certificate.serialPubKeyModulus"), pub.getModulus().toString(10)); return builder.toString(); }
/** * 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); } }
@Test public void parseNumberAsNegativeHex() { String aByte = "-0x80"; String aShort = "-0x8000"; String anInteger = "-0x80000000"; String aLong = "-0x8000000000000000"; String aReallyBigInt = "FEBD4E677898DFEBFFEE44"; assertNegativeByteEquals(aByte); assertNegativeShortEquals(aShort); assertNegativeIntegerEquals(anInteger); assertNegativeLongEquals(aLong); assertEquals("BigInteger did not parse", new BigInteger(aReallyBigInt, 16).negate(), NumberUtils.parseNumber("-0x" + aReallyBigInt, BigInteger.class)); }
@GwtIncompatible // TODO public void testConstantsHalfPowersOf10() { for (int i = 0; i < LongMath.halfPowersOf10.length; i++) { assertEquals( BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR), BigInteger.valueOf(LongMath.halfPowersOf10[i])); } BigInteger nextBigger = BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * LongMath.halfPowersOf10.length + 1), FLOOR); assertTrue(nextBigger.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0); }