This probably has probably been asked before, but I couldn’t find anything relevant.
Would it be possible/performant to implement a kind of arbitrary/decimal arithmatic through a specialized class or struct of an arbitrary/fixed amount of integers?
Let me clarify; floats (as I understand them), use a mantissa, exponent and perhaps a sign bit. They are capable of representing 1/2, 1/4, 1/8, etc… but not, say 1/10.
What I propose is something like this: let the number be represented by a plain int or int64_t or a series of these, keeping only the values up to a power of ten completely covered by the range of the integer (so for a 32-bit int, the limit would be 10^8-1). This only to keep calculation easy. Add to that an exponent, that will say what power of ten it should be multiplied to get the real number.
An example: 125.36 would be represented by an int equal to 12536 and a power of ten equal to -2. This number would in effect take as much as too ints, and a bunch of special functions.
Another example: 4,294,967,297 (which is 2^32+1) would take two ints, one equal to 4, the other equal to 294,967,297, and a power of ten equal to 0.
Is this a good idea, throwing away all this memory for a simpler/faster arithmetic? I haven’t quite thought out the overflow part, perhaps calculating int32_t+int32_t=int64_t would solve that beautifully?
See the General Decimal Arithmetic page