Skip to content

Commit 8692543

Browse files
author
runrevali
committed
Add inverse trigonometric functions
1 parent 74e6553 commit 8692543

3 files changed

Lines changed: 161 additions & 0 deletions

File tree

libscript/src/math.mlc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public foreign handler MCMathEvalCosNumber(in Operand as number, out Value as nu
4141
public foreign handler MCMathEvalTanReal(in Operand as double, out Value as double) as undefined binds to "<builtin>"
4242
public foreign handler MCMathEvalTanNumber(in Operand as number, out Value as number) as undefined binds to "<builtin>"
4343

44+
public foreign handler MCMathEvalAsinReal(in Operand as double, out Value as double) as undefined binds to "<builtin>"
45+
public foreign handler MCMathEvalAsinNumber(in Operand as number, out Value as number) as undefined binds to "<builtin>"
46+
47+
public foreign handler MCMathEvalAcosReal(in Operand as double, out Value as double) as undefined binds to "<builtin>"
48+
public foreign handler MCMathEvalAcosNumber(in Operand as number, out Value as number) as undefined binds to "<builtin>"
49+
50+
public foreign handler MCMathEvalAtanReal(in Operand as double, out Value as double) as undefined binds to "<builtin>"
51+
public foreign handler MCMathEvalAtanNumber(in Operand as number, out Value as number) as undefined binds to "<builtin>"
52+
53+
public foreign handler MCMathEvalAtan2Real(in First as double, in Second as double, out Value as double) as undefined binds to "<builtin>"
54+
public foreign handler MCMathEvalAtan2Number(in First as number, in Second as number, out Value as number) as undefined binds to "<builtin>"
55+
4456
public foreign handler MCMathEvalAbsInteger(in Operand as int, out Value as int) as undefined binds to "<builtin>"
4557
public foreign handler MCMathEvalAbsReal(in Operand as double, out Value as double) as undefined binds to "<builtin>"
4658
public foreign handler MCMathEvalAbsNumber(in Operand as number, out Value as number) as undefined binds to "<builtin>"
@@ -137,6 +149,79 @@ begin
137149
MCMathEvalTanNumber(Operand, output)
138150
end syntax
139151

152+
/*
153+
Summary: Arcsin operator.
154+
Operand: An expression that evaluates to a number.
155+
156+
Example:
157+
variable tVar as number
158+
put the arcsin of 1 into tVar -- tVar contains pi / 2
159+
160+
Tags: Math
161+
*/
162+
163+
syntax ArcsinOperator is prefix operator with precedence 1
164+
"the" "arcsin" "of" <Operand: Expression>
165+
begin
166+
MCMathEvalAsinNumber(Operand, output)
167+
end syntax
168+
169+
/*
170+
Summary: Arccos operator.
171+
Operand: An expression that evaluates to a number.
172+
173+
Example:
174+
variable tVar as number
175+
put the arccos of -1 into tVar -- tVar contains pi
176+
177+
Tags: Math
178+
*/
179+
180+
syntax ArccosOperator is prefix operator with precedence 1
181+
"the" "arccos" "of" <Operand: Expression>
182+
begin
183+
MCMathEvalAcosNumber(Operand, output)
184+
end syntax
185+
186+
/*
187+
Summary: Arctan operator.
188+
Operand: An expression that evaluates to a number.
189+
190+
Example:
191+
variable tVar as number
192+
put the arctan of 1 into tVar -- tVar contains pi / 4
193+
194+
Tags: Math
195+
*/
196+
197+
syntax ArctanOperator is prefix operator with precedence 1
198+
"the" "arctan" "of" <Operand: Expression>
199+
begin
200+
MCMathEvalAtanNumber(Operand, output)
201+
end syntax
202+
203+
/*
204+
Summary: Binary arctan operator.
205+
yCoord: An expression that evaluates to a number.
206+
xCoord: An expression that evaluates to a number.
207+
208+
Example:
209+
variable tVar as number
210+
put the arctan of -1,-1 into tVar --tVar contains −3π/4.
211+
212+
Description:
213+
The binary arctan operator returns the angle in radians between the x-axis and the line from the origin to the point (xCoord, yCoord).
214+
The angle returned has absolute value less than pi: −π < arctan2(y, x) ≤ π.
215+
216+
Tags: Math
217+
*/
218+
219+
syntax Arctan2Operator is prefix operator with precedence 1
220+
"the" "arctan2" "of" <yCoord: Expression> "," <xCoord: Expression>
221+
begin
222+
MCMathEvalAtan2Number(yCoord, xCoord, output)
223+
end syntax
224+
140225
--
141226

142227
/*

libscript/src/module-math.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,75 @@ extern "C" MC_DLLEXPORT void MCMathEvalTanNumber(MCNumberRef p_operand, MCNumber
150150
MCNumberCreateWithReal(t_result, r_output);
151151
}
152152

153+
extern "C" MC_DLLEXPORT void MCMathEvalAsinReal(double p_operand, double& r_output)
154+
{
155+
r_output = asin(p_operand);
156+
}
157+
158+
extern "C" MC_DLLEXPORT void MCMathEvalAsinNumber(MCNumberRef p_operand, MCNumberRef& r_output)
159+
{
160+
double t_operand;
161+
t_operand = MCNumberFetchAsReal(p_operand);
162+
163+
double t_result;
164+
MCMathEvalAsinReal(t_operand, t_result);
165+
166+
// if (!ctxt . HasError())
167+
MCNumberCreateWithReal(t_result, r_output);
168+
}
169+
170+
extern "C" MC_DLLEXPORT void MCMathEvalAcosReal(double p_operand, double& r_output)
171+
{
172+
r_output = acos(p_operand);
173+
}
174+
175+
extern "C" MC_DLLEXPORT void MCMathEvalAcosNumber(MCNumberRef p_operand, MCNumberRef& r_output)
176+
{
177+
double t_operand;
178+
t_operand = MCNumberFetchAsReal(p_operand);
179+
180+
double t_result;
181+
MCMathEvalAcosReal(t_operand, t_result);
182+
183+
// if (!ctxt . HasError())
184+
MCNumberCreateWithReal(t_result, r_output);
185+
}
186+
187+
extern "C" MC_DLLEXPORT void MCMathEvalAtanReal(double p_operand, double& r_output)
188+
{
189+
r_output = atan(p_operand);
190+
}
191+
192+
extern "C" MC_DLLEXPORT void MCMathEvalAtanNumber(MCNumberRef p_operand, MCNumberRef& r_output)
193+
{
194+
double t_operand;
195+
t_operand = MCNumberFetchAsReal(p_operand);
196+
197+
double t_result;
198+
MCMathEvalAtanReal(t_operand, t_result);
199+
200+
// if (!ctxt . HasError())
201+
MCNumberCreateWithReal(t_result, r_output);
202+
}
203+
204+
extern "C" MC_DLLEXPORT void MCMathEvalAtan2Real(double p_first, double p_second, double& r_output)
205+
{
206+
r_output = atan2(p_first, p_second);
207+
}
208+
209+
extern "C" MC_DLLEXPORT void MCMathEvalAtan2Number(MCNumberRef p_first, MCNumberRef p_second, MCNumberRef& r_output)
210+
{
211+
double t_first, t_second;
212+
t_first = MCNumberFetchAsReal(p_first);
213+
t_second = MCNumberFetchAsReal(p_second);
214+
215+
double t_result;
216+
MCMathEvalAtan2Real(t_first, t_second, t_result);
217+
218+
// if (!ctxt . HasError())
219+
MCNumberCreateWithReal(t_result, r_output);
220+
}
221+
153222
extern "C" MC_DLLEXPORT void MCMathEvalAbsInteger(integer_t p_operand, integer_t& r_output)
154223
{
155224
r_output = MCAbs(p_operand);

toolchain/lc-compile/test.mlc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ public handler testMath(inout xResults as list)
221221

222222
testLog("Math", "ConvertFromBaseToBase", ("1122" converted from base 4 to base 14) is "66", xResults)
223223

224+
// inverse trig functions
225+
testLog("Math", "Arcsin", the arcsin of 1 is pi / 2, xResults)
226+
testLog("Math", "Arccos", the arccos of -1 is pi, xResults)
227+
testLog("Math", "Arctan", the arctan of 1 is pi / 4, xResults)
228+
229+
testLog("Math", "Arctan2", the arctan2 of -1,-1 is -3 * pi / 4, xResults)
230+
224231
end handler
225232

226233
public handler testTypeConvert(inout xResults as list)

0 commit comments

Comments
 (0)