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;
}
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;
}
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);
}
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);
}
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;
}
Aggregations