Browse Source

Split is_equal_approx into float and double versions to match Godot

https://github.com/godotengine/godot/pull/48882
Aaron Franke 2 years ago
parent
commit
942cd466ed
3 changed files with 38 additions and 12 deletions
  1. 36 10
      include/godot_cpp/core/math.hpp
  2. 1 1
      src/variant/basis.cpp
  3. 1 1
      src/variant/quaternion.cpp

+ 36 - 10
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 <godot_cpp/core/defs.hpp>
 
@@ -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

+ 1 - 1
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

+ 1 - 1
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 {