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 22 public class Logarithm extends PostfixMathCommand 23 { 24 private static final double LOG10 = Math.log(10); 25 private static final Complex CLOG10 = new Complex(Math.log(10),0); 26 27 public Logarithm() { 28 numberOfParameters = 1; 29 } 30 31 public void run(Stack inStack) throws ParseException { 32 checkStack(inStack); Object param = inStack.pop(); 34 inStack.push(log(param)); return; 36 } 37 38 39 public Object log(Object param) throws ParseException 40 { 41 if (param instanceof Complex) { 42 return ((Complex)param).log().div(CLOG10); 43 } 44 else if (param instanceof Number ) 45 { 46 double num = ((Number ) param).doubleValue(); 47 if( num > 0) 48 return new Double (Math.log(num)/LOG10); 49 else 50 { 51 Complex temp = new Complex(num); 52 return temp.log().div(CLOG10); 53 } 54 } 55 throw new ParseException("Invalid parameter type"); 56 } 57 58 59 } 60 | Popular Tags |