diff options
| author | Yuri Rubinsky <chaosus89@gmail.com> | 2023-03-02 12:46:25 +0300 |
|---|---|---|
| committer | Yuri Rubinsky <chaosus89@gmail.com> | 2023-03-03 16:43:48 +0300 |
| commit | d11bb866ffb1d5e0215ffc11dd6848a11976d90e (patch) | |
| tree | acd104ac51132aee6f9c2ebd3c99f452e95700ce /core/math | |
| parent | 1c1524a651e6d670d0d591b050d8c4bbb721d6e9 (diff) | |
| download | redot-engine-d11bb866ffb1d5e0215ffc11dd6848a11976d90e.tar.gz | |
Fix randfn to prevent generating of nan values
Diffstat (limited to 'core/math')
| -rw-r--r-- | core/math/random_pcg.h | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/core/math/random_pcg.h b/core/math/random_pcg.h index fe40e7f76a..cc22b23b70 100644 --- a/core/math/random_pcg.h +++ b/core/math/random_pcg.h @@ -126,10 +126,18 @@ public: } _FORCE_INLINE_ double randfn(double p_mean, double p_deviation) { - return p_mean + p_deviation * (cos(Math_TAU * randd()) * sqrt(-2.0 * log(randd()))); // Box-Muller transform + double temp = randd(); + if (temp < CMP_EPSILON) { + temp += CMP_EPSILON; // To prevent generating of INF value in log function, resulting to return NaN value from this function. + } + return p_mean + p_deviation * (cos(Math_TAU * randd()) * sqrt(-2.0 * log(temp))); // Box-Muller transform. } _FORCE_INLINE_ float randfn(float p_mean, float p_deviation) { - return p_mean + p_deviation * (cos((float)Math_TAU * randf()) * sqrt(-2.0 * log(randf()))); // Box-Muller transform + float temp = randf(); + if (temp < CMP_EPSILON) { + temp += CMP_EPSILON; // To prevent generating of INF value in log function, resulting to return NaN value from this function. + } + return p_mean + p_deviation * (cos((float)Math_TAU * randf()) * sqrt(-2.0 * log(temp))); // Box-Muller transform. } double random(double p_from, double p_to); |
