Added DigitalRoot for short/int/long

Also introduces Mod for short/long
This commit is contained in:
Oliver Booth 2022-04-21 17:25:59 +01:00
parent 881ccb0474
commit 44c8b87069
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 85 additions and 1 deletions

View File

@ -9,6 +9,18 @@ namespace X10D;
/// </summary>
public static class Int16Extensions
{
/// <summary>
/// Computes the digital root of an integer.
/// </summary>
/// <param name="value">The value whose digital root to compute.</param>
/// <returns>The digital root of <paramref name="value" />.</returns>
/// <remarks>The digital root is defined as the recursive sum of digits until that result is a single digit.</remarks>
public static short DigitalRoot(this short value)
{
short root = value.Mod(9);
return root < 1 ? (short)(9 - root) : root;
}
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
@ -179,6 +191,26 @@ public static class Int16Extensions
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
/// Performs a modulo operation which supports a negative dividend.
/// </summary>
/// <param name="dividend">The dividend.</param>
/// <param name="divisor">The divisor.</param>
/// <returns>The result of <c>dividend mod divisor</c>.</returns>
/// <remarks>
/// The <c>%</c> operator (commonly called the modulo operator) in C# is not defined to be modulo, but is instead
/// remainder. This quirk inherently makes it difficult to use modulo in a negative context, as <c>x % y</c> where x is
/// negative will return a negative value, akin to <c>-(x % y)</c>, even if precedence is forced. This method provides a
/// modulo operation which supports negative dividends.
/// </remarks>
/// <author>ShreevatsaR, https://stackoverflow.com/a/1082938/1467293</author>
/// <license>CC-BY-SA 2.5</license>
public static short Mod(this short dividend, short divisor)
{
var r = (short)(dividend % divisor);
return (short)(r < 0 ? r + divisor : r);
}
/// <summary>
/// Returns an integer that indicates the sign of this 16-bit signed integer.
/// </summary>

View File

@ -61,6 +61,18 @@ public static class Int32Extensions
return (value >> shift) | cache;
}
/// <summary>
/// Computes the digital root of an integer.
/// </summary>
/// <param name="value">The value whose digital root to compute.</param>
/// <returns>The digital root of <paramref name="value" />.</returns>
/// <remarks>The digital root is defined as the recursive sum of digits until that result is a single digit.</remarks>
public static int DigitalRoot(this int value)
{
int root = value.Mod(9);
return root < 1 ? 9 - root : root;
}
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
@ -236,7 +248,15 @@ public static class Int32Extensions
/// </summary>
/// <param name="dividend">The dividend.</param>
/// <param name="divisor">The divisor.</param>
/// <returns>The result of <c>dividend % divisor</c>.</returns>
/// <returns>The result of <c>dividend mod divisor</c>.</returns>
/// <remarks>
/// The <c>%</c> operator (commonly called the modulo operator) in C# is not defined to be modulo, but is instead
/// remainder. This quirk inherently makes it difficult to use modulo in a negative context, as <c>x % y</c> where x is
/// negative will return a negative value, akin to <c>-(x % y)</c>, even if precedence is forced. This method provides a
/// modulo operation which supports negative dividends.
/// </remarks>
/// <author>ShreevatsaR, https://stackoverflow.com/a/1082938/1467293</author>
/// <license>CC-BY-SA 2.5</license>
public static int Mod(this int dividend, int divisor)
{
int r = dividend % divisor;

View File

@ -9,6 +9,18 @@ namespace X10D;
/// </summary>
public static class Int64Extensions
{
/// <summary>
/// Computes the digital root of an integer.
/// </summary>
/// <param name="value">The value whose digital root to compute.</param>
/// <returns>The digital root of <paramref name="value" />.</returns>
/// <remarks>The digital root is defined as the recursive sum of digits until that result is a single digit.</remarks>
public static long DigitalRoot(this long value)
{
long root = value.Mod(9);
return root < 1 ? 9 - root : root;
}
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
@ -204,6 +216,26 @@ public static class Int64Extensions
return MathUtils.Lerp(value, target, alpha);
}
/// <summary>
/// Performs a modulo operation which supports a negative dividend.
/// </summary>
/// <param name="dividend">The dividend.</param>
/// <param name="divisor">The divisor.</param>
/// <returns>The result of <c>dividend mod divisor</c>.</returns>
/// <remarks>
/// The <c>%</c> operator (commonly called the modulo operator) in C# is not defined to be modulo, but is instead
/// remainder. This quirk inherently makes it difficult to use modulo in a negative context, as <c>x % y</c> where x is
/// negative will return a negative value, akin to <c>-(x % y)</c>, even if precedence is forced. This method provides a
/// modulo operation which supports negative dividends.
/// </remarks>
/// <author>ShreevatsaR, https://stackoverflow.com/a/1082938/1467293</author>
/// <license>CC-BY-SA 2.5</license>
public static long Mod(this long dividend, long divisor)
{
long r = dividend % divisor;
return r < 0 ? r + divisor : r;
}
/// <summary>
/// Returns an integer that indicates the sign of this 64-bit signed integer.
/// </summary>