1 9 package org.nfunk.jep.function; 10 11 import java.lang.Math ; 12 import java.util.*; 13 import org.nfunk.jep.*; 14 import org.nfunk.jep.type.*; 15 16 public class Power extends PostfixMathCommand 17 { 18 public Power() 19 { 20 numberOfParameters = 2; 21 } 22 23 public void run(Stack inStack) 24 throws ParseException 25 { 26 checkStack(inStack); 28 Object param2 = inStack.pop(); 29 Object param1 = inStack.pop(); 30 31 inStack.push(power(param1, param2)); 32 } 33 34 public Object power(Object param1, Object param2) 35 throws ParseException 36 { 37 if (param1 instanceof Complex) { 38 if (param2 instanceof Complex) 39 return power((Complex)param1, (Complex)param2); 40 else if (param2 instanceof Number ) 41 return power((Complex)param1, (Number )param2); 42 } 43 else if (param1 instanceof Number ) { 44 if (param2 instanceof Complex) 45 return power((Number )param1, (Complex)param2); 46 else if (param2 instanceof Number ) 47 return power((Number )param1, (Number )param2); 48 } 49 50 throw new ParseException("Invalid parameter type"); 51 } 52 53 54 public Object power(Number d1, Number d2) 55 { 56 if (d1.doubleValue()<0 && d2.doubleValue() != d2.intValue()) 57 { 58 Complex c = new Complex(d1.doubleValue(), 0.0); 59 return c.power(d2.doubleValue()); 60 } 61 else 62 return new Double (Math.pow(d1.doubleValue(),d2.doubleValue())); 63 } 64 65 public Object power(Complex c1, Complex c2) 66 { 67 Complex temp = c1.power(c2); 68 69 if (temp.im()==0) 70 return new Double (temp.re()); 71 else 72 return temp; 73 } 74 75 public Object power(Complex c, Number d) 76 { 77 Complex temp = c.power(d.doubleValue()); 78 79 if (temp.im()==0) 80 return new Double (temp.re()); 81 else 82 return temp; 83 } 84 85 public Object power(Number d, Complex c) 86 { 87 Complex base = new Complex(d.doubleValue(), 0.0); 88 Complex temp = base.power(c); 89 90 if (temp.im()==0) 91 return new Double (temp.re()); 92 else 93 return temp; 94 } 95 96 } 97 | Popular Tags |