From deddacd6f725ff9f23b5fc476a70c9745d06b031 Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Tue, 8 Dec 2020 20:47:42 +0000 Subject: [PATCH] Fix some Math warnings --- include/core/Defs.hpp | 7 +++++-- include/core/Math.hpp | 26 ++++++++++++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/core/Defs.hpp b/include/core/Defs.hpp index 67e32796..e90f6ceb 100644 --- a/include/core/Defs.hpp +++ b/include/core/Defs.hpp @@ -70,9 +70,12 @@ enum class Error { typedef float real_t; -#define CMP_EPSILON 0.00001 +// This epsilon should match the one used by Godot for consistency. +// Using `f` when `real_t` is float. +#define CMP_EPSILON 0.00001f #define CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON) -#define Math_PI 3.14159265358979323846 + +#define Math_PI 3.1415926535897932384626433833 #define Math_TAU 6.2831853071795864769252867666 #define _PLANE_EQ_DOT_EPSILON 0.999 diff --git a/include/core/Math.hpp b/include/core/Math.hpp index 4d0d32eb..a971bb8e 100644 --- a/include/core/Math.hpp +++ b/include/core/Math.hpp @@ -107,21 +107,21 @@ inline T max(T a, T b) { template inline T sign(T x) { - return x < 0 ? -1 : 1; + return static_cast(x < 0 ? -1 : 1); } inline double deg2rad(double p_y) { return p_y * Math_PI / 180.0; } inline float deg2rad(float p_y) { - return p_y * Math_PI / 180.0; + return p_y * static_cast(Math_PI) / 180.f; } inline double rad2deg(double p_y) { return p_y * 180.0 / Math_PI; } inline float rad2deg(float p_y) { - return p_y * 180.0 / Math_PI; + return p_y * 180.f / static_cast(Math_PI); } inline double inverse_lerp(double p_from, double p_to, double p_value) { @@ -165,7 +165,7 @@ inline bool is_zero_approx(real_t s) { } inline double smoothstep(double p_from, double p_to, double p_weight) { - if (is_equal_approx(p_from, p_to)) { + if (is_equal_approx(static_cast(p_from), static_cast(p_to))) { return p_from; } double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0); @@ -205,7 +205,7 @@ inline double round(double p_val) { return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5); } inline float round(float p_val) { - return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5); + return (p_val >= 0) ? floor(p_val + 0.5f) : -floor(-p_val + 0.5f); } inline int64_t wrapi(int64_t value, int64_t min, int64_t max) { @@ -213,16 +213,18 @@ inline int64_t wrapi(int64_t value, int64_t min, int64_t max) { return range == 0 ? min : min + ((((value - min) % range) + range) % range); } -inline double wrapf(double value, double min, double max) { - double range = max - min; - return is_zero_approx(range) ? min : value - (range * floor((value - min) / range)); -} -inline float wrapf(float value, float min, float max) { - float range = max - min; +inline float wrapf(real_t value, real_t min, real_t max) { + const real_t range = max - min; return is_zero_approx(range) ? min : value - (range * floor((value - min) / range)); } -inline real_t stepify(real_t p_value, real_t p_step) { +inline float stepify(float p_value, float p_step) { + if (p_step != 0) { + p_value = floor(p_value / p_step + 0.5f) * p_step; + } + return p_value; +} +inline double stepify(double p_value, double p_step) { if (p_step != 0) { p_value = floor(p_value / p_step + 0.5) * p_step; }