diff options
author | Aaron Franke <arnfranke@yahoo.com> | 2021-05-20 06:04:41 -0400 |
---|---|---|
committer | Aaron Franke <arnfranke@yahoo.com> | 2021-05-20 06:18:11 -0400 |
commit | b5b6d3a8ec722719edd3f260e789315b87322b4c (patch) | |
tree | 1172f709e85e0396f37bbbcabb7903ffa3b420ae /core/math | |
parent | 42b6602f1d4b108cecb94b94c0d2b645acaebd4f (diff) |
Make is_equal_approx have explicit float and double versions
Diffstat (limited to 'core/math')
-rw-r--r-- | core/math/basis.cpp | 2 | ||||
-rw-r--r-- | core/math/math_funcs.h | 34 | ||||
-rw-r--r-- | core/math/quat.cpp | 2 | ||||
-rw-r--r-- | core/math/vector2.cpp | 2 | ||||
-rw-r--r-- | core/math/vector3.h | 2 |
5 files changed, 34 insertions, 8 deletions
diff --git a/core/math/basis.cpp b/core/math/basis.cpp index 50299902eb..037378b9d7 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -109,7 +109,7 @@ bool Basis::is_diagonal() const { } bool Basis::is_rotation() const { - return Math::is_equal_approx(determinant(), 1, UNIT_EPSILON) && is_orthogonal(); + return Math::is_equal_approx(determinant(), 1, (real_t)UNIT_EPSILON) && is_orthogonal(); } #ifdef MATH_CHECKS diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index c0d7649b65..40234f6ae5 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -311,20 +311,20 @@ public: static float random(float from, float to); static int random(int from, int to); - static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) { + static _ALWAYS_INLINE_ bool is_equal_approx(float a, float b) { // Check for exact equality first, required to handle "infinity" values. if (a == b) { return true; } // Then check for approximate equality. - real_t tolerance = CMP_EPSILON * abs(a); + float tolerance = CMP_EPSILON * abs(a); if (tolerance < CMP_EPSILON) { tolerance = CMP_EPSILON; } return abs(a - b) < tolerance; } - static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b, real_t tolerance) { + static _ALWAYS_INLINE_ bool is_equal_approx(float a, float b, float tolerance) { // Check for exact equality first, required to handle "infinity" values. if (a == b) { return true; @@ -333,7 +333,33 @@ public: return abs(a - b) < tolerance; } - static _ALWAYS_INLINE_ bool is_zero_approx(real_t s) { + static _ALWAYS_INLINE_ bool is_zero_approx(float s) { + return abs(s) < CMP_EPSILON; + } + + static _ALWAYS_INLINE_ bool is_equal_approx(double a, double b) { + // Check for exact equality first, required to handle "infinity" values. + if (a == b) { + return true; + } + // Then check for approximate equality. + double tolerance = CMP_EPSILON * abs(a); + if (tolerance < CMP_EPSILON) { + tolerance = CMP_EPSILON; + } + return abs(a - b) < tolerance; + } + + static _ALWAYS_INLINE_ bool is_equal_approx(double a, double b, double tolerance) { + // Check for exact equality first, required to handle "infinity" values. + if (a == b) { + return true; + } + // Then check for approximate equality. + return abs(a - b) < tolerance; + } + + static _ALWAYS_INLINE_ bool is_zero_approx(double s) { return abs(s) < CMP_EPSILON; } diff --git a/core/math/quat.cpp b/core/math/quat.cpp index 6f13e04027..3982a0b993 100644 --- a/core/math/quat.cpp +++ b/core/math/quat.cpp @@ -87,7 +87,7 @@ Quat Quat::normalized() const { } bool Quat::is_normalized() const { - return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON); //use less epsilon + return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); //use less epsilon } Quat Quat::inverse() const { diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index 5129ed336e..46a08b53ab 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -59,7 +59,7 @@ Vector2 Vector2::normalized() const { bool Vector2::is_normalized() const { // use length_squared() instead of length() to avoid sqrt(), makes it more stringent. - return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON); + return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); } real_t Vector2::distance_to(const Vector2 &p_vector2) const { diff --git a/core/math/vector3.h b/core/math/vector3.h index b47c3cc916..adfc52566f 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -423,7 +423,7 @@ Vector3 Vector3::normalized() const { bool Vector3::is_normalized() const { // use length_squared() instead of length() to avoid sqrt(), makes it more stringent. - return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON); + return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); } Vector3 Vector3::inverse() const { |