Code example for BigInteger

Methods: multiply

0
                    long n2 = second.longValue();
                    long n = n1 * n2;
                    return 
                        n1==0L || n / n1 == n2 // overflow check
                        ? (Number)new Long(n)
                        : (Number)toBigInteger(first).multiply(toBigInteger(second));
                } 
                case FLOAT: {
                    return new Float(first.floatValue() * second.floatValue());
                } 
                case DOUBLE: {
                    return new Double(first.doubleValue() * second.doubleValue());
                } 
                case BIGINTEGER: {
                    BigInteger n1 = toBigInteger(first);
                    BigInteger n2 = toBigInteger(second);
                    return n1.multiply(n2);
                } 
                case BIGDECIMAL: {
                    BigDecimal n1 = toBigDecimal(first);
                    BigDecimal n2 = toBigDecimal(second);
                    BigDecimal r = n1.multiply(n2);
                    return r.scale() > maxScale ? r.setScale(maxScale, roundingPolicy) : r;
                } 
            } 
            // Make the compiler happy. getCommonClassCode() is guaranteed to  
            // return only above codes, or throw an exception. 
            throw new Error();
        } 
     
        public Number divide(Number first, Number second) throws TemplateException {
            switch(getCommonClassCode(first, second)) {
                case INTEGER: {
                    int n1 = first.intValue();
                    int n2 = second.intValue();
                    if (n1 % n2 == 0) {
                        return new Integer(n1/n2);
                    } 
                    return new Double(((double)n1)/n2);
                } 
                case LONG: {
                    long n1 = first.longValue();
                    long n2 = second.longValue();
                    if (n1 % n2 == 0) {
                        return new Long(n1/n2);
                    } 
                    return new Double(((double)n1)/n2);
                } 
                case FLOAT: {
                    return new Float(first.floatValue() / second.floatValue());
                } 
                case DOUBLE: {
                    return new Double(first.doubleValue() / second.doubleValue());
                } 
                case BIGINTEGER: {
                    BigInteger n1 = toBigInteger(first);
                    BigInteger n2 = toBigInteger(second);
                    BigInteger[] divmod = n1.divideAndRemainder(n2);
                    if(divmod[1].equals(BigInteger.ZERO)) {
                        return divmod[0];
                    } 
                    else { 
                        BigDecimal bd1 = new BigDecimal(n1);
                        BigDecimal bd2 = new BigDecimal(n2);
                        return bd1.divide(bd2, minScale, roundingPolicy);
                    } 
                } 
                case BIGDECIMAL: {
                    BigDecimal n1 = toBigDecimal(first);
                    BigDecimal n2 = toBigDecimal(second);
                    int scale1 = n1.scale();
                    int scale2 = n2.scale();
                    int scale = Math.max(scale1, scale2);
                    scale = Math.max(minScale, scale);
                    return n1.divide(n2, scale, roundingPolicy);
                } 
            } 
            // Make the compiler happy. getCommonClassCode() is guaranteed to  
            // return only above codes, or throw an exception. 
            throw new Error();
        } 
     
        public Number modulus(Number first, Number second) throws TemplateException {
            switch(getCommonClassCode(first, second)) {
                case INTEGER: {
                    return new Integer(first.intValue() % second.intValue());
                } 
                case LONG: {
                    return new Long(first.longValue() % second.longValue());
                } 
                case FLOAT: {
                    return new Float(first.floatValue() % second.floatValue());
                } 
                case DOUBLE: {
                    return new Double(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 Error();
        } 
     
        public Number toNumber(String s) {
            return OptimizerUtil.optimizeNumberRepresentation(new BigDecimal(s));
        } 
         
        private static Map createClassCodesMap() {
            Map map = new HashMap(17);
            Integer intcode = new Integer(INTEGER);
            map.put(Byte.class, intcode);
            map.put(Short.class, intcode);
            map.put(Integer.class, intcode);
            map.put(Long.class, new Integer(LONG));
            map.put(Float.class, new Integer(FLOAT));
            map.put(Double.class, new Integer(DOUBLE));
            map.put(BigInteger.class, new Integer(BIGINTEGER));
            map.put(BigDecimal.class, new Integer(BIGDECIMAL));
            return map;
        } 
         
        private static int getClassCode(Number num) throws TemplateException {
            try { 
                return ((Integer)classCodes.get(num.getClass())).intValue();
            } 
            catch(NullPointerException e) {
                if(num == null) {
                    throw new _MiscTemplateException("The Number object was null."); 
                } else { 
                    throw new _MiscTemplateException(new Object[] {
                            "Unknown number type ", num.getClass().getName() });
                } 
            } 
        } 
         
        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 BigInteger toBigInteger(Number num) {
            return num instanceof BigInteger ? (BigInteger) num : new BigInteger(num.toString());
        } 
    } 
 
    private static BigDecimal toBigDecimal(Number num) {
        try {