Beispiel #1
0
def _mpz_to_pylong(a):
    """
    Convert a to a python long.

    :type a: mpz_t
    :rtype: long
    """

    size = ffi.sizeof('uint64_t')
    numb = 8 * size
    count = (gmp.mpz_sizeinbase(a, 2) + numb - 1) // numb
    p = ffi.new('uint64_t[]', count)
    gmp.mpz_export(p, ffi.NULL, 1, size, 0, 0, a)
    res = 0
    for n in p:
        res = (res << numb) + n

    return res * gmp.mpz_sgn(a)
Beispiel #2
0
def _mpz_to_pylong(a):
    """
    Convert a to a python long.

    :type a: mpz_t
    :rtype: long
    """

    size = ffi.sizeof('uint64_t')
    numb = 8 * size
    count = (gmp.mpz_sizeinbase(a, 2) + numb - 1) // numb
    p = ffi.new('uint64_t[]', count)
    gmp.mpz_export(p, ffi.NULL, 1, size, 0, 0, a)
    res = 0
    for n in p:
        res = (res << numb) + n

    return res * gmp.mpz_sgn(a)
Beispiel #3
0
 def __floordiv__(self, other):
     if isinstance(other, mpq):
         if gmp.mpq_sgn(other._mpq) == 0:
             raise ZeroDivisionError
         res = _new_mpq()
         gmp.mpq_div(res, self._mpq, other._mpq)
         return mpq._from_c_mpq(res)
     elif isinstance(other, (int, long)):
         if other == 0:
             raise ZeroDivisionError
         res = _new_mpq()
         _pyint_to_mpq(other, res)
         gmp.mpq_div(res, self._mpq, res)
         return mpq._from_c_mpq(res)
     elif isinstance(other, mpz):
         if gmp.mpz_sgn(other._mpz) == 0:
             raise ZeroDivisionError
         res = _new_mpq()
         gmp.mpq_set_z(res, other._mpz)
         gmp.mpq_div(res, self._mpq, res)
         return mpq._from_c_mpq(res)
     else:
         return NotImplemented