-
.NET7 C# INumberBuilderTo improve and to keep the .NET 7 API simple, in discussion with dotnet devs we came back to an old problem. But if it starts with numerics eg. arbitrary size types, there is a problem. The System.Numerics devs plan at this point to provide a special
I would prefer a stack-like instruction set, but I know other ideas from discussions and there are many different implementations thinkable based on different systems and with different priorities.
The 3 aspects for the solution
A full working test implementation for a minimalistic interface already exists. (interface) (implementation) API Proposal // This is just an outline for a stack-like approach
// only a minimum of standard operations.
public interface INumberBuilder
{
void Push<Number>(Number value) where Number : INumber<Number>;
Number Pop<Number>() where Number : INumber<Number>;
void Pop();
// binary operations
void Add();
void Subtract();
void Multiply();
void Divide();
// unary operations
void Negate();
void Sqr();
} API Usage static void test_INumberBuilder()
{
var builder = (INumberBuilder)new MyNumberBuilder();
// 2 * 3 + 4 * 5
builder.Push(2.0f);
builder.Push(3.0m);
builder.Multiply();
builder.Push(4UL);
builder.Push(5L);
builder.Multiply();
builder.Add();
var intval = builder.Pop<Int128>();
// PI * 10^10000
builder.Push(Math.PI);
builder.Push(BigInteger.Pow(10, 10000));
builder.Multiply();
var ratval = builder.Pop<BigRational>();
// result / 10^10000
builder.Push(ratval);
builder.Push(BigRational.Pow(10, 10000));
builder.Divide();
var dblval = builder.Pop<double>();
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
This is out of scope for now. |
Beta Was this translation helpful? Give feedback.
-
@huoyaoyuan For every solution is test code available, (type 1), (type 2), (type 3), (type 4) every approach works with better performance and memory usage but with pro and cons regarding the interface. They want a solution that works also on the base of the current BigInteger calculation core (BigIntegerCalculator). I also presented a theoretical solution with a swap buffer for this. The simulation works but such a simple approach has many limits. This is the situation. |
Beta Was this translation helpful? Give feedback.
-
....so, here's the thing. There ends up being this spectrum between where the operations are (relatively) few enough between needed results you don't want/need the stack analysis (but maybe you want a mutable version), or you get expression trees large enough that performing the extra analysis is valuable time spent - especially if you could "compile" them to run over arbitrary input. It's this latter end of things that's likely better served by a third party package. (And one that may benefit by writing them into source generators - because the compiler/JIT isn't likely to be able to perform that deep an analysis on its own for an arbitrary type like that) |
Beta Was this translation helpful? Give feedback.
-
Solution found using standard arithmetic expressions (using operators) that makes the builder approach obsolete.Sample code, benchmarks and case studies for several number types can be found in the Test app if compiled for .NET 7.0. |
Beta Was this translation helpful? Give feedback.
Solution found using standard arithmetic expressions (using operators) that makes the builder approach obsolete.
Sample code, benchmarks and case studies for several number types can be found in the Test app if compiled for .NET 7.0.
Also
BigInteger
could use the approach, which would solve at least the memory waste problem.Measured only 2% performance overhead to an ideal builder solution.
A detailed explanation of how it works will follow shortly in the BigRational Proposal discussion.