diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2019-05-01 08:16:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-01 08:16:10 +0200 |
commit | 8afc9c3938af385be17aab92ec5c4adcbfb0781a (patch) | |
tree | 466c4f416ce27811fa7f6f0d8d31c9c396895e8c /modules/mono | |
parent | a8194eaef30a32a5d8595fa16b279924ce184cc9 (diff) | |
parent | 620ec4703fa8790ac01a8c1e05212d85776cf9b9 (diff) |
Merge pull request #21425 from aaronfranke/decimal-functions
Make "decimal" functions more consistent
Diffstat (limited to 'modules/mono')
-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); |