use of java.math.BigInteger in project groovy by apache.
the class ObjectRangeTest method testSize.
public void testSize() {
Range r = createRange(0, 10);
assertEquals("Size of " + r, 11, r.size());
r = createRange(0, 1);
assertEquals("Size of " + r, 2, r.size());
r = createRange(1, 0);
assertEquals("Size of " + r, 2, r.size());
r = createRange(0, 0);
assertEquals("Size of " + r, 1, r.size());
r = createRange(new BigDecimal("2.1"), new BigDecimal("10.0"));
assertEquals("Size of " + r, 8, r.size());
r = createRange(new BigDecimal("10"), new BigDecimal("2.1"));
assertEquals("Size of " + r, 8, r.size());
r = createRange("a", "d");
assertEquals("Size of " + r, 4, r.size());
r = createRange("d", "a");
assertEquals("Size of " + r, 4, r.size());
r = createRange("aa1", "aa4");
assertEquals("Size of " + r, 4, r.size());
r = createRange("aa4", "aa1");
assertEquals("Size of " + r, 4, r.size());
r = createRange('7', ';');
assertEquals(5, r.size());
// '7', '8', '9', ':', ';'
Range mixed = createRange('7', ';');
assertEquals(5, mixed.size());
mixed = createRange('7', 59.5);
assertEquals(5, mixed.size());
mixed = createRange('7', 59);
assertEquals(5, mixed.size());
mixed = createRange('7', new BigInteger("59"));
assertEquals(5, mixed.size());
mixed = createRange('7', new BigDecimal("59.5"));
assertEquals(5, mixed.size());
// integer overflow cases
assertEquals(Integer.MAX_VALUE, new ObjectRange(0L, Integer.MAX_VALUE).size());
assertEquals(Integer.MAX_VALUE, new ObjectRange(Long.MIN_VALUE, Long.MAX_VALUE).size());
assertEquals(Integer.MAX_VALUE, new ObjectRange(new BigInteger("-10"), new BigInteger(Long.toString((long) Integer.MAX_VALUE) + 1L)).size());
}
use of java.math.BigInteger in project hadoop by apache.
the class TestCryptoCodec method calculateRef.
private static BigInteger calculateRef(byte[] initIV, long counter) {
byte[] cb = Longs.toByteArray(counter);
BigInteger bi = new BigInteger(1, initIV);
return bi.add(new BigInteger(1, cb));
}
use of java.math.BigInteger in project groovy by apache.
the class NumberRange method calcSize.
void calcSize(Comparable from, Comparable to, Number stepSize) {
int tempsize = 0;
boolean shortcut = false;
if (isIntegral(stepSize)) {
if ((from instanceof Integer || from instanceof Long) && (to instanceof Integer || to instanceof Long)) {
// let's fast calculate the size
final BigInteger fromNum = new BigInteger(from.toString());
final BigInteger toTemp = new BigInteger(to.toString());
final BigInteger toNum = inclusive ? toTemp : toTemp.subtract(BigInteger.ONE);
final BigInteger sizeNum = new BigDecimal(toNum.subtract(fromNum)).divide(new BigDecimal(stepSize.longValue()), BigDecimal.ROUND_DOWN).toBigInteger().add(BigInteger.ONE);
tempsize = sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == -1 ? sizeNum.intValue() : Integer.MAX_VALUE;
shortcut = true;
} else if (((from instanceof BigDecimal || from instanceof BigInteger) && to instanceof Number) || ((to instanceof BigDecimal || to instanceof BigInteger) && from instanceof Number)) {
// let's fast calculate the size
final BigDecimal fromNum = new BigDecimal(from.toString());
final BigDecimal toTemp = new BigDecimal(to.toString());
final BigDecimal toNum = inclusive ? toTemp : toTemp.subtract(new BigDecimal("1.0"));
final BigInteger sizeNum = toNum.subtract(fromNum).divide(new BigDecimal(stepSize.longValue()), BigDecimal.ROUND_DOWN).toBigInteger().add(BigInteger.ONE);
tempsize = sizeNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == -1 ? sizeNum.intValue() : Integer.MAX_VALUE;
shortcut = true;
}
}
if (!shortcut) {
// let's brute-force calculate the size by iterating start to end
final Iterator iter = new StepIterator(this, stepSize);
while (iter.hasNext()) {
tempsize++;
// integer overflow
if (tempsize < 0) {
break;
}
iter.next();
}
// integer overflow
if (tempsize < 0) {
tempsize = Integer.MAX_VALUE;
}
}
size = tempsize;
}
use of java.math.BigInteger in project groovy by apache.
the class Transformation method extract.
public int extract(int j) {
BigInteger bigj = BigInteger.valueOf(j);
BigInteger numerator = (q.multiply(bigj)).add(r);
BigInteger denominator = (s.multiply(bigj)).add(t);
return (numerator.divide(denominator)).intValue();
}
use of java.math.BigInteger in project groovy by apache.
the class Numbers method parseInteger.
/**
* Builds a Number from the given integer descriptor. Creates the narrowest
* type possible, or a specific type, if specified.
*
* @param reportNode at node for error reporting in the parser
* @param text literal text to parse
* @return instantiated Number object
* @throws NumberFormatException if the number does not fit within the type
* requested by the type specifier suffix (invalid numbers don't make
* it here)
*/
public static Number parseInteger(AST reportNode, String text) {
// remove place holder underscore before starting
text = text.replace("_", "");
char c = ' ';
int length = text.length();
//
// Strip off the sign, if present
boolean negative = false;
if ((c = text.charAt(0)) == '-' || c == '+') {
negative = (c == '-');
text = text.substring(1, length);
length -= 1;
}
//
// Determine radix (default is 10).
int radix = 10;
if (text.charAt(0) == '0' && length > 1) {
c = text.charAt(1);
if (c == 'X' || c == 'x') {
radix = 16;
text = text.substring(2, length);
length -= 2;
} else if (c == 'B' || c == 'b') {
radix = 2;
text = text.substring(2, length);
length -= 2;
} else {
radix = 8;
}
}
//
// Strip off any type specifier and convert it to lower
// case, if present.
// pick best fit
char type = 'x';
if (isNumericTypeSpecifier(text.charAt(length - 1), false)) {
type = Character.toLowerCase(text.charAt(length - 1));
text = text.substring(0, length - 1);
length -= 1;
}
if (negative) {
text = "-" + text;
}
//
// Build the specified type or, if no type was specified, the
// smallest type in which the number will fit.
BigInteger value = new BigInteger(text, radix);
switch(type) {
case 'i':
if (radix == 10 && reportNode != null && (value.compareTo(MAX_INTEGER) > 0 || value.compareTo(MIN_INTEGER) < 0)) {
throw new ASTRuntimeException(reportNode, "Number of value " + value + " does not fit in the range of int, but int was enforced.");
} else {
return Integer.valueOf(value.intValue());
}
case 'l':
if (radix == 10 && reportNode != null && (value.compareTo(MAX_LONG) > 0 || value.compareTo(MIN_LONG) < 0)) {
throw new ASTRuntimeException(reportNode, "Number of value " + value + " does not fit in the range of long, but long was enforced.");
} else {
return new Long(value.longValue());
}
case 'g':
return value;
default:
// of Integer, Long, and BigInteger.
if (value.compareTo(MAX_INTEGER) <= 0 && value.compareTo(MIN_INTEGER) >= 0) {
return Integer.valueOf(value.intValue());
} else if (value.compareTo(MAX_LONG) <= 0 && value.compareTo(MIN_LONG) >= 0) {
return new Long(value.longValue());
}
return value;
}
}
Aggregations