Working with big, big numbers

Started by
15 comments, last by silverphyre673 18 years, 10 months ago
A big number library is probably implemented as a string of 32-bit digits anyway. An array of longs would work, so long as you knew how to work with them. I implemented a big number library using straight std::strings, and encoded my numbers in base 256. It worked great, except that I was never able to find an efficient method for changing the numbers to decimal for display.
Advertisement
I just recently wrote an arbitrary arithmetic library for a contest. I stored the numbers as a std::vector of ints, and it worked great. No speed problems. I was able to calculate divisions with 100 thousand digits of precision, though it took literally about 15 minutes to calculate them all!!!

If you need absolutely huge numbers, I recommend storing them like I did and implementing the arithmetical operations by emulating the hand-written equivalents.
Having that much accuracy is rather silly. Say you take something like the mass of the sun, which we'll say has 10 significant digits and 20 insignificant digits, and you add the mass of something very small (as given) with 10 significant digits (note if you multiply, the only significant digits remaining are whichever is the lowest number, so we'd be down to 10). So, now you have a number with 10 significant digits, followed by 20 insignificant digits, followed by 10 more. Obviously those last 10, are in fact, insignificant. This is how significant digits work. Unless you have values which are significant to those 40 digits, don't bother.
nilk: Did you do it by the method suggested by Tuna, or my the method suggested by me? I don't need 100 thousand digits of precision, just a mere 50 :P

Right now I'm looking into <bitset>, which basically allows you to make an array of an arbitrary number of bits, and has methods that make setting, clearing and checking bits much easier (using operator [] rather than the arcase >>, <<, ~, etc). I'll let you know. Knowing me, I will come out of this with a whole big class that I will never use again.

my siteGenius is 1% inspiration and 99% perspiration
Quote:Original post by silverphyre673
nilk: Did you do it by the method suggested by Tuna, or my the method suggested by me? I don't need 100 thousand digits of precision, just a mere 50 :P

Right now I'm looking into <bitset>, which basically allows you to make an array of an arbitrary number of bits, and has methods that make setting, clearing and checking bits much easier (using operator [] rather than the arcase >>, <<, ~, etc). I'll let you know. Knowing me, I will come out of this with a whole big class that I will never use again.


I just took the easy way out and stored an array of integers. It really wasn't that slow at all, as long as I didn't use too huge of numbers, or require 100 thousand digits of precision :)

I handled numbers with a decimal by splitting the digits into two arrays: pre-decimal and post-decimal. This made it easy to write a function to print to the screen, and arithmetic operations were much simpler this way (especially division, which was the hard one [smile]).

Quote:I obviously can't use a double, because the mantissa keeps it from having the neccesary accuracy.


That's not very obvious to me. The general rule is that, as long as you only multiply and divide, then doubles are just fine. If you add or subtract, then the numbers need to be within, say, 10e5 from each other (more like 10e2 for floats) to retain useful precision.

Gravitation is just multiplication/division. The only addition is the summing up of forces for each body at the end, and there it doesn't matter much if a really large force drowns out the effect of a really small force.
enum Bool { True, False, FileNotFound };
Actually, thats true hplus. Just thought about that :)

I did get GMP, and even found a devpak of it, which was nice. I don't think I'll use it though, because of the whole multiplication/division vs. add/subtract thing. Although I might need it later. Anyways, thanks for your help, suggestions and comments guys. Somehow, we arrived at a solution.
my siteGenius is 1% inspiration and 99% perspiration

This topic is closed to new replies.

Advertisement