Merge pull request #479 from Zylann/fix_math_warnings

Fix some Math warnings
pull/500/head
Marc 2021-01-31 19:58:10 +00:00 committed by GitHub
commit ffcdef965e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 14 deletions

View File

@ -70,9 +70,12 @@ enum class Error {
typedef float real_t; 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 CMP_EPSILON2 (CMP_EPSILON * CMP_EPSILON)
#define Math_PI 3.14159265358979323846
#define Math_PI 3.1415926535897932384626433833
#define Math_TAU 6.2831853071795864769252867666 #define Math_TAU 6.2831853071795864769252867666
#define _PLANE_EQ_DOT_EPSILON 0.999 #define _PLANE_EQ_DOT_EPSILON 0.999

View File

@ -107,21 +107,21 @@ inline T max(T a, T b) {
template <typename T> template <typename T>
inline T sign(T x) { inline T sign(T x) {
return x < 0 ? -1 : 1; return static_cast<T>(x < 0 ? -1 : 1);
} }
inline double deg2rad(double p_y) { inline double deg2rad(double p_y) {
return p_y * Math_PI / 180.0; return p_y * Math_PI / 180.0;
} }
inline float deg2rad(float p_y) { inline float deg2rad(float p_y) {
return p_y * Math_PI / 180.0; return p_y * static_cast<float>(Math_PI) / 180.f;
} }
inline double rad2deg(double p_y) { inline double rad2deg(double p_y) {
return p_y * 180.0 / Math_PI; return p_y * 180.0 / Math_PI;
} }
inline float rad2deg(float p_y) { inline float rad2deg(float p_y) {
return p_y * 180.0 / Math_PI; return p_y * 180.f / static_cast<float>(Math_PI);
} }
inline double inverse_lerp(double p_from, double p_to, double p_value) { 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) { 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<real_t>(p_from), static_cast<real_t>(p_to))) {
return p_from; return p_from;
} }
double x = clamp((p_weight - p_from) / (p_to - p_from), 0.0, 1.0); 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); return (p_val >= 0) ? floor(p_val + 0.5) : -floor(-p_val + 0.5);
} }
inline float round(float p_val) { 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) { 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); return range == 0 ? min : min + ((((value - min) % range) + range) % range);
} }
inline double wrapf(double value, double min, double max) { inline float wrapf(real_t value, real_t min, real_t max) {
double range = max - min; const real_t 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;
return is_zero_approx(range) ? min : value - (range * floor((value - min) / range)); 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) { if (p_step != 0) {
p_value = floor(p_value / p_step + 0.5) * p_step; p_value = floor(p_value / p_step + 0.5) * p_step;
} }