Skip to content

Commit 0c06f39

Browse files
committed
[[ StdMlc ]] Add math foundation module into std lib
1 parent 15235ad commit 0c06f39

5 files changed

Lines changed: 86 additions & 49 deletions

File tree

libscript/src/math-foundation.mlc

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
module com.livecode.mathfoundation
22

3-
--
4-
53
public foreign handler MCMathFoundationExecRoundRealToNearest(inout Target as double) as undefined binds to "<builtin>"
64
public foreign handler MCMathFoundationExecRoundNumberToNearest(inout Target as number) as undefined binds to "<builtin>"
7-
public foreign handler MCMathFoundationEvalFloorOfReal(in Target as double, out Value as double) as undefined binds to "<builtin>"
8-
public foreign handler MCMathFoundationEvalFloorOfNumber(in Target as number, out Value as number) as undefined binds to "<builtin>"
9-
public foreign handler MCMathFoundationEvalCeilOfReal(in Target as double, out Value as double) as undefined binds to "<builtin>"
10-
public foreign handler MCMathFoundationEvalCeilOfNumber(in Target as number, out Value as number) as undefined binds to "<builtin>"
5+
6+
public foreign handler MCMathFoundationEvalRoundedRealToNearest(in Target as double, out Value as double) as undefined binds to "<builtin>"
7+
public foreign handler MCMathFoundationEvalRoundedNumberToNearest(in Target as number, out Value as number) as undefined binds to "<builtin>"
8+
9+
public foreign handler MCMathFoundationEvalFloorReal(in Target as double, out Value as double) as undefined binds to "<builtin>"
10+
public foreign handler MCMathFoundationEvalFloorNumber(in Target as number, out Value as number) as undefined binds to "<builtin>"
11+
public foreign handler MCMathFoundationEvalCeilingReal(in Target as double, out Value as double) as undefined binds to "<builtin>"
12+
public foreign handler MCMathFoundationEvalCeilingNumber(in Target as number, out Value as number) as undefined binds to "<builtin>"
1113

1214
--
1315

14-
//public constant pi is 3.14159265358979323846
16+
//public constant pi = 3.14159265358979323846
1517

1618
--
1719

1820
/*
19-
Rounds towards zero for negative numbers.
21+
Summary: Rounds <Target> to the nearest integer.
22+
Target: An expression that evaluates to a numeric container.
2023

21-
Summary: Rounds <Target> to the nearest integer, i.e. the greatest integer less than
22-
or equal to <Target>, or the least integer greater than or equal to <Target>
23-
depending on which is closest.
24-
If <Target> is exactly halfway between two integers, rounds to the one with greatest
25-
absolute value.
26-
27-
Target: An expression that evaluates to a number.
24+
Description:
25+
Replaces <Target> with the greatest integer less than or equal to <Target>, or the least integer greater than or equal to <Target> depending on which is closest. If <Target> is exactly halfway between two integers, rounds to the one with greatest absolute value.
2826

2927
*/
3028

@@ -35,6 +33,23 @@ begin
3533
MCMathFoundationExecRoundNumberToNearest(Target)
3634
end syntax
3735

36+
/*
37+
Summary: Rounds <Target> to the nearest integer.
38+
Target: An expression that evaluates to a number.
39+
output: The rounded value of <Target>
40+
41+
Description:
42+
Return the greatest integer less than or equal to <Target>, or the least integer greater than or equal to <Target> depending on which is closest. If <Target> is exactly halfway between two integers, returns the one with greatest absolute value.
43+
44+
*/
45+
46+
syntax RoundedToNearest is prefix operator with precedence 1
47+
"the" "rounded" "of" <Target: Expression>
48+
begin
49+
MCMathFoundationEvalRoundedRealToNearest(Target, output)
50+
MCMathFoundationEvalRoundedNumberToNearest(Target, output)
51+
end syntax
52+
3853
--
3954

4055
/*
@@ -48,8 +63,8 @@ output: The greatest integer less than or equal to <Target>.
4863
syntax FloorOperator is prefix operator with precedence 1
4964
"the" "floor" "of" <Target: Expression>
5065
begin
51-
MCMathFoundationEvalFloorOfReal(Target, output)
52-
MCMathFoundationEvalFloorOfNumber(Target, output)
66+
MCMathFoundationEvalFloorReal(Target, output)
67+
MCMathFoundationEvalFloorNumber(Target, output)
5368
end syntax
5469

5570
/*
@@ -62,8 +77,8 @@ output: The least integer greater than or equal to <Target>.
6277
syntax CeilOperator is prefix operator with precedence 1
6378
"the" "ceiling" "of" <Target: Expression>
6479
begin
65-
MCMathFoundationEvalCeilOfReal(Target, output)
66-
MCMathFoundationEvalCeilOfNumber(Target, output)
80+
MCMathFoundationEvalCeilingReal(Target, output)
81+
MCMathFoundationEvalCeilingNumber(Target, output)
6782
end syntax
6883

6984
end module

libscript/src/module-math_foundation.cpp

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,54 @@
1515
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1616

1717
#include <foundation.h>
18+
#include <foundation-auto.h>
1819

19-
void MCMathFoundationExecRoundDownReal(double& x_target)
20-
{
21-
x_target = floor(x_target + 0.5);
22-
}
23-
24-
void MCMathFoundationExecRoundUpReal(double& x_target)
25-
{
26-
x_target = ceil(x_target - 0.5);
27-
}
28-
29-
extern "C" void MCMathFoundationExecRoundToNearestReal(double& x_target)
20+
extern "C" MC_DLLEXPORT void MCMathFoundationExecRoundRealToNearest(double& x_target)
3021
{
3122
if (x_target < 0.0)
3223
x_target = ceil(x_target - 0.5);
3324
else
3425
x_target = floor(x_target + 0.5);
3526
}
3627

37-
extern "C" void MCMathFoundationExecRoundDownNumber(MCNumberRef& x_target)
28+
extern "C" MC_DLLEXPORT void MCMathFoundationExecRoundNumberToNearest(MCNumberRef& x_target)
3829
{
3930
double t_target = MCNumberFetchAsReal(x_target);
40-
MCMathFoundationExecRoundDownReal(t_target);
31+
MCMathFoundationExecRoundRealToNearest(t_target);
32+
33+
MCAutoNumberRef t_new_number;
34+
if (!MCNumberCreateWithReal(t_target, &t_new_number))
35+
return;
4136

42-
MCValueRelease(x_target);
43-
MCNumberCreateWithReal(t_target, x_target);
37+
MCValueAssign(x_target, *t_new_number);
4438
}
4539

46-
extern "C" void MCMathFoundationExecRoundUpNumber(MCNumberRef& x_target)
40+
extern "C" MC_DLLEXPORT void MCMathFoundationEvalRoundedRealToNearest(double p_target, double& r_output)
4741
{
48-
double t_target = MCNumberFetchAsReal(x_target);
49-
MCMathFoundationExecRoundUpReal(t_target);
50-
51-
MCValueRelease(x_target);
52-
MCNumberCreateWithReal(t_target, x_target);
42+
MCMathFoundationExecRoundRealToNearest(p_target);
43+
r_output = p_target;
5344
}
5445

55-
extern "C" void MCMathFoundationExecRoundToNearestNumber(MCNumberRef& x_target)
46+
extern "C" MC_DLLEXPORT void MCMathFoundationEvalRoundedNumberToNearest(MCNumberRef p_target, MCNumberRef& r_output)
5647
{
57-
double t_target = MCNumberFetchAsReal(x_target);
58-
MCMathFoundationExecRoundToNearestReal(t_target);
48+
double t_target = MCNumberFetchAsReal(p_target);
49+
MCMathFoundationExecRoundRealToNearest(t_target);
5950

60-
MCValueRelease(x_target);
61-
MCNumberCreateWithReal(t_target, x_target);
51+
if (!MCNumberCreateWithReal(t_target, r_output))
52+
return;
6253
}
6354

64-
extern "C" void MCMathFoundationEvalFloorReal(double p_target, double& r_output)
55+
extern "C" MC_DLLEXPORT void MCMathFoundationEvalFloorReal(double p_target, double& r_output)
6556
{
6657
r_output = floor(p_target);
6758
}
6859

69-
extern "C" void MCMathFoundationEvalCeilingReal(double p_target, double& r_output)
60+
extern "C" MC_DLLEXPORT void MCMathFoundationEvalCeilingReal(double p_target, double& r_output)
7061
{
7162
r_output = ceil(p_target);
7263
}
7364

74-
extern "C" void MCMathFoundationEvalFloorNumber(MCNumberRef p_target, MCNumberRef& r_output)
65+
extern "C" MC_DLLEXPORT void MCMathFoundationEvalFloorNumber(MCNumberRef p_target, MCNumberRef& r_output)
7566
{
7667
double t_target = MCNumberFetchAsReal(p_target);
7768
MCMathFoundationEvalFloorReal(t_target, t_target);
@@ -80,7 +71,7 @@ extern "C" void MCMathFoundationEvalFloorNumber(MCNumberRef p_target, MCNumberRe
8071
return;
8172
}
8273

83-
void MCMathFoundationEvalCeilingNumber(MCNumberRef p_target, MCNumberRef& r_output)
74+
extern "C" MC_DLLEXPORT void MCMathFoundationEvalCeilingNumber(MCNumberRef p_target, MCNumberRef& r_output)
8475
{
8576
double t_target = MCNumberFetchAsReal(p_target);
8677
MCMathFoundationEvalCeilingReal(t_target, t_target);

toolchain/lc-compile/lc-compile.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,7 @@
849849
"$(SRCROOT)/../../engine/src/canvas.mlc",
850850
"$(SRCROOT)/../../engine/src/widget.mlc",
851851
"$(SRCROOT)/../../libscript/src/math.mlc",
852+
"$(SRCROOT)/../../libscript/src/math-foundation.mlc",
852853
);
853854
name = Bootstrap;
854855
outputPaths = (

toolchain/lc-compile/src/module-helper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern builtin_module_descriptor __com_livecode_line_module_info;
3636
extern builtin_module_descriptor __com_livecode_list_module_info;
3737
extern builtin_module_descriptor __com_livecode_logic_module_info;
3838
extern builtin_module_descriptor __com_livecode_math_module_info;
39+
extern builtin_module_descriptor __com_livecode_mathfoundation_module_info;
3940
extern builtin_module_descriptor __com_livecode_segmentchunk_module_info;
4041
extern builtin_module_descriptor __com_livecode_sort_module_info;
4142
extern builtin_module_descriptor __com_livecode_string_module_info;
@@ -56,6 +57,7 @@ builtin_module_descriptor* g_builtin_modules[] =
5657
&__com_livecode_list_module_info,
5758
&__com_livecode_logic_module_info,
5859
&__com_livecode_math_module_info,
60+
&__com_livecode_mathfoundation_module_info,
5961
//&__com_livecode_segmentchunk_module_info,
6062
&__com_livecode_sort_module_info,
6163
&__com_livecode_string_module_info,
@@ -74,6 +76,7 @@ extern void (*MCCharEvalNumberOfCharsIn)();
7476
extern void (*MCListEvalHeadOf)();
7577
extern void (*MCLogicEvalNot)();
7678
extern void (*MCMathEvalRealToPowerOfReal)();
79+
extern void (*MCMathFoundationExecRoundRealToNearest)();
7780
extern void (*MCSortExecSortListAscendingText)();
7881
extern void (*MCStringEvalConcatenate)();
7982
extern void (*MCTypeConvertExecSplitStringByDelimiter)();
@@ -90,6 +93,7 @@ void *g_builtin_ptrs[] =
9093
&MCListEvalHeadOf,
9194
&MCLogicEvalNot,
9295
&MCMathEvalRealToPowerOfReal,
96+
&MCMathFoundationExecRoundRealToNearest,
9397
&MCSortExecSortListAscendingText,
9498
&MCStringEvalConcatenate,
9599
&MCTypeConvertExecSplitStringByDelimiter

toolchain/lc-compile/test.mlc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public handler test()
4343
--com.livecode.math
4444
testMath(tResults)
4545

46+
--com.livecode.mathfoundation
47+
testMathFoundation(tResults)
48+
4649
--com.livecode.sort
4750
testSort(tResults)
4851

@@ -88,6 +91,29 @@ public handler testType(inout xResults as list)
8891

8992
end handler
9093

94+
public handler testMathFoundation(inout xResults as list)
95+
96+
variable tNum as number
97+
variable tNumReal as double
98+
put -5.5 into tNum
99+
put 5.5 into tNumReal
100+
101+
testLog("MathFoundation", "FloorNum", the floor of tNum is -6, xResults)
102+
testLog("MathFoundation", "FloorReal", the floor of tNum is 5, xResults)
103+
104+
testLog("MathFoundation", "CeilNum", the ceiling of tNum is -5, xResults)
105+
testLog("MathFoundation", "CeilReal", the ceiling of tNum is 6, xResults)
106+
107+
round tNum
108+
round tNumReal
109+
110+
testLog("MathFoundation", "RoundToNearestNum", tNum is -6, xResults)
111+
testLog("MathFoundation", "RoundToNearestReal", tNumReal is 6, xResults)
112+
113+
testLog("MathFoundation", "RoundedToNearest", the rounded of 4.5 is 5, xResults)
114+
115+
end handler
116+
91117
public handler testMath(inout xResults as list)
92118

93119
testLog("Math", "PowerOf", 3 ^ 4 = 81, xResults)

0 commit comments

Comments
 (0)