diff options
Diffstat (limited to 'core/math/math_funcs.cpp')
| -rw-r--r-- | core/math/math_funcs.cpp | 182 |
1 files changed, 29 insertions, 153 deletions
diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index 0fbd03121..8353aa0eb 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -5,7 +5,7 @@ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -29,7 +29,7 @@ #include "math_funcs.h" #include "core/os/os.h" -#include <math.h> + #include "float.h" uint32_t Math::default_seed=1; @@ -41,37 +41,16 @@ static uint32_t Q[4096]; #endif uint32_t Math::rand_from_seed(uint32_t *seed) { - -#if 1 - uint32_t k; - uint32_t s = (*seed); - if (s == 0) - s = 0x12345987; - k = s / 127773; - s = 16807 * (s - k * 127773) - 2836 * k; -// if (s < 0) -// s += 2147483647; - (*seed) = s; - return (s & Math::RANDOM_MAX); -#else - *seed = *seed * 1103515245 + 12345; - return (*seed % ((unsigned int)RANDOM_MAX + 1)); -#endif + // Xorshift31 PRNG + if ( *seed == 0 ) *seed = Math::RANDOM_MAX; + (*seed) ^= (*seed) << 13; + (*seed) ^= (*seed) >> 17; + (*seed) ^= (*seed) << 5; + return (*seed) & Math::RANDOM_MAX; } void Math::seed(uint32_t x) { -#if 0 - int i; - - Q[0] = x; - Q[1] = x + PHI; - Q[2] = x + PHI + PHI; - - for (i = 3; i < 4096; i++) - Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; -#else default_seed=x; -#endif } void Math::randomize() { @@ -82,56 +61,14 @@ void Math::randomize() { uint32_t Math::rand() { - return rand_from_seed(&default_seed)&0x7FFFFFFF; + return rand_from_seed(&default_seed); } double Math::randf() { - return (double)rand() / (double)RANDOM_MAX; -} - -double Math::sin(double p_x) { - - return ::sin(p_x); - -} - -double Math::cos(double p_x) { - - return ::cos(p_x); - -} - -double Math::tan(double p_x) { - - return ::tan(p_x); - + return (double)rand() / (double)Math::RANDOM_MAX; } -double Math::sinh(double p_x) { - return ::sinh(p_x); -} - -double Math::cosh(double p_x) { - - return ::cosh(p_x); -} - -double Math::tanh(double p_x) { - - return ::tanh(p_x); -} - - -double Math::deg2rad(double p_y) { - - return p_y*Math_PI/180.0; -} - -double Math::rad2deg(double p_y) { - - return p_y*180.0/Math_PI; -} double Math::round(double p_val) { @@ -143,22 +80,6 @@ double Math::round(double p_val) { } } -double Math::asin(double p_x) { - - return ::asin(p_x); - -} - -double Math::acos(double p_x) { - - return ::acos(p_x); -} - -double Math::atan(double p_x) { - - return ::atan(p_x); -} - double Math::dectime(double p_value,double p_amount, double p_step) { float sgn = p_value < 0 ? -1.0 : 1.0; @@ -169,62 +90,30 @@ double Math::dectime(double p_value,double p_amount, double p_step) { return val*sgn; } -double Math::atan2(double p_y, double p_x) { - - return ::atan2(p_y,p_x); - -} -double Math::sqrt(double p_x) { - - return ::sqrt(p_x); -} -double Math::fmod(double p_x,double p_y) { - - return ::fmod(p_x,p_y); -} - -double Math::fposmod(double p_x,double p_y) { - - if (p_x>=0) { - - return Math::fmod(p_x,p_y); - - } else { - - return p_y-Math::fmod(-p_x,p_y); - } +int Math::step_decimals(double p_step) { -} -double Math::floor(double p_x) { - - return ::floor(p_x); -} - -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)); + 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) { @@ -261,20 +150,7 @@ double Math::stepify(double p_value,double p_step) { return p_value; } -bool Math::is_nan(double p_val) { - - return (p_val!=p_val); -} -bool Math::is_inf(double p_val) { - -#ifdef _MSC_VER - return !_finite(p_val); -#else - return isinf(p_val); -#endif - -} uint32_t Math::larger_prime(uint32_t p_val) { |
