Emulating numerical operations in software
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)
PS:
I am C/C++ programmer.
And this limita开发者_如何学运维tion started bugging me from my school days.
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
精彩评论