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 /modules/mono/glue/Managed | |
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".
Diffstat (limited to 'modules/mono/glue/Managed')
-rw-r--r-- | modules/mono/glue/Managed/Files/Mathf.cs | 29 | ||||
-rw-r--r-- | modules/mono/glue/Managed/Files/MathfEx.cs | 10 |
2 files changed, 31 insertions, 8 deletions
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); |