static PyObject * GMPy_Rational_FloorDiv(PyObject *x, PyObject *y, CTXT_Object *context) { MPZ_Object *result; MPQ_Object *tempq; CHECK_CONTEXT(context); result = GMPy_MPZ_New(context); tempq = GMPy_MPQ_New(context); if (!result || !tempq) { Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)tempq); return NULL; } if (MPQ_Check(x) && MPQ_Check(y)) { if (mpq_sgn(MPQ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); goto error; } mpq_div(tempq->q, MPQ(x), MPQ(y)); mpz_fdiv_q(result->z, mpq_numref(tempq->q), mpq_denref(tempq->q)); Py_DECREF((PyObject*)tempq); return (PyObject*)result; } if (IS_RATIONAL(x) && IS_RATIONAL(y)) { MPQ_Object *tempx, *tempy; tempx = GMPy_MPQ_From_Number(x, context); tempy = GMPy_MPQ_From_Number(y, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); goto error; } if (mpq_sgn(tempy->q) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); goto error; } mpq_div(tempq->q, tempx->q, tempy->q); mpz_fdiv_q(result->z, mpq_numref(tempq->q), mpq_denref(tempq->q)); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); Py_DECREF((PyObject*)tempq); return (PyObject*)result; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; error: Py_DECREF((PyObject*)result); Py_DECREF((PyObject*)tempq); return NULL; }
static PyObject * Pygmpy_t_divmod(PyObject *self, PyObject *args) { PyObject *x, *y, *result; PympzObject *q, *r, *tempx, *tempy; if (PyTuple_GET_SIZE(args) != 2) { TYPE_ERROR("t_divmod() requires 'mpz','mpz' arguments"); return NULL; } x = PyTuple_GET_ITEM(args, 0); y = PyTuple_GET_ITEM(args, 1); CREATE_TWO_MPZ_TUPLE(q, r, result); if (CHECK_MPZANY(x) && CHECK_MPZANY(y)) { if (mpz_sgn(Pympz_AS_MPZ(y)) == 0) { ZERO_ERROR("t_divmod() division by 0"); Py_DECREF((PyObject*)q); Py_DECREF((PyObject*)r); Py_DECREF(result); return NULL; } mpz_tdiv_qr(q->z, r->z, Pympz_AS_MPZ(x), Pympz_AS_MPZ(y)); } else { tempx = Pympz_From_Integer(x); tempy = Pympz_From_Integer(y); if (!tempx || !tempy) { TYPE_ERROR("t_divmod() requires 'mpz','mpz' arguments"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)q); Py_DECREF((PyObject*)r); Py_DECREF(result); return NULL; } if (mpz_sgn(Pympz_AS_MPZ(tempy)) == 0) { ZERO_ERROR("t_divmod() division by 0"); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); Py_DECREF((PyObject*)q); Py_DECREF((PyObject*)r); Py_DECREF(result); return NULL; } mpz_tdiv_qr(q->z, r->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); } PyTuple_SET_ITEM(result, 0, (PyObject*)q); PyTuple_SET_ITEM(result, 1, (PyObject*)r); return result; }
static PyObject * GMPy_MPZ_Function_Divexact(PyObject *self, PyObject *args) { PyObject *x, *y; MPZ_Object *result, *tempx= NULL, *tempy = NULL; if(PyTuple_GET_SIZE(args) != 2) { TYPE_ERROR("divexact() requires 'mpz','mpz' arguments"); return NULL; } if (!(result = GMPy_MPZ_New(NULL))) { /* LCOV_EXCL_START */ return NULL; /* LCOV_EXCL_STOP */ } x = PyTuple_GET_ITEM(args, 0); y = PyTuple_GET_ITEM(args, 1); if (MPZ_Check(x) && MPZ_Check(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("divexact() division by 0"); Py_DECREF((PyObject*)result); return NULL; } mpz_divexact(result->z, MPZ(x), MPZ(y)); } else { if (!(tempx = GMPy_MPZ_From_Integer(x, NULL)) || !(tempy = GMPy_MPZ_From_Integer(y, NULL))) { TYPE_ERROR("divexact() requires 'mpz','mpz' arguments"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } if (mpz_sgn(MPZ(tempy)) == 0) { ZERO_ERROR("divexact() division by 0"); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpz_divexact(result->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); } return (PyObject*)result; }
static PyObject * GMPy_MPZ_FloorDiv_Slot(PyObject *x, PyObject *y) { if (CHECK_MPZANY(x) && CHECK_MPZANY(y)) { MPZ_Object *result; if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); return NULL; } if ((result = GMPy_MPZ_New(NULL))) { mpz_fdiv_q(result->z, MPZ(x), MPZ(y)); } return (PyObject*)result; } if (IS_INTEGER(x) && IS_INTEGER(y)) return GMPy_Integer_FloorDiv(x, y, NULL); if (IS_RATIONAL(x) && IS_RATIONAL(y)) return GMPy_Rational_FloorDiv(x, y, NULL); if (IS_REAL(x) && IS_REAL(y)) return GMPy_Real_FloorDiv(x, y, NULL); if (IS_COMPLEX(x) && IS_COMPLEX(y)) return GMPy_Complex_FloorDiv(x, y, NULL); Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pygmpy_c_div(PyObject *self, PyObject *args) { PyObject *x, *y; PympzObject *q, *tempx, *tempy; if (PyTuple_GET_SIZE(args) != 2) { TYPE_ERROR("c_div() requires 'mpz','mpz' arguments"); return NULL; } x = PyTuple_GET_ITEM(args, 0); y = PyTuple_GET_ITEM(args, 1); if (!(q = Pympz_new())) return NULL; if (CHECK_MPZANY(x) && CHECK_MPZANY(y)) { if (mpz_sgn(Pympz_AS_MPZ(y)) == 0) { ZERO_ERROR("c_div() division by 0"); Py_DECREF((PyObject*)q); return NULL; } mpz_cdiv_q(q->z, Pympz_AS_MPZ(x), Pympz_AS_MPZ(y)); } else { tempx = Pympz_From_Integer(x); tempy = Pympz_From_Integer(y); if (!tempx || !tempy) { TYPE_ERROR("c_div() requires 'mpz','mpz' arguments"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)q); return NULL; } if (mpz_sgn(Pympz_AS_MPZ(tempy)) == 0) { ZERO_ERROR("c_div() division by 0"); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); Py_DECREF((PyObject*)q); return NULL; } mpz_cdiv_q(q->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); } return (PyObject*)q; }
static PyObject * GMPy_MPZ_IFloorDiv_Slot(PyObject *self, PyObject *other) { MPZ_Object *rz; if (!(rz = GMPy_MPZ_New(NULL))) return NULL; if (CHECK_MPZANY(other)) { if (mpz_sgn(MPZ(other)) == 0) { ZERO_ERROR("mpz division by zero"); return NULL; } mpz_fdiv_q(rz->z, MPZ(self), MPZ(other)); return (PyObject*)rz; } if (PyIntOrLong_Check(other)) { int error; long temp = GMPy_Integer_AsLongAndError(other, &error); if (!error) { if (temp == 0) { ZERO_ERROR("mpz division by zero"); return NULL; } else if(temp > 0) { mpz_fdiv_q_ui(rz->z, MPZ(self), temp); } else { mpz_cdiv_q_ui(rz->z, MPZ(self), -temp); mpz_neg(rz->z, rz->z); } } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_fdiv_q(rz->z, MPZ(self), tempz); mpz_cloc(tempz); } return (PyObject*)rz; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_Rational_DivMod(PyObject *x, PyObject *y, CTXT_Object *context) { MPQ_Object *tempx, *tempy, *rem; MPZ_Object *quo; PyObject *result; CHECK_CONTEXT(context); result = PyTuple_New(2); rem = GMPy_MPQ_New(context); quo = GMPy_MPZ_New(context); if (!result || !rem || !quo) { Py_XDECREF(result); Py_XDECREF((PyObject*)rem); Py_XDECREF((PyObject*)quo); return NULL; } if (IS_RATIONAL(x) && IS_RATIONAL(y)) { tempx = GMPy_MPQ_From_Number(x, context); tempy = GMPy_MPQ_From_Number(y, context); if (!tempx || !tempy) { SYSTEM_ERROR("could not convert Rational to mpq"); goto error; } if (mpq_sgn(tempy->q) == 0) { ZERO_ERROR("division or modulo by zero"); goto error; } mpq_div(rem->q, tempx->q, tempy->q); mpz_fdiv_q(quo->z, mpq_numref(rem->q), mpq_denref(rem->q)); /* Need to calculate x - quo * y. */ mpq_set_z(rem->q, quo->z); mpq_mul(rem->q, rem->q, tempy->q); mpq_sub(rem->q, tempx->q, rem->q); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return result; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; error: Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)rem); Py_DECREF((PyObject*)quo); Py_DECREF(result); return NULL; }
static PyObject * Pyxmpz_inplace_floordiv(PyObject *a, PyObject *b) { mpz_t tempz; mpir_si temp_si; int overflow; if (PyIntOrLong_Check(b)) { temp_si = PyLong_AsSIAndOverflow(b, &overflow); if (overflow) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, b); mpz_fdiv_q(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), tempz); mpz_cloc(tempz); } else if(temp_si == 0) { ZERO_ERROR("xmpz division by zero"); return NULL; } else if(temp_si > 0) { mpz_fdiv_q_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si); } else { mpz_cdiv_q_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), -temp_si); mpz_neg(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a)); } Py_INCREF(a); return a; } if (CHECK_MPZANY(b)) { if (mpz_sgn(Pyxmpz_AS_MPZ(b)) == 0) { ZERO_ERROR("xmpz division by zero"); return NULL; } mpz_fdiv_q(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(b)); Py_INCREF(a); return a; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_XMPZ_IRem_Slot(PyObject *self, PyObject *other) { if (PyIntOrLong_Check(other)) { int error; long temp = GMPy_Integer_AsLongAndError(other, &error); if (!error) { if (temp > 0) { mpz_fdiv_r_ui(MPZ(self), MPZ(self), temp); } else if (temp == 0) { ZERO_ERROR("xmpz modulo by zero"); return NULL; } else { mpz_cdiv_r_ui(MPZ(self), MPZ(self), -temp); } } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, other); mpz_fdiv_r(MPZ(self), MPZ(self), tempz); mpz_cloc(tempz); } Py_INCREF(self); return self; } if (CHECK_MPZANY(other)) { if(mpz_sgn(MPZ(other)) == 0) { ZERO_ERROR("xmpz modulo by zero"); return NULL; } mpz_fdiv_r(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_Rational_DivMod(PyObject *x, PyObject *y, CTXT_Object *context) { MPQ_Object *tempx = NULL, *tempy = NULL, *rem = NULL; MPZ_Object *quo = NULL; PyObject *result = NULL; if (!(result = PyTuple_New(2)) || !(rem = GMPy_MPQ_New(context)) || !(quo = GMPy_MPZ_New(context))) { /* LCOV_EXCL_START */ goto error; /* LCOV_EXCL_STOP */ } if (IS_RATIONAL(x) && IS_RATIONAL(y)) { if (!(tempx = GMPy_MPQ_From_Number(x, context)) || !(tempy = GMPy_MPQ_From_Number(y, context))) { /* LCOV_EXCL_START */ goto error; /* LCOV_EXCL_STOP */ } if (mpq_sgn(tempy->q) == 0) { ZERO_ERROR("division or modulo by zero"); goto error; } mpq_div(rem->q, tempx->q, tempy->q); mpz_fdiv_q(quo->z, mpq_numref(rem->q), mpq_denref(rem->q)); /* Need to calculate x - quo * y. */ mpq_set_z(rem->q, quo->z); mpq_mul(rem->q, rem->q, tempy->q); mpq_sub(rem->q, tempx->q, rem->q); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return result; } /* LCOV_EXCL_START */ SYSTEM_ERROR("Internal error in GMPy_Rational_DivMod()."); error: Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_XDECREF((PyObject*)rem); Py_XDECREF((PyObject*)quo); Py_XDECREF(result); return NULL; /* LCOV_EXCL_STOP */ }
static PyObject * GMPy_XMPZ_IFloorDiv_Slot(PyObject *self, PyObject *other) { if (PyIntOrLong_Check(other)) { int error; native_si temp = GMPy_Integer_AsNative_siAndError(other, &error); if (!error) { if (temp == 0) { ZERO_ERROR("xmpz division by zero"); return NULL; } else if(temp > 0) { mpz_fdiv_q_ui(MPZ(self), MPZ(self), temp); } else { mpz_cdiv_q_ui(MPZ(self), MPZ(self), -temp); mpz_neg(MPZ(self), MPZ(self)); } } else { mpz_set_PyIntOrLong(global.tempz, other); mpz_fdiv_q(MPZ(self), MPZ(self), global.tempz); } Py_INCREF(self); return self; } if (CHECK_MPZANY(other)) { if (mpz_sgn(MPZ(other)) == 0) { ZERO_ERROR("xmpz division by zero"); return NULL; } mpz_fdiv_q(MPZ(self), MPZ(self), MPZ(other)); Py_INCREF(self); return self; } Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_Rational_Mod(PyObject *x, PyObject *y, CTXT_Object *context) { mpz_t tempz; MPQ_Object *tempx, *tempy, *result; CHECK_CONTEXT(context); if (!(result = GMPy_MPQ_New(context))) return NULL; if (IS_RATIONAL(x) && IS_RATIONAL(y)) { tempx = GMPy_MPQ_From_Number(x, context); tempy = GMPy_MPQ_From_Number(y, context); if (!tempx || !tempy) { SYSTEM_ERROR("could not convert Rational to mpq"); goto error; } if (mpq_sgn(tempy->q) == 0) { ZERO_ERROR("division or modulo by zero"); goto error; } mpz_inoc(tempz); mpq_div(result->q, tempx->q, tempy->q); mpz_fdiv_q(tempz, mpq_numref(result->q), mpq_denref(result->q)); /* Need to calculate x - tempz * y. */ mpq_set_z(result->q, tempz); mpq_mul(result->q, result->q, tempy->q); mpq_sub(result->q, tempx->q, result->q); mpz_cloc(tempz); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return (PyObject*)result; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; error: Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; }
static PyObject * Pympq_div(PyObject *self, PyObject *args) { PympqObject *result; PyObject *other; PARSE_TWO_MPQ(other, "div() requires 'mpq','mpq' arguments"); if ((result = (PympqObject*)Pympq_new())) { if (mpq_sgn(Pympq_AS_MPQ(other)) == 0) { ZERO_ERROR("'mpq' division by zero"); Py_DECREF((PyObject*)result); result = 0; } else { mpq_div(result->q, Pympq_AS_MPQ(self), Pympq_AS_MPQ(other)); } } Py_DECREF(self); Py_DECREF(other); return (PyObject*)result; }
static PyObject * Pygmpy_mpq(PyObject *self, PyObject *args, PyObject *keywds) { PympqObject *result = 0, *temp; PyObject *n = 0, *m = 0; int base = 10; Py_ssize_t argc; static char *kwlist[] = {"s", "base", NULL }; argc = PyTuple_Size(args); if (argc > 2) { TYPE_ERROR("mpq() requires 0, 1 or 2 arguments"); return NULL; } if (argc == 0) { if ((result = (PympqObject*)Pympq_new())) { mpq_set_ui(result->q, 0, 0); } return (PyObject*)result; } n = PyTuple_GetItem(args, 0); if (PyStrOrUnicode_Check(n)) { /* keyword base is legal */ if (PyArg_ParseTupleAndKeywords(args, keywds, "O|i", kwlist, &n, &base)) { if ((base!=0) && ((base<2)||(base>62))) { VALUE_ERROR("base for mpq() must be 0 or in the " "interval 2 ... 62"); } else { result = Pympq_From_PyStr(n, base); } } return (PyObject*)result; } if (isDecimal(n)) { return (PyObject*)Pympq_From_Decimal(n); } if (argc == 2) m = PyTuple_GetItem(args, 1); #ifdef WITHMPFR if (!isReal(n) || (m && !isReal(m))) { #else if (!(isRational(n) || PyFloat_Check(n)) || (m && !(isRational(m) || PyFloat_Check(m)))) { #endif TYPE_ERROR("mpq() requires numeric or string argument"); return NULL; } /* should now have one or two numeric values */ result = Pympq_From_Number(n); if (!result && !PyErr_Occurred()) { TYPE_ERROR("mpq() requires numeric or string argument"); return NULL; } if (m) { temp = Pympq_From_Number(m); if (!temp && !PyErr_Occurred()) { TYPE_ERROR("mpq() requires numeric or string argument"); Py_DECREF((PyObject*)result); return NULL; } if (mpq_sgn(temp->q) == 0) { ZERO_ERROR("zero denominator in 'mpq'"); Py_DECREF((PyObject*)result); Py_DECREF((PyObject*)temp); return NULL; } mpq_div(result->q, result->q, temp->q); Py_DECREF((PyObject*)temp); } return (PyObject*)result; } /* Functions that operate strictly on mpq. */ /* produce digits for an mpq in requested base, default 10 */ PyDoc_STRVAR(doc_qdigitsm, "x.digits([base=10]) -> string\n\n" "Return a Python string representing x in the given base (2 to 62,\n" "default is 10). A leading '-' is present if x<0, but no leading '+'\n" "is present if x>=0.\n"); static PyObject * Pympq_digits(PyObject *self, PyObject *args) { int base = 10; PyObject *result; SELF_MPQ_ONE_ARG("|i", &base); result = Pympq_To_PyStr((PympqObject*)self, base, 0); Py_DECREF(self); return result; }
static PyObject * GMPy_Integer_FloorDiv(PyObject *x, PyObject *y, CTXT_Object *context) { MPZ_Object *result; if (!(result = GMPy_MPZ_New(context))) return NULL; if (CHECK_MPZANY(x)) { if (PyIntOrLong_Check(y)) { int error; native_si temp = GMPy_Integer_AsNative_siAndError(y, &error); if (!error) { if (temp > 0) { mpz_fdiv_q_ui(result->z, MPZ(x), temp); } else if (temp == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } else { mpz_cdiv_q_ui(result->z, MPZ(x), -temp); mpz_neg(result->z, result->z); } } else { mpz_set_PyIntOrLong(global.tempz, y); mpz_fdiv_q(result->z, MPZ(x), global.tempz); } return (PyObject*)result; } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } mpz_fdiv_q(result->z, MPZ(x), MPZ(y)); return (PyObject*)result; } } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } if (PyIntOrLong_Check(x)) { mpz_set_PyIntOrLong(global.tempz, x); mpz_fdiv_q(result->z, global.tempz, MPZ(y)); return (PyObject*)result; } } if (IS_INTEGER(x) && IS_INTEGER(y)) { MPZ_Object *tempx, *tempy; tempx = GMPy_MPZ_From_Integer(x, context); tempy = GMPy_MPZ_From_Integer(y, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } if (mpz_sgn(tempy->z) == 0) { ZERO_ERROR("division or modulo by zero"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpz_fdiv_q(result->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return (PyObject*)result; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pympq_pow(PyObject *base, PyObject *exp, PyObject *m) { PympqObject *rq, *tempbq; PympzObject *tempez; #ifdef WITHMPFR PympfrObject *rf, *tempbf, *tempef; #endif int esign, bsign; mpir_si tempexp; if ((PyObject*)m != Py_None) { TYPE_ERROR("mpq.pow() no modulo allowed"); return NULL; } /* Only support mpq**int. Everything else gets converted to mpf. */ if (isRational(base) && isInteger(exp)) { tempbq = Pympq_From_Rational(base); tempez = Pympz_From_Integer(exp); if (!tempbq || !tempez) { Py_XDECREF((PyObject*)tempbq); Py_XDECREF((PyObject*)tempez); return NULL; } if (!mpz_fits_slong_p(tempez->z)) { VALUE_ERROR("mpq.pow() outrageous exponent"); Py_DECREF((PyObject*)tempbq); Py_DECREF((PyObject*)tempez); return NULL; } if (!(rq = (PympqObject*)Pympq_new())) { Py_DECREF((PyObject*)tempbq); Py_DECREF((PyObject*)tempez); return NULL; } esign = mpz_sgn(tempez->z); if (esign == 0) { mpq_set_si(rq->q, 1, 1); Py_DECREF((PyObject*)tempbq); Py_DECREF((PyObject*)tempez); return (PyObject*)rq; } bsign = mpq_sgn(tempbq->q); if (esign < 0) { if (bsign == 0) { ZERO_ERROR("mpq.pow() 0 base to negative exponent"); Py_DECREF((PyObject*)rq); Py_DECREF((PyObject*)tempbq); Py_DECREF((PyObject*)tempez); return NULL; } if (bsign < 0) { mpz_neg(mpq_numref(rq->q), mpq_denref(tempbq->q)); } else { mpz_set(mpq_numref(rq->q), mpq_denref(tempbq->q)); } mpz_abs(mpq_denref(rq->q), mpq_numref(tempbq->q)); tempexp = -mpz_get_si(tempez->z); } else { mpq_set(rq->q, tempbq->q); tempexp = mpz_get_si(tempez->z); } if (tempexp>1) { mpz_pow_ui(mpq_numref(rq->q), mpq_numref(rq->q), tempexp); mpz_pow_ui(mpq_denref(rq->q), mpq_denref(rq->q), tempexp); } Py_DECREF((PyObject*)tempbq); Py_DECREF((PyObject*)tempez); return (PyObject*)rq; } else { #ifdef WITHMPFR tempbf = Pympfr_From_Real(base, 0); tempef = Pympfr_From_Real(exp, 0); rf = (PympfrObject*)Pympfr_new(0); if (!tempbf || !tempef || !rf) { TYPE_ERROR("mpq.pow() unsupported operands"); Py_XDECREF((PyObject*)tempbf); Py_XDECREF((PyObject*)tempef); Py_XDECREF((PyObject*)rf); return NULL; } rf->rc = mpfr_pow(rf->f, tempbf->f, tempef->f, context->ctx.mpfr_round); Py_DECREF((PyObject*)tempbf); Py_DECREF((PyObject*)tempef); return (PyObject*)rf; #else TYPE_ERROR("mpq.pow() unsupported operands"); return NULL; #endif } }
static PyObject * GMPy_MPQ_Factory(PyObject *self, PyObject *args, PyObject *keywds) { MPQ_Object *result, *temp; PyObject *n, *m; int base = 10; Py_ssize_t argc, keywdc = 0; static char *kwlist[] = {"s", "base", NULL }; CTXT_Object *context = NULL; if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } argc = PyTuple_Size(args); if (keywds) { keywdc = PyDict_Size(keywds); } if (argc + keywdc > 2) { TYPE_ERROR("mpq() takes at most 2 arguments"); return NULL; } if (argc + keywdc == 0) { if ((result = GMPy_MPQ_New(context))) { mpq_set_ui(result->q, 0, 1); } return (PyObject*)result; } if (argc == 0) { TYPE_ERROR("mpq() requires at least one non-keyword argument"); return NULL; } n = PyTuple_GetItem(args, 0); /* Handle the case where the first argument is a string. */ if (PyStrOrUnicode_Check(n)) { /* keyword base is legal */ if (keywdc || argc > 1) { if (!(PyArg_ParseTupleAndKeywords(args, keywds, "O|i", kwlist, &n, &base))) { return NULL; } } if ((base != 0) && ((base < 2) || (base > 62))) { VALUE_ERROR("base for mpq() must be 0 or in the interval [2, 62]"); return NULL; } return (PyObject*)GMPy_MPQ_From_PyStr(n, base, context); } /* Handle 1 argument. It must be non-complex number. */ if (argc == 1) { if (IS_REAL(n)) { return (PyObject*)GMPy_MPQ_From_Number(n, context); } } /* Handle 2 arguments. Both arguments must be integer or rational. */ if (argc == 2) { m = PyTuple_GetItem(args, 1); if (IS_RATIONAL(n) && IS_RATIONAL(m)) { result = GMPy_MPQ_From_Rational(n, context); temp = GMPy_MPQ_From_Rational(m, context); if (!result || !temp) { Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)temp); return NULL; } if (mpq_sgn(temp->q) == 0) { ZERO_ERROR("zero denominator in mpq()"); Py_DECREF((PyObject*)result); Py_DECREF((PyObject*)temp); return NULL; } mpq_div(result->q, result->q, temp->q); Py_DECREF((PyObject*)temp); return (PyObject*)result; } } TYPE_ERROR("mpq() requires numeric or string argument"); return NULL; }
static PyObject * GMPy_Rational_Pow(PyObject *base, PyObject *exp, PyObject *mod, CTXT_Object *context) { MPQ_Object *tempbq = NULL, *resultq = NULL; MPZ_Object *tempez = NULL; int bsign; long tempexp; if (mod != Py_None) { TYPE_ERROR("pow() 3rd argument not allowed unless all arguments are integers"); return NULL; } /* Only support mpq**int. Everything else gets converted to mpf. */ if (IS_RATIONAL(base) && IS_INTEGER(exp)) { resultq = GMPy_MPQ_New(context); tempbq = GMPy_MPQ_From_Rational(base, context); tempez = GMPy_MPZ_From_Integer(exp, context); if (!resultq || !tempbq || !tempez) { Py_XDECREF((PyObject*)resultq); Py_XDECREF((PyObject*)tempbq); Py_XDECREF((PyObject*)tempez); return NULL; } if (!mpz_fits_slong_p(tempez->z)) { VALUE_ERROR("mpq.pow() outrageous exponent"); Py_DECREF((PyObject*)resultq); Py_DECREF((PyObject*)tempbq); Py_DECREF((PyObject*)tempez); return NULL; } tempexp = mpz_get_si(tempez->z); if (tempexp == 0) { mpq_set_si(resultq->q, 1, 1); Py_DECREF((PyObject*)tempbq); Py_DECREF((PyObject*)tempez); return (PyObject*)resultq; } bsign = mpq_sgn(tempbq->q); if (tempexp < 0) { if (bsign == 0) { ZERO_ERROR("pow() 0 base to negative exponent"); Py_DECREF((PyObject*)resultq); Py_DECREF((PyObject*)tempbq); Py_DECREF((PyObject*)tempez); return NULL; } if (bsign < 0) { mpz_neg(mpq_numref(resultq->q), mpq_denref(tempbq->q)); } else { mpz_set(mpq_numref(resultq->q), mpq_denref(tempbq->q)); } mpz_abs(mpq_denref(resultq->q), mpq_numref(tempbq->q)); tempexp = -tempexp; } else { mpq_set(resultq->q, tempbq->q); } if (tempexp > 1) { mpz_pow_ui(mpq_numref(resultq->q), mpq_numref(resultq->q), tempexp); mpz_pow_ui(mpq_denref(resultq->q), mpq_denref(resultq->q), tempexp); } Py_DECREF((PyObject*)tempbq); Py_DECREF((PyObject*)tempez); return (PyObject*)resultq; } else { return GMPy_Real_Pow(base, exp, Py_None, context); } }
static PyObject * Pympq_qdiv(PyObject *self, PyObject *args) { PyObject *other = 0; PyObject *s = 0; int wasone; if ( self && Pympq_Check(self)) { if (!PyArg_ParseTuple(args, "|O", &other)) return NULL; } else { if (!PyArg_ParseTuple(args, "O|O", &self, &other)) return NULL; } wasone = isOne(other); /* optimize if self must be returned unchanged */ if (Pympq_Check(self) && wasone) { /* optimize if self is mpq and result must==self */ if (mpz_cmp_ui(mpq_denref(Pympq_AS_MPQ(self)), 1) != 0) { Py_INCREF(self); return self; } else { /* denominator is 1, optimize returning an mpz */ s = Pympz_new(); mpz_set(Pympz_AS_MPZ(s), mpq_numref(Pympq_AS_MPQ(self))); return s; } } else if (Pympz_Check(self) && wasone) { /* optimize if self is mpz and result must==self */ Py_INCREF(self); return self; } /* normal, non-optimized case: must make new object as result */ self = (PyObject*)Pympq_From_Rational(self); if (!self) { if (!PyErr_Occurred()) TYPE_ERROR("first argument cannot be converted to 'mpq'"); return NULL; } if (wasone) { /* self was mpf, float, int, long... */ s = self; } else { /* other explicitly present and !=1... must compute */ other = (PyObject*)Pympq_From_Rational(other); if (!other) { Py_DECREF(self); if (!PyErr_Occurred()) TYPE_ERROR("second argument cannot be converted to 'mpq'"); return NULL; } if (mpq_sgn(Pympq_AS_MPQ(other))==0) { PyObject* result = 0; ZERO_ERROR("division or modulo by zero in qdiv"); Py_DECREF(self); Py_DECREF(other); return result; } s = Pympq_new(); mpq_div(Pympq_AS_MPQ(s), Pympq_AS_MPQ(self), Pympq_AS_MPQ(other)); Py_DECREF(self); Py_DECREF(other); } if (mpz_cmp_ui(mpq_denref(Pympq_AS_MPQ(s)), 1) != 0) { return s; } else { /* denominator is 1, return an mpz */ PyObject* ss = Pympz_new(); if (ss) mpz_set(Pympz_AS_MPZ(ss), mpq_numref(Pympq_AS_MPQ(s))); Py_DECREF(s); return ss; } }
static PyObject * GMPy_Integer_DivMod(PyObject *x, PyObject *y, CTXT_Object *context) { PyObject *result; MPZ_Object *tempx, *tempy, *rem, *quo; mpz_t tempz; long temp; int error; result = PyTuple_New(2); rem = GMPy_MPZ_New(context); quo = GMPy_MPZ_New(context); if (!result || !rem || !quo) { Py_XDECREF((PyObject*)rem); Py_XDECREF((PyObject*)quo); Py_XDECREF(result); return NULL; } if (CHECK_MPZANY(x)) { if (PyIntOrLong_Check(y)) { temp = GMPy_Integer_AsLongAndError(y, &error); if (error) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, y); mpz_fdiv_qr(quo->z, rem->z, MPZ(x), tempz); mpz_cloc(tempz); } else if (temp > 0) { mpz_fdiv_qr_ui(quo->z, rem->z, MPZ(x), temp); } else if (temp == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)rem); Py_DECREF((PyObject*)quo); Py_DECREF(result); return NULL; } else { mpz_cdiv_qr_ui(quo->z, rem->z, MPZ(x), -temp); mpz_neg(quo->z, quo->z); } PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return result; } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)rem); Py_DECREF((PyObject*)quo); Py_DECREF(result); return NULL; } mpz_fdiv_qr(quo->z, rem->z, MPZ(x), MPZ(y)); PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return result; } } if (CHECK_MPZANY(y) && PyIntOrLong_Check(x)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)rem); Py_DECREF((PyObject*)quo); Py_DECREF(result); return NULL; } mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, x); mpz_fdiv_qr(quo->z, rem->z, tempz, MPZ(y)); mpz_cloc(tempz); PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return (PyObject*)result; } if (IS_INTEGER(x) && IS_INTEGER(y)) { tempx = GMPy_MPZ_From_Integer(x, context); tempy = GMPy_MPZ_From_Integer(y, context); if (!tempx || !tempy) { SYSTEM_ERROR("could not convert Integer to mpz"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)rem); Py_DECREF((PyObject*)quo); Py_DECREF(result); return NULL; } if (mpz_sgn(tempy->z) == 0) { ZERO_ERROR("division or modulo by zero"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)rem); Py_DECREF((PyObject*)quo); Py_DECREF(result); return NULL; } mpz_fdiv_qr(quo->z, rem->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return result; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_MPZ_Function_Divm(PyObject *self, PyObject *args) { MPZ_Object *result = NULL, *num = NULL, *den = NULL, *mod = NULL; mpz_t numz, denz, modz, gcdz; int ok = 0; if (PyTuple_GET_SIZE(args) != 3) { TYPE_ERROR("divm() requires 'mpz','mpz','mpz' arguments"); return NULL; } if (!(result = GMPy_MPZ_New(NULL))) { /* LCOV_EXCL_START */ return NULL; /* LCOV_EXCL_STOP */ } if (!(num = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL)) || !(den = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 1), NULL)) || !(mod = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 2), NULL))) { TYPE_ERROR("divm() requires 'mpz','mpz','mpz' arguments"); Py_XDECREF((PyObject*)num); Py_XDECREF((PyObject*)den); Py_XDECREF((PyObject*)mod); Py_DECREF((PyObject*)result); return NULL; } /* Make copies so we don't destroy the input. */ mpz_inoc(numz); mpz_inoc(denz); mpz_inoc(modz); mpz_set(numz, num->z); mpz_set(denz, den->z); mpz_set(modz, mod->z); Py_DECREF((PyObject*)num); Py_DECREF((PyObject*)den); Py_DECREF((PyObject*)mod); if (mpz_invert(result->z, denz, modz)) { /* inverse exists */ ok = 1; } else { /* last-ditch attempt: do num, den AND mod have a gcd>1 ? */ mpz_inoc(gcdz); mpz_gcd(gcdz, numz, denz); mpz_gcd(gcdz, gcdz, modz); mpz_divexact(numz, numz, gcdz); mpz_divexact(denz, denz, gcdz); mpz_divexact(modz, modz, gcdz); mpz_cloc(gcdz); ok = mpz_invert(result->z, denz, modz); } if (ok) { mpz_mul(result->z, result->z, numz); mpz_mod(result->z, result->z, modz); mpz_cloc(numz); mpz_cloc(denz); mpz_cloc(modz); return (PyObject*)result; } else { ZERO_ERROR("not invertible"); mpz_cloc(numz); mpz_cloc(denz); mpz_cloc(modz); Py_DECREF((PyObject*)result); return NULL; } }
static PyObject * GMPy_Integer_Mod(PyObject *x, PyObject *y, CTXT_Object *context) { MPZ_Object *result; CHECK_CONTEXT(context); if (!(result = GMPy_MPZ_New(context))) return NULL; if (CHECK_MPZANY(x)) { if (PyIntOrLong_Check(y)) { int error; long temp = GMPy_Integer_AsLongAndError(y, &error); if (!error) { if (temp > 0) { mpz_fdiv_r_ui(result->z, MPZ(x), temp); } else if (temp == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } else { mpz_cdiv_r_ui(result->z, MPZ(x), -temp); } } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, y); mpz_fdiv_r(result->z, MPZ(x), tempz); mpz_cloc(tempz); } return (PyObject*)result; } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } mpz_fdiv_r(result->z, MPZ(x), MPZ(y)); return (PyObject*)result; } } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); Py_DECREF((PyObject*)result); return NULL; } if (PyIntOrLong_Check(x)) { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, x); mpz_fdiv_r(result->z, tempz, MPZ(y)); mpz_cloc(tempz); return (PyObject*)result; } } if (IS_INTEGER(x) && IS_INTEGER(y)) { MPZ_Object *tempx, *tempy; tempx = GMPy_MPZ_From_Integer(x, context); tempy = GMPy_MPZ_From_Integer(y, context); if (!tempx || !tempy) { SYSTEM_ERROR("could not convert Integer to mpz"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } if (mpz_sgn(tempy->z) == 0) { ZERO_ERROR("division or modulo by zero"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpz_fdiv_r(result->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return (PyObject*)result; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_Integer_DivMod(PyObject *x, PyObject *y, CTXT_Object *context) { PyObject *result = NULL; MPZ_Object *tempx = NULL, *tempy = NULL, *rem = NULL, *quo = NULL; if (!(result = PyTuple_New(2)) || !(rem = GMPy_MPZ_New(context)) || !(quo = GMPy_MPZ_New(context))) { /* LCOV_EXCL_START */ goto error; /* LCOV_EXCL_STOP */ } if (CHECK_MPZANY(x)) { if (PyIntOrLong_Check(y)) { int error; long temp = GMPy_Integer_AsLongAndError(y, &error); if (error) { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, y); mpz_fdiv_qr(quo->z, rem->z, MPZ(x), tempz); mpz_cloc(tempz); } else if (temp > 0) { mpz_fdiv_qr_ui(quo->z, rem->z, MPZ(x), temp); } else if (temp == 0) { ZERO_ERROR("division or modulo by zero"); goto error; } else { mpz_cdiv_qr_ui(quo->z, rem->z, MPZ(x), -temp); mpz_neg(quo->z, quo->z); } PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return result; } if (CHECK_MPZANY(y)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); goto error; } mpz_fdiv_qr(quo->z, rem->z, MPZ(x), MPZ(y)); PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return result; } } if (CHECK_MPZANY(y) && PyIntOrLong_Check(x)) { if (mpz_sgn(MPZ(y)) == 0) { ZERO_ERROR("division or modulo by zero"); goto error; } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, x); mpz_fdiv_qr(quo->z, rem->z, tempz, MPZ(y)); mpz_cloc(tempz); PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return (PyObject*)result; } } if (IS_INTEGER(x) && IS_INTEGER(y)) { if (!(tempx = GMPy_MPZ_From_Integer(x, context)) || !(tempy = GMPy_MPZ_From_Integer(y, context))) { /* LCOV_EXCL_START */ goto error; /* LCOV_EXCL_STOP */ } if (mpz_sgn(tempy->z) == 0) { ZERO_ERROR("division or modulo by zero"); goto error; } mpz_fdiv_qr(quo->z, rem->z, tempx->z, tempy->z); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return result; } /* LCOV_EXCL_START */ SYSTEM_ERROR("Internal error in GMPy_Integer_DivMod()."); /* LCOV_EXCL_STOP */ error: Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_XDECREF((PyObject*)rem); Py_XDECREF((PyObject*)quo); Py_XDECREF(result); return NULL; }