Search in sources :

Example 36 with BigInteger

use of java.math.BigInteger in project gravel by gravel-st.

the class IntegerExtensions method bigIntegerRaisedToInteger_.

public static BigInteger bigIntegerRaisedToInteger_(int ibase, int iexp) {
    // From:
    // http://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int
    BigInteger result = BigInteger.valueOf(1);
    BigInteger base = BigInteger.valueOf(ibase);
    long exp = iexp;
    while (exp != 0) {
        if ((exp & 1) != 0) {
            result = result.multiply(base);
        }
        exp >>= 1;
        base = base.multiply(base);
    }
    return result;
}
Also used : BigInteger(java.math.BigInteger)

Example 37 with BigInteger

use of java.math.BigInteger in project gravel by gravel-st.

the class TimeExtensions method waitForNanoseconds.

public static Object waitForNanoseconds(final Object receiver, Object nanosecondsObject) {
    long timeout;
    int nanos;
    if (nanosecondsObject instanceof BigInteger) {
        final BigInteger nanoseconds = (BigInteger) nanosecondsObject;
        timeout = nanoseconds.divide(ONE_MILLION).longValue();
        nanos = nanoseconds.remainder(ONE_MILLION).intValue();
    } else if (nanosecondsObject instanceof Integer) {
        final int nanoseconds = (Integer) nanosecondsObject;
        timeout = nanoseconds / 1000000;
        nanos = nanoseconds % 1000000;
    } else {
        throw new RuntimeException("Unknown parameter type");
    }
    try {
        synchronized (receiver) {
            receiver.wait(timeout, nanos);
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    return receiver;
}
Also used : BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger)

Example 38 with BigInteger

use of java.math.BigInteger in project gravel by gravel-st.

the class BytecodeGeneratorTest method testPlusLargeInt.

@Test
public void testPlusLargeInt() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    Class stClass = new ClassBuilder("FooObject_testPlusLargeInt").method("foo ^2147483647 + 1").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo");
    Object result = method.invoke(fooObject);
    assertEquals(new BigInteger("2147483648"), result);
}
Also used : BigInteger(java.math.BigInteger) Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 39 with BigInteger

use of java.math.BigInteger in project gravel by gravel-st.

the class BytecodeGeneratorTest method testFactorial.

@Test
public void testFactorial() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    Class stClass = new ClassBuilder("FooObject_testFactorial").method("foo ^100 factorial").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo");
    Object result = null;
    for (int i = 0; i < 10; i++) {
        Date start = new Date();
        result = method.invoke(fooObject);
        Date stop = new Date();
        System.out.println("Duration: " + (stop.getTime() - start.getTime()) + " ms");
    }
    assertEquals(new BigInteger("93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"), result);
}
Also used : BigInteger(java.math.BigInteger) Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Date(java.util.Date) Test(org.junit.Test)

Example 40 with BigInteger

use of java.math.BigInteger in project j2objc by google.

the class Scanner method nextBigInteger.

/**
     * Returns the next token as a {@code BigInteger} with the specified radix.
     * This method will block if input is being read. If the next token can be translated
     * into a {@code BigInteger} the following is done: All {@code Locale}-specific
     * prefixes, group separators, and {@code Locale}-specific suffixes are removed.
     * Then non-ASCII digits are mapped into ASCII digits via
     * {@link Character#digit(char, int)}, and a negative sign (-) is added if the
     * {@code Locale}-specific negative prefix or suffix was present. Finally the
     * resulting String is passed to {@link BigInteger#BigInteger(String, int)}}
     * with the specified radix.
     *
     * @param radix
     *            the radix used to translate the token into a
     *            {@code BigInteger}.
     * @return the next token as a {@code BigInteger}
     * @throws IllegalStateException
     *             if this {@code Scanner} has been closed.
     * @throws NoSuchElementException
     *             if input has been exhausted.
     * @throws InputMismatchException
     *             if the next token can not be translated into a valid
     *             {@code BigInteger}.
     */
public BigInteger nextBigInteger(int radix) {
    checkOpen();
    Object obj = cachedNextValue;
    cachedNextValue = null;
    if (obj instanceof BigInteger) {
        findStartIndex = cachedNextIndex;
        return (BigInteger) obj;
    }
    Pattern integerPattern = getIntegerPattern(radix);
    String intString = next(integerPattern);
    intString = removeLocaleInfo(intString, int.class);
    BigInteger bigIntegerValue;
    try {
        bigIntegerValue = new BigInteger(intString, radix);
    } catch (NumberFormatException e) {
        matchSuccessful = false;
        recoverPreviousStatus();
        throw new InputMismatchException();
    }
    return bigIntegerValue;
}
Also used : Pattern(java.util.regex.Pattern) BigInteger(java.math.BigInteger)

Aggregations

BigInteger (java.math.BigInteger)3503 BigDecimal (java.math.BigDecimal)635 Test (org.junit.Test)337 IOException (java.io.IOException)115 ArrayList (java.util.ArrayList)105 MessageDigest (java.security.MessageDigest)101 RoundingMode (java.math.RoundingMode)88 Date (java.util.Date)80 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)77 MathContext (java.math.MathContext)71 Random (java.util.Random)65 Test (org.junit.jupiter.api.Test)50 QuickTest (com.hazelcast.test.annotation.QuickTest)47 KeyFactory (java.security.KeyFactory)47 ParallelTest (com.hazelcast.test.annotation.ParallelTest)46 HashMap (java.util.HashMap)45 List (java.util.List)44 SecureRandom (java.security.SecureRandom)42 X509Certificate (java.security.cert.X509Certificate)42 EllipticCurve (java.security.spec.EllipticCurve)40