Search code examples
c++gmparbitrary-precision

Arbitrary precision arithmetic with GMP


I'm using the GMP library to make a Pi program, that will calculate about 7 trillion digits of Pi. Problem is, I can't figure out how many bits are needed to hold that many decimal places.


Solution

  • 7 trillion digits can represent any of 10^(7 trillion) distinct numbers.

    x bits can represent 2^x distinct numbers.

    So you want to solve:

    2^x = 10^7000000000000
    

    Take the log-base-2 of both sides:

    x = log2(10^7000000000000)
    

    Recall that log(a^b) = b * log(a):

    x = 7000000000000 * log2(10)
    

    I get 23253496664212 bits. I would add one or two more just to be safe. Good luck finding the petabytes to hold them, though.

    I suspect you are going to need a more interesting algorithm.