pythonmathbinarydecimalradix

Convert float base 2 to base 10 to 100+ decimal places


I'm trying to find more decimal places to the 'Prime Constant'. The output maxes out at 51 decimal places after using getcontext().prec=100

from decimal import *
getcontext().prec = 100
base = 2
s = "0.0110101000101000101000100000101000001000101000100000100000101000001000101000001000100000100000001000101000101000100000000000001000100000101000000000101000001000001000100000100000101000000000101000101000000000001000000000001000101000100000101000000000100000100000100000101000001000101000000000100000000000001000101000100000000000001000001000000000101000100000100000001000001000001000100000100000001000100000001000000000101000000000101000001000100000100000001000101000100000000000100000001000100000001000100000100000000000101000000000000000001000001000000000100000100000101000001000000000100000100000101000001000001000101000000000001000000000101000100000100000101000000000001000100000100000001000000000100000001000000000100000001000001000001000100000001000001000100000001000100000000000001000000000100000000000101000000000101000101000000000100000000000001000101000100000000000001000101000100000000000000000001000100000001000000000100000001000100000100000100000000000001000100000100000100000001000001000000000001000100000101000"
split = s.split(".")
joinsplit = "".join(split)
position = len(split[1]) if len(split) > 1 else 0
divisor = base ** position
base10 = Decimal(int(joinsplit, base=base) / divisor)
print(base10)

Solution

  • Try

    base10 = Decimal(int(joinsplit, base=base)) / Decimal(divisor)
    

    You need to have the Decimal package doing the division.