From 4c235e3230acb87d97906a057fd6d32c7bc16eb0 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 2 May 2025 00:14:41 +0300 Subject: [PATCH] feat: added basic math operations as Math methods --- Engine.Core/Math.cs | 60 +++++++++++++++++++++++++++++++++++ Engine.Core/MathExtensions.cs | 18 +++++++++++ 2 files changed, 78 insertions(+) diff --git a/Engine.Core/Math.cs b/Engine.Core/Math.cs index 48b11e2..cfa52d2 100644 --- a/Engine.Core/Math.cs +++ b/Engine.Core/Math.cs @@ -30,6 +30,57 @@ public static class Math /// public const float DegreeToRadian = PI / 180f; + /// + /// Adds two s. + /// + /// The first . + /// The second . + /// The sum of the two s. + public static T Add(T left, T value) where T : INumber => left + value; + + /// + /// Subtracts one from another. + /// + /// The to subtract from. + /// The to subtract. + /// The result of subtracting the second from the first. + public static T Subtract(T left, T value) where T : INumber => left - value; + + /// + /// Multiplies a by a scalar value. + /// + /// The . + /// The scalar value. + /// The result of multiplying the by the scalar value. + public static T Multiply(T left, T multiplier) where T : INumber => left * multiplier; + + /// + /// Divides a by a scalar value. + /// + /// The . + /// The scalar value. + /// The result of dividing the by the scalar value. + public static T Divide(T left, T divider) where T : INumber => left / divider; + + /// + /// Returns the true mathematical modulus of a value. + /// Unlike the remainder operator (%), this result is always non-negative, + /// even when the operand is negative. + /// + /// A numeric type that implements . + /// The dividend value. + /// The modulus value (must be non-zero). + /// + /// The non-negative remainder of divided by . + /// + public static T Mod(T value, T modulus) where T : INumber + { + T result = value % modulus; + if (result < T.Zero) + result += modulus; + return result; + } + /// /// Returns the absolute value of a number. /// @@ -190,6 +241,15 @@ public static class Math /// The number rounded to fractional digits. 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. + /// + /// 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); + /// /// Returns the square of a number. /// diff --git a/Engine.Core/MathExtensions.cs b/Engine.Core/MathExtensions.cs index 5232fbc..55bcdcc 100644 --- a/Engine.Core/MathExtensions.cs +++ b/Engine.Core/MathExtensions.cs @@ -5,6 +5,21 @@ namespace Syntriax.Engine.Core; public static class MathExtensions { + /// + public static T Add(this T left, T value) where T : INumber => Math.Add(left, value); + + /// + public static T Subtract(this T left, T value) where T : INumber => Math.Subtract(left, value); + + /// + public static T Multiply(this T left, T multiplier) where T : INumber => Math.Multiply(left, multiplier); + + /// + public static T Divide(this T left, T divider) where T : INumber => Math.Divide(left, divider); + + /// + public static T Mod(this T value, T modulus) where T : INumber => Math.Mod(value, modulus); + /// public static T Abs(this T x) where T : INumber => Math.Abs(x); @@ -65,6 +80,9 @@ 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 T Sqr(this T x) where T : INumber => Math.Sqr(x);