def __rsub__(self, other): res = _new_mpfr() if isinstance(other, mpq): # There is no mpfr_q_sub gmp.mpfr_sub_q(res, self._mpfr, other._mpq, gmp.MPFR_RNDN) gmp.mpfr_neg(res, res, gmp.MPFR_RNDN) elif isinstance(other, mpz): gmp.mpfr_z_sub(res, other._mpz, self._mpfr, gmp.MPFR_RNDN) elif isinstance(other, float): gmp.mpfr_d_sub(res, other, self._mpfr, gmp.MPFR_RNDN) elif isinstance(other, (int, long)): if -sys.maxsize - 1 <= other <= sys.maxsize: gmp.mpfr_si_sub(res, other, self._mpfr, gmp.MPFR_RNDN) elif 0 <= other <= MAX_UI: gmp.mpfr_ui_sub(res, other, self._mpfr, gmp.MPFR_RNDN) else: tmp_mpz = _new_mpz() _pylong_to_mpz(other, tmp_mpz) gmp.mpfr_z_sub(res, tmp_mpz, self._mpfr, gmp.MPFR_RNDN) _del_mpz(tmp_mpz) else: return NotImplemented return mpfr._from_c_mpfr(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)