KickJava   Java API By Example, From Geeks To Geeks.

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


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 TanH extends PostfixMathCommand
17 {
18     public TanH()
19     {
20         numberOfParameters = 1;
21     }
22
23     public void run(Stack inStack)
24         throws ParseException
25     {
26         checkStack(inStack);// check the stack
27
Object JavaDoc param = inStack.pop();
28         inStack.push(tanh(param));//push the result on the inStack
29
return;
30     }
31
32     public Object JavaDoc tanh(Object JavaDoc param)
33         throws ParseException
34     {
35         if (param instanceof Complex)
36         {
37             return ((Complex)param).tanh();
38         }
39         else if (param instanceof Number JavaDoc)
40         {
41             double value = ((Number JavaDoc)param).doubleValue();
42             return new Double JavaDoc((Math.exp(value)-Math.exp(-value))/(Math.pow(Math.E,value)+Math.pow(Math.E,-value)));
43         }
44         throw new ParseException("Invalid parameter type");
45     }
46
47 }
48
Popular Tags