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>
|
||||
|
||||
@@ -586,8 +586,7 @@ namespace FlaxEngine
|
||||
/// <param name="maxSpeed">The maximum speed.</param>
|
||||
/// <param name="deltaTime">The delta time (in seconds) since last update.</param>
|
||||
/// <returns>The smoothed value.</returns>
|
||||
public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, [DefaultValue("float.PositiveInfinity")]
|
||||
float maxSpeed, [DefaultValue("Time.DeltaTime")] float deltaTime)
|
||||
public static float SmoothDamp(float current, float target, ref float currentVelocity, float smoothTime, [DefaultValue("float.PositiveInfinity")] float maxSpeed, [DefaultValue("Time.DeltaTime")] float deltaTime)
|
||||
{
|
||||
smoothTime = Max(0.0001f, smoothTime);
|
||||
float a = 2f / smoothTime;
|
||||
@@ -646,8 +645,7 @@ namespace FlaxEngine
|
||||
/// <param name="maxSpeed">The maximum speed.</param>
|
||||
/// <param name="deltaTime">The delta time (in seconds) since last update.</param>
|
||||
/// <returns>The smoothed value.</returns>
|
||||
public static float SmoothDampAngle(float current, float target, ref float currentVelocity, float smoothTime, [DefaultValue("float.PositiveInfinity")]
|
||||
float maxSpeed, [DefaultValue("Time.DeltaTime")] float deltaTime)
|
||||
public static float SmoothDampAngle(float current, float target, ref float currentVelocity, float smoothTime, [DefaultValue("float.PositiveInfinity")] float maxSpeed, [DefaultValue("Time.DeltaTime")] float deltaTime)
|
||||
{
|
||||
target = current + DeltaAngle(current, target);
|
||||
return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
|
||||
@@ -905,26 +903,16 @@ namespace FlaxEngine
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unlike
|
||||
/// <see cref="WithinEpsilon" /> method). See Remarks.
|
||||
/// See remarks.
|
||||
/// Checks if a and b are almost equals, taking into account the magnitude of floating point numbers (unlike <see cref="WithinEpsilon" /> method).
|
||||
/// </summary>
|
||||
/// <param name="a">The left value to compare.</param>
|
||||
/// <param name="b">The right value to compare.</param>
|
||||
/// <returns><c>true</c> if a almost equal to b, <c>false</c> otherwise</returns>
|
||||
/// <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>
|
||||
/// <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>
|
||||
public static unsafe bool NearEqual(float a, float b)
|
||||
{
|
||||
// Check if the numbers are really close -- needed
|
||||
// when comparing numbers near zero.
|
||||
if (IsZero(a - b))
|
||||
// Check if the numbers are really close -- needed when comparing numbers near zero.
|
||||
if (Math.Abs(a - b) < Epsilon)
|
||||
return true;
|
||||
|
||||
// Original from Bruce Dawson: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
|
||||
@@ -973,7 +961,7 @@ namespace FlaxEngine
|
||||
/// <returns><c>true</c> if the specified value is close to one (1.0f); otherwise, <c>false</c>.</returns>
|
||||
public static bool IsOne(float a)
|
||||
{
|
||||
return IsZero(a - 1.0f);
|
||||
return Math.Abs(a - 1.0f) < Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user