Optimize Math.NearEqual
This commit is contained in:
@@ -419,7 +419,7 @@ namespace Math
|
||||
return (T)0;
|
||||
return Saturate((value - a) / (b - a));
|
||||
}
|
||||
|
||||
|
||||
// Performs smooth (cubic Hermite) interpolation between 0 and 1
|
||||
// @param amount Value between 0 and 1 indicating interpolation amount
|
||||
static float SmoothStep(float amount)
|
||||
@@ -480,44 +480,16 @@ namespace Math
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a and b are not even almost equal, taking into account the magnitude of floating point numbers
|
||||
/// </summary>
|
||||
/// <param name="a">The left value to compare</param>
|
||||
/// <param name="b">The right value to compare</param>
|
||||
/// <returns>False if a almost equal to b, otherwise true</returns>
|
||||
static bool NotNearEqual(float a, float b)
|
||||
{
|
||||
// Check if the numbers are really close - needed when comparing numbers near zero
|
||||
if (IsZero(a - b))
|
||||
return false;
|
||||
|
||||
// Original from Bruce Dawson: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
|
||||
const int32 aInt = *(int32*)&a;
|
||||
const int32 bInt = *(int32*)&b;
|
||||
|
||||
// Different signs means they do not match
|
||||
if (aInt < 0 != bInt < 0)
|
||||
return true;
|
||||
|
||||
// Find the difference in ULPs
|
||||
const int ulp = Abs(aInt - bInt);
|
||||
|
||||
// Choose of maxUlp = 4
|
||||
// according to http://code.google.com/p/googletest/source/browse/trunk/include/gtest/internal/gtest-internal.h
|
||||
const int maxUlp = 4;
|
||||
return ulp > maxUlp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a and b are almost equals, taking into account the magnitude of floating point numbers
|
||||
/// Checks if a and b are almost equals, taking into account the magnitude of floating point numbers.
|
||||
/// </summary>
|
||||
/// <remarks>The code is using the technique described by Bruce Dawson in <a href="http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/">Comparing Floating point numbers 2012 edition</a>.</remarks>
|
||||
/// <param name="a">The left value to compare</param>
|
||||
/// <param name="b">The right value to compare</param>
|
||||
/// <returns>True if a almost equal to b, otherwise false</returns>
|
||||
static bool NearEqual(float a, float b)
|
||||
{
|
||||
// Check if the numbers are really close - needed when comparing numbers near zero
|
||||
if (IsZero(a - b))
|
||||
if (Abs(a) < ZeroTolerance)
|
||||
return true;
|
||||
|
||||
// Original from Bruce Dawson: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
|
||||
@@ -537,6 +509,17 @@ namespace Math
|
||||
return ulp <= maxUlp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a and b are not even almost equal, taking into account the magnitude of floating point numbers
|
||||
/// </summary>
|
||||
/// <param name="a">The left value to compare</param>
|
||||
/// <param name="b">The right value to compare</param>
|
||||
/// <returns>False if a almost equal to b, otherwise true</returns>
|
||||
static FORCE_INLINE bool NotNearEqual(float a, float b)
|
||||
{
|
||||
return !NearEqual(a, b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a and b are almost equals within the given epsilon value.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user