diff options
author | Aaron Franke <arnfranke@yahoo.com> | 2018-10-25 20:20:20 -0400 |
---|---|---|
committer | Aaron Franke <arnfranke@yahoo.com> | 2019-04-30 13:49:17 -0400 |
commit | 620ec4703fa8790ac01a8c1e05212d85776cf9b9 (patch) | |
tree | 3e90d3bcbda95a4f148367b0e6aced874e369c95 | |
parent | dd2cd06165670bb0d78bf4aa397935be15716e76 (diff) |
Make "decimal" functions more consistent
In GDScript, rename "decimals" to "step_decimals". In C#, add "StepDecimals", but keep the old functionality in a method called "DecimalCount".
-rw-r--r-- | core/math/expression.cpp | 7 | ||||
-rw-r--r-- | core/math/expression.h | 1 | ||||
-rw-r--r-- | doc/classes/@GDScript.xml | 23 | ||||
-rw-r--r-- | modules/gdscript/gdscript_functions.cpp | 16 | ||||
-rw-r--r-- | modules/gdscript/gdscript_functions.h | 1 | ||||
-rw-r--r-- | modules/mono/glue/Managed/Files/Mathf.cs | 29 | ||||
-rw-r--r-- | modules/mono/glue/Managed/Files/MathfEx.cs | 10 |
7 files changed, 73 insertions, 14 deletions
diff --git a/core/math/expression.cpp b/core/math/expression.cpp index 133dcc7ab9..079c9b524f 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -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); diff --git a/core/math/expression.h b/core/math/expression.h index f9075cb689..f20619f0b6 100644 --- a/core/math/expression.h +++ b/core/math/expression.h @@ -62,6 +62,7 @@ public: MATH_ISINF, MATH_EASE, MATH_DECIMALS, + MATH_STEP_DECIMALS, MATH_STEPIFY, MATH_LERP, MATH_INVERSE_LERP, diff --git a/doc/classes/@GDScript.xml b/doc/classes/@GDScript.xml index 22834a4c81..2f7f05b2e4 100644 --- a/doc/classes/@GDScript.xml +++ b/doc/classes/@GDScript.xml @@ -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> diff --git a/modules/gdscript/gdscript_functions.cpp b/modules/gdscript/gdscript_functions.cpp index 7552bc6bff..5ebcddfd7c 100644 --- a/modules/gdscript/gdscript_functions.cpp +++ b/modules/gdscript/gdscript_functions.cpp @@ -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: { diff --git a/modules/gdscript/gdscript_functions.h b/modules/gdscript/gdscript_functions.h index 0dbd172acf..c594480ff8 100644 --- a/modules/gdscript/gdscript_functions.h +++ b/modules/gdscript/gdscript_functions.h @@ -63,6 +63,7 @@ public: MATH_ISZEROAPPROX, MATH_EASE, MATH_DECIMALS, + MATH_STEP_DECIMALS, MATH_STEPIFY, MATH_LERP, MATH_INVERSE_LERP, diff --git a/modules/mono/glue/Managed/Files/Mathf.cs b/modules/mono/glue/Managed/Files/Mathf.cs index 947fbb6665..ff26c7fddf 100644 --- a/modules/mono/glue/Managed/Files/Mathf.cs +++ b/modules/mono/glue/Managed/Files/Mathf.cs @@ -79,14 +79,27 @@ namespace Godot 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) diff --git a/modules/mono/glue/Managed/Files/MathfEx.cs b/modules/mono/glue/Managed/Files/MathfEx.cs index d6eb65b097..b96f01bc2e 100644 --- a/modules/mono/glue/Managed/Files/MathfEx.cs +++ b/modules/mono/glue/Managed/Files/MathfEx.cs @@ -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); |