KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class Power extends PostfixMathCommand
17 {
18     public Power()
19     {
20         numberOfParameters = 2;
21     }
22     
23     public void run(Stack inStack)
24         throws ParseException
25     {
26         checkStack(inStack); // check the stack
27

28         Object JavaDoc param2 = inStack.pop();
29         Object JavaDoc param1 = inStack.pop();
30         
31         inStack.push(power(param1, param2));
32     }
33     
34     public Object JavaDoc power(Object JavaDoc param1, Object JavaDoc param2)
35         throws ParseException
36     {
37         if (param1 instanceof Complex) {
38             if (param2 instanceof Complex)
39                 return power((Complex)param1, (Complex)param2);
40             else if (param2 instanceof Number JavaDoc)
41                 return power((Complex)param1, (Number JavaDoc)param2);
42         }
43         else if (param1 instanceof Number JavaDoc) {
44             if (param2 instanceof Complex)
45                 return power((Number JavaDoc)param1, (Complex)param2);
46             else if (param2 instanceof Number JavaDoc)
47                 return power((Number JavaDoc)param1, (Number JavaDoc)param2);
48         }
49
50         throw new ParseException("Invalid parameter type");
51     }
52     
53
54     public Object JavaDoc power(Number JavaDoc d1, Number JavaDoc d2)
55     {
56         if (d1.doubleValue()<0 && d2.doubleValue() != d2.intValue())
57         {
58             Complex c = new Complex(d1.doubleValue(), 0.0);
59             return c.power(d2.doubleValue());
60         }
61         else
62             return new Double JavaDoc(Math.pow(d1.doubleValue(),d2.doubleValue()));
63     }
64     
65     public Object JavaDoc power(Complex c1, Complex c2)
66     {
67         Complex temp = c1.power(c2);
68
69         if (temp.im()==0)
70             return new Double JavaDoc(temp.re());
71         else
72             return temp;
73     }
74     
75     public Object JavaDoc power(Complex c, Number JavaDoc d)
76     {
77         Complex temp = c.power(d.doubleValue());
78         
79         if (temp.im()==0)
80             return new Double JavaDoc(temp.re());
81         else
82             return temp;
83     }
84
85     public Object JavaDoc power(Number JavaDoc d, Complex c)
86     {
87         Complex base = new Complex(d.doubleValue(), 0.0);
88         Complex temp = base.power(c);
89         
90         if (temp.im()==0)
91             return new Double JavaDoc(temp.re());
92         else
93             return temp;
94     }
95     
96 }
97
Popular Tags