Recently a friend asked me a some questions:
“97 raised to the power of 242 has equalled infinity on every calculator I’ve used, but it’s not infinite just very big. Why do they say infinity? And what is 97^242?”
The answer to the first part is easy; precision (and hence maximum values) are limited. Since this isn’t a basic intro to binary and computing we won’t go into it but just say in the good old days this would have resulted in the number simply wrapping. Modern devices and systems detect this overflow and show a special case result, usually Infinity/Inf or sometimes Not-a-Number/NaN.
Above are two common tools (R and Matlab) both running on 64-bit Linux and overflowing for 97^242. The difference in overflow can be seen in that the OSX Calculator can handle 97^71 but overflows at 96^72, whereas both Matlab and R will handle 97^166 but not 97^156.
Ok well Google has a calculator function so maybe we can just ask it for 97^242?
Alas, no. But maybe if we trick it with 97+242 to get it’s calculator up and then use that?
Nope. So how do we go about trying to calculate 97^242?
There are approaches we can use to estimate it (one of the most promising being looking for differences between powers of 100 and 97 then extrapolating, maybe something for another blog post) but we want an exact answer. As shown by Matlab/R and common logic built-in types are just too small and will overflow.
The solution comes, as with so much in life, from GNU in the GNU Multiple Precision Arithmetic Library aka GNU MP Bignum.
Once installed (yum install gmp-devel on Fedora) it’s just a case of hacking together some C to calculate the result (note the below isn’t supposed to be efficient, more transparent):
int main(int ac, char** av)
unsigned long int startnumber = 97;
for (i=0; i<(242-1); ++i)
mpz_mul(number, number, start);
gmp_printf ("Answer: %Zd\n\n", number);
Build this with: gcc bignum.c -lgmp -o bignum
Which for those who can’t be bothered to count digits is rounded as 6.29E+480.
Though of course after all this loading of libraries and writing of C it turns out that although Google couldn’t answer it, naturally Wolfram|Alpha could: