Skip to content

Commit

Permalink
Use dirty generic math
Browse files Browse the repository at this point in the history
  • Loading branch information
FreePhoenix888 committed Jan 11, 2023
1 parent 8ee00c4 commit 2c951a8
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions csharp/Platform.Numbers/Math.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;

namespace Platform.Numbers
Expand Down Expand Up @@ -49,11 +50,11 @@ public static class Math
/// <para>The product of all positive integers less than or equal to the number specified as an argument.</para>
/// <para>Произведение всех положительных чисел меньше или равных указанному, в качестве аргумента, числу.</para>
/// </returns>
public static ulong Factorial(ulong n)
public static TLinkAddress Factorial<TLinkAddress>(TLinkAddress n) where TLinkAddress : IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
{
if (n >= 0 && n <= MaximumFactorialNumber)
if (n >= TLinkAddress.Zero && n <= TLinkAddress.CreateTruncating(MaximumCatalanIndex))
{
return _factorials[n];
return TLinkAddress.CreateTruncating(_factorials[ulong.CreateTruncating(n)]);
}
else
{
Expand All @@ -73,11 +74,11 @@ public static ulong Factorial(ulong n)
/// <para>The Catalan Number with the number specified as an argument.</para>
/// <para>Число Катанала с номером, указанным в качестве аргумента.</para>
/// </returns>
public static ulong Catalan(ulong n)
public static TLinkAddress Catalan<TLinkAddress>(TLinkAddress n) where TLinkAddress : IUnsignedNumber<TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
{
if (n >= 0 && n <= MaximumCatalanIndex)
if (n >= TLinkAddress.Zero && n <= TLinkAddress.CreateTruncating(MaximumCatalanIndex))
{
return _catalans[n];
return TLinkAddress.CreateTruncating(_catalans[ulong.CreateTruncating(n)]);
}
else
{
Expand All @@ -98,6 +99,9 @@ public static ulong Catalan(ulong n)
/// <para>True, если число является степенью двойки, иначе - false.</para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPowerOfTwo(ulong x) => (x & x - 1) == 0;
public static bool IsPowerOfTwo<TLinkAddress>(TLinkAddress x) where TLinkAddress : IUnsignedNumber<TLinkAddress>, IBitwiseOperators<TLinkAddress, TLinkAddress, TLinkAddress>, IComparisonOperators<TLinkAddress, TLinkAddress, bool>
{
return (x & x - TLinkAddress.One) == TLinkAddress.Zero;
}
}
}

0 comments on commit 2c951a8

Please sign in to comment.