java.math.BigInteger.compareTo()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(194)

本文整理了Java中java.math.BigInteger.compareTo()方法的一些代码示例,展示了BigInteger.compareTo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigInteger.compareTo()方法的具体详情如下:
包路径:java.math.BigInteger
类名称:BigInteger
方法名:compareTo

BigInteger.compareTo介绍

[英]Compares this BigInteger with the specified Object. If the Object is a BigInteger, this method behaves like compareTo(BigInteger). Otherwise, it throws a ClassCastException (as BigIntegers are comparable only to other BigIntegers).
[中]

代码示例

代码示例来源:origin: kiegroup/optaplanner

@Override
public boolean contains(BigInteger value) {
  if (value == null || value.compareTo(from) < 0 || value.compareTo(to) >= 0) {
    return false;
  }
  return value.subtract(from).remainder(incrementUnit).compareTo(BigInteger.ZERO) == 0;
}

代码示例来源:origin: prestodb/presto

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();
}

代码示例来源:origin: Graylog2/graylog2-server

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);
}

代码示例来源:origin: apache/hive

/**
 * 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;
}

代码示例来源:origin: google/guava

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();

代码示例来源:origin: ethereum/ethereumj

@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;
  }
}

代码示例来源:origin: stackoverflow.com

for (BigInteger bi = BigInteger.valueOf(5);
     bi.compareTo(BigInteger.ZERO) > 0;
     bi = bi.subtract(BigInteger.ONE)) {
   System.out.println(bi);
 }
 // prints "5", "4", "3", "2", "1"

代码示例来源:origin: square/wire

@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();
}

代码示例来源:origin: apache/flink

@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();
}

代码示例来源:origin: google/guava

BigInteger sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
if (sqrt0.equals(sqrt1)) {
 return sqrt0;
 sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
} while (sqrt1.compareTo(sqrt0) < 0);
return sqrt0;

代码示例来源:origin: google/guava

@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);
 }
}

代码示例来源:origin: kiegroup/optaplanner

@Override
public BigInteger next() {
  if (upcoming.compareTo(to) >= 0) {
    throw new NoSuchElementException();
  }
  BigInteger next = upcoming;
  upcoming = upcoming.add(incrementUnit);
  return next;
}

代码示例来源:origin: Sable/soot

/** Factorial */
public static BigInteger fact(BigInteger n_) {
 BigInteger result = BigInteger.ONE;
 for (BigInteger i = BigInteger.ONE; i.compareTo(n_) <= 0; i = i.add(BigInteger.ONE)) {
  result = result.multiply(i);
 }
 return result;
}

代码示例来源:origin: google/guava

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();

代码示例来源:origin: deeplearning4j/nd4j

/**
 * 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 */

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * 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;
}

代码示例来源:origin: i2p/i2p.i2p

/**
 *  @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);
  }
}

代码示例来源:origin: prestodb/presto

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;

代码示例来源:origin: apache/activemq

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;
  }
}

代码示例来源:origin: stackoverflow.com

BigInteger r;
do {
  r = new BigInteger(n.bitLength(), rnd);
} while (r.compareTo(n) >= 0);

相关文章