This might be more suitable for Stack Overflow, but since I'm asking about the mathematical theory, rather than the implementation details, I'm initially posting the question here.
Background
The .NET 7 Framework is about to introduce an INumber<T> interface, which is designed to allow for the generic implementation of mathematical algorithms that will work with any type of number: int, float, double, decimal, etc.
However, when creating an implementation of this interface, one is only required to satisfy the signature of the interface, there is nothing inherent in the framework to ensure your interface makes sense. For instance, the INumber<T>.AdditiveIdentity is required to return an object of type T, but there's nothing inherent in the framework that would stop me setting additive identity of my type to something nonsensical.
So, I'm attempting to write a test framework that assess these implementations.
Actual Question
So, I'm starting by writing a suite of tests for the integer type, just to get into the swing of things, I'll make it generic later. The problem is, I've realised that I can only really define the common operators +-*/ in terms of eachother.
For instance, I can test that x + x = 2 * x but what is addition in the first place and how can I assess whether it's been done sensibly, even if I restrict this to the most simple case of integers.
I suppose I could test for commutivity, associativity, and that addition of the additive identity doesn't change the value (amongst some other things) and that would be a good smell test, but I'm wondering if there's anything more formal I can look for.
INumber<T>(2)+INumber<T>(2)and give youINumber<T>(4), but that doesn't imply that next time you evaluate the same expression you get the same result. – plop Sep 03 '22 at 00:19