def _mpq_to_str(a, base): l = (gmp.mpz_sizeinbase(gmp.mpq_numref(a), base) + gmp.mpz_sizeinbase(gmp.mpq_denref(a), base) + 3) p = ffi.new('char[]', l) gmp.mpq_get_str(p, base, a) if PY3: return ffi.string(p).decode('UTF-8') else: return ffi.string(p)
def _mpz_to_str(a, base): """ Return string representation of a in base base. :type a: mpz_t :param base: 2..62 :type base: int :rtype: str """ l = gmp.mpz_sizeinbase(a, base) + 2 p = ffi.new('char[]', l) gmp.mpz_get_str(p, base, a) if PY3: return ffi.string(p).decode('UTF-8') else: return ffi.string(p)
def _mpz_to_pylong(a): """ Convert a to a python long. :type a: mpz_t :rtype: long """ size = ffi.sizeof('uint64_t') numb = 8 * size count = (gmp.mpz_sizeinbase(a, 2) + numb - 1) // numb p = ffi.new('uint64_t[]', count) gmp.mpz_export(p, ffi.NULL, 1, size, 0, 0, a) res = 0 for n in p: res = (res << numb) + n return res * gmp.mpz_sgn(a)