1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-12 22:35:43 +00:00

feat: add IBinaryInteger<T>.CountDigits

This commit is contained in:
Oliver Booth 2023-08-25 02:34:55 +01:00
parent 8bc7372ba4
commit 423fb90cc4
Signed by: oliverbooth
GPG Key ID: B89D139977693FED

View File

@ -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>