1 /********************************************************************************
2 *
3 * Copyright (C) 2008, 2009 Fujitsu Services Ltd.
4 *
5 * Author: Nick Battle
6 *
7 * This file is part of VDMJ.
8 *
9 * VDMJ is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * VDMJ is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21 *
22 ******************************************************************************/
23
24 package org.overturetool.vdmj.util;
25
26 import java.util.Random;
27
28 import org.overturetool.vdmj.lex.LexNameToken;
29 import org.overturetool.vdmj.runtime.Context;
30 import org.overturetool.vdmj.runtime.ValueException;
31 import org.overturetool.vdmj.values.IntegerValue;
32 import org.overturetool.vdmj.values.RealValue;
33 import org.overturetool.vdmj.values.Value;
34
35 public class MATH
36 {
37 private static Random random = new Random();
38 private static long seed = 0;
39
40 public static Value sin(Context ctxt) throws ValueException, Exception
41 {
42 Value arg = ctxt.lookup(new LexNameToken("MATH", "v", null));
43 return new RealValue(Math.sin(arg.realValue(ctxt)));
44 }
45
46 public static Value cos(Context ctxt) throws ValueException, Exception
47 {
48 Value arg = ctxt.lookup(new LexNameToken("MATH", "v", null));
49 return new RealValue(Math.cos(arg.realValue(ctxt)));
50 }
51
52 public static Value tan(Context ctxt) throws ValueException, Exception
53 {
54 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
55 return new RealValue(Math.tan(arg.realValue(ctxt)));
56 }
57
58 public static Value cot(Context ctxt) throws ValueException, Exception
59 {
60 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
61 return new RealValue(1/Math.tan(arg.realValue(ctxt)));
62 }
63
64 public static Value asin(Context ctxt) throws ValueException, Exception
65 {
66 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
67 return new RealValue(Math.asin(arg.realValue(ctxt)));
68 }
69
70 public static Value acos(Context ctxt) throws ValueException, Exception
71 {
72 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
73 return new RealValue(Math.acos(arg.realValue(ctxt)));
74 }
75
76 public static Value atan(Context ctxt) throws ValueException, Exception
77 {
78 Value arg = ctxt.lookup(new LexNameToken("MATH", "v", null));
79 return new RealValue(Math.atan(arg.realValue(ctxt)));
80 }
81
82 public static Value sqrt(Context ctxt) throws ValueException, Exception
83 {
84 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
85 return new RealValue(Math.sqrt(arg.realValue(ctxt)));
86 }
87
88 public static Value pi(@SuppressWarnings("unused") Context ctxt)
89 throws Exception
90 {
91 return new RealValue(Math.PI);
92 }
93
94 public static Value rand(Context ctxt) throws ValueException
95 {
96 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
97 long lv = arg.intValue(ctxt);
98
99 if (seed == 0)
100 {
101 return new IntegerValue(lv);
102 }
103 else
104 {
105 return new IntegerValue(random.nextLong() % lv);
106 }
107 }
108
109 public static Value srand2(Context ctxt) throws ValueException
110 {
111 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
112 seed = arg.intValue(ctxt);
113 random.setSeed(seed);
114 return new IntegerValue(seed);
115 }
116
117 public static Value exp(Context ctxt) throws ValueException, Exception
118 {
119 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
120 return new RealValue(Math.exp(arg.realValue(ctxt)));
121 }
122
123 public static Value ln(Context ctxt) throws ValueException, Exception
124 {
125 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
126 return new RealValue(Math.log(arg.realValue(ctxt)));
127 }
128
129 public static Value log(Context ctxt) throws ValueException, Exception
130 {
131 Value arg = ctxt.lookup(new LexNameToken("MATH", "a", null));
132 return new RealValue(Math.log10(arg.realValue(ctxt)));
133 }
134 }