From 44c8b87069528db87be55cf441025934a5dde45e Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 21 Apr 2022 17:25:59 +0100 Subject: [PATCH] Added DigitalRoot for short/int/long Also introduces Mod for short/long --- X10D/src/Int16Extensions/Int16Extensions.cs | 32 +++++++++++++++++++++ X10D/src/Int32Extensions/Int32Extensions.cs | 22 +++++++++++++- X10D/src/Int64Extensions/Int64Extensions.cs | 32 +++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/X10D/src/Int16Extensions/Int16Extensions.cs b/X10D/src/Int16Extensions/Int16Extensions.cs index b294b55..4abae6d 100644 --- a/X10D/src/Int16Extensions/Int16Extensions.cs +++ b/X10D/src/Int16Extensions/Int16Extensions.cs @@ -9,6 +9,18 @@ namespace X10D; /// public static class Int16Extensions { + /// + /// Computes the digital root of an integer. + /// + /// The value whose digital root to compute. + /// The digital root of . + /// The digital root is defined as the recursive sum of digits until that result is a single digit. + public static short DigitalRoot(this short value) + { + short root = value.Mod(9); + return root < 1 ? (short)(9 - root) : root; + } + /// /// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a /// value. @@ -179,6 +191,26 @@ public static class Int16Extensions return MathUtils.Lerp(value, target, alpha); } + /// + /// Performs a modulo operation which supports a negative dividend. + /// + /// The dividend. + /// The divisor. + /// The result of dividend mod divisor. + /// + /// The % 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 x % y where x is + /// negative will return a negative value, akin to -(x % y), even if precedence is forced. This method provides a + /// modulo operation which supports negative dividends. + /// + /// ShreevatsaR, https://stackoverflow.com/a/1082938/1467293 + /// CC-BY-SA 2.5 + public static short Mod(this short dividend, short divisor) + { + var r = (short)(dividend % divisor); + return (short)(r < 0 ? r + divisor : r); + } + /// /// Returns an integer that indicates the sign of this 16-bit signed integer. /// diff --git a/X10D/src/Int32Extensions/Int32Extensions.cs b/X10D/src/Int32Extensions/Int32Extensions.cs index b56ef1f..2bca722 100644 --- a/X10D/src/Int32Extensions/Int32Extensions.cs +++ b/X10D/src/Int32Extensions/Int32Extensions.cs @@ -61,6 +61,18 @@ public static class Int32Extensions return (value >> shift) | cache; } + /// + /// Computes the digital root of an integer. + /// + /// The value whose digital root to compute. + /// The digital root of . + /// The digital root is defined as the recursive sum of digits until that result is a single digit. + public static int DigitalRoot(this int value) + { + int root = value.Mod(9); + return root < 1 ? 9 - root : root; + } + /// /// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a /// value. @@ -236,7 +248,15 @@ public static class Int32Extensions /// /// The dividend. /// The divisor. - /// The result of dividend % divisor. + /// The result of dividend mod divisor. + /// + /// The % 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 x % y where x is + /// negative will return a negative value, akin to -(x % y), even if precedence is forced. This method provides a + /// modulo operation which supports negative dividends. + /// + /// ShreevatsaR, https://stackoverflow.com/a/1082938/1467293 + /// CC-BY-SA 2.5 public static int Mod(this int dividend, int divisor) { int r = dividend % divisor; diff --git a/X10D/src/Int64Extensions/Int64Extensions.cs b/X10D/src/Int64Extensions/Int64Extensions.cs index affb235..28774ce 100644 --- a/X10D/src/Int64Extensions/Int64Extensions.cs +++ b/X10D/src/Int64Extensions/Int64Extensions.cs @@ -9,6 +9,18 @@ namespace X10D; /// public static class Int64Extensions { + /// + /// Computes the digital root of an integer. + /// + /// The value whose digital root to compute. + /// The digital root of . + /// The digital root is defined as the recursive sum of digits until that result is a single digit. + public static long DigitalRoot(this long value) + { + long root = value.Mod(9); + return root < 1 ? 9 - root : root; + } + /// /// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a /// value. @@ -204,6 +216,26 @@ public static class Int64Extensions return MathUtils.Lerp(value, target, alpha); } + /// + /// Performs a modulo operation which supports a negative dividend. + /// + /// The dividend. + /// The divisor. + /// The result of dividend mod divisor. + /// + /// The % 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 x % y where x is + /// negative will return a negative value, akin to -(x % y), even if precedence is forced. This method provides a + /// modulo operation which supports negative dividends. + /// + /// ShreevatsaR, https://stackoverflow.com/a/1082938/1467293 + /// CC-BY-SA 2.5 + public static long Mod(this long dividend, long divisor) + { + long r = dividend % divisor; + return r < 0 ? r + divisor : r; + } + /// /// Returns an integer that indicates the sign of this 64-bit signed integer. ///