Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit d7b3ceb

Browse files
committed
[[ Bug 20079 ]] Add square root operator to math lib
1 parent a3bea84 commit d7b3ceb

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

docs/notes/bugfix-20079.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Add `the square root of <number>` to the math library

libscript/src/math.lcb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public foreign handler MCMathEvalMaxNumber(in Left as Number, in Right as Number
7676

7777
public foreign handler MCMathEvalRandomReal(out Value as CDouble) returns nothing binds to "<builtin>"
7878

79+
public foreign handler MCMathEvalSqrtReal(in Operand as CDouble, out Value as CDouble) returns nothing binds to "<builtin>"
80+
public foreign handler MCMathEvalSqrtNumber(in Operand as Number, out Value as Number) returns nothing binds to "<builtin>"
81+
7982
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>"
8083
public foreign handler MCMathEvalConvertToBase10(in Operand as String, in Source as LCInt, out Value as LCInt) returns nothing binds to "<builtin>"
8184
public foreign handler MCMathEvalConvertFromBase10(in Operand as LCInt, in Target as LCInt, out Value as String) returns nothing binds to "<builtin>"
@@ -565,6 +568,42 @@ end syntax
565568

566569
--
567570

571+
/**
572+
Syntax: sqrt(<Operand>)
573+
Summary: Square root operator.
574+
Operand: An expression that evaluates to a number.
575+
576+
Example:
577+
variable tVar as Number
578+
put sqrt(9) into tVar -- tVar contains 3
579+
580+
Example:
581+
variable tVar as Number
582+
put the square root of 4 into tVar -- tVar contains 2
583+
584+
Description:
585+
The square root of a number is the number which must be
586+
squared to obtain number : sqrt(number)^2 is equal to number.
587+
588+
Tags: Math
589+
*/
590+
591+
syntax SquareRootOperator is prefix operator with function chunk precedence
592+
"the" "square" "root" "of" <Operand: Expression>
593+
begin
594+
MCMathEvalSqrtNumber(Operand, output)
595+
end syntax
596+
597+
public handler Sqrt(in pOperand as Number) returns Number
598+
variable tVar as Number
599+
unsafe
600+
MCMathEvalSqrtNumber(pOperand, tVar)
601+
end unsafe
602+
return tVar
603+
end handler
604+
605+
--
606+
568607
/**
569608
Summary: Converts the base of <Operand>
570609
Operand: An expression that evaluates to a string.

libscript/src/module-math.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,22 @@ extern "C" MC_DLLEXPORT_DEF void MCMathEvalRandomReal(double& r_output)
407407
r_output = MCSRandomReal();
408408
}
409409

410+
extern "C" MC_DLLEXPORT_DEF void MCMathEvalSqrtReal(double p_operand, double& r_output)
411+
{
412+
r_output = sqrt(p_operand);
413+
}
414+
415+
extern "C" MC_DLLEXPORT_DEF void MCMathEvalSqrtNumber(MCNumberRef p_operand, MCNumberRef& r_output)
416+
{
417+
double t_operand;
418+
t_operand = MCNumberFetchAsReal(p_operand);
419+
420+
double t_result;
421+
MCMathEvalSqrtReal(t_operand, t_result);
422+
423+
MCNumberCreateWithReal(t_result, r_output);
424+
}
425+
410426
////////////////////////////////////////////////////////////////////////////////////////////////////
411427

412428
extern "C" MC_DLLEXPORT_DEF void MCMathEvalConvertToBase10(MCStringRef p_operand, integer_t p_source_base, integer_t& r_output)

0 commit comments

Comments
 (0)