Calculating big numbers

Started by
14 comments, last by Bob Janova 17 years, 4 months ago
Quote:Original post by Rockoon1
Is an estimate good enough?

For instance the practical difference between 10^1000 frames and 10^1000 + 100,0000,000 frames is moot, right? The extra 100 million is a very minor component in such a case.

Doing estimation, just two 64 bit integers should be more than enough for you. One holds the 'mantissa' of a value and the other holds the 'exponent' (preferably base-2)

Working out the precise details involved should be quite easy. Multiplication involves multiplying mantissas and adding exponents, taking care to compensate for overflows by shifting the mantissa and biasing the exponent.


Yeah, the main reason for which I need that number is for remaining time calculations. A result of 100 years with a +-1 year error margin won't trouble me much (as far as I can give a rough error esteem).

Note that I don't need to keep track of the current frame's number, unless I decide to show a progress bar and a dynamic time countdown, which sounds pretty useless. Maybe I might do the opposite, like telling the algorithm "start from 35%", alias "skip the first n frames", but I can work around this so that I can keep every big number outside the main loop.

Can you give me a better explanation of what you said? Are you telling me to "simulate" a larger IEEE float functioning?
"Your time has long pasted... Be gone now... You are ancient history..." - Vampire Night
Advertisement
Quote:Original post by AnitraZ
Are you telling me to "simulate" a larger IEEE float functioning?


Yes thats basically it, 'cept you dont have to mess with the dirty details (such as an implied bit, a biased exponent, or obsessive normalization)

While it might seem intimidating at first, its really not. Its only slightly more complex than 'fixed point' math.

You can work it out on paper (or in notepad) by using scientific notation (base-10 rather than base-2) and a bit of reasoning ability.. for instance:

x = 4E+12
y = 6E+14

multiplication:

x * y = (4 * 6)E+(12+14) = 24E+26

having worked it out, if you define a structure with '.m' being the mantissa and '.e' being the exponent, then multiplication in general:

z.m = x.m * y.m;
z.e = x.e + y.e;

From here its just a matter of avoiding (or handling) overflows in the mantissa (its highly unlikely that you will overflow a 64-bit exponent in any reasonable calculation)
How about rather than having events based on the absolute position of the frame you could do amount of frames until the next event. That could result in smaller managable numbers.
You could just make your own base system with an array. Each element holds an integer representing the next digit, up to the base number.

So a Base-100 system with the array being {99,34,0,3} stores the value 3*(100^3) + 0*(100^2) + 34*(100^1) + 99*(100^0).

So if you had a Base-10,000 system, you'd just need an array of 750 elements to hold a number of size 10^3000.


I just realized that's exactly what grhodes said in his first post, but maybe that example helps.
Rockoon1:
Thanks a lot, I'm sure that will be of help.

Swarmer:
I didn't solve the maths, but I guess using a 232 base with DWORD elements will yeld Graham's result. Though, it's an interesting point of view, this thread has revealed to be a good source for ideas :)

SuperNerd:
I still need the total count. At launch, I'm doing some dummy speed measurement, assuming my CPU can handle m frames per second and that I have a total of n frames, I need to compute n / m / 3600 / 24 etc, having sub-numbers is gonna turn into Swarmer's solution.

Thanks for answering, I'll let u guys know if things go well :)
"Your time has long pasted... Be gone now... You are ancient history..." - Vampire Night
I wrote a large number class in C# which can count up to 1e3000, though I wouldn't recommend starting from 1 ;). I really ought to write it up and post it on CodeProject or something, but if you're interested I'll post the code.

This topic is closed to new replies.

Advertisement