示例#1
0
文件: mpfr.py 项目: sn6uv/gmpy_cffi
 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)
示例#2
0
 def __rpow__(self, other):
     res = _new_mpfr()
     if isinstance(other, mpq):
         # There is no mpfr_pow_q
         gmp.mpfr_set_q(res, other._mpq, gmp.MPFR_RNDN)
         gmp.mpfr_pow(res, res, self._mpfr, gmp.MPFR_RNDN)
     elif isinstance(other, mpz):
         # There is no mpfr_pow_z
         gmp.mpfr_set_z(res, other._mpz, gmp.MPFR_RNDN)
         gmp.mpfr_pow(res, res, self._mpfr, 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, res, self._mpfr, gmp.MPFR_RNDN)
     elif isinstance(other, (int, long)):
         # There is no mpfr_si_pow
         _pyint_to_mpfr(other, res)
         gmp.mpfr_pow(res, res, self._mpfr, gmp.MPFR_RNDN)
     else:
         return NotImplemented
     return mpfr._from_c_mpfr(res)