def base3int(x):
    x = int(x)
exponents = list(range(int(math.log(x, 3)), -1, -1))
for e in exponents:
    d = int(x // 3 ** e)
x -= d * 3 ** e
yield d

def base3fraction(x, precision=1000):
    x = x - int(x)
exponents = list(range(-1, (-precision - 1) * 2, -1))
for e in exponents:
    d = int(x // 3 ** e)
x -= d * 3 ** e
yield d
if x == 0:
    break
How to convert floating point number to base 3 in python