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 _mpfr_to_str(a): precision = int(log10(2) * gmp.mpfr_get_prec(a) + 2) buf = ffi.new('char []', precision + 10) fmtstr = "%.{0}Rg".format(precision) buflen = gmp.mpfr_sprintf(buf, fmtstr.encode('UTF-8'), a) if PY3: pybuf = ffi.string(buf).decode('UTF-8') else: pybuf = ffi.string(buf) if gmp.mpfr_number_p(a) and '.' not in pybuf: pybuf = pybuf + '.0' return pybuf
def mpc_version(): """ mpc_version() -> string Return string giving current MPC version. Return None if MPC support is not available. """ return "MPC {0}".format(ffi.string(gmp.mpc_get_version()))
def mp_version(): """ mp_version() -> string Return string giving the name and version of the multiple precision library used. """ return "GMP {0}".format(ffi.string(gmp.gmp_version))
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)