From 063ea087075c9df96b4f31237feac5037ef16f57 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sat, 3 May 2025 23:30:02 +0300 Subject: [PATCH] feat: added RoundToInt RoundMode for midway values --- Engine.Core/Math.cs | 10 +++++----- Engine.Core/MathExtensions.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Engine.Core/Math.cs b/Engine.Core/Math.cs index 2e20485..4fe4d52 100644 --- a/Engine.Core/Math.cs +++ b/Engine.Core/Math.cs @@ -249,13 +249,13 @@ public static class Math public static float Round(float x, int digits, MidpointRounding mode) => MathF.Round(x, digits, mode); /// - /// Rounds a number to a specified number of fractional digits. + /// Rounds a number to an integer. /// /// The number to round. - /// The number of fractional digits in the return value. - /// Specification for how to round if it is midway between two other numbers. - /// The number rounded to fractional digits. - public static int RoundToInt(float x) => (int)MathF.Round(x, 0, MidpointRounding.ToEven); + /// Specification for how to round if it's midway between two numbers + /// + public static int RoundToInt(float x, RoundMode roundMode = RoundMode.Ceil) => (int)MathF.Round(x, 0, roundMode == RoundMode.Ceil ? MidpointRounding.ToPositiveInfinity : MidpointRounding.ToNegativeInfinity); + public enum RoundMode { Ceil, Floor }; /// /// Returns the square of a number. diff --git a/Engine.Core/MathExtensions.cs b/Engine.Core/MathExtensions.cs index 7b6725d..e98df7f 100644 --- a/Engine.Core/MathExtensions.cs +++ b/Engine.Core/MathExtensions.cs @@ -83,8 +83,8 @@ public static class MathExtensions /// public static float Round(this float x, int digits, MidpointRounding mode) => Math.Round(x, digits, mode); - /// - public static int RoundToInt(this float x) => Math.RoundToInt(x); + /// + public static int RoundToInt(this float x, Math.RoundMode roundMode = Math.RoundMode.Ceil) => Math.RoundToInt(x, roundMode); /// public static T Sqr(this T x) where T : INumber => Math.Sqr(x);