@Override public Number divide(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); BigInteger[] divmod = n1.divideAndRemainder(n2); if (divmod[1].equals(BigInteger.ZERO)) {
@Override public Number divide(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); BigInteger[] divmod = n1.divideAndRemainder(n2); if (divmod[1].equals(BigInteger.ZERO)) {
public Number divide(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); BigInteger[] divmod = n1.divideAndRemainder(n2); if(divmod[1].equals(BigInteger.ZERO)) {
@Override public Number divide(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); BigInteger[] divmod = n1.divideAndRemainder(n2); if (divmod[1].equals(BigInteger.ZERO)) {
@Override public Number multiply(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); : (Number) toBigInteger(first).multiply(toBigInteger(second)); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.multiply(n2);
public Number add(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); return ((n ^ n1) < 0 && (n ^ n2) < 0) // overflow check ? (Number)toBigInteger(first).add(toBigInteger(second)) : (Number)new Long(n); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.add(n2);
@Override public Number multiply(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); : (Number) toBigInteger(first).multiply(toBigInteger(second)); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.multiply(n2);
@Override public Number add(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); return ((n ^ n1) < 0 && (n ^ n2) < 0) // overflow check ? toBigInteger(first).add(toBigInteger(second)) : Long.valueOf(n); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.add(n2);
public Number subtract(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); return ((n ^ n1) < 0 && (n ^ ~n2) < 0) // overflow check ? (Number)toBigInteger(first).subtract(toBigInteger(second)) : (Number)new Long(n); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.subtract(n2);
@Override public int compareNumbers(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.compareTo(n2);
public Number multiply(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); : (Number)toBigInteger(first).multiply(toBigInteger(second)); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.multiply(n2);
@Override public Number subtract(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); return ((n ^ n1) < 0 && (n ^ ~n2) < 0) // overflow check ? (Number) toBigInteger(first).subtract(toBigInteger(second)) : (Number) Long.valueOf(n); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.subtract(n2);
@Override public Number add(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); return ((n ^ n1) < 0 && (n ^ n2) < 0) // overflow check ? toBigInteger(first).add(toBigInteger(second)) : Long.valueOf(n); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.add(n2);
@Override public Number subtract(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); return ((n ^ n1) < 0 && (n ^ ~n2) < 0) // overflow check ? (Number) toBigInteger(first).subtract(toBigInteger(second)) : (Number) Long.valueOf(n); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.subtract(n2);
public int compareNumbers(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.compareTo(n2);
@Override public int compareNumbers(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { int n1 = first.intValue(); BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.compareTo(n2);
@Override public Number modulus(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { return Integer.valueOf(first.intValue() % second.intValue()); } case LONG: { return Long.valueOf(first.longValue() % second.longValue()); } case FLOAT: { return Float.valueOf(first.floatValue() % second.floatValue()); } case DOUBLE: { return Double.valueOf(first.doubleValue() % second.doubleValue()); } case BIGINTEGER: { BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.mod(n2); } case BIGDECIMAL: { throw new _MiscTemplateException("Can't calculate remainder on BigDecimals"); } } // Make the compiler happy. getCommonClassCode() is guaranteed to // return only above codes, or throw an exception. throw new BugException(); }
private static int getCommonClassCode(Number num1, Number num2) throws TemplateException { int c1 = getClassCode(num1); int c2 = getClassCode(num2); int c = c1 > c2 ? c1 : c2; // If BigInteger is combined with a Float or Double, the result is a // BigDecimal instead of BigInteger in order not to lose the // fractional parts. If Float is combined with Long, the result is a // Double instead of Float to preserve the bigger bit width. switch(c) { case FLOAT: { if ((c1 < c2 ? c1 : c2) == LONG) { return DOUBLE; } break; } case BIGINTEGER: { int min = c1 < c2 ? c1 : c2; if (min == DOUBLE || min == FLOAT) { return BIGDECIMAL; } break; } } return c; }
private static int getCommonClassCode(Number num1, Number num2) throws TemplateException { int c1 = getClassCode(num1); int c2 = getClassCode(num2); int c = c1 > c2 ? c1 : c2; // If BigInteger is combined with a Float or Double, the result is a // BigDecimal instead of BigInteger in order not to lose the // fractional parts. If Float is combined with Long, the result is a // Double instead of Float to preserve the bigger bit width. switch(c) { case FLOAT: { if ((c1 < c2 ? c1 : c2) == LONG) { return DOUBLE; } break; } case BIGINTEGER: { int min = c1 < c2 ? c1 : c2; if (min == DOUBLE || min == FLOAT) { return BIGDECIMAL; } break; } } return c; }
@Override public Number modulus(Number first, Number second) throws TemplateException { switch(getCommonClassCode(first, second)) { case INTEGER: { return Integer.valueOf(first.intValue() % second.intValue()); } case LONG: { return Long.valueOf(first.longValue() % second.longValue()); } case FLOAT: { return Float.valueOf(first.floatValue() % second.floatValue()); } case DOUBLE: { return Double.valueOf(first.doubleValue() % second.doubleValue()); } case BIGINTEGER: { BigInteger n1 = toBigInteger(first); BigInteger n2 = toBigInteger(second); return n1.mod(n2); } case BIGDECIMAL: { throw new _MiscTemplateException("Can't calculate remainder on BigDecimals"); } } // Make the compiler happy. getCommonClassCode() is guaranteed to // return only above codes, or throw an exception. throw new BugException(); }