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 227
Expand file tree
/
Copy pathmath.lcb
More file actions
673 lines (505 loc) · 19.7 KB
/
math.lcb
File metadata and controls
673 lines (505 loc) · 19.7 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/* 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/>. */
/**
This library consists of the mathematical operations included in the standard library of LiveCode Builder.
*/
module com.livecode.math
use com.livecode.foreign
public foreign handler MCMathEvalRealToPowerOfReal(in Left as CDouble, in Right as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalNumberToPowerOfNumber(in Left as Number, in Right as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalBase10LogReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalBase10LogNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalNaturalLogReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalNaturalLogNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalExpReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalExpNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalSinReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalSinNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalCosReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalCosNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalTanReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalTanNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAsinReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAsinNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAcosReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAcosNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAtanReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAtanNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAtan2Real(in yCoord as CDouble, in xCoord as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAtan2Number(in yCoord as Number, in xCoord as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAbsInteger(in Operand as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAbsReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalAbsNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalTruncInteger(in Operand as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalTruncReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalTruncNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalMinInteger(in Left as LCInt, in Right as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalMinReal(in Left as CDouble, in Right as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalMinNumber(in Left as Number, in Right as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalMinList(in ValueList as List, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalMaxList(in ValueList as List, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalMaxInteger(in Left as LCInt, in Right as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalMaxReal(in Left as CDouble, in Right as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalMaxNumber(in Left as Number, in Right as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalRandomReal(out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalSqrtReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalSqrtNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalConvertBase(in Operand as String, in Source as LCInt, in Target as LCInt, out Value as String) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalConvertToBase10(in Operand as String, in Source as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
public foreign handler MCMathEvalConvertFromBase10(in Operand as LCInt, in Target as LCInt, out Value as String) returns nothing binds to "<builtin>"
--
/**
Summary: Binary exponentiation operation.
Left: An expression that evaluates to a number.
Right: An expression that evaluates to a number.
Returns: The result of raising <Left> to the power <Right>.
Example:
variable tVar as Number
put 5^2 into tVar -- tVar contains 25
Tags: Math
*/
syntax PowOperator is left binary operator with exponentiation precedence
<Left: Expression> "^" <Right: Expression>
begin
MCMathEvalNumberToPowerOfNumber(Left, Right, output)
end syntax
--
/**
Syntax: sin(<Operand>)
Summary: Sin operator.
Operand: An expression that evaluates to a number.
Example:
variable tVar as Number
put sin(pi / 2) into tVar -- tVar contains 1
Example:
variable tVar as Number
put the sine of pi into tVar -- tVar contains 0
Tags: Math
*/
syntax SinOperator is prefix operator with function chunk precedence
"the" "sine" "of" <Operand: Expression>
begin
MCMathEvalSinNumber(Operand, output)
end syntax
public handler Sin(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalSinNumber(pOperand, tVar)
end unsafe
return tVar
end handler
/**
Syntax: cos(<Operand>)
Summary: Cos operator.
Operand: An expression that evaluates to a number.
Example:
variable tVar as Number
put cos(pi) into tVar -- tVar contains -1
Tags: Math
*/
syntax CosOperator is prefix operator with function chunk precedence
"the" "cosine" "of" <Operand: Expression>
begin
MCMathEvalCosNumber(Operand, output)
end syntax
public handler Cos(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalCosNumber(pOperand, tVar)
end unsafe
return tVar
end handler
/**
Syntax: tan(<Operand>)
Summary: Tan operator.
Operand: An expression that evaluates to a number.
Example:
variable tVar as Number
put the tan(pi) into tVar -- tVar contains 0
Example:
variable tVar as Number
put the tangent of pi /4 into tVar -- tVar contains approximately 1
Tags: Math
*/
syntax TanOperator is prefix operator with function chunk precedence
"the" "tangent" "of" <Operand: Expression>
begin
MCMathEvalTanNumber(Operand, output)
end syntax
public handler Tan(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalTanNumber(pOperand, tVar)
end unsafe
return tVar
end handler
/**
Syntax: asin(<Operand>)
Summary: Arcsin operator.
Operand: An expression that evaluates to a number.
Example:
variable tVar as Number
put the asin(-1) into tVar -- tVar contains -pi/2
Example:
variable tVar as Number
put the arcsine of 1 into tVar -- tVar contains pi/2
Description:
The inverse of the sin operator.
Tags: Math
*/
syntax ArcsinOperator is prefix operator with function chunk precedence
"the" "arcsine" "of" <Operand: Expression>
begin
MCMathEvalAsinNumber(Operand, output)
end syntax
public handler Asin(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalAsinNumber(pOperand, tVar)
end unsafe
return tVar
end handler
/**
Syntax: acos(<Operand>)
Summary: Arccos operator.
Operand: An expression that evaluates to a number.
Example:
variable tVar as Number
put acos(0) into tVar -- tVar contains pi/2
Example:
variable tVar as Number
put the arccosine of -1 into tVar -- tVar contains pi
Description:
The inverse of the cos operator.
Tags: Math
*/
syntax ArccosOperator is prefix operator with function chunk precedence
"the" "arccosine" "of" <Operand: Expression>
begin
MCMathEvalAcosNumber(Operand, output)
end syntax
public handler Acos(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalAcosNumber(pOperand, tVar)
end unsafe
return tVar
end handler
/**
Syntax: atan(<Operand>)
Summary: Arctan operator.
Operand: An expression that evaluates to a number.
Example:
variable tVar as Number
put atan(-1) into tVar -- tVar contains -pi / 4
Example:
variable tVar as Number
put the arctangent of 1 into tVar -- tVar contains pi / 4
Description:
The inverse of the tan operator.
Tags: Math
*/
syntax ArctanOperator is prefix operator with function chunk precedence
"the" "arctangent" "of" <Operand: Expression>
begin
MCMathEvalAtanNumber(Operand, output)
end syntax
public handler Atan(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalAtanNumber(pOperand, tVar)
end unsafe
return tVar
end handler
/**
Syntax: atan2(<yCoord>,<xCoord>)
Summary: Binary arctan operator.
yCoord: An expression that evaluates to a number.
xCoord: An expression that evaluates to a number.
Example:
variable tVar as Number
put atan2(-1,-1) into tVar --tVar contains −3π/4.
Example:
variable tVar as Number
put the binary arctangent of -1 and -1 into tVar --tVar contains −3π/4.
Description:
The binary arctan operator returns the angle in radians between the x-axis and the line from the origin to the point (xCoord, yCoord).
The angle returned has absolute value less than pi: −π < arctan2(y, x) ≤ π.
Tags: Math
*/
syntax BinaryArctanOperator is prefix operator with function chunk precedence
"the" "binary" "arctangent" "of" <yCoord: Expression> "and" <xCoord: Expression>
begin
MCMathEvalAtan2Number(yCoord, xCoord, output)
end syntax
public handler atan2(in pY as Number, in pX as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalAtan2Number(pY, pX, tVar)
end unsafe
return tVar
end handler
--
/**
Syntax: log(<Operand>)
Summary: Base 10 log operator.
Operand: An expression that evaluates to a number.
Example:
variable tVar as Number
put the log of 10000 into tVar -- tVar contains 5
Tags: Math
*/
syntax Base10LogOperator is prefix operator with function chunk precedence
"the" "log" "of" <Operand: Expression>
begin
MCMathEvalBase10LogNumber(Operand, output)
end syntax
public handler log10(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalBase10LogNumber(pOperand, tVar)
end unsafe
return tVar
end handler
--
/**
Syntax: ln(<Operand>)
Summary: Natural log operator.
Operand: An expression that evaluates to a number.
Example:
variable tVar as Number
put ln(exp(3)) into tVar -- tVar contains approximately 3
Tags: Math
*/
syntax BaseELogOperator is prefix operator with function chunk precedence
"the" "natural" "log" "of" <Operand: Expression>
begin
MCMathEvalNaturalLogNumber(Operand, output)
end syntax
public handler Ln(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalNaturalLogNumber(pOperand, tVar)
end unsafe
return tVar
end handler
/**
Syntax: exp(<Operand>)
Summary: Exponentiation operator.
Operand: An expression that evaluates to a number.
Returns: e to the power of <Operand>
Example:
variable tVar as Number
put the exp(ln(100)) into tVar -- tVar contains approximately 100
Tags: Math
*/
syntax ExpOperator is prefix operator with function chunk precedence
"the" "exponential" "of" <Operand: Expression>
begin
MCMathEvalExpNumber(Operand, output)
end syntax
public handler Exp(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalExpNumber(pOperand, tVar)
end unsafe
return tVar
end handler
--
/**
Summary: Truncation operator.
Operand: An expression that evaluates to a number.
Returns: <Operand> truncated.
Example:
variable tVar as Number
put the trunc of 3.9 into tVar -- tVar contains 3
Description:
The trunc of <Operand> is the integer part of the decimal representation of <Operand>.
Tags: Math
*/
syntax TruncOperator is prefix operator with function chunk precedence
"the" "trunc" "of" <Operand: Expression>
begin
MCMathEvalTruncNumber(Operand, output)
end syntax
/**
Summary: Absolute value operator.
Operand: An expression that evaluates to a number.
Returns: The absolute value of <Operand>.
Example:
variable tVar as Number
put the trunc of 3.9 into tVar -- tVar contains 3
Description:
The absolute value of <Operand> is the value of <Operand> if it is greater than or equal to 0, or the negation of the value of <Operand> if it is less than zero.
Tags: Math
*/
syntax AbsOperator is prefix operator with function chunk precedence
"the" "abs" "of" <Operand: Expression>
begin
MCMathEvalAbsNumber(Operand, output)
end syntax
--
/**
Summary: Generates a pseudo-random number.
Returns: A real between 0.0 and 1.0.
Example:
variable tVar as Number
put any number into tVar -- 0 <= tVar <= 1
Tags: Math
*/
syntax RandomNumber is expression
"any" "number"
begin
MCMathEvalRandomReal(output)
end syntax
--
/**
Summary: Min operator.
Left: An expression that evaluates to a number.
Right: An expression that evaluates to a number.
Returns: The value of <Left>, if it is less than the value of <Right>, and the value of <Right> otherwise.
Tags: Math
*/
syntax MinOperator is prefix operator with function chunk precedence
"the" "minimum" "of" <Left: Expression> "and" <Right: Expression>
begin
MCMathEvalMinNumber(Left, Right, output)
end syntax
public handler Min(in pX as Number, in pY as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalMinNumber(pX, pY, tVar)
end unsafe
return tVar
end handler
/**
Summary: Max operator.
Left: An expression that evaluates to a number.
Right: An expression that evaluates to a number.
Returns: The value of <Left>, if it is greater than the value of <Right>, and the value of <Right> otherwise.
Tags: Math
*/
syntax MaxOperator is prefix operator with function chunk precedence
"the" "maximum" "of" <Left: Expression> "and" <Right: Expression>
begin
MCMathEvalMaxNumber(Left, Right, output)
end syntax
public handler Max(in pX as Number, in pY as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalMaxNumber(pX, pY, tVar)
end unsafe
return tVar
end handler
/**
Summary: Min operator.
List: An expression that evaluates to a list.
Returns: The minimal value attained by the elements of <List>.
Description:
>*Note:* It is an error if any element of <List> is non-numeric.
Tags: Math
*/
syntax MinListOperator is prefix operator with function chunk precedence
"the" "minimum" "value" "of" <ValueList: Expression>
begin
MCMathEvalMinList(ValueList, output)
end syntax
/**
Summary: Max operator.
List: An expression that evaluates to a list.
Returns: The maximum value attained by the elements of <List>.
Description:
>*Note:* It is an error if any element of <List> is non-numeric.
Tags: Math
*/
syntax MaxListOperator is prefix operator with function chunk precedence
"the" "maximum" "value" "of" <ValueList: Expression>
begin
MCMathEvalMaxList(ValueList, output)
end syntax
--
/**
Syntax: sqrt(<Operand>)
Summary: Square root operator.
Operand: An expression that evaluates to a number.
Example:
variable tVar as Number
put sqrt(9) into tVar -- tVar contains 3
Example:
variable tVar as Number
put the square root of 4 into tVar -- tVar contains 2
Description:
The square root of a number is the number which must be
squared to obtain number : sqrt(number)^2 is equal to number.
Tags: Math
*/
syntax SquareRootOperator is prefix operator with function chunk precedence
"the" "square" "root" "of" <Operand: Expression>
begin
MCMathEvalSqrtNumber(Operand, output)
end syntax
public handler Sqrt(in pOperand as Number) returns Number
variable tVar as Number
unsafe
MCMathEvalSqrtNumber(pOperand, tVar)
end unsafe
return tVar
end handler
--
/**
Summary: Converts the base of <Operand>
Operand: An expression that evaluates to a string.
Source: An expression that evaluates to an integer.
Returns: <Operand> converted to base 10.
Description:
Interprets a string in the desired base and converts it to decimal.
>*Note:* The source base must be an integer between 2 and 32.
Tags: Math
*/
syntax BaseConvertFrom is left binary operator with conversion precedence
<Operand: Expression> "converted" "from" "base" <Source: Expression>
begin
MCMathEvalConvertToBase10(Operand, Source, output)
end syntax
/**
Summary: Converts the base of <Operand>
Operand: An expression that evaluates to an integer.
Target: An expression that evaluates to an integer.
Returns: A string representation of <Operand> in base <Target>.
Description:
Converts a decimal into the desired base, and returns a string representation.
>*Note:* The destination base must be an integer between 2 and 32.
Tags: Math
*/
syntax BaseConvertTo is left binary operator with conversion precedence
<Operand: Expression> "converted" "to" "base" <Target: Expression>
begin
MCMathEvalConvertFromBase10(Operand, Target, output)
end syntax
/**
Summary: Converts the base of <Operand>
Operand: An expression that evaluates to a string.
Source: An expression that evaluates to an integer.
Target: An expression that evaluates to an integer.
Returns: A string representation <Operand> interpreted as a number in base <From>, converted to base <To>.
Description:
Interprets a string in the desired source base and converts it to the desired target base, and returns a string representation.
>*Note:* The source and destination bases must be integers between 2 and 32.
Tags: Math
*/
syntax BaseConvert is left binary operator with conversion precedence
<Operand: Expression> "converted" "from" "base" <Source: Expression> "to" "base" <Target: Expression>
begin
MCMathEvalConvertBase(Operand, Source, Target, output)
end syntax
--
end module