Make equals method symmetric

This commit is contained in:
savilli 2023-12-21 02:14:54 +01:00 committed by sfan5
parent b349266855
commit dda9b23c3d
3 changed files with 15 additions and 38 deletions

@ -9,6 +9,7 @@
#include <float.h>
#include <stdlib.h> // for abs() etc.
#include <limits.h> // For INT_MAX / UINT_MAX
#include <type_traits>
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 <class T>
inline T relativeErrorFactor()
{
@ -212,13 +186,19 @@ namespace core
return 8;
}
//! returns if a equals b, taking possible rounding errors into account
template <class T>
inline bool equals(const T a, const T b, const T tolerance = roundingError<T>())
//! returns if a equals b, for types without rounding errors
template <class T, std::enable_if_t<std::is_integral<T>::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 <class T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
inline bool equals(const T a, const T b, const T tolerance = roundingError<T>())
{
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.

@ -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<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
bool equals(const vector2d<T>& 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<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }

@ -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<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
bool equals(const vector3d<T>& 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<T>& set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; return *this;}