Add various improvements for C# math library

This commit is contained in:
Wojtek Figat
2022-05-18 22:30:50 +02:00
parent 8c971cd11e
commit 523d961f28
6 changed files with 21 additions and 53 deletions

View File

@@ -14,7 +14,7 @@ namespace FlaxEngine
/// <summary>
/// The value for which all absolute numbers smaller than are considered equal to zero.
/// </summary>
public const double Epsilon = 1e-7f;
public const double Epsilon = 1e-300;
/// <summary>
/// A value specifying the approximation of π which is 180 degrees.
@@ -721,21 +721,12 @@ 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). See Remarks. See remarks.
/// </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(double a, double b)
{
// Check if the numbers are really close -- needed
@@ -888,17 +879,14 @@ namespace FlaxEngine
public static double UnwindRadians(double angle)
{
// TODO: make it faster?
while (angle > Pi)
{
angle -= TwoPi;
}
while (angle < -Pi)
{
angle += TwoPi;
}
return angle;
}
@@ -910,17 +898,14 @@ namespace FlaxEngine
public static double UnwindDegrees(double angle)
{
// TODO: make it faster?
while (angle > 180.0f)
{
angle -= 360.0f;
}
while (angle < -180.0f)
{
angle += 360.0f;
}
return angle;
}
@@ -962,9 +947,7 @@ namespace FlaxEngine
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static double SmoothStep(double amount)
{
return amount <= 0d ? 0d
: amount >= 1d ? 1d
: amount * amount * (3d - 2d * amount);
return amount <= 0d ? 0d : amount >= 1d ? 1d : amount * amount * (3d - 2d * amount);
}
/// <summary>
@@ -976,9 +959,7 @@ namespace FlaxEngine
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static double SmootherStep(double amount)
{
return amount <= 0d ? 0d
: amount >= 1d ? 1d
: amount * amount * amount * (amount * (amount * 6d - 15d) + 10d);
return amount <= 0d ? 0d : amount >= 1d ? 1d : amount * amount * amount * (amount * (amount * 6d - 15d) + 10d);
}
/// <summary>
@@ -991,7 +972,6 @@ namespace FlaxEngine
{
if (modulo == 0d)
return value;
return value % modulo;
}

View File

@@ -65,7 +65,7 @@ namespace FlaxEngine
/// <param name="f"></param>
public static float Acos(float f)
{
return (float)Math.Acos(f);
return (float)Mathf.Acos(f);
}
/// <summary>
@@ -1092,17 +1092,14 @@ namespace FlaxEngine
public static float UnwindRadians(float angle)
{
// TODO: make it faster?
while (angle > Pi)
{
angle -= TwoPi;
}
while (angle < -Pi)
{
angle += TwoPi;
}
return angle;
}
@@ -1114,17 +1111,14 @@ namespace FlaxEngine
public static float UnwindDegrees(float angle)
{
// TODO: make it faster?
while (angle > 180.0f)
{
angle -= 360.0f;
}
while (angle < -180.0f)
{
angle += 360.0f;
}
return angle;
}
@@ -1204,8 +1198,7 @@ namespace FlaxEngine
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and
/// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// </remarks>
/// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
@@ -1220,8 +1213,7 @@ namespace FlaxEngine
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and
/// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// </remarks>
/// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
@@ -1236,8 +1228,7 @@ namespace FlaxEngine
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and
/// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// </remarks>
/// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
@@ -1252,8 +1243,7 @@ namespace FlaxEngine
/// Interpolates between two values using a linear function by a given amount.
/// </summary>
/// <remarks>
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and
/// http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// See http://www.encyclopediaofmath.org/index.php/Linear_interpolation and http://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
/// </remarks>
/// <param name="from">Value to interpolate from.</param>
/// <param name="to">Value to interpolate to.</param>
@@ -1273,9 +1263,7 @@ namespace FlaxEngine
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static float SmoothStep(float amount)
{
return amount <= 0 ? 0
: amount >= 1 ? 1
: amount * amount * (3 - 2 * amount);
return amount <= 0 ? 0 : amount >= 1 ? 1 : amount * amount * (3 - 2 * amount);
}
/// <summary>
@@ -1287,9 +1275,7 @@ namespace FlaxEngine
/// <param name="amount">Value between 0 and 1 indicating interpolation amount.</param>
public static float SmootherStep(float amount)
{
return amount <= 0 ? 0
: amount >= 1 ? 1
: amount * amount * amount * (amount * (amount * 6 - 15) + 10);
return amount <= 0 ? 0 : amount >= 1 ? 1 : amount * amount * amount * (amount * (amount * 6 - 15) + 10);
}
/// <summary>
@@ -1302,7 +1288,6 @@ namespace FlaxEngine
{
if (modulo == 0.0f)
return value;
return value % modulo;
}

View File

@@ -36,7 +36,8 @@ namespace FlaxEngine.TypeConverters
{
if (destinationType == typeof(string))
{
return ((Vector2)value).X + "," + ((Vector2)value).Y;
var v = (Vector2)value;
return v.X + "," + v.Y;
}
return base.ConvertTo(context, culture, value, destinationType);

View File

@@ -36,7 +36,8 @@ namespace FlaxEngine.TypeConverters
{
if (destinationType == typeof(string))
{
return ((Vector3)value).X + "," + ((Vector3)value).Y + "," + ((Vector3)value).Z;
var v = (Vector3)value;
return v.X + "," + v.Y + "," + v.Z;
}
return base.ConvertTo(context, culture, value, destinationType);

View File

@@ -36,7 +36,8 @@ namespace FlaxEngine.TypeConverters
{
if (destinationType == typeof(string))
{
return ((Vector4)value).X + "," + ((Vector4)value).Y + "," + ((Vector4)value).Z + "," + ((Vector4)value).W;
var v = (Vector4)value;
return v.X + "," + v.Y + "," + v.Z + "," + v.W;
}
return base.ConvertTo(context, culture, value, destinationType);

View File

@@ -1204,9 +1204,9 @@ namespace FlaxEngine
/// <returns>The angle (in degrees).</returns>
public static float Angle(Vector3 from, Vector3 to)
{
float dot = Mathf.Clamp(Dot(from.Normalized, to.Normalized), -1F, 1F);
if (Mathf.Abs(dot) > (1F - Mathf.Epsilon))
return dot > 0F ? 0F : 180F;
float dot = Mathf.Clamp(Dot(from.Normalized, to.Normalized), -1.0f, 1.0f);
if (Mathf.Abs(dot) > (1.0f - Mathf.Epsilon))
return dot > 0.0f ? 0.0f : 180.0f;
return Mathf.Acos(dot) * Mathf.RadiansToDegrees;
}