Math/Algorithm: Optimize NumberEquals unsigned and float cases
This commit is contained in:
parent
d8a2d08e27
commit
883d7b02a7
|
|
@ -98,7 +98,13 @@ namespace Nz
|
|||
return 0;
|
||||
}
|
||||
|
||||
template<typename T> /*constexpr*/ std::enable_if_t<!std::is_signed<T>::value || !std::is_integral<T>::value, bool> NumberEquals(T a, T b, T maxDifference)
|
||||
template<typename T> /*constexpr*/ std::enable_if_t<std::is_floating_point<T>::value, bool> NumberEquals(T a, T b, T maxDifference)
|
||||
{
|
||||
T diff = std::abs(a - b);
|
||||
return diff <= maxDifference;
|
||||
}
|
||||
|
||||
template<typename T> /*constexpr*/ std::enable_if_t<!std::is_signed<T>::value || (!std::is_integral<T>::value && !std::is_floating_point<T>::value), bool> NumberEquals(T a, T b, T maxDifference)
|
||||
{
|
||||
if (b > a)
|
||||
std::swap(a, b);
|
||||
|
|
@ -112,13 +118,8 @@ namespace Nz
|
|||
if (b > a)
|
||||
std::swap(a, b);
|
||||
|
||||
if ((b < 0) && (a > std::numeric_limits<T>::max() + b))
|
||||
return false;
|
||||
|
||||
if ((b > 0) && (a < std::numeric_limits<T>::min() + b))
|
||||
return false;
|
||||
|
||||
return std::abs(a - b) <= maxDifference;
|
||||
using UnsignedT = std::make_unsigned_t<T>;
|
||||
return static_cast<UnsignedT>(a) - static_cast<UnsignedT>(b) <= static_cast<UnsignedT>(maxDifference);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -487,7 +488,7 @@ namespace Nz
|
|||
template<typename T, typename T2>
|
||||
constexpr T Lerp(const T& from, const T& to, const T2& interpolation)
|
||||
{
|
||||
return from + interpolation * (to - from);
|
||||
return static_cast<T>(from + interpolation * (to - from));
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
|||
Loading…
Reference in New Issue