Java Code Examples for com.google.common.math.LongMath#pow()

The following examples show how to use com.google.common.math.LongMath#pow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QuotientFilter.java    From nuls-v2 with MIT License 6 votes vote down vote up
static int bitsForNumElementsWithLoadFactor(long numElements) {
    if (numElements == 0) {
        return 1;
    }

    int candidateBits = Long.bitCount(numElements) == 1 ?
            Math.max(1, Long.numberOfTrailingZeros(numElements)) :
            Long.numberOfTrailingZeros(Long.highestOneBit(numElements) << 1L);

    //May need an extra bit due to load factor
    if (((long) (LongMath.pow(2, candidateBits) * 0.75)) < numElements) {
        candidateBits++;
    }

    return candidateBits;
}
 
Example 2
Source File: QuotientFilter.java    From nuls with MIT License 6 votes vote down vote up
static int bitsForNumElementsWithLoadFactor(long numElements) {
    if (numElements == 0) {
        return 1;
    }

    int candidateBits = Long.bitCount(numElements) == 1 ?
            Math.max(1, Long.numberOfTrailingZeros(numElements)) :
            Long.numberOfTrailingZeros(Long.highestOneBit(numElements) << 1L);

    //May need an extra bit due to load factor
    if (((long) (LongMath.pow(2, candidateBits) * 0.75)) < numElements) {
        candidateBits++;
    }

    return candidateBits;
}
 
Example 3
Source File: SendProducerBatchTask.java    From aliyun-log-java-producer with Apache License 2.0 5 votes vote down vote up
private long calculateRetryBackoffMs() {
  int retry = batch.getRetries();
  long retryBackoffMs = producerConfig.getBaseRetryBackoffMs() * LongMath.pow(2, retry);
  if (retryBackoffMs <= 0) {
    retryBackoffMs = producerConfig.getMaxRetryBackoffMs();
  }
  return Math.min(retryBackoffMs, producerConfig.getMaxRetryBackoffMs());
}
 
Example 4
Source File: Size.java    From james-project with Apache License 2.0 5 votes vote down vote up
public long asBytes() {
    switch (unit) {
        case G:
            return value * LongMath.pow(base, 3);
        case M:
            return value * LongMath.pow(base, 2);
        case K:
            return value * LongMath.pow(base, 1);
        default:
            return value;
    }
}
 
Example 5
Source File: OfferBookChartViewModel.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
private void buildChartAndTableEntries(List<Offer> sortedList,
                                       OfferPayload.Direction direction,
                                       List<XYChart.Data<Number, Number>> data,
                                       ObservableList<OfferListItem> offerTableList) {
    data.clear();
    double accumulatedAmount = 0;
    List<OfferListItem> offerTableListTemp = new ArrayList<>();
    for (Offer offer : sortedList) {
        Price price = offer.getPrice();
        if (price != null) {
            double amount = (double) offer.getAmount().value / LongMath.pow(10, offer.getAmount().smallestUnitExponent());
            accumulatedAmount += amount;
            offerTableListTemp.add(new OfferListItem(offer, accumulatedAmount));

            double priceAsDouble = (double) price.getValue() / LongMath.pow(10, price.smallestUnitExponent());
            if (CurrencyUtil.isCryptoCurrency(getCurrencyCode())) {
                if (direction.equals(OfferPayload.Direction.SELL))
                    data.add(0, new XYChart.Data<>(priceAsDouble, accumulatedAmount));
                else
                    data.add(new XYChart.Data<>(priceAsDouble, accumulatedAmount));
            } else {
                if (direction.equals(OfferPayload.Direction.BUY))
                    data.add(0, new XYChart.Data<>(priceAsDouble, accumulatedAmount));
                else
                    data.add(new XYChart.Data<>(priceAsDouble, accumulatedAmount));
            }
        }
    }
    offerTableList.setAll(offerTableListTemp);
}
 
Example 6
Source File: ProducerInvalidTest.java    From aliyun-log-java-producer with Apache License 2.0 4 votes vote down vote up
@Test
public void testSendWithRequestError() throws InterruptedException, ProducerException {
  ProducerConfig producerConfig = new ProducerConfig();
  int retries = 5;
  producerConfig.setRetries(retries);
  producerConfig.setMaxReservedAttempts(retries + 1);
  Producer producer = new LogProducer(producerConfig);
  producer.putProjectConfig(buildProjectConfig());
  ListenableFuture<Result> f = producer.send("project", "logStore", ProducerTest.buildLogItem());
  try {
    f.get();
  } catch (ExecutionException e) {
    ResultFailedException resultFailedException = (ResultFailedException) e.getCause();
    Result result = resultFailedException.getResult();
    Assert.assertFalse(result.isSuccessful());
    Assert.assertEquals("RequestError", result.getErrorCode());
    Assert.assertTrue(
        result.getErrorMessage().startsWith("Web request failed: project.endpoint"));
    List<Attempt> attempts = result.getReservedAttempts();
    Assert.assertEquals(retries + 1, attempts.size());
    long t1;
    long t2 = -1;
    for (int i = 0; i < attempts.size(); ++i) {
      Attempt attempt = attempts.get(i);
      Assert.assertFalse(attempt.isSuccess());
      Assert.assertEquals("RequestError", attempt.getErrorCode());
      Assert.assertTrue(
          attempt.getErrorMessage().startsWith("Web request failed: project.endpoint"));
      Assert.assertEquals("", attempt.getRequestId());
      t1 = t2;
      t2 = attempt.getTimestampMs();
      if (i == 0) {
        continue;
      }
      long diff = t2 - t1;
      long retryBackoffMs = producerConfig.getBaseRetryBackoffMs() * LongMath.pow(2, i - 1);
      long low = retryBackoffMs - (long) (producerConfig.getBaseRetryBackoffMs() * 0.1);
      long high = retryBackoffMs + (long) (producerConfig.getBaseRetryBackoffMs() * 0.2);
      if (i == 1) {
        Assert.assertTrue(low <= diff);
      } else {
        Assert.assertTrue(low <= diff && diff <= high);
      }
    }
  }
  producer.close();
  ProducerTest.assertProducerFinalState(producer);
}
 
Example 7
Source File: GuavaLongMathUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenPowTwoLongValues_shouldPowThemAndReturnTheResult() {
    long result = LongMath.pow(6L, 4);
    assertEquals(1296L, result);
}
 
Example 8
Source File: MathUtil.java    From vjtools with Apache License 2.0 2 votes vote down vote up
/**
 * 平方
 * 
 * @param k 平方次数,不能为负数, k=0时返回1.
 */
public static long pow(long b, int k) {
	return LongMath.pow(b, k);
}
 
Example 9
Source File: MathUtil.java    From vjtools with Apache License 2.0 2 votes vote down vote up
/**
 * 平方
 * 
 * @param k 平方次数,不能为负数, k=0时返回1.
 */
public static long pow(long b, int k) {
	return LongMath.pow(b, k);
}
 
Example 10
Source File: MathUtil.java    From j360-dubbo-app-all with Apache License 2.0 2 votes vote down vote up
/**
 * 平方
 * 
 * @param k 平方次数,不能为负数, k=0时返回1.
 */
public static long pow(long b, int k) {
	return LongMath.pow(b, k);
}