java - BigInteger.pow() with a BigInteger -
I'm bound to throw an exception when the bigteger is greater than integer. MAX_VALUE This will not allow me to throw that exception for the case of the ferries. I'm not sure how to cross it in the BigInteger.Pow () method when the value of the large object is very large, how to throw an exception.
Thanks in advance
Here is the Postfix Method:
Public BigInter evalPostfix (String Postfix) {BigInter A, B; Stack stack = new stack (); (Int i = 0; i & lt; postfix.length (); i ++) {if (this.isOp (postfix.charAt (0)) Throw new arithmetic exception ("deformed postfix expression"); Switch (postfix.charAt (i)) {case '+': a = (BigInteger) stack.pop (); B = (BigIntega) stack.pop (); Stack.push (b.add (a)); break; Cases '-': A = (BigIntega) stack.pop (); B = (BigIntega) stack.pop (); Stack.push (b.subtract (a)); break; Case '*': a = (BigIntega) stack.pop (); B = (BigIntega) stack.pop (); Stack.push (b.multiply (a)); break; Case '/': a = (bigentengar) stack.pop (); B = (BigIntega) stack.pop (); If (a == BigInteger.valueOf (0)) {new arithmetic exception ("can not be divided by 0"); } Else {stack.push (b.divide (a)); } break; Case '%': a = (big intanger) stack.pop (); B = (BigIntega) stack.pop (); Stack.push (b.mod (a)); break; Case '^': a = (big intanger) stack.pop (); B = (BigIntega) stack.pop (); If (b.compareTo (BigInteger.valueOf (Integer.MAX_VALUE)) gt; 0) new arithmetic exception throw ("BigInter value is too big"); Stack.push (a.pow (b.intValue ())); break; Default: if (this.numbers.get (postfix.substring (i, i + 1)) == New new answerpoint expression (postfix.substring (i, i + 1) + "is not a map for any value") Throw; stack.push (this.numbers.get (postfix.substring (I, I + 1));}} Return (BigInter) Stack pop ();}
The way in which it has been written, it will be ArithmeticException ("negative exponent error")
if exponent integer .MAX_VALUE
. What happens when you try it?
Comments
Post a Comment