TL;DR How do I access Decimal.Radix
?
For the learning of it, I am trying to implement my own fraction type. The type should implement System.Numerics.INumber<TSelf>
, which includes System.Numerics.INumberBase<TSelf>
, which includes the property definition public static abstract int Radix { get; }
.
The documentation is surprisingly short so I tried simple experiments to aid in understanding of what .Radix
really means. None of these experiments could successfully access the INumberBase<TSelf>.Radix
property of any BCL type which implements it.
I checked the documentation and introductory writings on Generic Math. The closest I came to an answer was the introductory blog post: .NET 7 Preview 5 – Generic Math, Tanner Gooding which had this to say on the matter, although specifically about IFloatingPoint
:
you can get the
Sign
,Radix
,Exponent
, andSignificand
out allowing you to construct the exact value as-1^Sign * Radix^Exponent * Significand
(the standard algorithm for IEEE 754 values and which is more generally extensible to rational values).
So it seems that if a type implements either IFloatingPoint
or INumberBase<TSelf>
then one should be able to do something like int a = T.Radix
and get back the base of T
, however:
// .NET 7, 8
using System.Numerics;
int a = Decimal.Radix;
results in a CS0117 'decimal' does not contain a definition for 'Radix'
whose link has explanation:
'type' does not contain a definition for 'identifier'.
This error occurs in some situations when a reference is made to a member that does not exist for the data type.
Edit for Possible Duplicate:
The workaround found in Access Abstract Interface Member C#11:
public static T GetAdditiveIdentity<T>() where T: INumber<T> => T.AdditiveIdentity;
Works for AdditiveIdentity
but not for Radix
:
Console.WriteLine(GetRadix<decimal>());
static T GetRadix<T>() where T : INumberBase<T> => T.Radix;
// where T : INumber<T> does not change the error or message
// where T : IFloatingPoint<decimal> does not change the error or message
Results in a CS0029 Cannot implicitly convert type 'int' to T, which has information about explicit conversions.
How do I access Decimal.Radix
?