From 942cd466ed116f29f7f48c0d658e36e2fcae44cf Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Sun, 18 Sep 2022 17:37:31 -0500 Subject: [PATCH] Split is_equal_approx into float and double versions to match Godot https://github.com/godotengine/godot/pull/48882 --- include/godot_cpp/core/math.hpp | 46 ++++++++++++++++++++++++++------- src/variant/basis.cpp | 2 +- src/variant/quaternion.cpp | 2 +- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/include/godot_cpp/core/math.hpp b/include/godot_cpp/core/math.hpp index e0d93dbe..e25926d1 100644 --- a/include/godot_cpp/core/math.hpp +++ b/include/godot_cpp/core/math.hpp @@ -28,8 +28,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef GODOT_MATH_H -#define GODOT_MATH_H +#ifndef GODOT_MATH_HPP +#define GODOT_MATH_HPP #include @@ -368,30 +368,56 @@ inline bool is_inf(double p_val) { return std::isinf(p_val); } -inline bool is_equal_approx(real_t a, real_t b) { +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 * std::abs(a); + float tolerance = (float)CMP_EPSILON * abs(a); + if (tolerance < (float)CMP_EPSILON) { + tolerance = (float)CMP_EPSILON; + } + return abs(a - b) < tolerance; +} + +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; + } + // Then check for approximate equality. + return abs(a - b) < tolerance; +} + +inline bool is_zero_approx(float s) { + return abs(s) < (float)CMP_EPSILON; +} + +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 std::abs(a - b) < tolerance; + return abs(a - b) < tolerance; } -inline bool is_equal_approx(real_t a, real_t b, real_t tolerance) { +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 std::abs(a - b) < tolerance; + return abs(a - b) < tolerance; } -inline bool is_zero_approx(real_t s) { - return std::abs(s) < CMP_EPSILON; +inline bool is_zero_approx(double s) { + return abs(s) < CMP_EPSILON; } inline double smoothstep(double p_from, double p_to, double p_weight) { @@ -509,4 +535,4 @@ inline double snapped(double p_value, double p_step) { } // namespace Math } // namespace godot -#endif // GODOT_MATH_H +#endif // GODOT_MATH_HPP diff --git a/src/variant/basis.cpp b/src/variant/basis.cpp index c0c79d8b..0684dcad 100644 --- a/src/variant/basis.cpp +++ b/src/variant/basis.cpp @@ -110,7 +110,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(), (real_t)1, (real_t)UNIT_EPSILON) && is_orthogonal(); } #ifdef MATH_CHECKS diff --git a/src/variant/quaternion.cpp b/src/variant/quaternion.cpp index 7d242f9e..7a06cda1 100644 --- a/src/variant/quaternion.cpp +++ b/src/variant/quaternion.cpp @@ -86,7 +86,7 @@ Quaternion Quaternion::normalized() const { } bool Quaternion::is_normalized() const { - return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON); //use less epsilon + return Math::is_equal_approx(length_squared(), (real_t)1.0, (real_t)UNIT_EPSILON); //use less epsilon } Quaternion Quaternion::inverse() const {