Skip to content

Commit

Permalink
feat: add IBinaryInteger<T>.CountDigits
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Aug 25, 2023
1 parent 8bc7372 commit 423fb90
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions X10D/src/Math/NumberExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ namespace X10D.Math;
/// </summary>
public static class NumberExtensions
{
/// <summary>
/// Returns the number of digits in the current binary integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits<TNumber>(this TNumber value)
where TNumber : IBinaryInteger<TNumber>
{
if (TNumber.IsZero(value))
{
return 1;
}

return 1 + (int)System.Math.Floor(System.Math.Log10(System.Math.Abs(double.CreateChecked(value))));
}

/// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary>
Expand Down

0 comments on commit 423fb90

Please sign in to comment.