示例#1
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
示例#2
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
示例#3
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