Question

There is a calculator program that I have came across on Windows long ago. I couldn't recall its name, but one impressive thing about it is that it can calculate numbers up to 512 bytes size. Requesting a pi value, for instance, it can give out numbers with hundreds of digits. (but of course it takes a few seconds to output) Normally, an int would be 4 bytes, double 8 bytes, etc.

Now, how can we do that? How can we allocate a variable for numbers that can exceed the normal range? (as in, int is 4 bytes, long is 8 bytes) And how to prevent overflow and underflow in this case? Assume that this is C++.

Was it helpful?

Solution

Google around for the terms arbitrary-precision arithmetic and multiple-precision arithmetic. The number of libraries plus the number of applications which implement such arithmetic probably exceeds 2^32.

OTHER TIPS

You can do this using the GNU MP library, or one of its competitors.

You can also roll your own, if you're good at maths.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top