示例#1
0
 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)
示例#2
0
文件: mpc.py 项目: sn6uv/gmpy_cffi
 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)
示例#3
0
文件: mpc.py 项目: sn6uv/gmpy_cffi
 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)
示例#4
0
 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)
示例#5
0
 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)
示例#6
0
文件: mpz.py 项目: sn6uv/gmpy_cffi
    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)
示例#7
0
 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)
示例#8
0
文件: mpz.py 项目: sn6uv/gmpy_cffi
 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)
示例#9
0
    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)
示例#10
0
 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
示例#11
0
文件: mpz.py 项目: sn6uv/gmpy_cffi
 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
示例#12
0
 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
示例#13
0
 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)
示例#14
0
文件: mpz.py 项目: sn6uv/gmpy_cffi
 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
示例#15
0
    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)
示例#16
0
 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
示例#17
0
 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
示例#18
0
文件: mpz.py 项目: sn6uv/gmpy_cffi
    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)
示例#19
0
文件: mpz.py 项目: sn6uv/gmpy_cffi
    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)
示例#20
0
    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)
示例#21
0
文件: mpc.py 项目: sn6uv/gmpy_cffi
    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
示例#22
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