KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Natural logarithm.
18  *
19  * RJM Change: fixed so ln(positive Double) is Double.
20  */

21 public class NaturalLogarithm extends PostfixMathCommand
22 {
23     public NaturalLogarithm()
24     {
25         numberOfParameters = 1;
26
27     }
28
29     public void run(Stack inStack)
30         throws ParseException
31     {
32         checkStack(inStack);// check the stack
33
Object JavaDoc param = inStack.pop();
34         inStack.push(ln(param));//push the result on the inStack
35
return;
36     }
37
38     public Object JavaDoc ln(Object JavaDoc param)
39         throws ParseException
40     {
41         if (param instanceof Complex)
42         {
43             return ((Complex)param).log();
44         }
45         else if (param instanceof Number JavaDoc)
46         {
47             // Now returns Complex if param is <0
48
double num = ((Number JavaDoc) param).doubleValue();
49             if( num > 0)
50                 return new Double JavaDoc(Math.log(num));
51             else
52             {
53                 Complex temp = new Complex(num);
54                 return temp.log();
55             }
56         }
57
58         throw new ParseException("Invalid parameter type");
59     }
60 }
61
Popular Tags