Split is_equal_approx into float and double versions to match Godot

https://github.com/godotengine/godot/pull/48882
pull/852/head
Aaron Franke 2022-09-18 17:37:31 -05:00
parent 3450a1ab16
commit 942cd466ed
No known key found for this signature in database
GPG Key ID: 40A1750B977E56BF
3 changed files with 38 additions and 12 deletions

View File

@ -28,8 +28,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
#ifndef GODOT_MATH_H #ifndef GODOT_MATH_HPP
#define GODOT_MATH_H #define GODOT_MATH_HPP
#include <godot_cpp/core/defs.hpp> #include <godot_cpp/core/defs.hpp>
@ -368,30 +368,56 @@ inline bool is_inf(double p_val) {
return std::isinf(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. // Check for exact equality first, required to handle "infinity" values.
if (a == b) { if (a == b) {
return true; return true;
} }
// Then check for approximate equality. // 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) { if (tolerance < CMP_EPSILON) {
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. // Check for exact equality first, required to handle "infinity" values.
if (a == b) { if (a == b) {
return true; return true;
} }
// Then check for approximate equality. // Then check for approximate equality.
return std::abs(a - b) < tolerance; return abs(a - b) < tolerance;
} }
inline bool is_zero_approx(real_t s) { inline bool is_zero_approx(double s) {
return std::abs(s) < CMP_EPSILON; return abs(s) < CMP_EPSILON;
} }
inline double smoothstep(double p_from, double p_to, double p_weight) { 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 Math
} // namespace godot } // namespace godot
#endif // GODOT_MATH_H #endif // GODOT_MATH_HPP

View File

@ -110,7 +110,7 @@ bool Basis::is_diagonal() const {
} }
bool Basis::is_rotation() 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 #ifdef MATH_CHECKS

View File

@ -86,7 +86,7 @@ Quaternion Quaternion::normalized() const {
} }
bool Quaternion::is_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 { Quaternion Quaternion::inverse() const {