package org.nfunk.jep.function;

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

public class SquareRoot extends PostfixMathCommand {
  public void run(Stack paramStack) throws ParseException {
    checkStack(paramStack);
    Object object = paramStack.pop();
    paramStack.push(sqrt(object));
  }
  
  public Object sqrt(Object paramObject) throws ParseException {
    if (paramObject instanceof Complex)
      return ((Complex)paramObject).sqrt(); 
    if (paramObject instanceof Number) {
      double d = ((Number)paramObject).doubleValue();
      return (d < 0.0D) ? (new Complex(d)).sqrt() : new Double(Math.sqrt(d));
    } 
    throw new ParseException("Invalid parameter type");
  }
}
