def __xor__(self, other): res = _new_mpz() if isinstance(other, (int, long)): oth = _new_mpz() _pyint_to_mpz(other, oth) gmp.mpz_xor(res, self._mpz, oth) _del_mpz(oth) else: gmp.mpz_xor(res, self._mpz, other._mpz) return mpz._from_c_mpz(res)
def __rmod__(self, other): if not isinstance(other, (int, long)): return NotImplemented if self == 0: raise ZeroDivisionError("mpz modulo by zero") r = _new_mpz() oth = _new_mpz() _pylong_to_mpz(other, oth) gmp.mpz_fdiv_r(r, oth, self._mpz) _del_mpz(oth) return mpz._from_c_mpz(r)
def __rmod__(self, other): if not isinstance(other, (int, long)): return NotImplemented if self == 0: raise ZeroDivisionError('mpz modulo by zero') r = _new_mpz() oth = _new_mpz() _pylong_to_mpz(other, oth) gmp.mpz_fdiv_r(r, oth, self._mpz) _del_mpz(oth) return mpz._from_c_mpz(r)
def __sub__(self, other): if isinstance(other, (int, long)): res = _new_mpz() if 0 <= other <= MAX_UI: gmp.mpz_sub_ui(res, self._mpz, other) else: _pylong_to_mpz(other, res) gmp.mpz_sub(res, self._mpz, res) return mpz._from_c_mpz(res) elif isinstance(other, mpz): res = _new_mpz() gmp.mpz_sub(res, self._mpz, other._mpz) return mpz._from_c_mpz(res) else: return NotImplemented
def __pow__(self, other): res = _new_mpfr() if isinstance(other, mpfr): gmp.mpfr_pow(res, self._mpfr, other._mpfr, gmp.MPFR_RNDN) elif isinstance(other, mpq): # There is no mpfr_pow_q gmp.mpfr_set_q(res, other._mpq, gmp.MPFR_RNDN) gmp.mpfr_pow(res, self._mpfr, res, gmp.MPFR_RNDN) elif isinstance(other, mpz): gmp.mpfr_pow_z(res, self._mpfr, other._mpz, gmp.MPFR_RNDN) elif isinstance(other, float): # There is no mpfr_pow_d gmp.mpfr_set_d(res, other, gmp.MPFR_RNDN) gmp.mpfr_pow(res, self._mpfr, res, gmp.MPFR_RNDN) elif isinstance(other, (int, long)): if -sys.maxsize - 1 <= other <= sys.maxsize: gmp.mpfr_pow_si(res, self._mpfr, other, gmp.MPFR_RNDN) elif 0 <= other <= MAX_UI: gmp.mpfr_pow_ui(res, self._mpfr, other, gmp.MPFR_RNDN) else: tmp_mpz = _new_mpz() _pylong_to_mpz(other, tmp_mpz) gmp.mpfr_pow_z(res, self._mpfr, tmp_mpz, gmp.MPFR_RNDN) _del_mpz(tmp_mpz) else: return NotImplemented return mpfr._from_c_mpfr(res)
def __rtruediv__(self, other): res = _new_mpfr() if isinstance(other, mpq): # There is no mpfr_q_div gmp.mpfr_set_q(res, other._mpq, gmp.MPFR_RNDN) gmp.mpfr_div(res, res, self._mpfr, gmp.MPFR_RNDN) pass elif isinstance(other, mpz): # There is no mpfr_z_div gmp.mpfr_set_z(res, other._mpz, gmp.MPFR_RNDN) gmp.mpfr_div(res, res, self._mpfr, gmp.MPFR_RNDN) elif isinstance(other, float): gmp.mpfr_d_div(res, other, self._mpfr, gmp.MPFR_RNDN) elif isinstance(other, (int, long)): if -sys.maxsize - 1 <= other <= sys.maxsize: gmp.mpfr_si_div(res, other, self._mpfr, gmp.MPFR_RNDN) elif 0 <= other <= MAX_UI: gmp.mpfr_ui_div(res, other, self._mpfr, gmp.MPFR_RNDN) else: tmp_mpz = _new_mpz() _pylong_to_mpz(other, tmp_mpz) gmp.mpfr_set_z(res, tmp_mpz, gmp.MPFR_RNDN) gmp.mpfr_div(res, res, self._mpfr, gmp.MPFR_RNDN) _del_mpz(tmp_mpz) else: return NotImplemented return mpfr._from_c_mpfr(res)
def __rpow__(self, other): res = _new_mpc() # TODO use context precision if isinstance(other, mpfr): gmp.mpc_set_fr(res, other._mpfr, gmp.MPFR_RNDN) gmp.mpc_pow(res, res, self._mpc, gmp.MPFR_RNDN) elif isinstance(other, mpq): gmp.mpc_set_q(res, other._mpq, gmp.MPFR_RNDN) gmp.mpc_pow(res, res, self._mpc, gmp.MPFR_RNDN) elif isinstance(other, mpz): gmp.mpc_set_z(res, other._mpz, gmp.MPC_RNDNN) gmp.mpc_pow(res, res, self._mpc, gmp.MPFR_RNDN) elif isinstance(other, complex): gmp.mpc_set_d_d(res, other.real, other.imag, gmp.MPC_RNDNN) gmp.mpc_pow(res, res, self._mpc, gmp.MPC_RNDNN) elif isinstance(other, float): gmp.mpc_set_d(res, other, gmp.MPFR_RNDN) gmp.mpc_pow(res, res, self._mpc, gmp.MPC_RNDNN) elif isinstance(other, (int, long)): if 0 <= other <= MAX_UI: gmp.mpc_set_ui(res, other, gmp.MPC_RNDNN) elif -sys.maxsize - 1 <= other <= sys.maxsize: gmp.mpc_set_si(res, other, gmp.MPC_RNDNN) else: tmp_mpz = _new_mpz() _pyint_to_mpz(other, tmp_mpz) gmp.mpc_set_z(res, tmp_mpz, gmp.MPC_RNDNN) _del_mpz(tmp_mpz) gmp.mpc_pow(res, res, self._mpc, gmp.MPC_RNDNN) else: return NotImplemented return mpc._from_c_mpc(res)
def __rshift__(self, other): if not isinstance(other, (int, long, mpz)): return NotImplemented oth = gmp.mpz_get_ui(other._mpz) if isinstance(other, mpz) else other res = _new_mpz() gmp.mpz_fdiv_q_2exp(res, self._mpz, oth) return mpz._from_c_mpz(res)
def __rsub__(self, other): res = _new_mpc() # TODO use context precision if isinstance(other, mpfr): gmp.mpc_fr_sub(res, other._mpfr, self._mpc, gmp.MPC_RNDNN) elif isinstance(other, mpq): gmp.mpfr_sub_q(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other._mpq, gmp.MPFR_RNDN) gmp.mpfr_neg(gmp.mpc_realref(res), gmp.mpc_realref(res), gmp.MPFR_RNDN) gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) elif isinstance(other, mpz): gmp.mpfr_z_sub(gmp.mpc_realref(res), other._mpz, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN) gmp.mpfr_neg(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) elif isinstance(other, complex): gmp.mpfr_d_sub(gmp.mpc_realref(res), other.real, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN) gmp.mpfr_d_sub(gmp.mpc_imagref(res), other.imag, gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) elif isinstance(other, float): gmp.mpfr_d_sub(gmp.mpc_realref(res), other, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN) gmp.mpfr_neg(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) elif isinstance(other, (int, long)): if 0 <= other <= MAX_UI: gmp.mpc_ui_sub(res, other, self._mpc, gmp.MPC_RNDNN) elif -sys.maxsize - 1 <= other <= sys.maxsize: gmp.mpfr_si_sub(gmp.mpc_realref(res), other, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN) gmp.mpfr_neg(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) else: tmp_mpz = _new_mpz() _pyint_to_mpz(other, tmp_mpz) gmp.mpfr_z_sub(gmp.mpc_realref(res), tmp_mpz, gmp.mpc_realref(self._mpc), gmp.MPFR_RNDN) gmp.mpfr_neg(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) _del_mpz(tmp_mpz) else: return NotImplemented return mpc._from_c_mpc(res)
def __rfloordiv__(self, other): if isinstance(other, (int, long)): if self == 0: raise ZeroDivisionError('mpz division by zero') res = _new_mpz() _pylong_to_mpz(other, res) gmp.mpz_fdiv_q(res, res, self._mpz) return mpz._from_c_mpz(res) else: return NotImplemented
def __floordiv__(self, other): if isinstance(other, (int, long)): if other == 0: raise ZeroDivisionError('mpz division by zero') res = _new_mpz() if 0 < other <= MAX_UI: gmp.mpz_fdiv_q_ui(res, self._mpz, other) else: _pylong_to_mpz(other, res) gmp.mpz_fdiv_q(res, self._mpz, res) return mpz._from_c_mpz(res) elif isinstance(other, mpz): if other == 0: raise ZeroDivisionError('mpz division by zero') res = _new_mpz() gmp.mpz_fdiv_q(res, self._mpz, other._mpz) return mpz._from_c_mpz(res) else: return NotImplemented
def __rfloordiv__(self, other): if isinstance(other, (int, long)): if self == 0: raise ZeroDivisionError("mpz division by zero") res = _new_mpz() _pylong_to_mpz(other, res) gmp.mpz_fdiv_q(res, res, self._mpz) return mpz._from_c_mpz(res) else: return NotImplemented
def __floordiv__(self, other): if isinstance(other, (int, long)): if other == 0: raise ZeroDivisionError("mpz division by zero") res = _new_mpz() if 0 < other <= MAX_UI: gmp.mpz_fdiv_q_ui(res, self._mpz, other) else: _pylong_to_mpz(other, res) gmp.mpz_fdiv_q(res, self._mpz, res) return mpz._from_c_mpz(res) elif isinstance(other, mpz): if other == 0: raise ZeroDivisionError("mpz division by zero") res = _new_mpz() gmp.mpz_fdiv_q(res, self._mpz, other._mpz) return mpz._from_c_mpz(res) else: return NotImplemented
def __mod__(self, other): if isinstance(other, (int, long)): if other == 0: raise ZeroDivisionError("mpz modulo by zero") r = _new_mpz() if 0 <= other <= MAX_UI: gmp.mpz_fdiv_r_ui(r, self._mpz, other) else: oth = _new_mpz() _pylong_to_mpz(other, oth) gmp.mpz_fdiv_r(r, self._mpz, oth) _del_mpz(oth) return mpz._from_c_mpz(r) elif isinstance(other, mpz): if other == 0: raise ZeroDivisionError("mpz modulo by zero") r = _new_mpz() gmp.mpz_fdiv_r(r, self._mpz, other._mpz) return mpz._from_c_mpz(r) else: return NotImplemented
def __rpow__(self, other): if not isinstance(other, (int, long)): return NotImplemented if self < 0: raise ValueError('mpz.pow with negative exponent') res = _new_mpz() exp = int(self) if exp > MAX_UI: raise ValueError('mpz.pow with outragous exponent') if 0 <= other <= MAX_UI: gmp.mpz_ui_pow_ui(res, other, exp) else: base = _new_mpz() _pylong_to_mpz(other, base) gmp.mpz_pow_ui(res, base, exp) _del_mpz(base) return mpz._from_c_mpz(res)
def __mod__(self, other): if isinstance(other, (int, long)): if other == 0: raise ZeroDivisionError('mpz modulo by zero') r = _new_mpz() if 0 <= other <= MAX_UI: gmp.mpz_fdiv_r_ui(r, self._mpz, other) else: oth = _new_mpz() _pylong_to_mpz(other, oth) gmp.mpz_fdiv_r(r, self._mpz, oth) _del_mpz(oth) return mpz._from_c_mpz(r) elif isinstance(other, mpz): if other == 0: raise ZeroDivisionError('mpz modulo by zero') r = _new_mpz() gmp.mpz_fdiv_r(r, self._mpz, other._mpz) return mpz._from_c_mpz(r) else: return NotImplemented
def __rpow__(self, other): if not isinstance(other, (int, long)): return NotImplemented if self < 0: raise ValueError("mpz.pow with negative exponent") res = _new_mpz() exp = int(self) if exp > MAX_UI: raise ValueError("mpz.pow with outragous exponent") if 0 <= other <= MAX_UI: gmp.mpz_ui_pow_ui(res, other, exp) else: base = _new_mpz() _pylong_to_mpz(other, base) gmp.mpz_pow_ui(res, base, exp) _del_mpz(base) return mpz._from_c_mpz(res)
def __pow__(self, power, modulo=None): if not isinstance(power, (int, long, mpz)): return NotImplemented if modulo is not None and not isinstance(modulo, (int, long, mpz)): return NotImplemented if power < 0: raise ValueError('mpz.pow with negative exponent') res = _new_mpz() if modulo is None: exp = int(power) if exp > MAX_UI: raise ValueError('mpz.pow with outragous exponent') gmp.mpz_pow_ui(res, self._mpz, exp) else: del_mod = del_exp = False if isinstance(modulo, (int, long)): mod = _new_mpz() _pylong_to_mpz(abs(modulo), mod) del_mod = True else: mod = modulo._mpz if isinstance(power, (int, long)) and power <= MAX_UI: gmp.mpz_powm_ui(res, self._mpz, power, mod) else: if isinstance(power, (int, long)): exp = _new_mpz() _pylong_to_mpz(power, exp) del_exp = True else: exp = power._mpz gmp.mpz_powm(res, self._mpz, exp, mod) if del_exp: _del_mpz(exp) if del_mod: _del_mpz(mod) return mpz._from_c_mpz(res)
def __int__(self): if not gmp.mpfr_number_p(self._mpfr): raise ValueError("Cannot convert '%s' to int" % self) elif gmp.mpfr_fits_slong_p(self._mpfr, gmp.MPFR_RNDN): return gmp.mpfr_get_si(self._mpfr, gmp.MPFR_RNDN) elif gmp.mpfr_fits_ulong_p(self._mpfr, gmp.MPFR_RNDN): return gmp.mpfr_get_ui(self._mpfr, gmp.MPFR_RNDN) else: tmp_mpz = _new_mpz() gmp.mpfr_get_z(tmp_mpz, self._mpfr, gmp.MPFR_RNDN) res = _mpz_to_pylong(tmp_mpz) _del_mpz(tmp_mpz) return res
def __pow__(self, power, modulo=None): if not isinstance(power, (int, long, mpz)): return NotImplemented if modulo is not None and not isinstance(modulo, (int, long, mpz)): return NotImplemented if power < 0: raise ValueError("mpz.pow with negative exponent") res = _new_mpz() if modulo is None: exp = int(power) if exp > MAX_UI: raise ValueError("mpz.pow with outragous exponent") gmp.mpz_pow_ui(res, self._mpz, exp) else: del_mod = del_exp = False if isinstance(modulo, (int, long)): mod = _new_mpz() _pylong_to_mpz(abs(modulo), mod) del_mod = True else: mod = modulo._mpz if isinstance(power, (int, long)) and power <= MAX_UI: gmp.mpz_powm_ui(res, self._mpz, power, mod) else: if isinstance(power, (int, long)): exp = _new_mpz() _pylong_to_mpz(power, exp) del_exp = True else: exp = power._mpz gmp.mpz_powm(res, self._mpz, exp, mod) if del_exp: _del_mpz(exp) if del_mod: _del_mpz(mod) return mpz._from_c_mpz(res)
def __cmp(self, other): if isinstance(other, mpz): res = gmp.mpz_cmp(self._mpz, other._mpz) elif isinstance(other, (int, long)): if 0 <= other <= MAX_UI: res = gmp.mpz_cmp_ui(self._mpz, other) else: oth = _new_mpz() _pylong_to_mpz(other, oth) res = gmp.mpz_cmp(self._mpz, oth) _del_mpz(oth) elif isinstance(other, float): res = gmp.mpz_cmp_d(self._mpz, other) else: return None return res
def __sub__(self, other): res = _new_mpc() # TODO use context precision if isinstance(other, mpc): gmp.mpc_sub(res, self._mpc, other._mpc, gmp.MPC_RNDNN) elif isinstance(other, mpfr): gmp.mpc_sub_fr(res, self._mpc, other._mpfr, gmp.MPC_RNDNN) elif isinstance(other, mpq): gmp.mpfr_sub_q(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other._mpq, gmp.MPFR_RNDN) gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) elif isinstance(other, mpz): gmp.mpfr_sub_z(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other._mpz, gmp.MPFR_RNDN) gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) elif isinstance(other, complex): gmp.mpfr_sub_d(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other.real, gmp.MPFR_RNDN) gmp.mpfr_sub_d(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), other.imag, gmp.MPFR_RNDN) elif isinstance(other, float): gmp.mpfr_sub_d(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other, gmp.MPFR_RNDN) gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) elif isinstance(other, (int, long)): if 0 <= other <= MAX_UI: gmp.mpc_sub_ui(res, self._mpc, other, gmp.MPC_RNDNN) elif -sys.maxsize - 1 <= other <= sys.maxsize: gmp.mpfr_sub_si(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), other, gmp.MPFR_RNDN) gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) else: tmp_mpz = _new_mpz() _pyint_to_mpz(other, tmp_mpz) gmp.mpfr_sub_z(gmp.mpc_realref(res), gmp.mpc_realref(self._mpc), tmp_mpz, gmp.MPFR_RNDN) gmp.mpfr_set(gmp.mpc_imagref(res), gmp.mpc_imagref(self._mpc), gmp.MPFR_RNDN) _del_mpz(tmp_mpz) else: return NotImplemented return mpc._from_c_mpc(res)
def __init__(self, n=0, base=None): """ mpz() -> mpz(0) If no argument is given, return mpz(0). mpz(n) -> mpz Return an 'mpz' object with a numeric value 'n' (truncating n to its integer part if it's a Fraction, 'mpq', Decimal, float or 'mpfr'). mpz(s[, base=0]): Return an 'mpz' object from a string 's' made of digits in the given base. If base=0, binary, octal, or hex Python strings are recognized by leading 0b, 0o, or 0x characters, otherwise the string is assumed to be decimal. Values for base can range between 2 and 62. """ if isinstance(n, self.__class__): self._mpz = n._mpz return a = self._mpz = ffi.gc(_new_mpz(), _del_mpz) if isinstance(n, str): if base is None: base = 10 if base == 0 or 2 <= base <= 62: if gmp.mpz_set_str(a, n.encode('UTF-8'), base) != 0: raise ValueError("Can't create mpz from %s with base %s" % (n, base)) else: raise ValueError('base must be 0 or 2..62, not %s' % base) elif base is not None: raise ValueError('Base only allowed for str, not for %s.' % type(n)) elif isinstance(n, float): gmp.mpz_set_d(a, n) elif isinstance(n, (int, long)): _pyint_to_mpz(n, a) else: raise TypeError
def __init__(self, n=0, base=None): """ mpz() -> mpz(0) If no argument is given, return mpz(0). mpz(n) -> mpz Return an 'mpz' object with a numeric value 'n' (truncating n to its integer part if it's a Fraction, 'mpq', Decimal, float or 'mpfr'). mpz(s[, base=0]): Return an 'mpz' object from a string 's' made of digits in the given base. If base=0, binary, octal, or hex Python strings are recognized by leading 0b, 0o, or 0x characters, otherwise the string is assumed to be decimal. Values for base can range between 2 and 62. """ if isinstance(n, self.__class__): self._mpz = n._mpz return a = self._mpz = ffi.gc(_new_mpz(), _del_mpz) if isinstance(n, str): if base is None: base = 10 if base == 0 or 2 <= base <= 62: if gmp.mpz_set_str(a, n.encode("UTF-8"), base) != 0: raise ValueError("Can't create mpz from %s with base %s" % (n, base)) else: raise ValueError("base must be 0 or 2..62, not %s" % base) elif base is not None: raise ValueError("Base only allowed for str, not for %s." % type(n)) elif isinstance(n, float): gmp.mpz_set_d(a, n) elif isinstance(n, (int, long)): _pyint_to_mpz(n, a) else: raise TypeError
def __cmp(self, other): if isinstance(other, mpfr): return gmp.mpfr_cmp(self._mpfr, other._mpfr) elif isinstance(other, float): return gmp.mpfr_cmp_d(self._mpfr, other) if isinstance(other, (int, long)): if -sys.maxsize - 1 <= other < sys.maxsize: return gmp.mpfr_cmp_ui(self._mpfr, other) elif 0 <= other <= MAX_UI: return gmp.mpfr_cmp_ui(self._mpfr, other) else: tmp_mpz = _new_mpz() _pylong_to_mpz(other, tmp_mpz) result = gmp.mpfr_cmp_z(self._mpfr, tmp_mpz) _del_mpz(tmp_mpz) return result elif isinstance(other, mpz): return gmp.mpfr_cmp_z(self._mpfr, other._mpz) elif isinstance(other, mpq): return gmp.mpfr_cmp_q(self._mpfr, other._mpq) return None
def __eq__(self, other): # Complex Comparison if isinstance(other, mpc): return gmp.mpc_cmp(self._mpc, other._mpc) == 0 elif isinstance(other, complex): return ( gmp.mpfr_cmp_d(gmp.mpc_realref(self._mpc), other.real) == 0 and gmp.mpfr_cmp_d(gmp.mpc_imagref(self._mpc), other.imag) == 0 ) # Real Comparison realref = gmp.mpc_realref(self._mpc) if isinstance(other, mpfr): result = gmp.mpfr_cmp(realref, other._mpfr) elif isinstance(other, float): result = gmp.mpfr_cmp_d(realref, other) elif isinstance(other, (int, long)): if -sys.maxsize - 1 <= other < sys.maxsize: result = gmp.mpfr_cmp_ui(realref, other) elif 0 <= other <= MAX_UI: result = gmp.mpfr_cmp_ui(realref, other) else: tmp_mpz = _new_mpz() _pyint_to_mpz(other, tmp_mpz) result = gmp.mpfr_cmp_z(realref, tmp_mpz) _del_mpz(tmp_mpz) result = result elif isinstance(other, mpz): result = gmp.mpfr_cmp_z(realref, other._mpz) elif isinstance(other, mpq): result = gmp.mpfr_cmp_q(realref, other._mpq) else: return NotImplemented if not gmp.mpfr_zero_p(gmp.mpc_imagref(self._mpc)): return False return result == 0
def __eq__(self, other): # Complex Comparison if isinstance(other, mpc): return gmp.mpc_cmp(self._mpc, other._mpc) == 0 elif isinstance(other, complex): return (gmp.mpfr_cmp_d(gmp.mpc_realref(self._mpc), other.real) == 0 and gmp.mpfr_cmp_d(gmp.mpc_imagref(self._mpc), other.imag) == 0) # Real Comparison realref = gmp.mpc_realref(self._mpc) if isinstance(other, mpfr): result = gmp.mpfr_cmp(realref, other._mpfr) elif isinstance(other, float): result = gmp.mpfr_cmp_d(realref, other) elif isinstance(other, (int, long)): if -sys.maxsize - 1 <= other < sys.maxsize: result = gmp.mpfr_cmp_ui(realref, other) elif 0 <= other <= MAX_UI: result = gmp.mpfr_cmp_ui(realref, other) else: tmp_mpz = _new_mpz() _pyint_to_mpz(other, tmp_mpz) result = gmp.mpfr_cmp_z(realref, tmp_mpz) _del_mpz(tmp_mpz) result = result elif isinstance(other, mpz): result = gmp.mpfr_cmp_z(realref, other._mpz) elif isinstance(other, mpq): result = gmp.mpfr_cmp_q(realref, other._mpq) else: return NotImplemented if not gmp.mpfr_zero_p(gmp.mpc_imagref(self._mpc)): return False return result == 0
def __floor__(self): res = _new_mpz() gmp.mpz_fdiv_q(res, gmp.mpq_numref(self._mpq), gmp.mpq_denref(self._mpq)) return mpz._from_c_mpz(res)
def __long__(self): res = _new_mpz() gmp.mpz_tdiv_q(res, gmp.mpq_numref(self._mpq), gmp.mpq_denref(self._mpq)) return long(mpz._from_c_mpz(res))
def denominator(self): if self._denominator is None: den = _new_mpz() gmp.mpq_get_den(den, self._mpq) self._denominator = mpz._from_c_mpz(den) return self._denominator
def numerator(self): if self._numerator is None: num = _new_mpz() gmp.mpq_get_num(num, self._mpq) self._numerator = mpz._from_c_mpz(num) return self._numerator
def __abs__(self): res = _new_mpz() gmp.mpz_abs(res, self._mpz) return mpz._from_c_mpz(res)
def __neg__(self): res = _new_mpz() gmp.mpz_neg(res, self._mpz) return mpz._from_c_mpz(res)
def __invert__(self): res = _new_mpz() gmp.mpz_com(res, self._mpz) return mpz._from_c_mpz(res)