feat: added basic math operations as Math methods

This commit is contained in:
Syntriax 2025-05-02 00:14:41 +03:00
parent 131203d578
commit 4c235e3230
2 changed files with 78 additions and 0 deletions

View File

@ -30,6 +30,57 @@ public static class Math
/// </summary>
public const float DegreeToRadian = PI / 180f;
/// <summary>
/// Adds two <see cref="T"/>s.
/// </summary>
/// <param name="left">The first <see cref="T"/>.</param>
/// <param name="value">The second <see cref="T"/>.</param>
/// <returns>The sum of the two <see cref="T"/>s.</returns>
public static T Add<T>(T left, T value) where T : INumber<T> => left + value;
/// <summary>
/// Subtracts one <see cref="T"/> from another.
/// </summary>
/// <param name="left">The <see cref="T"/> to subtract from.</param>
/// <param name="value">The <see cref="T"/> to subtract.</param>
/// <returns>The result of subtracting the second <see cref="T"/> from the first.</returns>
public static T Subtract<T>(T left, T value) where T : INumber<T> => left - value;
/// <summary>
/// Multiplies a <see cref="T"/> by a scalar value.
/// </summary>
/// <param name="left">The <see cref="T"/>.</param>
/// <param name="multiplier">The scalar value.</param>
/// <returns>The result of multiplying the <see cref="T"/> by the scalar value.</returns>
public static T Multiply<T>(T left, T multiplier) where T : INumber<T> => left * multiplier;
/// <summary>
/// Divides a <see cref="T"/> by a scalar value.
/// </summary>
/// <param name="left">The <see cref="T"/>.</param>
/// <param name="divider">The scalar value.</param>
/// <returns>The result of dividing the <see cref="T"/> by the scalar value.</returns>
public static T Divide<T>(T left, T divider) where T : INumber<T> => left / divider;
/// <summary>
/// Returns the true mathematical modulus of a <see cref="T"/> value.
/// Unlike the remainder operator (%), this result is always non-negative,
/// even when the <paramref name="value"/> operand is negative.
/// </summary>
/// <typeparam name="T">A numeric type that implements <see cref="INumber{T}"/>.</typeparam>
/// <param name="value">The dividend <see cref="T"/> value.</param>
/// <param name="modulus">The modulus <see cref="T"/> value (must be non-zero).</param>
/// <returns>
/// The non-negative remainder of <paramref name="value"/> divided by <paramref name="modulus"/>.
/// </returns>
public static T Mod<T>(T value, T modulus) where T : INumber<T>
{
T result = value % modulus;
if (result < T.Zero)
result += modulus;
return result;
}
/// <summary>
/// Returns the absolute value of a number.
/// </summary>
@ -190,6 +241,15 @@ public static class Math
/// <returns>The number <paramref name="x"/> rounded to <paramref name="digits"/> fractional digits.</returns>
public static float Round(float x, int digits, MidpointRounding mode) => MathF.Round(x, digits, mode);
/// <summary>
/// Rounds a number to a specified number of fractional digits.
/// </summary>
/// <param name="x">The number to round.</param>
/// <param name="digits">The number of fractional digits in the return value.</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 <paramref name="digits"/> fractional digits.</returns>
public static int RoundToInt(float x) => (int)MathF.Round(x, 0, MidpointRounding.ToEven);
/// <summary>
/// Returns the square of a number.
/// </summary>

View File

@ -5,6 +5,21 @@ namespace Syntriax.Engine.Core;
public static class MathExtensions
{
/// <inheritdoc cref="Math.Add{T}(T, T)" />
public static T Add<T>(this T left, T value) where T : INumber<T> => Math.Add(left, value);
/// <inheritdoc cref="Math.Subtract{T}(T, T)" />
public static T Subtract<T>(this T left, T value) where T : INumber<T> => Math.Subtract(left, value);
/// <inheritdoc cref="Math.Multiply{T}(T, T)" />
public static T Multiply<T>(this T left, T multiplier) where T : INumber<T> => Math.Multiply(left, multiplier);
/// <inheritdoc cref="Math.Divide{T}(T, T)" />
public static T Divide<T>(this T left, T divider) where T : INumber<T> => Math.Divide(left, divider);
/// <inheritdoc cref="Math.Mod{T}(T, T)" />
public static T Mod<T>(this T value, T modulus) where T : INumber<T> => Math.Mod(value, modulus);
/// <inheritdoc cref="Math.Abs{T}(T)" />
public static T Abs<T>(this T x) where T : INumber<T> => Math.Abs(x);
@ -65,6 +80,9 @@ public static class MathExtensions
/// <inheritdoc cref="Math.Round(float, int, MidpointRounding)" />
public static float Round(this float x, int digits, MidpointRounding mode) => Math.Round(x, digits, mode);
/// <inheritdoc cref="Math.RoundToInt(float)" />
public static int RoundToInt(this float x) => Math.RoundToInt(x);
/// <inheritdoc cref="Math.Sqr{T}(T)" />
public static T Sqr<T>(this T x) where T : INumber<T> => Math.Sqr(x);