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

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

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

BigInteger.pow介绍

[英]Returns a BigInteger whose value is pow(this, exp).
[中]返回一个BigInteger,其值为pow(this,exp)。

代码示例

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

private static BigInteger newBigInt(int value) {
  return BigInteger.valueOf(10).pow(value);
}

代码示例来源: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: ethereum/ethereumj

public long mine(long fullSize, byte[][] dataset, byte[] blockHeaderTruncHash, long difficulty) {
  BigInteger target = valueOf(2).pow(256).divide(valueOf(difficulty));
  long nonce = new Random().nextLong();
  while(!Thread.currentThread().isInterrupted()) {
    nonce++;
    Pair<byte[], byte[]> pair = hashimotoFull(fullSize, dataset, blockHeaderTruncHash, longToBytes(nonce));
    BigInteger h = new BigInteger(1, pair.getRight() /* ?? */);
    if (h.compareTo(target) < 0) break;
  }
  return nonce;
}

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

checkNonNegative("x", x);
if (fitsInLong(x)) {
 return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode));
  checkRoundingUnnecessary(sqrtFloor.pow(2).equals(x)); // fall through
 case FLOOR:
 case DOWN:
  boolean sqrtFloorIsExact =
      && sqrtFloor.pow(2).equals(x); // slow exact check
  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: stackoverflow.com

return canonical(new BigInteger("" + numerator), new BigInteger("" + denominator), true);
  return canonical(new BigInteger(numerator), new BigInteger(denominator), true);
  BigInteger denominator;
  if (exp < 0) {
    denominator = BigInteger.TEN.pow(-exp);
  } else {
    numerator = numerator.multiply(BigInteger.TEN.pow(exp));
    denominator = BigInteger.ONE;
  } else {
    BigInteger i1 = numerator.multiply(o.denominator);
    BigInteger i2 = o.numerator.multiply(denominator);
    return i1.compareTo(i2); // expensive!
    return o;
  } else if (denominator.equals(o.denominator)) {
    return new BigRational(numerator.add(o.numerator), denominator);
  } else {
    return canonical(numerator.multiply(o.denominator).add(o.numerator.multiply(denominator)), denominator.multiply(o.denominator), true);
public BigRational invert() { return canonical(denominator, numerator, false); }
public BigRational abs() { return numerator.signum() < 0 ? negate() : this; }
public BigRational pow(int exp) { return canonical(numerator.pow(exp), denominator.pow(exp), true); }
public BigRational subtract(BigRational o) { return add(o.negate()); }
public BigRational divide(BigRational o) { return multiply(o.invert()); }

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

@Test
public void testGetRingFraction()
{
  assertEquals(tokenRing.getTokenCountInRange("0", "1"), ONE);
  assertEquals(tokenRing.getTokenCountInRange("0", "200"), new BigInteger("200"));
  assertEquals(tokenRing.getTokenCountInRange("0", "10"), new BigInteger("10"));
  assertEquals(tokenRing.getTokenCountInRange("1", "11"), new BigInteger("10"));
  assertEquals(tokenRing.getTokenCountInRange("0", "0"), ZERO);
  assertEquals(tokenRing.getTokenCountInRange("-1", "-1"), BigInteger.valueOf(2).pow(127).add(ONE));
  assertEquals(tokenRing.getTokenCountInRange("1", "0"), BigInteger.valueOf(2).pow(127));
}

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

BigInteger base = new BigInteger("" + DICTIONARY.length);
int exponent = 1;
BigInteger remaining = new BigInteger(value.toString());
while (true) {
 BigInteger a = base.pow(exponent); // 16^1 = 16
 BigInteger c = base.pow(exponent - 1);
 BigInteger d = b.divide(c);

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

@Test
public void testGetTokenCountInRange()
{
  assertEquals(tokenRing.getTokenCountInRange("0", "1"), ONE);
  assertEquals(tokenRing.getTokenCountInRange("-1", "1"), new BigInteger("2"));
  assertEquals(tokenRing.getTokenCountInRange("-100", "100"), new BigInteger("200"));
  assertEquals(tokenRing.getTokenCountInRange("0", "10"), new BigInteger("10"));
  assertEquals(tokenRing.getTokenCountInRange("1", "11"), new BigInteger("10"));
  assertEquals(tokenRing.getTokenCountInRange("0", "0"), ZERO);
  assertEquals(tokenRing.getTokenCountInRange("1", "1"), ZERO);
  assertEquals(tokenRing.getTokenCountInRange(Long.toString(Long.MIN_VALUE), Long.toString(Long.MIN_VALUE)), BigInteger.valueOf(2).pow(64).subtract(ONE));
  assertEquals(tokenRing.getTokenCountInRange("1", "0"), BigInteger.valueOf(2).pow(64).subtract(BigInteger.valueOf(2)));
}

代码示例来源:origin: palantir/atlasdb

public static long estimateSize(Transaction transaction,
                TableReference table,
                int batchSize,
                Function<byte[], byte[]> uniformizer) {
  final AtomicLong estimate = new AtomicLong();
  transaction.getRange(table, RangeRequest.all()).batchAccept(batchSize, batch -> {
    if (batch.size() < batchSize) {
      estimate.set(batch.size());
    } else {
      byte[] row = uniformizer.apply(batch.get(batchSize - 1).getRowName());
      estimate.set(BigInteger.valueOf(2)
          .pow(row.length * 8)
          .multiply(BigInteger.valueOf(batchSize))
          .divide(new BigInteger(1, row))
          .longValue());
    }
    return false;
  });
  return estimate.get();
}

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

@Test
public void testShiftLeftCompareToBigInteger()
{
  assertShiftLeft(new BigInteger("446319580078125"), 19);
  assertShiftLeft(TWO.pow(1), 10);
  assertShiftLeft(TWO.pow(5).add(TWO.pow(1)), 10);
  assertShiftLeft(TWO.pow(1), 100);
  assertShiftLeft(TWO.pow(5).add(TWO.pow(1)), 100);
  assertShiftLeft(TWO.pow(70), 30);
  assertShiftLeft(TWO.pow(70).add(TWO.pow(1)), 30);
  assertShiftLeft(TWO.pow(106), 20);
  assertShiftLeft(TWO.pow(106).add(TWO.pow(1)), 20);
  assertShiftLeftOverflow(TWO.pow(2), 127);
  assertShiftLeftOverflow(TWO.pow(64), 64);
  assertShiftLeftOverflow(TWO.pow(100), 28);
}

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

private Rational doubleSum(int n) {
    Rational resul = Rational.ZERO;
    for (int k = 0; k <= n; k++) {
      Rational jsum = Rational.ZERO;
      BigInteger bin = BigInteger.ONE;
      for (int j = 0; j <= k; j++) {
        BigInteger jpown = BigInteger.valueOf(j).pow(n);
        if (j % 2 == 0) {
          jsum = jsum.add(bin.multiply(jpown));
        } else {
          jsum = jsum.subtract(bin.multiply(jpown));
        }
        /* update binomial(k,j) recursively
         */
        bin = bin.multiply(BigInteger.valueOf(k - j)).divide(BigInteger.valueOf(j + 1));
      }
      resul = resul.add(jsum.divide(BigInteger.valueOf(k + 1)));
    }
    return resul;
  }
} /* Bernoulli */

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

import java.math.BigInteger;

public class BigIntegerTest {
  public static void main(String[] args) {
    BigInteger b1 = new BigInteger("200000000000000000000000000000000001");
    BigInteger b2 = new BigInteger("400000000000000000000000000000000000");

    System.out.println(b1.multiply(b2));
    System.out.println(b1.bitCount());
    System.out.println(b1.pow(13));
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Power of a long to an integer certain exponent. If the
 * exponent is positive, convert to a BigInteger and call
 * BigInteger.pow(int) method to maintain precision. Called by the
 * '**' operator.
 *
 * @param self     a Long
 * @param exponent an Integer exponent
 * @return a Number to the power of a the exponent
 */
public static Number power(Long self, Integer exponent) {
  if (exponent >= 0) {
    BigInteger answer = BigInteger.valueOf(self).pow(exponent);
    if (answer.compareTo(BI_LONG_MIN) >= 0 && answer.compareTo(BI_LONG_MAX) <= 0) {
      return answer.longValue();
    } else {
      return answer;
    }
  } else {
    return power(self, (double) exponent);
  }
}

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

@Test
public void testCombineOverflow()
{
  addToState(state, TWO.pow(125));
  addToState(state, TWO.pow(126));
  LongDecimalWithOverflowAndLongState otherState = new LongDecimalWithOverflowAndLongStateFactory().createSingleState();
  addToState(otherState, TWO.pow(125));
  addToState(otherState, TWO.pow(126));
  DecimalAverageAggregation.combine(state, otherState);
  assertEquals(state.getLong(), 4);
  assertEquals(state.getOverflow(), 1);
  assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126)));
  BigInteger expectedAverage = BigInteger.ZERO
      .add(TWO.pow(126))
      .add(TWO.pow(126))
      .add(TWO.pow(125))
      .add(TWO.pow(125))
      .divide(BigInteger.valueOf(4));
  assertEquals(average(state, TYPE), new BigDecimal(expectedAverage));
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * Power of a BigInteger to a BigInteger certain exponent. Called by the '**' operator.
 *
 * @param self     a BigInteger
 * @param exponent a BigInteger exponent
 * @return a BigInteger to the power of a the exponent
 * @since 2.3.8
 */
public static BigInteger power(BigInteger self, BigInteger exponent) {
  if ((exponent.signum() >= 0) && (exponent.compareTo(BI_INT_MAX) <= 0)) {
    return self.pow(exponent.intValue());
  } else {
    return BigDecimal.valueOf(Math.pow(self.doubleValue(), exponent.doubleValue())).toBigInteger();
  }
}

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

BigInteger approxPow = BigInteger.TEN.pow(approxLog10);
int approxCmp = approxPow.compareTo(x);
  approxLog10--;
  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);
 case HALF_EVEN:
  BigInteger x2 = x.pow(2);
  BigInteger halfPowerSquared = floorPow.pow(2).multiply(BigInteger.TEN);
  return (x2.compareTo(halfPowerSquared) <= 0) ? floorLog : floorLog + 1;
 default:

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

@Test
public void testShiftRightArray8()
{
  assertShiftRightArray8(TWO.pow(1), 0);
  assertShiftRightArray8(TWO.pow(1), 1);
  assertShiftRightArray8(TWO.pow(1), 10);
  assertShiftRightArray8(TWO.pow(15).add(TWO.pow(3)), 2);
  assertShiftRightArray8(TWO.pow(15).add(TWO.pow(3)), 10);
  assertShiftRightArray8(TWO.pow(15).add(TWO.pow(3)), 20);
  assertShiftRightArray8(TWO.pow(70), 30);
  assertShiftRightArray8(TWO.pow(70).subtract(TWO.pow(1)), 30, true);
  assertShiftRightArray8(TWO.pow(70), 32);
  assertShiftRightArray8(TWO.pow(70).subtract(TWO.pow(1)), 32, true);
  assertShiftRightArray8(TWO.pow(120), 70);
  assertShiftRightArray8(TWO.pow(120).subtract(TWO.pow(1)), 70, true);
  assertShiftRightArray8(TWO.pow(120), 96);
  assertShiftRightArray8(TWO.pow(120).subtract(TWO.pow(1)), 96, true);
  assertShiftRightArray8(MAX_DECIMAL_UNSCALED_VALUE, 20, true);
  assertShiftRightArray8(MAX_DECIMAL_UNSCALED_VALUE.multiply(MAX_DECIMAL_UNSCALED_VALUE), 130);
  assertShiftRightArray8(TWO.pow(256).subtract(BigInteger.ONE), 130, true);
  assertShiftRightArray8Overflow(TWO.pow(156), 1);
  assertShiftRightArray8Overflow(MAX_DECIMAL_UNSCALED_VALUE.multiply(MAX_DECIMAL_UNSCALED_VALUE), 20);
  assertShiftRightArray8Overflow(TWO.pow(256).subtract(BigInteger.ONE), 129);
}

代码示例来源:origin: robovm/robovm

/**
   * Multiplies a number by a power of five.
   * This method is used in {@code BigDecimal} class.
   * @param val the number to be multiplied
   * @param exp a positive {@code int} exponent
   * @return {@code val * 5<sup>exp</sup>}
   */
  static BigInteger multiplyByFivePow(BigInteger val, int exp) {
    // PRE: exp >= 0
    if (exp < fivePows.length) {
      return multiplyByPositiveInt(val, fivePows[exp]);
    } else if (exp < bigFivePows.length) {
      return val.multiply(bigFivePows[exp]);
    } else {// Large powers of five
      return val.multiply(bigFivePows[1].pow(exp));
    }
  }
}

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

private void assertAddReturnOverflow(BigInteger left, BigInteger right)
{
  Slice result = unscaledDecimal();
  long overflow = addWithOverflow(unscaledDecimal(left), unscaledDecimal(right), result);
  BigInteger actual = unscaledDecimalToBigInteger(result);
  BigInteger expected = left.add(right).remainder(TWO.pow(UnscaledDecimal128Arithmetic.UNSCALED_DECIMAL_128_SLICE_LENGTH * 8 - 1));
  BigInteger expectedOverflow = left.add(right).divide(TWO.pow(UnscaledDecimal128Arithmetic.UNSCALED_DECIMAL_128_SLICE_LENGTH * 8 - 1));
  assertEquals(actual, expected);
  assertEquals(overflow, expectedOverflow.longValueExact());
}

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

checkNonNegative("x", x);
if (fitsInLong(x)) {
 return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode));
  checkRoundingUnnecessary(sqrtFloor.pow(2).equals(x)); // fall through
 case FLOOR:
 case DOWN:
  boolean sqrtFloorIsExact =
      && sqrtFloor.pow(2).equals(x); // slow exact check
  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();

相关文章