Merge pull request #21425 from aaronfranke/decimal-functions

Make "decimal" functions more consistent
This commit is contained in:
Rémi Verschelde 2019-05-01 08:16:10 +02:00 committed by GitHub
commit 8afc9c3938
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 13 deletions

View File

@ -64,6 +64,7 @@ const char *Expression::func_name[Expression::FUNC_MAX] = {
"is_inf",
"ease",
"decimals",
"step_decimals",
"stepify",
"lerp",
"inverse_lerp",
@ -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:
@ -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);

View File

@ -62,6 +62,7 @@ public:
MATH_ISINF,
MATH_EASE,
MATH_DECIMALS,
MATH_STEP_DECIMALS,
MATH_STEPIFY,
MATH_LERP,
MATH_INVERSE_LERP,

View File

@ -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">
@ -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>

View File

@ -72,6 +72,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
"is_zero_approx",
"ease",
"decimals",
"step_decimals",
"stepify",
"lerp",
"inverse_lerp",
@ -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);
@ -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:
@ -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: {

View File

@ -63,6 +63,7 @@ public:
MATH_ISZEROAPPROX,
MATH_EASE,
MATH_DECIMALS,
MATH_STEP_DECIMALS,
MATH_STEPIFY,
MATH_LERP,
MATH_INVERSE_LERP,

View File

@ -79,14 +79,27 @@ namespace Godot
return (real_t)Math.Cosh(s);
}
public static int Decimals(real_t step)
public static int StepDecimals(real_t step)
{
return Decimals((decimal)step);
}
public static int Decimals(decimal step)
{
return BitConverter.GetBytes(decimal.GetBits(step)[3])[2];
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)

View File

@ -21,6 +21,16 @@ namespace Godot
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);