I'll start that off with a little dramatization.
Me: Give me a number, please.
The compiler: What kind of a number would you like, sir? We have ints, we have longs, we have de-ci-mals...
Me: Don't know. Don't care. Just a number - you know, one-two-three... A number.
The compiler: I'm afraid that won't do, sir. You'll have to be more specific.
Me (frustrated): Will you give me a ... ... ... NUMBER, you ... ... ... ... ... of a ... ... ... !!!!!!!
The compiler: Sir, you'll have to leave now.
OK, so the point is - what do I care what kind of a number? Just a number! The .Net Framework has gone to great pains in order to abstract away many of the boilerplate programming tasks, but the .Net languages still retain the numerical types system inherited from C - and even add new types! So you have to choose between ints, longs, sbytes, etc., etc. And you always stick in ints anyway, right? Granted, for some more specialized sorts of applications you still need the different numerical types. But for your everyday programming, wouldn't it be nice if we had something like a generic Number type that can hold any kind of number, be it int or float or decimal? And why should it be limited to decimal.MaxValue? What if I want decimal.MaxValue + 1?
What I have in mind is a class that represents a number as a list of digits. That way the only limitation for the size of the number is going to be your available memory. And you'll save space, because the list will contain just as much digits as you need. If you have the number 1, all the memory you need is the memory to save one digit. If you stick 1 in a decimal, it remains as obscenely big as if you put in a billion.
To try that idea in practice, I've devised a simple Number class that holds its digits in a LinkedList<int>. I've overloaded the arithmetic operators, so you can do something like that:
Number biggerThanTheDevil = decimal.MaxValue;
biggerThanTheDevil *= decimal.MaxValue;
biggerThanTheDevil *= decimal.MaxValue;
biggerThanTheDevil += 1;
// And so on, and so forth
Also, you get some additional functionality for free - like adding or removing digits from both ends of the number:
Number shrinker = new Number("1234567");
shrinker = Number.RemoveLastDigit(shrinker); // shrinker == 123456
shrinker = Number.RemoveLastDigit(shrinker); // shrinker == 12345
shrinker = Number.AppendFirstDigit(shrinker, 9); // shrinker == 912345The Number class is immutable, so you can assign one Number to another by simply assigning the reference:
Number srcNumber = 12.345;
Number destNumber = srcNumber; // No problem - the class is immutable
There's one more difference I'd like to point out. I've overloaded the << and >> operators, but I've let them perform
decimal shifts, not binary ones. That means that (10 >> 1 == 100) and (234.5 << 2 == 2.345). I think that is a better and more intuitive way for these operators to work in a high-level framework as .Net. Anyway, it's just an experiment.
The Number class is really just a simple show-case. The algorithms I've come up with for the arithmetic operations are not the most optimal as I'm not that good at maths. But if something like that gets implemented by a better developer than myself and then is included in the core .Net functionality, I think we'd all benefit.
In the end I'd like to say that I intend to re-write the Number class in C++ using direct memory management to see what performance boost will I get. When I have some results, I'll post them here.
The source code for the Number class is
here.
PS: I've asked a question on
StackOverflow (an excellent site, by the way!) about some aspects of the Number class and I've got some very good replies that helped me improve it. Thanks goes out to
Jon Skeet and
cristianlibardo.
Regards,
Boyan