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 SquareRoot extends PostfixMathCommand 17 { 18 public SquareRoot() { 19 numberOfParameters = 1; 20 } 21 22 25 public void run(Stack inStack) throws ParseException { 26 27 checkStack(inStack); Object param = inStack.pop(); 29 inStack.push(sqrt(param)); return; 31 } 32 33 39 public Object sqrt(Object param) throws ParseException 40 { 41 if (param instanceof Complex) 42 return ((Complex)param).sqrt(); 43 if (param instanceof Number ) { 44 double value = ((Number )param).doubleValue(); 45 46 if (value < 0.0) { 48 return (new Complex(value).sqrt()); 49 } else { 50 return new Double (Math.sqrt(value)); 51 } 52 } 53 54 throw new ParseException("Invalid parameter type"); 55 } 56 } 57 | Popular Tags |