views:

19

answers:

1

Hi,

The numerical operations we do in our programs are limited by the number of bytes that a language specifies for a given datatype (or maybe hardware supports). Say I can use integer to do calculations on my paycheck (even "short" is more than enough for a year's earning!!! ;) ) but can't do the same with Bill Gates wealth. So, we go for things like long long and stuff. But aren't we still at the mercy of number of bits that are given to us.

So, how about if I emulate numerical operations in software? Say a class that abstracts and can do numerical operations on numbers with 1000s of digits... Of course it will be too too slow, but I am not much worried about complexity but looking more at just computability...

Maybe I can use it to calculate PI to 1000 digit accuracy in a months or a Mersenne Primes in few years and take home $100K ;)

So now my question, 1) Are there already any such libraries to do this kind of stuff out there (In C/C++). 2) If I go about implementing one, do you have any suggestions for me? (+, -, *, /, %, <<, >> operations should enough I guess)

Regards,

Microkernel

PS:

1) I am C/C++ programmer if you want to know.

2) And this limitation started bugging me from my school days.

+2  A: 

Such datatypes are known as Arbitrary-precision numbers. In Java, there are the classes BigDecimal and BigInteger which handle basic operations (+, -, *, /) on digit level. They have no 'built-in' size limitation. They are actually not that slow and are used in a lot of real-world domains.

C/C++ don't have it built-in but there are a lot of libraries out there. See a list here: http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#Libraries

Wolfgang
Actually, arbitrary-precision numbers are pretty much the standard type for integers in the majority of languages. Languages like C, C++, Java, C#, Objective-C which cannot even properly add two integers are really the exception.
Jörg W Mittag