This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathmodule-math.cpp
More file actions
506 lines (391 loc) · 14.9 KB
/
module-math.cpp
File metadata and controls
506 lines (391 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include "foundation-math.h"
#include <foundation-system.h>
#include <float.h>
#include <errno.h>
#ifndef _WIN32
# include <fenv.h>
#endif
// Older versions of MSVC don't supply "trunc"
#ifdef _WIN32
double trunc(double f) { return f < 0 ? ceil(f) : floor(f); }
# define isnan(x) _isnan(x)
#endif
////////////////////////////////////////////////////////////////
MCTypeInfoRef kMCMathDomainErrorTypeInfo;
////////////////////////////////////////////////////////////////
static inline bool
__MCMathPropagateNanUnary (double p_in, double p_out)
{
if (isnan (p_out) && !isnan (p_in))
return false;
return true;
}
static inline bool
__MCMathPropagateNanBinary (double p_left, double p_right, double p_out)
{
if (isnan (p_out) && (!(isnan (p_left) || isnan (p_right))))
return false;
return true;
}
////////////////////////////////////////////////////////////////
extern "C" MC_DLLEXPORT_DEF void MCMathEvalRealToPowerOfReal(double p_left, double p_right, double& r_output)
{
r_output = pow(p_left, p_right);
if (__MCMathPropagateNanBinary (p_left, p_right, r_output))
return;
MCErrorCreateAndThrow (kMCMathDomainErrorTypeInfo, nil);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalNumberToPowerOfNumber(MCNumberRef p_left, MCNumberRef p_right, MCNumberRef& r_output)
{
double t_left, t_right;
t_left = MCNumberFetchAsReal(p_left);
t_right = MCNumberFetchAsReal(p_right);
double t_result;
MCMathEvalRealToPowerOfReal(t_left, t_right, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalBase10LogReal(double p_operand, double& r_output)
{
r_output = log10(p_operand);
if (__MCMathPropagateNanUnary (p_operand, r_output))
return;
MCErrorCreateAndThrow (kMCMathDomainErrorTypeInfo, nil);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalBase10LogNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalBase10LogReal(t_operand, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalNaturalLogReal(double p_operand, double& r_output)
{
r_output = log(p_operand);
if (__MCMathPropagateNanUnary (p_operand, r_output))
return;
MCErrorCreateAndThrow (kMCMathDomainErrorTypeInfo, nil);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalNaturalLogNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalNaturalLogReal(t_operand, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalExpReal(double p_operand, double& r_output)
{
r_output = exp(p_operand);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalExpNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalExpReal(t_operand, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalSinReal(double p_operand, double& r_output)
{
r_output = sin(p_operand);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalSinNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalSinReal(t_operand, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalCosReal(double p_operand, double& r_output)
{
r_output = cos(p_operand);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalCosNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalCosReal(t_operand, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalTanReal(double p_operand, double& r_output)
{
r_output = tan(p_operand);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalTanNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalTanReal(t_operand, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAsinReal(double p_operand, double& r_output)
{
r_output = asin(p_operand);
if (__MCMathPropagateNanUnary (p_operand, r_output))
return;
MCErrorCreateAndThrow (kMCMathDomainErrorTypeInfo, nil);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAsinNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalAsinReal(t_operand, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAcosReal(double p_operand, double& r_output)
{
r_output = acos(p_operand);
if (__MCMathPropagateNanUnary (p_operand, r_output))
return;
MCErrorCreateAndThrow (kMCMathDomainErrorTypeInfo, nil);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAcosNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalAcosReal(t_operand, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAtanReal(double p_operand, double& r_output)
{
r_output = atan(p_operand);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAtanNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalAtanReal(t_operand, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAtan2Real(double p_first, double p_second, double& r_output)
{
r_output = atan2(p_first, p_second);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAtan2Number(MCNumberRef p_first, MCNumberRef p_second, MCNumberRef& r_output)
{
double t_first, t_second;
t_first = MCNumberFetchAsReal(p_first);
t_second = MCNumberFetchAsReal(p_second);
double t_result;
MCMathEvalAtan2Real(t_first, t_second, t_result);
// if (!ctxt . HasError())
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAbsInteger(integer_t p_operand, integer_t& r_output)
{
r_output = MCAbs(p_operand);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAbsReal(double p_operand, double& r_output)
{
r_output = MCAbs(p_operand);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalAbsNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
if (MCNumberIsInteger(p_operand))
{
integer_t t_abs;
MCMathEvalAbsInteger(MCNumberFetchAsInteger(p_operand), t_abs);
MCNumberCreateWithInteger(t_abs, r_output);
return;
}
double t_abs_real;
MCMathEvalAbsReal(MCNumberFetchAsReal(p_operand), t_abs_real);
MCNumberCreateWithReal(t_abs_real, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalTruncInteger(integer_t p_operand, integer_t& r_output)
{
r_output = p_operand;
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalTruncReal(double p_operand, double& r_output)
{
r_output = trunc(p_operand);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalTruncNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
if (MCNumberIsInteger(p_operand))
{
integer_t t_abs;
MCMathEvalTruncInteger(MCNumberFetchAsInteger(p_operand), t_abs);
MCNumberCreateWithInteger(t_abs, r_output);
return;
}
double t_abs_real;
MCMathEvalTruncReal(MCNumberFetchAsReal(p_operand), t_abs_real);
MCNumberCreateWithReal(t_abs_real, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalMinInteger(integer_t p_left, integer_t p_right, integer_t& r_output)
{
r_output = MCMin(p_left, p_right);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalMinReal(double p_left, double p_right, double& r_output)
{
r_output = MCMin(p_left, p_right);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalMinNumber(MCNumberRef p_left, MCNumberRef p_right, MCNumberRef& r_output)
{
double t_left, t_right, t_result;
t_left = MCNumberFetchAsReal(p_left);
t_right = MCNumberFetchAsReal(p_right);
MCMathEvalMinReal(t_left, t_right, t_result);
MCNumberCreateWithReal(t_result, r_output);
}
static void MCMathEvalMinMaxList(MCProperListRef p_list, bool p_is_min, MCNumberRef& r_output)
{
if (MCProperListIsEmpty(p_list))
{
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("list must be non-empty"), nil);
return;
}
if (!MCProperListIsListOfType(p_list, kMCValueTypeCodeNumber))
{
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("list must be numeric"), nil);
return;
}
double t_minmax, t_cur_real;
t_cur_real = MCNumberFetchAsReal((MCNumberRef)MCProperListFetchElementAtIndex(p_list, 0));
t_minmax = t_cur_real;
bool t_replace;
for (uindex_t i = 1; i < MCProperListGetLength(p_list); i++)
{
t_cur_real = MCNumberFetchAsReal((MCNumberRef)MCProperListFetchElementAtIndex(p_list, i));
t_replace = p_is_min ? t_cur_real < t_minmax : t_cur_real > t_minmax;
if (t_replace)
t_minmax = t_cur_real;
}
MCNumberCreateWithReal(t_minmax, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalMinList(MCProperListRef p_list, MCNumberRef& r_output)
{
MCMathEvalMinMaxList(p_list, true, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalMaxList(MCProperListRef p_list, MCNumberRef& r_output)
{
MCMathEvalMinMaxList(p_list, false, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalMaxInteger(integer_t p_left, integer_t p_right, integer_t& r_output)
{
r_output = MCMax(p_left, p_right);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalMaxReal(double p_left, double p_right, double& r_output)
{
r_output = MCMax(p_left, p_right);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalMaxNumber(MCNumberRef p_left, MCNumberRef p_right, MCNumberRef& r_output)
{
double t_left, t_right, t_result;
t_left = MCNumberFetchAsReal(p_left);
t_right = MCNumberFetchAsReal(p_right);
MCMathEvalMaxReal(t_left, t_right, t_result);
MCNumberCreateWithReal(t_result, r_output);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalRandomReal(double& r_output)
{
r_output = MCSRandomReal();
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalSqrtReal(double p_operand, double& r_output)
{
r_output = sqrt(p_operand);
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalSqrtNumber(MCNumberRef p_operand, MCNumberRef& r_output)
{
double t_operand;
t_operand = MCNumberFetchAsReal(p_operand);
double t_result;
MCMathEvalSqrtReal(t_operand, t_result);
MCNumberCreateWithReal(t_result, r_output);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
extern "C" MC_DLLEXPORT_DEF void MCMathEvalConvertToBase10(MCStringRef p_operand, integer_t p_source_base, integer_t& r_output)
{
if (p_source_base < 2 || p_source_base > 32)
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("source base must be between 2 and 32"), nil);
bool t_negative;
uinteger_t t_result;
bool t_error = false;
if (MCMathConvertToBase10(p_operand, p_source_base, t_negative, t_result, t_error))
{
if ((t_negative && t_result > INTEGER_MAX) || (!t_negative && t_result > abs(INTEGER_MIN)))
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("integer overflow"), nil);
else
r_output = t_negative ? -t_result : t_result;
}
else if (t_error)
{
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("integer overflow, or invalid character in source"), nil);
}
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalConvertFromBase10(integer_t p_operand, integer_t p_dest_base, MCStringRef& r_output)
{
if (p_dest_base < 2 || p_dest_base > 32)
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("destination base must be between 2 and 32"), nil);
if (p_operand < 0)
{
if (MCMathConvertFromBase10(-p_operand, true, p_dest_base, r_output))
return;
}
else
{
if (MCMathConvertFromBase10(p_operand, false, p_dest_base, r_output))
return;
}
// ctxt . Throw();
}
extern "C" MC_DLLEXPORT_DEF void MCMathEvalConvertBase(MCStringRef p_operand, integer_t p_source_base, integer_t p_dest_base, MCStringRef& r_output)
{
if (p_source_base < 2 || p_source_base > 32)
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("source base must be between 2 and 32"), nil);
if (p_dest_base < 2 || p_dest_base > 32)
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("destination base must be between 2 and 32"), nil);
bool t_negative;
uinteger_t t_result;
bool t_error;
if (MCMathConvertToBase10(p_operand, p_source_base, t_negative, t_result, t_error))
{
if (MCMathConvertFromBase10(t_result, t_negative, p_dest_base, r_output))
return;
}
// If t_error is false then we failed because of a memory error, so no need to throw
if (t_error)
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("integer overflow, or invalid character in source"), nil);
}
////////////////////////////////////////////////////////////////
extern "C" bool com_livecode_math_Initialize (void)
{
if (!MCNamedErrorTypeInfoCreate (MCNAME("com.livecode.math.DomainError"), MCNAME("math"), MCSTR("mathematical function domain error"), kMCMathDomainErrorTypeInfo))
return false;
return true;
}
extern "C" void com_livecode_math_Finalize (void)
{
MCValueRelease (kMCMathDomainErrorTypeInfo);
}
////////////////////////////////////////////////////////////////////////////////////////////////////