UPDATE :
This question is not homework. And not waterproof apparantly…
I wanted a discussion about internal representation.
Of course : the add1000 ought to add 1000.
**Please answer in the spirit of this question… Making this waterproof would make this question longer without no reason.. **
You can beat a pure decimal representation
Changing internal representation in runtime
UPDATE 2 : see
Create a type that implements this interface :
interface INumber
{
void add1000();
void SetValue(decimal d);
decimal GetValue();
}
so that i iterates as fast as possible from 0 to 10 billion (american billion, so till 10e9) in this for loop :
private static void DoSomeAdding(INumber n)
{
Debug.Assert(n.GetValue()==0);
for (long i=0; i<10000000000; i += 1000)
{
n.add1000();
}
Debug.Assert(n.GetValue() == 10000000000);
}
So you can call it as :
DoSomeAdding(new YourNumberClass());
Like Anton’s solution, but with a bit more care 🙂 Oh, and I’ve changed the names to be more .NET-like.