From 423fb90cc4cc64e1dade357c900147c3f33d9546 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 25 Aug 2023 02:34:55 +0100 Subject: [PATCH] feat: add IBinaryInteger.CountDigits --- X10D/src/Math/NumberExtensions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/X10D/src/Math/NumberExtensions.cs b/X10D/src/Math/NumberExtensions.cs index 12f00f8..b2c68a1 100644 --- a/X10D/src/Math/NumberExtensions.cs +++ b/X10D/src/Math/NumberExtensions.cs @@ -11,6 +11,22 @@ namespace X10D.Math; /// public static class NumberExtensions { + /// + /// Returns the number of digits in the current binary integer. + /// + /// The value whose digit count to compute. + /// The number of digits in . + public static int CountDigits(this TNumber value) + where TNumber : IBinaryInteger + { + if (TNumber.IsZero(value)) + { + return 1; + } + + return 1 + (int)System.Math.Floor(System.Math.Log10(System.Math.Abs(double.CreateChecked(value)))); + } + /// /// Returns a value indicating whether the current value is evenly divisible by 2. ///