Java Code Examples for java.math.BigInteger#pow()

The following examples show how to use java.math.BigInteger#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: ClassicBlockProcessor.java    From besu with Apache License 2.0 6 votes vote down vote up
private Wei getBlockWinnerRewardByEra(final int era) {
  if (era == 0) {
    return this.blockReward;
  }

  // MaxBlockReward _r_ * (4/5)**era == MaxBlockReward * (4**era) / (5**era)
  // since (q/d)**n == q**n / d**n
  // qed

  BigInteger disinflationRateQuotient = BigInteger.valueOf(4);
  BigInteger q;
  q = disinflationRateQuotient.pow(era);

  BigInteger disinflationRateDivisor = BigInteger.valueOf(5);
  BigInteger d;
  d = disinflationRateDivisor.pow(era);

  BigInteger maximumBlockReward = this.blockReward.toBigInteger();
  BigInteger r;
  r = maximumBlockReward.multiply(q);

  r = r.divide(d);
  return Wei.of(r);
}
 
Example 2
Source File: BigIntegerTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void pow(int order) {
    int failCount1 = 0;

    for (int i=0; i<SIZE; i++) {
        // Test identity x^power == x*x*x ... *x
        int power = rnd.nextInt(6) + 2;
        BigInteger x = fetchNumber(order);
        BigInteger y = x.pow(power);
        BigInteger z = x;

        for (int j=1; j<power; j++)
            z = z.multiply(x);

        if (!y.equals(z))
            failCount1++;
    }
    report("pow for " + order + " bits", failCount1);
}
 
Example 3
Source File: BigIntegerTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void pow(int order) {
    int failCount1 = 0;

    for (int i=0; i<SIZE; i++) {
        // Test identity x^power == x*x*x ... *x
        int power = rnd.nextInt(6) + 2;
        BigInteger x = fetchNumber(order);
        BigInteger y = x.pow(power);
        BigInteger z = x;

        for (int j=1; j<power; j++)
            z = z.multiply(x);

        if (!y.equals(z))
            failCount1++;
    }
    report("pow for " + order + " bits", failCount1);
}
 
Example 4
Source File: LargeValueExceptions.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void powOverflow1() {
    int shift = 20;
    int exponent = 1 << shift;
    BigInteger x = ONE.shiftLeft((int)(MAX_BITS / exponent));
    BigInteger y = x.pow(exponent);
}
 
Example 5
Source File: BigIntegerTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void square(int order) {
    int failCount1 = 0;

    for (int i=0; i<SIZE; i++) {
        // Test identity x^2 == x*x
        BigInteger x  = fetchNumber(order);
        BigInteger xx = x.multiply(x);
        BigInteger x2 = x.pow(2);

        if (!x2.equals(xx))
            failCount1++;
    }
    report("square for " + order + " bits", failCount1);
}
 
Example 6
Source File: Cardumen_0053_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 * @param k number to raise
 * @param e exponent (must be positive or null)
 * @return k<sup>e</sup>
 * @exception IllegalArgumentException if e is negative
 */
public static BigInteger pow(final BigInteger k, int e)
    throws IllegalArgumentException {

    if (e < 0) {
        throw MathRuntimeException.createIllegalArgumentException(
            LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
            k, e);
    }

    return k.pow(e);

}
 
Example 7
Source File: MathUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 * @param k number to raise
 * @param e exponent (must be positive or null)
 * @return k<sup>e</sup>
 * @exception IllegalArgumentException if e is negative
 */
public static BigInteger pow(final BigInteger k, int e)
    throws IllegalArgumentException {

    if (e < 0) {
        throw MathRuntimeException.createIllegalArgumentException(
            "cannot raise an integral value to a negative power ({0}^{1})",
            k, e);
    }

    return k.pow(e);

}
 
Example 8
Source File: Primality.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
private static BigInteger multiplicativeOrder(BigInteger a, BigInteger prime, int exponent) {
	BigInteger m = prime.pow(exponent);
	BigInteger t = m.divide(prime).multiply(prime.subtract(BigInteger.ONE));
	List<BigInteger> divisors = divisors(t);
	int len = divisors.size();
	for (int i = 0; i < len; i++) {
		BigInteger factor = divisors.get(i);
		if (a.modPow(factor, m).equals(BigInteger.ONE)) {
			return factor;
		}
	}
	return BigInteger.ZERO;
}
 
Example 9
Source File: NPEfix13_thirteen_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 * @param k number to raise
 * @param e exponent (must be positive or null)
 * @return k<sup>e</sup>
 * @exception IllegalArgumentException if e is negative
 */
public static BigInteger pow(final BigInteger k, int e)
        throws IllegalArgumentException {

    if (e < 0) {
        throw MathRuntimeException.createIllegalArgumentException(
                "cannot raise an integral value to a negative power ({0}^{1})",
                k, e);
    }

    return k.pow(e);

}
 
Example 10
Source File: BigIntegerTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void square(int order) {
    int failCount1 = 0;

    for (int i=0; i<SIZE; i++) {
        // Test identity x^2 == x*x
        BigInteger x  = fetchNumber(order);
        BigInteger xx = x.multiply(x);
        BigInteger x2 = x.pow(2);

        if (!x2.equals(xx))
            failCount1++;
    }
    report("square for " + order + " bits", failCount1);
}
 
Example 11
Source File: BigIntegerMultiplyTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Exponentiation of a negative number to zero exponent.
 */
public void testPowPositiveNumToZeroExp() {
    byte aBytes[] = {50, -26, 90, 69, 120, 32, 63, -103, -14, 35};
    int aSign = 1;
    int exp = 0;
    byte rBytes[] = {1};
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    BigInteger result = aNumber.pow(exp);
    byte resBytes[] = new byte[rBytes.length];
    resBytes = result.toByteArray();
    for(int i = 0; i < resBytes.length; i++) {
        assertTrue(resBytes[i] == rBytes[i]);
    }
    assertEquals("incorrect sign", 1, result.signum());
}
 
Example 12
Source File: Cardumen_00224_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 * @param k number to raise
 * @param e exponent (must be positive or null)
 * @return k<sup>e</sup>
 * @exception IllegalArgumentException if e is negative
 */
public static BigInteger pow(final BigInteger k, int e)
    throws IllegalArgumentException {

    if (e < 0) {
        throw MathRuntimeException.createIllegalArgumentException(
            LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
            k, e);
    }

    return k.pow(e);

}
 
Example 13
Source File: LargeValueExceptions.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class,enabled=false)
public void powOverflow3() {
    int shift = 20;
    int exponent = 1 << shift;
    BigInteger x = ONE.shiftLeft((int)(MAX_BITS / exponent)).subtract(ONE);
    BigInteger y = x.pow(exponent);
}
 
Example 14
Source File: BigIntegerMath.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the base-2 logarithm of {@code x}, rounded according to the specified rounding mode.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @throws ArithmeticException if {@code mode} is {@link RoundingMode#UNNECESSARY} and {@code x}
 *     is not a power of two
 */

@SuppressWarnings("fallthrough")
// TODO(kevinb): remove after this warning is disabled globally
public static int log2(BigInteger x, RoundingMode mode) {
  checkPositive("x", checkNotNull(x));
  int logFloor = x.bitLength() - 1;
  switch (mode) {
    case UNNECESSARY:
      checkRoundingUnnecessary(isPowerOfTwo(x)); // fall through
    case DOWN:
    case FLOOR:
      return logFloor;
    case UP:
    case CEILING:
      return isPowerOfTwo(x) ? logFloor : logFloor + 1;
    case HALF_DOWN:
    case HALF_UP:
    case HALF_EVEN:
      if (logFloor < SQRT2_PRECOMPUTE_THRESHOLD) {
        BigInteger halfPower = SQRT2_PRECOMPUTED_BITS.shiftRight(SQRT2_PRECOMPUTE_THRESHOLD - logFloor);
        if (x.compareTo(halfPower) <= 0) {
          return logFloor;
        } else {
          return logFloor + 1;
        }
      }
      // Since sqrt(2) is irrational, log2(x) - logFloor cannot be exactly 0.5
      //
      // To determine which side of logFloor.5 the logarithm is,
      // we compare x^2 to 2^(2 * logFloor + 1).
      BigInteger x2 = x.pow(2);
      int logX2Floor = x2.bitLength() - 1;
    return (logX2Floor < 2 * logFloor + 1) ? logFloor : logFloor + 1;
    default:
      throw new AssertionError();
  }
}
 
Example 15
Source File: MathUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 * @param k number to raise
 * @param e exponent (must be positive or null)
 * @return k<sup>e</sup>
 * @exception IllegalArgumentException if e is negative
 */
public static BigInteger pow(final BigInteger k, int e)
    throws IllegalArgumentException {

    if (e < 0) {
        throw MathRuntimeException.createIllegalArgumentException(
            LocalizedFormats.POWER_NEGATIVE_PARAMETERS,
            k, e);
    }

    return k.pow(e);

}
 
Example 16
Source File: Wire.java    From jsnark with MIT License 4 votes vote down vote up
public Wire mul(long base, int exp, String... desc) {
	BigInteger b = new BigInteger(base + "");
	b = b.pow(exp);
	return mul(b, desc);
}
 
Example 17
Source File: Operators.java    From beanshell with Apache License 2.0 4 votes vote down vote up
static Object bigIntegerBinaryOperation(BigInteger lhs, BigInteger rhs, int kind)
{

    switch(kind)
    {
        // arithmetic
        case PLUS:
            return lhs.add(rhs);

        case MINUS:
            return lhs.subtract(rhs);

        case STAR:
            return lhs.multiply(rhs);

        case SLASH:
            return lhs.divide(rhs);

        case MOD:
        case MODX:
            return lhs.mod(rhs);

        case POWER:
        case POWERX:
            return lhs.pow(rhs.intValue());

        // bitwise
        case LSHIFT:
        case LSHIFTX:
            return lhs.shiftLeft(rhs.intValue());

        case RSIGNEDSHIFT:
        case RSIGNEDSHIFTX:
            return lhs.shiftRight(rhs.intValue());

        case RUNSIGNEDSHIFT:
        case RUNSIGNEDSHIFTX:
            if ( lhs.signum() >= 0 )
                return lhs.shiftRight(rhs.intValue());
            BigInteger opener = BigInteger.ONE.shiftLeft(lhs.toString(2).length() + 1);
            BigInteger opened = lhs.subtract(opener);
            BigInteger mask = opener.subtract(BigInteger.ONE).shiftRight(rhs.intValue() + 1);
            return opened.shiftRight(rhs.intValue()).and(mask);


        case BIT_AND:
        case BIT_ANDX:
            return lhs.and(rhs);

        case BIT_OR:
        case BIT_ORX:
            return lhs.or(rhs);

        case XOR:
        case XORX:
            return lhs.xor(rhs);

    }
    throw new InterpreterError(
            "Unimplemented binary integer operator");
}
 
Example 18
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, int e) throws NotPositiveException {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    return k.pow(e);
}
 
Example 19
Source File: ArithmeticUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, int e) {
    if (e < 0) {
        throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
    }

    return k.pow(e);
}
 
Example 20
Source File: ArithmeticUtils.java    From commons-numbers with Apache License 2.0 3 votes vote down vote up
/**
 * Raise a BigInteger to an int power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws IllegalArgumentException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, int e) {
    if (e < 0) {
        throw new IllegalArgumentException(NEGATIVE_EXPONENT_1 + e + NEGATIVE_EXPONENT_2);
    }

    return k.pow(e);
}