From dda9b23c3d5100451a66a5fd9e26a21b31364406 Mon Sep 17 00:00:00 2001 From: savilli <78875209+savilli@users.noreply.github.com> Date: Thu, 21 Dec 2023 02:14:54 +0100 Subject: [PATCH] Make equals method symmetric --- include/irrMath.h | 42 +++++++++++------------------------------- include/vector2d.h | 5 ++--- include/vector3d.h | 6 ++---- 3 files changed, 15 insertions(+), 38 deletions(-) diff --git a/include/irrMath.h b/include/irrMath.h index e3b75e02..0219bd48 100644 --- a/include/irrMath.h +++ b/include/irrMath.h @@ -9,6 +9,7 @@ #include #include // for abs() etc. #include // For INT_MAX / UINT_MAX +#include namespace irr { @@ -17,9 +18,6 @@ namespace core //! Rounding error constant often used when comparing f32 values. - const s32 ROUNDING_ERROR_S32 = 0; - - const s64 ROUNDING_ERROR_S64 = 0; const f32 ROUNDING_ERROR_f32 = 0.000001f; const f64 ROUNDING_ERROR_f64 = 0.00000001; @@ -170,30 +168,6 @@ namespace core return ROUNDING_ERROR_f64; } - template <> - inline s32 roundingError() - { - return ROUNDING_ERROR_S32; - } - - template <> - inline u32 roundingError() - { - return ROUNDING_ERROR_S32; - } - - template <> - inline s64 roundingError() - { - return ROUNDING_ERROR_S64; - } - - template <> - inline u64 roundingError() - { - return ROUNDING_ERROR_S64; - } - template inline T relativeErrorFactor() { @@ -212,13 +186,19 @@ namespace core return 8; } - //! returns if a equals b, taking possible rounding errors into account - template - inline bool equals(const T a, const T b, const T tolerance = roundingError()) + //! returns if a equals b, for types without rounding errors + template ::value, bool> = true> + inline bool equals(const T a, const T b) { - return (a + tolerance >= b) && (a - tolerance <= b); + return a == b; } + //! returns if a equals b, taking possible rounding errors into account + template ::value, bool> = true> + inline bool equals(const T a, const T b, const T tolerance = roundingError()) + { + return abs(a - b) <= tolerance; + } //! returns if a equals b, taking relative error in form of factor //! this particular function does not involve any division. diff --git a/include/vector2d.h b/include/vector2d.h index 75ca08b7..549661ad 100644 --- a/include/vector2d.h +++ b/include/vector2d.h @@ -111,11 +111,10 @@ public: //! Checks if this vector equals the other one. /** Takes floating point rounding errors into account. \param other Vector to compare with. - \param tolerance Epsilon value for both - comparing X and Y. \return True if the two vector are (almost) equal, else false. */ - bool equals(const vector2d& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const + bool equals(const vector2d& other) const { - return core::equals(X, other.X, tolerance) && core::equals(Y, other.Y, tolerance); + return core::equals(X, other.X) && core::equals(Y, other.Y); } vector2d& set(T nx, T ny) {X=nx; Y=ny; return *this; } diff --git a/include/vector3d.h b/include/vector3d.h index b89bfbcb..7c6b98a4 100644 --- a/include/vector3d.h +++ b/include/vector3d.h @@ -114,11 +114,9 @@ namespace core // functions //! returns if this vector equals the other one, taking floating point rounding errors into account - bool equals(const vector3d& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const + bool equals(const vector3d& other) const { - return core::equals(X, other.X, tolerance) && - core::equals(Y, other.Y, tolerance) && - core::equals(Z, other.Z, tolerance); + return core::equals(X, other.X) && core::equals(Y, other.Y) && core::equals(Z, other.Z); } vector3d& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}