KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > function > Logarithm


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9 package org.nfunk.jep.function;
10
11 import java.lang.Math JavaDoc;
12 import java.util.*;
13 import org.nfunk.jep.*;
14 import org.nfunk.jep.type.*;
15
16 /**
17  * Log bass 10.
18  * <p>
19  * RJM change return real results for positive real arguments.
20  * Speedup by using static final fields.
21  */

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);// check the stack
33
Object JavaDoc param = inStack.pop();
34         inStack.push(log(param));//push the result on the inStack
35
return;
36     }
37     
38
39     public Object JavaDoc log(Object JavaDoc param) throws ParseException
40     {
41         if (param instanceof Complex) {
42            return ((Complex)param).log().div(CLOG10);
43         }
44         else if (param instanceof Number JavaDoc)
45         {
46             double num = ((Number JavaDoc) param).doubleValue();
47             if( num > 0)
48                 return new Double JavaDoc(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