KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************************
2
3 Exp function
4
5 created by R. Morris
6
7 JEP - Java Math Expression Parser 2.24
8       December 30 2002
9       (c) Copyright 2002, Nathan Funk
10       See LICENSE.txt for license information.
11
12 *****************************************************************************/

13
14 package org.nfunk.jep.function;
15
16 import java.lang.Math JavaDoc;
17 import java.util.*;
18 import org.nfunk.jep.*;
19 import org.nfunk.jep.type.*;
20 import org.nfunk.jep.function.PostfixMathCommand;
21
22 /**
23  * The exp function.
24  * Defines a method exp(Object param)
25  * which calculates
26  * @author Rich Morris
27  * Created on 20-Jun-2003
28  */

29 public class Exp extends PostfixMathCommand
30 {
31     public Exp()
32     {
33         numberOfParameters = 1;
34     }
35     
36     public void run(Stack inStack)
37         throws ParseException
38     {
39         checkStack(inStack);// check the stack
40
Object JavaDoc param = inStack.pop();
41         inStack.push(exp(param));//push the result on the inStack
42
return;
43     }
44
45     public Object JavaDoc exp(Object JavaDoc param)
46         throws ParseException
47     {
48         if (param instanceof Complex)
49         {
50             Complex z = (Complex) param;
51             double x = z.re();
52             double y = z.im();
53             double mod = Math.exp(x);
54             return new Complex(mod*Math.cos(y),mod*Math.sin(y));
55         }
56         else if (param instanceof Number JavaDoc)
57         {
58             return new Double JavaDoc(Math.exp(((Number JavaDoc)param).doubleValue()));
59         }
60
61         throw new ParseException("Invalid parameter type");
62     }
63 }
64
Popular Tags