APISonar


java.math.BigInteger.pow

> java > math > BigInteger > pow
java APIs math APIs BigInteger APIs pow APIs

Example 1
private static final BigInteger MIN_VALUE = MAX_VALUE.negate();

    private static void testPow() {
        System.out.println("Testing BigInteger.pow");
        check("BigInteger.MIN_VALUE.pow(1)",
                MIN_VALUE.pow(1), MIN_VALUE);
        try {
            BigInteger actual = BigInteger.valueOf(4).pow(Integer.MAX_VALUE);
            throw new RuntimeException("BigInteger.valueOf(4).pow(Integer.MAX_VALUE).bitLength()=" + actual.bitLength());
        } catch (ArithmeticException e) {
            // expected
        }
    }
Example 2
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 3
BigInteger ten = new BigInteger("10", 10);

	public void test_powI() {
		assertTrue("Incorrect exponent returned for 2**10", two.pow(10).equals(
				twoToTheTen));
		assertTrue("Incorrect exponent returned for 2**70", two.pow(30)
				.multiply(two.pow(40)).equals(twoToTheSeventy));
		assertTrue("Incorrect exponent returned for 10**50", ten.pow(50)
				.equals(aZillion));
	}
Example 4
BigInteger b;

    public Rational pow(int exponent) {
        if (exponent == 0) {
            return new Rational(1, 1);
        }
        BigInteger num = a.pow(Math.abs(exponent));
        BigInteger deno = b.pow(Math.abs(exponent));
        if (exponent > 0) {
            return (new Rational(num, deno));
        } else {
            return (new Rational(deno, num));
        }
    } /* Rational.pow */
Example 5
public static Number power(BigInteger self, Integer exponent) {
        if (exponent >= 0) {
            return self.pow(exponent);
        } else {
            return power(self, (double) exponent);
        }
    }
Example 6
public void testPowException() {
    byte aBytes[] = {1, 2, 3, 4, 5, 6, 7};
    int aSign = 1;
    int exp = -5;
    BigInteger aNumber = new BigInteger(aSign, aBytes);
    try {
      aNumber = aNumber.pow(exp);
      fail("ArithmeticException has not been caught");
    } catch (ArithmeticException e) {
      assertEquals("Improper exception message", "Negative exponent",
          e.getMessage());
    }
  }
Example 7
private static BigInteger[] doublePointA(BigInteger[] P,
      ECParameterSpec params) {
    final BigInteger p = ((ECFieldFp) params.getCurve().getField()).getP();
    final BigInteger a = params.getCurve().getA();

    if (P[0] == null || P[1] == null) return P;

    BigInteger d = (P[0].pow(2).multiply(THREE).add(a)).multiply(P[1]
        .shiftLeft(1).modInverse(p));
    BigInteger[] R = new BigInteger[2];
    R[0] = d.pow(2).subtract(P[0].shiftLeft(1)).mod(p);
    R[1] = d.multiply(P[0].subtract(R[0])).subtract(P[1]).mod(p);

    return R;
  }
Example 8
public void testPowNegativeNumToZeroExp() {
        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 9
private static final BigInteger TWO = new BigInteger("2");

    public void testCombineOverflow()
    {
        addToState(state, TWO.pow(125));
        addToState(state, TWO.pow(126));

        LongDecimalWithOverflowState otherState = new LongDecimalWithOverflowStateFactory().createSingleState();

        addToState(otherState, TWO.pow(125));
        addToState(otherState, TWO.pow(126));

        DecimalSumAggregation.combine(state, otherState);
        assertEquals(state.getOverflow(), 1);
        assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126)));
    }
Example 10
public void setBitSize(int bitSize) {
		BigInteger b = BigInteger.valueOf(2);
		maxSignedValue = b.pow(bitSize-1).subtract(BigInteger.ONE);
		minSignedValue = b.pow(bitSize-1).negate();
		maxUnsignedValue = b.pow(bitSize).subtract(BigInteger.ONE);

		maxValue = signed ? maxSignedValue : maxUnsignedValue;
		minValue = signed ? minSignedValue : BigInteger.ZERO;
		setValue(getValue());
	}
Example 11
public Point createAPointOnCurve(BigInteger x) {
        BigInteger y = x.pow(3).add(x.multiply(a.getData())).add(b.getData()).mod(getModulus());
        y = y.modPow(getModulus().add(BigInteger.ONE).shiftRight(2), getModulus());
        return getPoint(x, y);
    }
Example 12
public void smoke() throws Exception {
    BigInteger bigBigInt =
        new BigInteger("9999999999999999999999999999999999999999999999999999999999999");

    new SerializationTester(
            BigInteger.ZERO,
            BigInteger.ONE,
            BigInteger.valueOf(-1),
            BigInteger.valueOf(Long.MAX_VALUE),
            BigInteger.valueOf(Long.MIN_VALUE),
            bigBigInt,
            bigBigInt.pow(10_000))
        .runTests();
  }
Example 13
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 14
public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		while (input.hasNext()) {
			BigInteger sum = BigInteger.ZERO;
			int N = input.nextInt();
			int A = input.nextInt();
			BigInteger aAsBigInteger = BigInteger.valueOf(A);
			BigInteger product = BigInteger.ONE;
			for (int i = 1; i < N + 1; i++) {
				product = BigInteger.valueOf(i).multiply(aAsBigInteger.pow(i));
				sum = sum.add(product);
			}
			System.out.println(sum);
		}
	}
Example 15
public void powOverflow2() {
        int shift = 20;
        int exponent = 1 << shift;
        BigInteger x = ONE.shiftLeft((int)(MAX_BITS / exponent)).add(ONE);
        BigInteger y = x.pow(exponent);
    }
Example 16
private static BigInteger recoverX(BigInteger y) {
    // x^2 = (y^2 - 1) / (d * y^2 + 1) mod 2^255-19
    BigInteger xx =
        y.pow(2)
            .subtract(BigInteger.ONE)
            .multiply(D_BI.multiply(y.pow(2)).add(BigInteger.ONE).modInverse(P_BI));
    BigInteger x = xx.modPow(P_BI.add(BigInteger.valueOf(3)).divide(BigInteger.valueOf(8)), P_BI);
    if (!x.pow(2).subtract(xx).mod(P_BI).equals(BigInteger.ZERO)) {
      x = x.multiply(SQRTM1_BI).mod(P_BI);
    }
    if (x.testBit(0)) {
      x = P_BI.subtract(x);
    }
    return x;
  }
Example 17
private static ECPoint doublePoint(ECPoint r, EllipticCurve curve) {
        if (r.equals(ECPoint.POINT_INFINITY)) 
            return r;
        BigInteger slope = (r.getAffineX().pow(2)).multiply(THREE);
        slope = slope.add(curve.getA());
        BigInteger prime = ((ECFieldFp) curve.getField()).getP();
        // use NBI modInverse();
        BigInteger tmp = r.getAffineY().multiply(TWO);
        tmp = new NativeBigInteger(tmp);
        slope = slope.multiply(tmp.modInverse(prime));
        BigInteger xOut = slope.pow(2).subtract(r.getAffineX().multiply(TWO)).mod(prime);
        BigInteger yOut = (r.getAffineY().negate()).add(slope.multiply(r.getAffineX().subtract(xOut))).mod(prime);
        ECPoint out = new ECPoint(xOut, yOut);
        return out;
    }
Example 18
public void testSqrtExact() {
    for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
      BigInteger floor = BigIntegerMath.sqrt(x, FLOOR);
      // We only expect an exception if x was not a perfect square.
      boolean isPerfectSquare = floor.pow(2).equals(x);
      try {
        assertEquals(floor, BigIntegerMath.sqrt(x, UNNECESSARY));
        assertTrue(isPerfectSquare);
      } catch (ArithmeticException e) {
        assertFalse(isPerfectSquare);
      }
    }
  }
Example 19
private static final BigInteger BASE = BigInteger.valueOf(58);

    public static BigInteger decodeToBigInteger(String input) throws Exception {
	BigInteger bi = BigInteger.valueOf(0);
	// Work backwards through the string.
	for (int i = input.length() - 1; i >= 0; i--) {
	    int alphaIndex = ALPHABET.indexOf(input.charAt(i));
	    if (alphaIndex == -1) {
		throw new Exception("Illegal character " + input.charAt(i) + " at " + i);
	    }
	    bi = bi.add(BigInteger.valueOf(alphaIndex).multiply(BASE.pow(input.length() - 1 - i)));
	}
	return bi;
    }
Example 20
private static final BigInteger TWO = BigInteger.valueOf(2);

    public void testMultiplyByInt()
    {
        assertEquals(multiply(unscaledDecimal(0), 1), unscaledDecimal(0));
        assertEquals(multiply(unscaledDecimal(2), Integer.MAX_VALUE), unscaledDecimal(2L * Integer.MAX_VALUE));
        assertEquals(multiply(unscaledDecimal(Integer.MAX_VALUE), -3), unscaledDecimal(-3L * Integer.MAX_VALUE));
        assertEquals(multiply(unscaledDecimal(Integer.MIN_VALUE), -3), unscaledDecimal(-3L * Integer.MIN_VALUE));
        assertEquals(multiply(unscaledDecimal(TWO.pow(100).subtract(BigInteger.ONE)), 2), unscaledDecimal(TWO.pow(101).subtract(TWO)));
    }
Example 21
private static final BigInteger TWO = BigInteger.valueOf(2);

    private void assertShiftLeft(BigInteger value, int leftShifts)
    {
        Slice decimal = unscaledDecimal(value);
        BigInteger expectedResult = value.multiply(TWO.pow(leftShifts));
        shiftLeftDestructive(decimal, leftShifts);
        assertEquals(decodeUnscaledValue(decimal), expectedResult);
    }
Example 22
private static final BigInteger TWO = BigInteger.valueOf(2);

    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());
    }
Example 23
public void testSqrtHalfUp() {
    for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
      BigInteger result = BigIntegerMath.sqrt(x, HALF_UP);
      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);
    }
  }