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(); }
public boolean contains(InetAddress address) { final BigInteger start = new BigInteger(1, this.startAddress.getAddress()); final BigInteger end = new BigInteger(1, this.endAddress.getAddress()); final BigInteger target = new BigInteger(1, address.getAddress()); final int st = start.compareTo(target); final int te = target.compareTo(end); return (st == -1 || st == 0) && (te == -1 || te == 0); }
/** * Scale down a BigInteger by a power of 10 and round off if necessary using ROUND_HALF_UP. * @return The scaled and rounded BigInteger. */ private static BigInteger doBigIntegerScaleDown(BigInteger unscaledValue, int scaleDown) { BigInteger[] quotientAndRemainder = unscaledValue.divideAndRemainder(BigInteger.TEN.pow(scaleDown)); BigInteger quotient = quotientAndRemainder[0]; BigInteger round = quotientAndRemainder[1].divide(BigInteger.TEN.pow(scaleDown - 1)); if (round.compareTo(BIG_INTEGER_FIVE) >= 0) { quotient = quotient.add(BigInteger.ONE); } return quotient; }
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode)); return sqrtFloorIsExact ? sqrtFloor : sqrtFloor.add(BigInteger.ONE); case HALF_DOWN: case HALF_UP: case HALF_EVEN: BigInteger halfSquare = sqrtFloor.pow(2).add(sqrtFloor); return (halfSquare.compareTo(x) >= 0) ? sqrtFloor : sqrtFloor.add(BigInteger.ONE); default: throw new AssertionError();
@Override public ValidationResult validate(BlockHeader header) { if (new BigInteger(1, header.getGasLimit()).compareTo(BigInteger.valueOf(MIN_GAS_LIMIT)) < 0) { return fault("header.getGasLimit() < MIN_GAS_LIMIT"); } return Success; } }
@Override public Long fromJson(JsonReader reader) throws IOException { BigInteger bigInteger = new BigInteger(reader.nextString()); return bigInteger.compareTo(maxLong) > 0 ? bigInteger.subtract(power64).longValue() : bigInteger.longValue(); }
@Override protected long vertexCount() { BigInteger vertexCount = BigInteger.ONE; for (Dimension dimension : dimensions) { vertexCount = vertexCount.multiply(BigInteger.valueOf(dimension.size)); } if (vertexCount.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { throw new ProgramParametrizationException("Number of vertices in grid graph '" + vertexCount + "' is greater than Long.MAX_VALUE."); } return vertexCount.longValue(); }
@GwtIncompatible // TODO public void testSqrtHalfDown() { for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) { BigInteger result = BigIntegerMath.sqrt(x, HALF_DOWN); BigInteger plusHalfSquared = result.pow(2).add(result).shiftLeft(2).add(ONE); BigInteger x4 = x.shiftLeft(2); // sqrt(x) <= result + 0.5, so 4 * x <= (result + 0.5)^2 * 4 // (result + 0.5)^2 * 4 = (result^2 + result)*4 + 1 assertTrue(x4.compareTo(plusHalfSquared) <= 0); BigInteger minusHalfSquared = result.pow(2).subtract(result).shiftLeft(2).add(ONE); // sqrt(x) > result - 0.5, so 4 * x > (result - 0.5)^2 * 4 // (result - 0.5)^2 * 4 = (result^2 - result)*4 + 1 assertTrue(result.equals(ZERO) || x4.compareTo(minusHalfSquared) > 0); } }
int approxCmp = approxPow.compareTo(x); approxPow = approxPow.divide(BigInteger.TEN); approxCmp = approxPow.compareTo(x); } while (approxCmp > 0); } else { BigInteger nextPow = BigInteger.TEN.multiply(approxPow); int nextCmp = nextPow.compareTo(x); while (nextCmp <= 0) { approxLog10++; approxPow = nextPow; approxCmp = nextCmp; nextPow = BigInteger.TEN.multiply(approxPow); nextCmp = nextPow.compareTo(x); BigInteger halfPowerSquared = floorPow.pow(2).multiply(BigInteger.TEN); return (x2.compareTo(halfPowerSquared) <= 0) ? floorLog : floorLog + 1; default: throw new AssertionError();
/** * floor(): the nearest integer not greater than this. * * @return The integer rounded towards negative infinity. */ public BigInteger floor() { /* is already integer: return the numerator */ if (b.compareTo(BigInteger.ONE) == 0) { return a; } else if (a.compareTo(BigInteger.ZERO) > 0) { return a.divide(b); } else { return a.divide(b).subtract(BigInteger.ONE); } } /* Rational.floor */
/** * Apply delta to accumulators. * @param elapsedJiffies updated jiffies * @param newTime new sample time */ public void updateElapsedJiffies(BigInteger elapsedJiffies, long newTime) { BigInteger newValue = elapsedJiffies.multiply(jiffyLengthInMillis); cumulativeCpuTime = newValue.compareTo(cumulativeCpuTime) >= 0 ? newValue : cumulativeCpuTime; sampleTime = newTime; }
/** * @param sigBytes ASN.1 R,S */ @Override protected boolean engineVerify(byte[] sigBytes) throws SignatureException { BigInteger elgp = key.getParams().getP(); BigInteger pm1 = elgp.subtract(BigInteger.ONE); BigInteger elgg = key.getParams().getG(); BigInteger y = ((ElGamalPublicKey) key).getY(); if (!(y instanceof NativeBigInteger)) y = new NativeBigInteger(y); byte[] data = digest.digest(); try { BigInteger[] rs = SigUtil.aSN1ToBigInteger(sigBytes, 256); BigInteger r = rs[0]; BigInteger s = rs[1]; if (r.signum() != 1 || s.signum() != 1 || r.compareTo(elgp) != -1 || s.compareTo(pm1) != -1) return false; NativeBigInteger h = new NativeBigInteger(1, data); BigInteger modvalr = r.modPow(s, elgp); BigInteger modvaly = y.modPow(r, elgp); BigInteger modmulval = modvalr.multiply(modvaly).mod(elgp); BigInteger v = elgg.modPow(h, elgp); boolean ok = v.compareTo(modmulval) == 0; return ok; } catch (RuntimeException e) { throw new SignatureException("verify", e); } }
BigInteger rescaledDividend = dividendBigInteger.multiply(bigIntegerTenToNth(dividendRescaleFactor)); BigInteger rescaledDivisor = divisorBigInteger.multiply(bigIntegerTenToNth(divisorRescaleFactor)); BigInteger[] expectedQuotientAndRemainder = rescaledDividend.divideAndRemainder(rescaledDivisor); BigInteger expectedQuotient = expectedQuotientAndRemainder[0]; BigInteger expectedRemainder = expectedQuotientAndRemainder[1]; boolean overflowIsExpected = expectedQuotient.abs().compareTo(bigIntegerTenToNth(38)) >= 0 || expectedRemainder.abs().compareTo(bigIntegerTenToNth(38)) >= 0;
private static String toHumanReadableSizeString(final BigInteger size) { String displaySize; final BigInteger ONE_KB_BI = BigInteger.valueOf(1024); final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI); final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI); if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) { displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB"; } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) { displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB"; } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) { displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB"; } else { displaySize = String.valueOf(size) + " bytes"; } return displaySize; } }
BigInteger r; do { r = new BigInteger(n.bitLength(), rnd); } while (r.compareTo(n) >= 0);