views:

66

answers:

2

I am tying to throw an exception when a BigInteger is greater than Integer.MAX_VALUE. It will not allow me to throw that exception for the exponent case. I am not sure how to get it to throw an exception when the biginteger value is too large to pass into the BigInteger.pow() method.

Thanks in advance.

here is the toPostfix method:

public BigInteger evalPostfix(String postfix){
    BigInteger a, b;
    Stack stack = new Stack();

        for(int i=0; i<postfix.length(); i++){
            if(this.isOp(postfix.charAt(0)))
                throw new ArithmeticException("Malformed Postfix Expression");
            switch(postfix.charAt(i)){
                case '+':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.add(a));
                    break;
                case '-':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.subtract(a));
                    break;
                case '*':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.multiply(a));
                    break;
                case '/':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    if(a == BigInteger.valueOf(0)){
                        throw new ArithmeticException("Cannot divide by 0");
                    }else{
                        stack.push(b.divide(a));
                    }
                    break;
                case '%':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    stack.push(b.mod(a));
                    break;
                case '^':
                    a = (BigInteger)stack.pop();
                    b = (BigInteger)stack.pop();
                    if(b.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
                        throw new ArithmeticException("BigInteger value is too large");
                    stack.push(a.pow(b.intValue()));
                    break;
                default:
                    if(this.numbers.get(postfix.substring(i, i+1)) == null)
                        throw new NullPointerException(postfix.substring(i, i+1) + " is not mapped to any value");
                    stack.push(this.numbers.get(postfix.substring(i,i+1)));
            }
        }

    return (BigInteger)stack.pop();
}
A: 

The way it is written, it should throw ArithmeticException("Negative Exponent Error") if the exponent is greater than Integer.MAX_VALUE. What happens when you try it?

erickson
nothing happens is the problem I am getting. it just executes and i get a really long result, like 100+ integer value
Karl
I got it figured out. the variables i was using were backwards. So i set the value to exceed Integer.MAX_VALUE but was using another value in testing so it never would throw the exception.Thanks
Karl
No, that's not the whole story, see below.
EJP
A: 

You're popping the stack in the wrong order. The exponent will be on top of the stack, not under the mantissa. You have the same problem in subtraction, division, and modulus, and it wouldn't hurt to do it the same way for addition and multiplication either. In every case it should be b = stack.pop(); then a = stack.pop(). And if you declare the stack as Stack stack = new Stack() you won' t need all those typecasts.

EJP