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