fix: math round methods not working properly
This commit is contained in:
@@ -240,21 +240,33 @@ public static class Math
|
|||||||
public static T Lerp<T>(T x, T y, T t) where T : IFloatingPoint<T> => x + (y - x) * t;
|
public static T Lerp<T>(T x, T y, T t) where T : IFloatingPoint<T> => x + (y - x) * t;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Rounds a number to a specified number of fractional digits.
|
/// Rounds a number to the closest integer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x">The number to round.</param>
|
/// <param name="x">The number to round.</param>
|
||||||
/// <param name="digits">The number of fractional digits in the return value.</param>
|
/// <param name="roundMode">Specification for how to round <paramref name="x"/> if it is midway between two other numbers.</param>
|
||||||
/// <param name="mode">Specification for how to round <paramref name="x"/> if it is midway between two other numbers.</param>
|
/// <returns>The number <paramref name="x"/> rounded to the closest integer.</returns>
|
||||||
/// <returns>The number <paramref name="x"/> rounded to <paramref name="digits"/> fractional digits.</returns>
|
public static float Round(float x, RoundMode roundMode) => RoundToInt(x, roundMode);
|
||||||
public static float Round(float x, int digits, MidpointRounding mode) => MathF.Round(x, digits, mode);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Rounds a number to an integer.
|
/// Rounds a number to the closest integer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="x">The number to round.</param>
|
/// <param name="x">The number to round.</param>
|
||||||
/// <param name="roundMode">Specification for how to round <paramref name="x"/> if it's midway between two numbers</param>
|
/// <param name="roundMode">Specification for how to round <paramref name="x"/> if it's midway between two numbers</param>
|
||||||
/// <returns></returns>
|
/// <returns>The number <paramref name="x"/> rounded to the closest integer.</returns>
|
||||||
public static int RoundToInt(float x, RoundMode roundMode = RoundMode.Ceil) => (int)MathF.Round(x, 0, roundMode == RoundMode.Ceil ? MidpointRounding.ToPositiveInfinity : MidpointRounding.ToNegativeInfinity);
|
public static int RoundToInt(float x, RoundMode roundMode = RoundMode.Ceil)
|
||||||
|
{
|
||||||
|
float remainder = x.Mod(1f);
|
||||||
|
|
||||||
|
if (remainder == .5f)
|
||||||
|
if (roundMode == RoundMode.Floor)
|
||||||
|
return (int)x;
|
||||||
|
else
|
||||||
|
return (int)(x + .5f);
|
||||||
|
|
||||||
|
if (x < 0f)
|
||||||
|
return (int)(x - .5f);
|
||||||
|
return (int)(x + .5f);
|
||||||
|
}
|
||||||
public enum RoundMode { Ceil, Floor };
|
public enum RoundMode { Ceil, Floor };
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@@ -81,7 +81,7 @@ public static class MathExtensions
|
|||||||
public static T Lerp<T>(this T x, T y, T t) where T : IFloatingPoint<T> => Math.Lerp(x, y, t);
|
public static T Lerp<T>(this T x, T y, T t) where T : IFloatingPoint<T> => Math.Lerp(x, y, t);
|
||||||
|
|
||||||
/// <inheritdoc cref="Math.Round(float, int, MidpointRounding)" />
|
/// <inheritdoc cref="Math.Round(float, int, MidpointRounding)" />
|
||||||
public static float Round(this float x, int digits, MidpointRounding mode) => Math.Round(x, digits, mode);
|
public static float Round(this float x, Math.RoundMode mode) => Math.Round(x, mode);
|
||||||
|
|
||||||
/// <inheritdoc cref="Math.RoundToInt(float, Math.RoundMode)" />
|
/// <inheritdoc cref="Math.RoundToInt(float, Math.RoundMode)" />
|
||||||
public static int RoundToInt(this float x, Math.RoundMode roundMode = Math.RoundMode.Ceil) => Math.RoundToInt(x, roundMode);
|
public static int RoundToInt(this float x, Math.RoundMode roundMode = Math.RoundMode.Ceil) => Math.RoundToInt(x, roundMode);
|
||||||
|
Reference in New Issue
Block a user