package org.nfunk.jep.function;

import java.util.Stack;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.type.Complex;

public class Power extends PostfixMathCommand {
  public void run(Stack paramStack) throws ParseException {
    checkStack(paramStack);
    Object object1 = paramStack.pop();
    Object object2 = paramStack.pop();
    paramStack.push(power(object2, object1));
  }
  
  public Object power(Object paramObject1, Object paramObject2) throws ParseException {
    if (paramObject1 instanceof Complex) {
      if (paramObject2 instanceof Complex)
        return power((Complex)paramObject1, (Complex)paramObject2); 
      if (paramObject2 instanceof Number)
        return power((Complex)paramObject1, (Number)paramObject2); 
    } else if (paramObject1 instanceof Number) {
      if (paramObject2 instanceof Complex)
        return power((Number)paramObject1, (Complex)paramObject2); 
      if (paramObject2 instanceof Number)
        return power((Number)paramObject1, (Number)paramObject2); 
    } 
    throw new ParseException("Invalid parameter type");
  }
  
  public Object power(Number paramNumber1, Number paramNumber2) {
    if (paramNumber1.doubleValue() < 0.0D && paramNumber2.doubleValue() != paramNumber2.intValue()) {
      Complex complex = new Complex(paramNumber1.doubleValue(), 0.0D);
      return complex.power(paramNumber2.doubleValue());
    } 
    return new Double(Math.pow(paramNumber1.doubleValue(), paramNumber2.doubleValue()));
  }
  
  public Object power(Complex paramComplex1, Complex paramComplex2) {
    Complex complex = paramComplex1.power(paramComplex2);
    return (complex.im() == 0.0D) ? new Double(complex.re()) : complex;
  }
  
  public Object power(Complex paramComplex, Number paramNumber) {
    Complex complex = paramComplex.power(paramNumber.doubleValue());
    return (complex.im() == 0.0D) ? new Double(complex.re()) : complex;
  }
  
  public Object power(Number paramNumber, Complex paramComplex) {
    Complex complex1 = new Complex(paramNumber.doubleValue(), 0.0D);
    Complex complex2 = complex1.power(paramComplex);
    return (complex2.im() == 0.0D) ? new Double(complex2.re()) : complex2;
  }
}
