0
|
1
|
|
2 /* Digital Mars DMDScript source code.
|
|
3 * Copyright (c) 2000-2002 by Chromium Communications
|
|
4 * D version Copyright (c) 2004-2005 by Digital Mars
|
|
5 * All Rights Reserved
|
|
6 * written by Walter Bright
|
|
7 * www.digitalmars.com
|
|
8 * Use at your own risk. There is no warranty, express or implied.
|
|
9 * License for redistribution is by the GNU General Public License in gpl.txt.
|
|
10 *
|
|
11 * A binary, non-exclusive license for commercial use can be
|
|
12 * purchased from www.digitalmars.com/dscript/buy.html.
|
|
13 *
|
|
14 * DMDScript is implemented in the D Programming Language,
|
|
15 * www.digitalmars.com/d/
|
|
16 *
|
|
17 * For a C++ implementation of DMDScript, including COM support,
|
|
18 * see www.digitalmars.com/dscript/cppscript.html.
|
|
19 */
|
|
20
|
|
21
|
3
|
22 module dmdscript_tango.dmath;
|
0
|
23
|
|
24 import std.math;
|
|
25 import std.random;
|
|
26
|
3
|
27 import dmdscript_tango.script;
|
|
28 import dmdscript_tango.value;
|
|
29 import dmdscript_tango.dobject;
|
|
30 import dmdscript_tango.dnative;
|
|
31 import dmdscript_tango.threadcontext;
|
|
32 import dmdscript_tango.text;
|
|
33 import dmdscript_tango.property;
|
0
|
34
|
|
35 d_number math_helper(Value[] arglist)
|
|
36 {
|
|
37 Value *v;
|
|
38
|
|
39 v = arglist.length ? &arglist[0] : &vundefined;
|
|
40 return v.toNumber();
|
|
41 }
|
|
42
|
|
43 void* Dmath_abs(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
44 {
|
|
45 // ECMA 15.8.2.1
|
|
46 d_number result;
|
|
47
|
|
48 result = fabs(math_helper(arglist));
|
|
49 ret.putVnumber(result);
|
|
50 return null;
|
|
51 }
|
|
52
|
|
53 void* Dmath_acos(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
54 {
|
|
55 // ECMA 15.8.2.2
|
|
56 d_number result;
|
|
57
|
|
58 result = acos(math_helper(arglist));
|
|
59 ret.putVnumber(result);
|
|
60 return null;
|
|
61 }
|
|
62
|
|
63 void* Dmath_asin(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
64 {
|
|
65 // ECMA 15.8.2.3
|
|
66 d_number result;
|
|
67
|
|
68 result = asin(math_helper(arglist));
|
|
69 ret.putVnumber(result);
|
|
70 return null;
|
|
71 }
|
|
72
|
|
73 void* Dmath_atan(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
74 {
|
|
75 // ECMA 15.8.2.4
|
|
76 d_number result;
|
|
77
|
|
78 result = atan(math_helper(arglist));
|
|
79 ret.putVnumber(result);
|
|
80 return null;
|
|
81 }
|
|
82
|
|
83 void* Dmath_atan2(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
84 {
|
|
85 // ECMA 15.8.2.5
|
|
86 d_number n1;
|
|
87 Value *v2;
|
|
88 d_number result;
|
|
89
|
|
90 n1 = math_helper(arglist);
|
|
91 v2 = (arglist.length >= 2) ? &arglist[1] : &vundefined;
|
|
92 result = atan2(n1, v2.toNumber());
|
|
93 ret.putVnumber(result);
|
|
94 return null;
|
|
95 }
|
|
96
|
|
97 void* Dmath_ceil(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
98 {
|
|
99 // ECMA 15.8.2.6
|
|
100 d_number result;
|
|
101
|
|
102 result = ceil(math_helper(arglist));
|
|
103 ret.putVnumber(result);
|
|
104 return null;
|
|
105 }
|
|
106
|
|
107 void* Dmath_cos(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
108 {
|
|
109 // ECMA 15.8.2.7
|
|
110 d_number result;
|
|
111
|
|
112 result = cos(math_helper(arglist));
|
|
113 ret.putVnumber(result);
|
|
114 return null;
|
|
115 }
|
|
116
|
|
117 void* Dmath_exp(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
118 {
|
|
119 // ECMA 15.8.2.8
|
|
120 d_number result;
|
|
121
|
|
122 result = std.math.exp(math_helper(arglist));
|
|
123 ret.putVnumber(result);
|
|
124 return null;
|
|
125 }
|
|
126
|
|
127 void* Dmath_floor(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
128 {
|
|
129 // ECMA 15.8.2.9
|
|
130 d_number result;
|
|
131
|
|
132 result = std.math.floor(math_helper(arglist));
|
|
133 ret.putVnumber(result);
|
|
134 return null;
|
|
135 }
|
|
136
|
|
137 void* Dmath_log(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
138 {
|
|
139 // ECMA 15.8.2.10
|
|
140 d_number result;
|
|
141
|
|
142 result = log(math_helper(arglist));
|
|
143 ret.putVnumber(result);
|
|
144 return null;
|
|
145 }
|
|
146
|
|
147 void* Dmath_max(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
148 {
|
|
149 // ECMA v3 15.8.2.11
|
|
150 d_number n;
|
|
151 d_number result;
|
|
152 uint a;
|
|
153
|
|
154 result = -d_number.infinity;
|
|
155 foreach (Value v; arglist)
|
|
156 {
|
|
157 n = v.toNumber();
|
|
158 if (isnan(n))
|
|
159 { result = d_number.nan;
|
|
160 break;
|
|
161 }
|
|
162 if (result == n)
|
|
163 {
|
|
164 // if n is +0 and result is -0, pick n
|
|
165 if (n == 0 && !signbit(n))
|
|
166 result = n;
|
|
167 }
|
|
168 else if (n > result)
|
|
169 result = n;
|
|
170 }
|
|
171 ret.putVnumber(result);
|
|
172 return null;
|
|
173 }
|
|
174
|
|
175 void* Dmath_min(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
176 {
|
|
177 // ECMA v3 15.8.2.12
|
|
178 d_number n;
|
|
179 d_number result;
|
|
180 uint a;
|
|
181
|
|
182 result = d_number.infinity;
|
|
183 foreach (Value v; arglist)
|
|
184 {
|
|
185 n = v.toNumber();
|
|
186 if (isnan(n))
|
|
187 { result = d_number.nan;
|
|
188 break;
|
|
189 }
|
|
190 if (result == n)
|
|
191 {
|
|
192 // if n is -0 and result is +0, pick n
|
|
193 if (n == 0 && signbit(n))
|
|
194 result = n;
|
|
195 }
|
|
196 else if (n < result)
|
|
197 result = n;
|
|
198 }
|
|
199 ret.putVnumber(result);
|
|
200 return null;
|
|
201 }
|
|
202
|
|
203 void* Dmath_pow(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
204 {
|
|
205 // ECMA 15.8.2.13
|
|
206 d_number n1;
|
|
207 Value *v2;
|
|
208 d_number result;
|
|
209
|
|
210 n1 = math_helper(arglist);
|
|
211 v2 = (arglist.length >= 2) ? &arglist[1] : &vundefined;
|
|
212 result = pow(n1, v2.toNumber());
|
|
213 ret.putVnumber(result);
|
|
214 return null;
|
|
215 }
|
|
216
|
|
217 void* Dmath_random(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
218 {
|
|
219 // ECMA 15.8.2.14
|
|
220 // 0.0 <= result < 1.0
|
|
221 d_number result;
|
|
222
|
|
223 ulong x;
|
|
224
|
|
225 // Only want 53 bits of precision
|
|
226 x = (cast(ulong)std.random.rand() << 32) + std.random.rand();
|
|
227 //PRINTF("x = x%016llx\n",x);
|
|
228 x &= 0xFFFFFFFFFFFFF800L;
|
|
229 result = x * (1 / (0x100000000L * cast(double)0x100000000L))
|
|
230 + (1 / (0x200000000L * cast(double)0x100000000L));
|
|
231
|
|
232 // Experiments on linux show that this will never be exactly
|
|
233 // 1.0, so is the assert() worth it?
|
|
234 assert(result >= 0 && result < 1.0);
|
|
235 ret.putVnumber(result);
|
|
236 return null;
|
|
237 }
|
|
238
|
|
239 void* Dmath_round(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
240 {
|
|
241 // ECMA 15.8.2.15
|
|
242 d_number result;
|
|
243
|
|
244 result = math_helper(arglist);
|
|
245 if (!isnan(result))
|
|
246 result = copysign(std.math.floor(result + .5), result);
|
|
247 ret.putVnumber(result);
|
|
248 return null;
|
|
249 }
|
|
250
|
|
251 void* Dmath_sin(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
252 {
|
|
253 // ECMA 15.8.2.16
|
|
254 d_number result;
|
|
255
|
|
256 result = sin(math_helper(arglist));
|
|
257 ret.putVnumber(result);
|
|
258 return null;
|
|
259 }
|
|
260
|
|
261 void* Dmath_sqrt(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
262 {
|
|
263 // ECMA 15.8.2.17
|
|
264 d_number result;
|
|
265
|
|
266 result = sqrt(math_helper(arglist));
|
|
267 ret.putVnumber(result);
|
|
268 return null;
|
|
269 }
|
|
270
|
|
271 void* Dmath_tan(Dobject pthis, CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
272 {
|
|
273 // ECMA 15.8.2.18
|
|
274 d_number result;
|
|
275
|
|
276 result = tan(math_helper(arglist));
|
|
277 ret.putVnumber(result);
|
|
278 return null;
|
|
279 }
|
|
280
|
|
281 /* ===================== Dmath ==================== */
|
|
282
|
|
283 class Dmath : Dobject
|
|
284 {
|
|
285 this(ThreadContext *tc)
|
|
286 {
|
|
287 super(tc.Dobject_prototype);
|
|
288
|
|
289 //writef("Dmath::Dmath(%x)\n", this);
|
|
290 uint attributes = DontEnum | DontDelete | ReadOnly;
|
|
291
|
|
292 struct MathConst
|
|
293 { d_string *name;
|
|
294 d_number value;
|
|
295 }
|
|
296
|
|
297 static MathConst table[] =
|
|
298 [
|
|
299 { &TEXT_E, std.math.E },
|
|
300 { &TEXT_LN10, std.math.LN10 },
|
|
301 { &TEXT_LN2, std.math.LN2 },
|
|
302 { &TEXT_LOG2E, std.math.LOG2E },
|
|
303 { &TEXT_LOG10E, std.math.LOG10E },
|
|
304 { &TEXT_PI, std.math.PI },
|
|
305 { &TEXT_SQRT1_2, std.math.SQRT1_2 },
|
|
306 { &TEXT_SQRT2, std.math.SQRT2 },
|
|
307 ];
|
|
308
|
|
309 for (size_t u = 0; u < table.length; u++)
|
|
310 { Value *v;
|
|
311
|
|
312 v = Put(*table[u].name, table[u].value, attributes);
|
|
313 //writef("Put(%s,%.5g) = %x\n", *table[u].name, table[u].value, v);
|
|
314 }
|
|
315
|
|
316 classname = TEXT_Math;
|
|
317
|
|
318 static NativeFunctionData nfd[] =
|
|
319 [
|
|
320 { &TEXT_abs, &Dmath_abs, 1 },
|
|
321 { &TEXT_acos, &Dmath_acos, 1 },
|
|
322 { &TEXT_asin, &Dmath_asin, 1 },
|
|
323 { &TEXT_atan, &Dmath_atan, 1 },
|
|
324 { &TEXT_atan2, &Dmath_atan2, 2 },
|
|
325 { &TEXT_ceil, &Dmath_ceil, 1 },
|
|
326 { &TEXT_cos, &Dmath_cos, 1 },
|
|
327 { &TEXT_exp, &Dmath_exp, 1 },
|
|
328 { &TEXT_floor, &Dmath_floor, 1 },
|
|
329 { &TEXT_log, &Dmath_log, 1 },
|
|
330 { &TEXT_max, &Dmath_max, 2 },
|
|
331 { &TEXT_min, &Dmath_min, 2 },
|
|
332 { &TEXT_pow, &Dmath_pow, 2 },
|
|
333 { &TEXT_random, &Dmath_random, 0 },
|
|
334 { &TEXT_round, &Dmath_round, 1 },
|
|
335 { &TEXT_sin, &Dmath_sin, 1 },
|
|
336 { &TEXT_sqrt, &Dmath_sqrt, 1 },
|
|
337 { &TEXT_tan, &Dmath_tan, 1 },
|
|
338 ];
|
|
339
|
|
340 DnativeFunction.init(this, nfd, attributes);
|
|
341 }
|
|
342
|
|
343 static void init(ThreadContext *tc)
|
|
344 {
|
|
345 tc.Dmath_object = new Dmath(tc);
|
|
346 }
|
|
347 }
|
|
348
|