def convert_base(x, base=3, precision=None):
    length_of_int = int(math.log(x, base))
iexps = list(range(length_of_int, -1, -1))
if precision == None:
    fexps = itertools.count(-1, -1)
else:
    fexps = list(range(-1, -int(precision + 1), -1))

def cbgen(x, base, exponents):
    for e in exponents:
        d = int(x // base ** e)
x -= d * base ** e
yield d
if x == 0 and e < 0:
    break
return cbgen(int(x), base, iexps), cbgen(x - int(x), base, fexps)
How to convert floating point number to base 3 in python