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 __truediv__(self, other): res = _new_mpfr() if isinstance(other, mpfr): gmp.mpfr_div(res, self._mpfr, other._mpfr, gmp.MPFR_RNDN) elif isinstance(other, mpq): gmp.mpfr_div_q(res, self._mpfr, other._mpq, gmp.MPFR_RNDN) elif isinstance(other, mpz): gmp.mpfr_div_z(res, self._mpfr, other._mpz, gmp.MPFR_RNDN) elif isinstance(other, float): gmp.mpfr_div_d(res, self._mpfr, other, gmp.MPFR_RNDN) elif isinstance(other, (int, long)): if -sys.maxsize - 1 <= other <= sys.maxsize: gmp.mpfr_div_si(res, self._mpfr, other, gmp.MPFR_RNDN) elif 0 <= other <= MAX_UI: gmp.mpfr_div_ui(res, self._mpfr, other, gmp.MPFR_RNDN) else: tmp_mpz = _new_mpz() _pylong_to_mpz(other, tmp_mpz) gmp.mpfr_div_z(res, self._mpfr, tmp_mpz, gmp.MPFR_RNDN) _del_mpz(tmp_mpz) else: return NotImplemented return mpfr._from_c_mpfr(res)