Skip to content

Commit

Permalink
Merge pull request #21425 from aaronfranke/decimal-functions
Browse files Browse the repository at this point in the history
Make "decimal" functions more consistent
  • Loading branch information
akien-mga authored May 1, 2019
2 parents a8194ea + 620ec47 commit 8afc9c3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 14 deletions.
7 changes: 7 additions & 0 deletions core/math/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const char *Expression::func_name[Expression::FUNC_MAX] = {
"is_inf",
"ease",
"decimals",
"step_decimals",
"stepify",
"lerp",
"inverse_lerp",
Expand Down Expand Up @@ -149,6 +150,7 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) {
case MATH_ISNAN:
case MATH_ISINF:
case MATH_DECIMALS:
case MATH_STEP_DECIMALS:
case MATH_SEED:
case MATH_RANDSEED:
case MATH_DEG2RAD:
Expand Down Expand Up @@ -365,6 +367,11 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
VALIDATE_ARG_NUM(0);
*r_return = Math::step_decimals((double)*p_inputs[0]);
} break;
case MATH_STEP_DECIMALS: {

VALIDATE_ARG_NUM(0);
*r_return = Math::step_decimals((double)*p_inputs[0]);
} break;
case MATH_STEPIFY: {

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 @@ -62,6 +62,7 @@ class Expression : public Reference {
MATH_ISINF,
MATH_EASE,
MATH_DECIMALS,
MATH_STEP_DECIMALS,
MATH_STEPIFY,
MATH_LERP,
MATH_INVERSE_LERP,
Expand Down
23 changes: 18 additions & 5 deletions doc/classes/@GDScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,7 @@
<argument index="0" name="step" type="float">
</argument>
<description>
Returns the position of the first non-zero digit, after the decimal point.
[codeblock]
# n is 2
n = decimals(0.035)
[/codeblock]
Deprecated alias for "[method step_decimals]".
</description>
</method>
<method name="dectime">
Expand Down Expand Up @@ -1023,6 +1019,23 @@
[/codeblock]
</description>
</method>
<method name="step_decimals">
<return type="float">
</return>
<argument index="0" name="step" type="float">
</argument>
<description>
Returns the position of the first non-zero digit, after the decimal point.
[codeblock]
# n is 0
n = step_decimals(5)
# n is 4
n = step_decimals(1.0005)
# n is 9
n = step_decimals(0.000000005)
[/codeblock]
</description>
</method>
<method name="stepify">
<return type="float">
</return>
Expand Down
16 changes: 15 additions & 1 deletion modules/gdscript/gdscript_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
"is_zero_approx",
"ease",
"decimals",
"step_decimals",
"stepify",
"lerp",
"inverse_lerp",
Expand Down Expand Up @@ -339,6 +340,13 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
VALIDATE_ARG_COUNT(1);
VALIDATE_ARG_NUM(0);
r_ret = Math::step_decimals((double)*p_args[0]);
ERR_EXPLAIN("GDScript method 'decimals' is deprecated and has been renamed to 'step_decimals', please update your code accordingly.");
WARN_DEPRECATED
} break;
case MATH_STEP_DECIMALS: {
VALIDATE_ARG_COUNT(1);
VALIDATE_ARG_NUM(0);
r_ret = Math::step_decimals((double)*p_args[0]);
} break;
case MATH_STEPIFY: {
VALIDATE_ARG_COUNT(2);
Expand Down Expand Up @@ -1452,6 +1460,7 @@ bool GDScriptFunctions::is_deterministic(Function p_func) {
case MATH_ISINF:
case MATH_EASE:
case MATH_DECIMALS:
case MATH_STEP_DECIMALS:
case MATH_STEPIFY:
case MATH_LERP:
case MATH_INVERSE_LERP:
Expand Down Expand Up @@ -1626,7 +1635,12 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
} break;
case MATH_DECIMALS: {
MethodInfo mi("decimals", PropertyInfo(Variant::REAL, "step"));
mi.return_val.type = Variant::REAL;
mi.return_val.type = Variant::INT;
return mi;
} break;
case MATH_STEP_DECIMALS: {
MethodInfo mi("step_decimals", PropertyInfo(Variant::REAL, "step"));
mi.return_val.type = Variant::INT;
return mi;
} break;
case MATH_STEPIFY: {
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 @@ -63,6 +63,7 @@ class GDScriptFunctions {
MATH_ISZEROAPPROX,
MATH_EASE,
MATH_DECIMALS,
MATH_STEP_DECIMALS,
MATH_STEPIFY,
MATH_LERP,
MATH_INVERSE_LERP,
Expand Down
29 changes: 21 additions & 8 deletions modules/mono/glue/Managed/Files/Mathf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,27 @@ public static real_t Cosh(real_t s)
return (real_t)Math.Cosh(s);
}

public static int Decimals(real_t step)
{
return Decimals((decimal)step);
}

public static int Decimals(decimal step)
{
return BitConverter.GetBytes(decimal.GetBits(step)[3])[2];
public static int StepDecimals(real_t step)
{
double[] sd = new double[] {
0.9999,
0.09999,
0.009999,
0.0009999,
0.00009999,
0.000009999,
0.0000009999,
0.00000009999,
0.000000009999,
};
double abs = Mathf.Abs(step);
double decs = abs - (int)abs; // Strip away integer part
for (int i = 0; i < sd.Length; i++) {
if (decs >= sd[i]) {
return i;
}
}
return 0;
}

public static real_t Deg2Rad(real_t deg)
Expand Down
10 changes: 10 additions & 0 deletions modules/mono/glue/Managed/Files/MathfEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public static partial class Mathf
public const real_t Epsilon = 1e-06f;
#endif

public static int DecimalCount(real_t s)
{
return DecimalCount((decimal)s);
}

public static int DecimalCount(decimal s)
{
return BitConverter.GetBytes(decimal.GetBits(s)[3])[2];
}

public static int CeilToInt(real_t s)
{
return (int)Math.Ceiling(s);
Expand Down

0 comments on commit 8afc9c3

Please sign in to comment.