diff options
author | Juan Linietsky <reduzio@gmail.com> | 2016-07-26 17:24:34 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2016-07-26 17:25:10 -0300 |
commit | 9151eb591dcab408d3a7e4d9e3b3874c2e281acf (patch) | |
tree | 15531f24cc3ec63a13810bb442a68472f3f77de9 /core | |
parent | 8d4d167234e08fe0de74ea29814febd5b7a272f8 (diff) |
Changed the way the step decimals are computed to a safer way, fixes many issues.
Diffstat (limited to 'core')
-rw-r--r-- | core/math/math_funcs.cpp | 36 | ||||
-rw-r--r-- | core/math/math_funcs.h | 2 |
2 files changed, 21 insertions, 17 deletions
diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index 0fbd031214..64615fe6b4 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -206,25 +206,29 @@ double Math::ceil(double p_x) { return ::ceil(p_x); } -int Math::decimals(double p_step) { - - int max=4; - double llimit = Math::pow(0.1,max); - double ulimit = 1.0-llimit; - int i=0; - while( max) { - - float d = absf(p_step) - Math::floor(absf(p_step)); +int Math::step_decimals(double p_step) { + + static const int maxn=9; + static const double sd[maxn]={ + 0.9999, // somehow compensate for floating point error + 0.09999, + 0.009999, + 0.0009999, + 0.00009999, + 0.000009999, + 0.0000009999, + 0.00000009999, + 0.000000009999 + }; - if (d<llimit || d>ulimit) - break; - p_step*=10.0; - max--; - i++; + double as=absf(p_step); + for(int i=0;i<maxn;i++) { + if (as>=sd[i]) { + return i; + } } - return i; - + return maxn; } double Math::ease(double p_x, double p_c) { diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 2e1b9c989e..fc76d96b2e 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -66,7 +66,7 @@ public: static double floor(double p_x); static double ceil(double p_x); static double ease(double p_x, double p_c); - static int decimals(double p_step); + static int step_decimals(double p_step); static double stepify(double p_value,double p_step); static void seed(uint32_t x=0); static void randomize(); |