Skip to content

Commit

Permalink
Add integer posmod and rename default arg names
Browse files Browse the repository at this point in the history
"posmod" is the integer version of "fposmod". We do not need a "mod" because of the % operator.

I changed the default arg names from "x" and "y" to "a" and "b" because they are not coordinates. I also changed pow's arg names to "base" and "exp". Also, I reorganized the code in the VS built-in funcs switch statement.
  • Loading branch information
aaronfranke committed Jul 18, 2019
1 parent 20a3bb9 commit a60f242
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 111 deletions.
8 changes: 8 additions & 0 deletions core/math/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const char *Expression::func_name[Expression::FUNC_MAX] = {
"sqrt",
"fmod",
"fposmod",
"posmod",
"floor",
"ceil",
"round",
Expand Down Expand Up @@ -175,6 +176,7 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) {
case MATH_ATAN2:
case MATH_FMOD:
case MATH_FPOSMOD:
case MATH_POSMOD:
case MATH_POW:
case MATH_EASE:
case MATH_STEPIFY:
Expand Down Expand Up @@ -283,6 +285,12 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
VALIDATE_ARG_NUM(1);
*r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]);
} break;
case MATH_POSMOD: {

VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::posmod((int)*p_inputs[0], (int)*p_inputs[1]);
} break;
case MATH_FLOOR: {

VALIDATE_ARG_NUM(0);
Expand Down
1 change: 1 addition & 0 deletions core/math/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Expression : public Reference {
MATH_SQRT,
MATH_FMOD,
MATH_FPOSMOD,
MATH_POSMOD,
MATH_FLOOR,
MATH_CEIL,
MATH_ROUND,
Expand Down
7 changes: 7 additions & 0 deletions core/math/math_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ class Math {
value += 0.0;
return value;
}
static _ALWAYS_INLINE_ int posmod(int p_x, int p_y) {
int value = p_x % p_y;
if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) {
value += p_y;
}
return value;
}

static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * Math_PI / 180.0; }
static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * Math_PI / 180.0; }
Expand Down
71 changes: 52 additions & 19 deletions modules/gdscript/doc_classes/@GDScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -345,45 +345,78 @@
<method name="fmod">
<return type="float">
</return>
<argument index="0" name="x" type="float">
<argument index="0" name="a" type="float">
</argument>
<argument index="1" name="y" type="float">
<argument index="1" name="b" type="float">
</argument>
<description>
Returns the floating-point remainder of [code]x/y[/code].
Returns the floating-point remainder of [code]a/b[/code], keeping the sign of [code]a[/code].
[codeblock]
# Remainder is 1.5
var remainder = fmod(7, 5.5)
[/codeblock]
For the integer remainder operation, use the % operator.
</description>
</method>
<method name="fposmod">
<return type="float">
</return>
<argument index="0" name="x" type="float">
<argument index="0" name="a" type="float">
</argument>
<argument index="1" name="y" type="float">
<argument index="1" name="b" type="float">
</argument>
<description>
Returns the floating-point modulus of [code]a/b[/code] that wraps equally in positive and negative.
[codeblock]
var i = -6
while i &lt; 5:
prints(i, fposmod(i, 3))
i += 1
[/codeblock]
Produces:
[codeblock]
-6 0
-5 1
-4 2
-3 0
-2 1
-1 2
0 0
1 1
2 2
3 0
4 1
[/codeblock]
</description>
</method>
<method name="posmod">
<return type="int">
</return>
<argument index="0" name="a" type="int">
</argument>
<argument index="1" name="b" type="int">
</argument>
<description>
Returns the floating-point remainder of [code]x/y[/code] that wraps equally in positive and negative.
Returns the integer modulus of [code]a/b[/code] that wraps equally in positive and negative.
[codeblock]
var i = -10
while i &lt; 0:
prints(i, fposmod(i, 10))
var i = -6
while i &lt; 5:
prints(i, posmod(i, 3))
i += 1
[/codeblock]
Produces:
[codeblock]
-10 10
-9 1
-8 2
-7 3
-6 4
-5 5
-4 6
-3 7
-2 8
-1 9
-6 0
-5 1
-4 2
-3 0
-2 1
-1 2
0 0
1 1
2 2
3 0
4 1
[/codeblock]
</description>
</method>
Expand Down
19 changes: 16 additions & 3 deletions modules/gdscript/gdscript_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
"sqrt",
"fmod",
"fposmod",
"posmod",
"floor",
"ceil",
"round",
Expand Down Expand Up @@ -243,6 +244,12 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
VALIDATE_ARG_NUM(1);
r_ret = Math::fposmod((double)*p_args[0], (double)*p_args[1]);
} break;
case MATH_POSMOD: {
VALIDATE_ARG_COUNT(2);
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
r_ret = Math::posmod((int)*p_args[0], (int)*p_args[1]);
} break;
case MATH_FLOOR: {
VALIDATE_ARG_COUNT(1);
VALIDATE_ARG_NUM(0);
Expand Down Expand Up @@ -1456,6 +1463,7 @@ bool GDScriptFunctions::is_deterministic(Function p_func) {
case MATH_SQRT:
case MATH_FMOD:
case MATH_FPOSMOD:
case MATH_POSMOD:
case MATH_FLOOR:
case MATH_CEIL:
case MATH_ROUND:
Expand Down Expand Up @@ -1568,15 +1576,20 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
return mi;
} break;
case MATH_FMOD: {
MethodInfo mi("fmod", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
MethodInfo mi("fmod", PropertyInfo(Variant::REAL, "a"), PropertyInfo(Variant::REAL, "b"));
mi.return_val.type = Variant::REAL;
return mi;
} break;
case MATH_FPOSMOD: {
MethodInfo mi("fposmod", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
MethodInfo mi("fposmod", PropertyInfo(Variant::REAL, "a"), PropertyInfo(Variant::REAL, "b"));
mi.return_val.type = Variant::REAL;
return mi;
} break;
case MATH_POSMOD: {
MethodInfo mi("posmod", PropertyInfo(Variant::INT, "a"), PropertyInfo(Variant::INT, "b"));
mi.return_val.type = Variant::INT;
return mi;
} break;
case MATH_FLOOR: {
MethodInfo mi("floor", PropertyInfo(Variant::REAL, "s"));
mi.return_val.type = Variant::REAL;
Expand All @@ -1603,7 +1616,7 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
return mi;
} break;
case MATH_POW: {
MethodInfo mi("pow", PropertyInfo(Variant::REAL, "x"), PropertyInfo(Variant::REAL, "y"));
MethodInfo mi("pow", PropertyInfo(Variant::REAL, "base"), PropertyInfo(Variant::REAL, "exp"));
mi.return_val.type = Variant::REAL;
return mi;
} break;
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class GDScriptFunctions {
MATH_SQRT,
MATH_FMOD,
MATH_FPOSMOD,
MATH_POSMOD,
MATH_FLOOR,
MATH_CEIL,
MATH_ROUND,
Expand Down
Loading

0 comments on commit a60f242

Please sign in to comment.