package org.nfunk.jep.function;

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

public class Subtract extends PostfixMathCommand {
  public void run(Stack paramStack) throws ParseException {
    checkStack(paramStack);
    Object object1 = paramStack.pop();
    Object object2 = paramStack.pop();
    paramStack.push(sub(object2, object1));
  }
  
  public Object sub(Object paramObject1, Object paramObject2) throws ParseException {
    if (paramObject1 instanceof Complex) {
      if (paramObject2 instanceof Complex)
        return sub((Complex)paramObject1, (Complex)paramObject2); 
      if (paramObject2 instanceof Number)
        return sub((Complex)paramObject1, (Number)paramObject2); 
    } else if (paramObject1 instanceof Number) {
      if (paramObject2 instanceof Complex)
        return sub((Number)paramObject1, (Complex)paramObject2); 
      if (paramObject2 instanceof Number)
        return sub((Number)paramObject1, (Number)paramObject2); 
    } 
    throw new ParseException("Invalid parameter type");
  }
  
  public Double sub(Number paramNumber1, Number paramNumber2) {
    return new Double(paramNumber1.doubleValue() - paramNumber2.doubleValue());
  }
  
  public Complex sub(Complex paramComplex1, Complex paramComplex2) {
    return new Complex(paramComplex1.re() - paramComplex2.re(), paramComplex1.im() - paramComplex2.im());
  }
  
  public Complex sub(Complex paramComplex, Number paramNumber) {
    return new Complex(paramComplex.re() - paramNumber.doubleValue(), paramComplex.im());
  }
  
  public Complex sub(Number paramNumber, Complex paramComplex) {
    return new Complex(paramNumber.doubleValue() - paramComplex.re(), -paramComplex.im());
  }
}
