static PyObject * GMPy_Context_Radians(PyObject *self, PyObject *other) { MPFR_Object *result, *tempx, *temp; CTXT_Object *context = NULL; if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } result = GMPy_MPFR_New(0, context); temp = GMPy_MPFR_New(context->ctx.mpfr_prec + 100, context); tempx = GMPy_MPFR_From_Real(other, 1, context); if (!result || !temp || !tempx) { Py_XDECREF((PyObject*)temp); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)result); return NULL; } mpfr_const_pi(temp->f, MPFR_RNDN); mpfr_div_ui(temp->f, temp->f, 180, MPFR_RNDN); mpfr_mul(result->f, MPFR(self), temp->f, MPFR_RNDN); Py_DECREF((PyObject*)temp); Py_DECREF((PyObject*)tempx); _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * GMPy_Real_RemQuo(PyObject *x, PyObject *y, CTXT_Object *context) { PyObject *result; MPFR_Object *value, *tempx, *tempy; long quobits = 0; CHECK_CONTEXT(context); value = GMPy_MPFR_New(0, context); tempx = GMPy_MPFR_From_Real(x, 1, context); tempy = GMPy_MPFR_From_Real(y, 1, context); result = PyTuple_New(2); if (!value || !tempx || !tempx || !result) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_XDECREF((PyObject*)value); Py_XDECREF(result); return NULL; } mpfr_clear_flags(); value->rc = mpfr_remquo(value->f, &quobits, tempx->f, tempy->f, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); _GMPy_MPFR_Cleanup(&value, context); PyTuple_SET_ITEM(result, 0, (PyObject*)value); PyTuple_SET_ITEM(result, 1, PyIntOrLong_FromLong(quobits)); return result; }
static PyObject * GMPy_Real_Lgamma(PyObject *x, CTXT_Object *context) { PyObject *result; MPFR_Object *value, *tempx; int signp = 0; CHECK_CONTEXT(context) tempx = GMPy_MPFR_From_Real(x, 1, context); value = GMPy_MPFR_New(0, context); result = PyTuple_New(2); if (!tempx || !value || !result) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)value); Py_XDECREF(result); return NULL; } mpfr_clear_flags(); value->rc = mpfr_lgamma(value->f, &signp, tempx->f, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); _GMPy_MPFR_Cleanup(&value, context); if (!value) { Py_DECREF(result); return NULL; } PyTuple_SET_ITEM(result, 0, (PyObject*)value); PyTuple_SET_ITEM(result, 1, PyIntOrLong_FromLong((long)signp)); return result; }
static PyObject * GMPy_Context_NextBelow(PyObject *self, PyObject *other) { MPFR_Object *result, *tempx; CTXT_Object *context = NULL; mpfr_rnd_t temp_round; if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } if (!(tempx = GMPy_MPFR_From_Real(other, 1, context))) { TYPE_ERROR("next_below() argument type not supported"); return NULL; } if (!(result = GMPy_MPFR_New(mpfr_get_prec(tempx->f), context))) { Py_DECREF((PyObject*)tempx); return NULL; } mpfr_clear_flags(); mpfr_set(result->f, tempx->f, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); mpfr_nextbelow(result->f); result->rc = 0; temp_round = GET_MPFR_ROUND(context); context->ctx.mpfr_round = MPFR_RNDD; _GMPy_MPFR_Cleanup(&result, context); context->ctx.mpfr_round = temp_round; return (PyObject*)result; }
static PyObject * GMPy_Context_Factorial(PyObject *self, PyObject *other) { MPFR_Object *result; long n; CTXT_Object *context = NULL; if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } n = PyLong_AsLong(other); if ((n == -1) && PyErr_Occurred()) { TYPE_ERROR("factorial() requires 'int' argument"); return NULL; } if (n < 0) { VALUE_ERROR("factorial() of negative number"); return NULL; } if (!(result = GMPy_MPFR_New(0, context))) { return NULL; } mpfr_clear_flags(); mpfr_fac_ui(result->f, n, GET_MPFR_ROUND(context)); _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * GMPy_Complex_Abs(PyObject *x, CTXT_Object *context) { MPFR_Object *result = NULL; MPC_Object *tempx = NULL; CHECK_CONTEXT(context); if (!(tempx = GMPy_MPC_From_Complex(x, 1, 1, context)) || !(result = GMPy_MPFR_New(0, context))) { /* LCOV_EXCL_START */ Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)result); return NULL; /* LCOV_EXCL_STOP */ } mpfr_clear_flags(); SET_MPC_WAS_NAN(context, tempx); result->rc = mpc_abs(result->f, tempx->c, GET_MPC_ROUND(context)); Py_DECREF((PyObject*)tempx); _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * GMPy_Real_Round2(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *result, *tempx; long n = 0; CHECK_CONTEXT(context); if (y) { n = PyIntOrLong_AsLong(y); if ( (n == -1 && PyErr_Occurred()) || n < MPFR_PREC_MIN || n > MPFR_PREC_MAX) { VALUE_ERROR("invalid precision"); return NULL; } } if (!(tempx = GMPy_MPFR_From_Real(x, 1, context))) { return NULL; } if (!(result = GMPy_MPFR_New(mpfr_get_prec(tempx->f), context))) { Py_DECREF((PyObject*)tempx); return NULL; } mpfr_set(result->f, tempx->f, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); mpfr_clear_flags(); result->rc = mpfr_prec_round(result->f, n, GET_MPFR_ROUND(context)); _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * GMPy_Real_Mul_2exp(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *result, *tempx; unsigned long exp = 0; CHECK_CONTEXT(context); exp = c_ulong_From_Integer(y); if (exp == (unsigned long)(-1) && PyErr_Occurred()) { return NULL; } result = GMPy_MPFR_New(0, context); tempx = GMPy_MPFR_From_Real(x, 1, context); if (!result || !tempx) { Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)tempx); return NULL; } mpfr_clear_flags(); result->rc = mpfr_mul_2ui(result->f, tempx->f, exp, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); GMPY_MPFR_CLEANUP(result, context, "mul_2exp()"); return (PyObject*)result; }
static PyObject * GMPy_Real_RelDiff(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *tempx, *tempy, *result; CHECK_CONTEXT(context); result = GMPy_MPFR_New(0, context); tempx = GMPy_MPFR_From_Real(x, 1, context); tempy = GMPy_MPFR_From_Real(y, 1, context); if (!result || !tempx || !tempy) { Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); return NULL; } mpfr_clear_flags(); mpfr_reldiff(result->f, tempx->f, tempy->f, GET_MPFR_ROUND(context)); result->rc = 0; _GMPy_MPFR_Cleanup(&result, context); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return (PyObject*)result; }
static PyObject * GMPy_Real_Frexp(PyObject *x, CTXT_Object *context) { PyObject *result; MPFR_Object *value, *tempx; mpfr_exp_t exp = 0; CHECK_CONTEXT(context); value = GMPy_MPFR_New(0, context); tempx = GMPy_MPFR_From_Real(x, 1, context); result = PyTuple_New(2); if (!value || !result || !tempx) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)value); Py_XDECREF(result); return NULL; } mpfr_clear_flags(); value->rc = mpfr_frexp(&exp, value->f, tempx->f, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); _GMPy_MPFR_Cleanup(&value, context); PyTuple_SET_ITEM(result, 0, PyIntOrLong_FromSsize_t((Py_ssize_t)exp)); PyTuple_SET_ITEM(result, 1, (PyObject*)value); return result; }
static PyObject * GMPy_MPFR_Add_Slot(PyObject *x, PyObject *y) { if (MPFR_Check(x) && MPFR_Check(y)) { MPFR_Object *result; CTXT_Object *context = NULL; CHECK_CONTEXT(context); if ((result = GMPy_MPFR_New(0, context))) { mpfr_clear_flags(); SET_MPFR_MPFR_WAS_NAN(context, x, y); result->rc = mpfr_add(result->f, MPFR(x), MPFR(y), GET_MPFR_ROUND(context)); _GMPy_MPFR_Cleanup(&result, context); } return (PyObject*)result; } if (IS_REAL(x) && IS_REAL(y)) return GMPy_Real_Add(x, y, NULL); if (IS_COMPLEX(x) && IS_COMPLEX(y)) return GMPy_Complex_Add(x, y, NULL); Py_RETURN_NOTIMPLEMENTED; }
static PyObject * GMPy_Real_Modf(PyObject *x, CTXT_Object *context) { MPFR_Object *s, *c, *tempx; PyObject *result; int code; CHECK_CONTEXT(context); tempx = GMPy_MPFR_From_Real(x, 1, context); s = GMPy_MPFR_New(0, context); c = GMPy_MPFR_New(0, context); result = PyTuple_New(2); if (! tempx || !s || !c || !result) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)s); Py_XDECREF((PyObject*)c); Py_XDECREF(result); return NULL; } mpfr_clear_flags(); code = mpfr_modf(s->f, c->f, tempx->f, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); s->rc = code & 0x03; c->rc = code >> 2; if (s->rc == 2) s->rc = -1; if (c->rc == 2) c->rc = -1; _GMPy_MPFR_Cleanup(&s, context); _GMPy_MPFR_Cleanup(&c, context); if (!s || !c) { Py_XDECREF((PyObject*)s); Py_XDECREF((PyObject*)c); Py_DECREF(result); return NULL; } PyTuple_SET_ITEM(result, 0, (PyObject*)s); PyTuple_SET_ITEM(result, 1, (PyObject*)c); return result; }
static PyObject * _GMPy_MPFR_Sinh_Cosh(PyObject *x, CTXT_Object *context) { MPFR_Object *s, *c; PyObject *result; int code; CHECK_CONTEXT(context); s = GMPy_MPFR_New(0, context); c = GMPy_MPFR_New(0, context); result = PyTuple_New(2); if (!s || !c || !result) { Py_XDECREF((PyObject*)s); Py_XDECREF((PyObject*)c); Py_XDECREF(result); return NULL; } mpfr_clear_flags(); code = mpfr_sinh_cosh(s->f, c->f, MPFR(x), GET_MPFR_ROUND(context)); s->rc = code & 0x03; c->rc = code >> 2; if (s->rc == 2) s->rc = -1; if (c->rc == 2) c->rc = -1; _GMPy_MPFR_Cleanup(&s, context); _GMPy_MPFR_Cleanup(&c, context); if (!s || !c) { Py_XDECREF((PyObject*)s); Py_XDECREF((PyObject*)c); Py_XDECREF(result); return NULL; } PyTuple_SET_ITEM(result, 0, (PyObject*)s); PyTuple_SET_ITEM(result, 1, (PyObject*)c); return result; }
static PyObject * GMPy_Context_NextToward(PyObject *self, PyObject *args) { MPFR_Object *result, *tempx, *tempy; CTXT_Object *context = NULL; int direction; mpfr_rnd_t temp_round; if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } if (PyTuple_GET_SIZE(args) != 2) { TYPE_ERROR("next_toward() requires 2 arguments"); return NULL; } tempx = GMPy_MPFR_From_Real(PyTuple_GET_ITEM(args, 0), 1, context); tempy = GMPy_MPFR_From_Real(PyTuple_GET_ITEM(args, 1), 1, context); if (!tempx || !tempy) { TYPE_ERROR("next_toward() argument type not supported"); Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); return NULL; } if (!(result = GMPy_MPFR_New(mpfr_get_prec(tempx->f), context))) { Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return NULL; } mpfr_clear_flags(); mpfr_set(result->f, tempx->f, GET_MPFR_ROUND(context)); mpfr_nexttoward(result->f, tempy->f); result->rc = 0; direction = mpfr_signbit(tempy->f); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); temp_round = GET_MPFR_ROUND(context); if (direction) context->ctx.mpfr_round = MPFR_RNDD; else context->ctx.mpfr_round = MPFR_RNDU; _GMPy_MPFR_Cleanup(&result, context); context->ctx.mpfr_round = temp_round; return (PyObject*)result; }
static PyObject * GMPy_MPFR_grandom_Function(PyObject *self, PyObject *args) { MPFR_Object *result1, *result2; PyObject *result; CTXT_Object *context = NULL; CHECK_CONTEXT(context); if (PyTuple_GET_SIZE(args) != 1) { TYPE_ERROR("mpfr_grandom() requires 1 argument"); return NULL; } if (!RandomState_Check(PyTuple_GET_ITEM(args, 0))) { TYPE_ERROR("mpfr_grandom() requires 'random_state' argument"); return NULL; } result1 = GMPy_MPFR_New(0, context); result2 = GMPy_MPFR_New(0, context); if (!result1 || !result2) { Py_XDECREF((PyObject*)result1); Py_XDECREF((PyObject*)result2); return NULL; } mpfr_grandom(result1->f, result2->f, RANDOM_STATE(PyTuple_GET_ITEM(args, 0)), GET_MPFR_ROUND(context)); result = Py_BuildValue("(NN)", (PyObject*)result1, (PyObject*)result2); if (!result) { Py_DECREF((PyObject*)result1); Py_DECREF((PyObject*)result2); } return result; }
static PyObject * GMPy_MPC_GetImag_Attrib(MPC_Object *self, void *closure) { MPFR_Object *result = NULL; CTXT_Object *context = NULL; CHECK_CONTEXT(context); if ((result = GMPy_MPFR_New(0, context))) { result->rc = mpc_imag(result->f, self->c, GET_MPFR_ROUND(context)); GMPY_MPFR_CLEANUP(result, context, "imag()"); } return (PyObject*)result; }
static PyObject * GMPy_MPC_GetReal_Attrib(MPC_Object *self, void *closure) { MPFR_Object *result = NULL; CTXT_Object *context = NULL; CHECK_CONTEXT(context); if ((result = GMPy_MPFR_New(0, context))) { result->rc = mpc_real(result->f, self->c, context->ctx.mpfr_round); GMPY_MPFR_CLEANUP(result, context, "real()"); } return (PyObject*)result; }
static PyObject * _GMPy_MPFR_Minus(PyObject *x, CTXT_Object *context) { MPFR_Object *result; CHECK_CONTEXT(context); if (!(result = GMPy_MPFR_New(0, context))) { return NULL; } mpfr_clear_flags(); result->rc = mpfr_neg(result->f, MPFR(x), GET_MPFR_ROUND(context)); GMPY_MPFR_CLEANUP(result, context, "minus()"); return (PyObject*)result; }
static PyObject * GMPy_Complex_Phase(PyObject *x, CTXT_Object *context) { MPFR_Object *result; MPC_Object *tempx; CHECK_CONTEXT(context) result = GMPy_MPFR_New(0, context); tempx = GMPy_MPC_From_Complex(x, 1, 1, context); if (!result || !tempx) { return NULL; } result->rc = mpc_arg(result->f, tempx->c, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); GMPY_MPFR_CLEANUP(result, context, "phase()"); return (PyObject*)result; }
static PyObject * _GMPy_MPFR_Sqrt(PyObject *x, CTXT_Object *context) { MPFR_Object *result; CHECK_CONTEXT(context); if (mpfr_sgn(MPFR(x)) < 0 && context->ctx.allow_complex) { return GMPy_Complex_Sqrt(x, context); } if (!(result = GMPy_MPFR_New(0, context))) { return NULL; } mpfr_clear_flags(); result->rc = mpfr_sqrt(result->f, MPFR(x), GET_MPFR_ROUND(context)); _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * _GMPy_MPFR_Acos(PyObject *x, CTXT_Object *context) { MPFR_Object *result; if (!mpfr_nan_p(MPFR(x)) && (mpfr_cmp_si(MPFR(x), 1) > 0 || mpfr_cmp_si(MPFR(x), -1) < 0) && context->ctx.allow_complex ) { return GMPy_Complex_Acos(x, context); } if (!(result = GMPy_MPFR_New(0, context))) { return NULL; } mpfr_clear_flags(); result->rc = mpfr_acos(result->f, MPFR(x), GET_MPFR_ROUND(context)); _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * GMPy_MPFR_random_Function(PyObject *self, PyObject *args) { MPFR_Object *result; CTXT_Object *context = NULL; CHECK_CONTEXT(context); if (PyTuple_GET_SIZE(args) != 1) { TYPE_ERROR("mpfr_random() requires 1 argument"); return NULL; } if (!RandomState_Check(PyTuple_GET_ITEM(args, 0))) { TYPE_ERROR("mpfr_random() requires 'random_state' argument"); return NULL; } if ((result = GMPy_MPFR_New(0, context))) { mpfr_urandom(result->f, RANDOM_STATE(PyTuple_GET_ITEM(args, 0)), GET_MPFR_ROUND(context)); } return (PyObject*)result; }
static PyObject * GMPy_Real_Add(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *result; CHECK_CONTEXT(context); if (!(result = GMPy_MPFR_New(0, context))) return NULL; if (MPFR_Check(x) && MPFR_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_add(result->f, MPFR(x), MPFR(y), GET_MPFR_ROUND(context)); goto done; } if (MPFR_Check(x)) { if (PyIntOrLong_Check(y)) { mpz_t tempz; long temp; int error; temp = GMPy_Integer_AsLongAndError(y, &error); if (error) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, y); mpfr_clear_flags(); result->rc = mpfr_add_z(result->f, MPFR(x), tempz, GET_MPFR_ROUND(context)); mpz_cloc(tempz); goto done; } else { mpfr_clear_flags(); result->rc = mpfr_add_si(result->f, MPFR(x), temp, GET_MPFR_ROUND(context)); goto done; } } if (CHECK_MPZANY(y)) { mpfr_clear_flags(); result->rc = mpfr_add_z(result->f, MPFR(x), MPZ(y), GET_MPFR_ROUND(context)); goto done; } if (IS_RATIONAL(y)) { MPQ_Object *tempy; if (!(tempy = GMPy_MPQ_From_Number(y, context))) { Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_add_q(result->f, MPFR(x), tempy->q, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempy); goto done; } if (PyFloat_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_add_d(result->f, MPFR(x), PyFloat_AS_DOUBLE(y), GET_MPFR_ROUND(context)); goto done; } } if (MPFR_Check(y)) { if (PyIntOrLong_Check(x)) { mpz_t tempz; long temp; int error; temp = GMPy_Integer_AsLongAndError(x, &error); if (error) { mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, x); mpfr_clear_flags(); result->rc = mpfr_add_z(result->f, MPFR(y), tempz, GET_MPFR_ROUND(context)); mpz_cloc(tempz); goto done; } else { mpfr_clear_flags(); result->rc = mpfr_add_si(result->f, MPFR(y), temp, GET_MPFR_ROUND(context)); goto done; } } if (CHECK_MPZANY(x)) { mpfr_clear_flags(); result->rc = mpfr_add_z(result->f, MPFR(y), MPZ(x), GET_MPFR_ROUND(context)); goto done; } if (IS_RATIONAL(x)) { MPQ_Object *tempx; if (!(tempx = GMPy_MPQ_From_Number(x, context))) { Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_add_q(result->f, MPFR(y), tempx->q, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); goto done; } if (PyFloat_Check(x)) { mpfr_clear_flags(); result->rc = mpfr_add_d(result->f, MPFR(y), PyFloat_AS_DOUBLE(x), GET_MPFR_ROUND(context)); goto done; } } if (IS_REAL(x) && IS_REAL(y)) { MPFR_Object *tempx, *tempy; tempx = GMPy_MPFR_From_Real(x, 1, context); tempy = GMPy_MPFR_From_Real(y, 1, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_add(result->f, MPFR(tempx), MPFR(tempy), GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); goto done; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; done: GMPY_MPFR_CLEANUP(result, context, "addition"); return (PyObject*)result; }
static PyObject * GMPy_Real_Sub(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *result; CHECK_CONTEXT(context); if (!(result = GMPy_MPFR_New(0, context))) return NULL; /* This only processes mpfr if the exponent is still in-bounds. Need * to handle the rare case at the end. */ if (MPFR_Check(x) && MPFR_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_sub(result->f, MPFR(x), MPFR(y), GET_MPFR_ROUND(context)); goto done; } if (MPFR_Check(x)) { if (PyIntOrLong_Check(y)) { int error; long temp = GMPy_Integer_AsLongAndError(y, &error); if (!error) { mpfr_clear_flags(); result->rc = mpfr_sub_si(result->f, MPFR(x), temp, GET_MPFR_ROUND(context)); goto done; } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, y); mpfr_clear_flags(); result->rc = mpfr_sub_z(result->f, MPFR(x), tempz, GET_MPFR_ROUND(context)); mpz_cloc(tempz); goto done; } } if (CHECK_MPZANY(y)) { mpfr_clear_flags(); result->rc = mpfr_sub_z(result->f, MPFR(x), MPZ(y), GET_MPFR_ROUND(context)); goto done; } if (IS_RATIONAL(y)) { MPQ_Object *tempy; if (!(tempy = GMPy_MPQ_From_Number(y, context))) { Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_sub_q(result->f, MPFR(x), tempy->q, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempy); goto done; } if (PyFloat_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_sub_d(result->f, MPFR(x), PyFloat_AS_DOUBLE(y), GET_MPFR_ROUND(context)); goto done; } } if (MPFR_Check(y)) { if (PyIntOrLong_Check(x)) { int error; long temp = GMPy_Integer_AsLongAndError(x, &error); if (!error) { mpfr_clear_flags(); result->rc = mpfr_sub_si(result->f, MPFR(y), temp, GET_MPFR_ROUND(context)); mpfr_neg(result->f, result->f, GET_MPFR_ROUND(context)); goto done; } else { mpz_t tempz; mpz_inoc(tempz); mpz_set_PyIntOrLong(tempz, x); mpfr_clear_flags(); result->rc = mpfr_sub_z(result->f, MPFR(y), tempz, GET_MPFR_ROUND(context)); mpfr_neg(result->f, result->f, GET_MPFR_ROUND(context)); mpz_cloc(tempz); goto done; } } if (CHECK_MPZANY(x)) { mpfr_clear_flags(); result->rc = mpfr_sub_z(result->f, MPFR(y), MPZ(x), GET_MPFR_ROUND(context)); mpfr_neg(result->f, result->f, GET_MPFR_ROUND(context)); goto done; } if (IS_RATIONAL(x)) { MPQ_Object *tempx; if (!(tempx = GMPy_MPQ_From_Number(x, context))) { Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_sub_q(result->f, MPFR(y), tempx->q, GET_MPFR_ROUND(context)); mpfr_neg(result->f, result->f, GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); goto done; } if (PyFloat_Check(x)) { mpfr_clear_flags(); result->rc = mpfr_sub_d(result->f, MPFR(y), PyFloat_AS_DOUBLE(x), GET_MPFR_ROUND(context)); mpfr_neg(result->f, result->f, GET_MPFR_ROUND(context)); goto done; } } if (IS_REAL(x) && IS_REAL(y)) { MPFR_Object *tempx, *tempy; tempx = GMPy_MPFR_From_Real(x, 1, context); tempy = GMPy_MPFR_From_Real(y, 1, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_sub(result->f, MPFR(tempx), MPFR(tempy), GET_MPFR_ROUND(context)); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); goto done; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; done: GMPY_MPFR_CLEANUP(result, context, "subtraction"); return (PyObject*)result; }
static PyObject * GMPy_Context_Fsum(PyObject *self, PyObject *other) { MPFR_Object *temp, *result; mpfr_ptr *tab; int errcode; Py_ssize_t i, seq_length = 0; CTXT_Object *context = NULL; if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } if (!(result = GMPy_MPFR_New(0, context))) { return NULL; } if (!(other = PySequence_List(other))) { Py_DECREF((PyObject*)result); TYPE_ERROR("argument must be an iterable"); return NULL; } /* other contains a new list containing all the values from the * iterable. Now make sure each item in the list is an mpfr. */ seq_length = PyList_GET_SIZE(other); for (i=0; i < seq_length; i++) { if (!(temp = GMPy_MPFR_From_Real(PyList_GET_ITEM(other, i), 1, context))) { Py_DECREF(other); Py_DECREF((PyObject*)result); TYPE_ERROR("all items in iterable must be real numbers"); return NULL; } errcode = PyList_SetItem(other, i,(PyObject*)temp); if (errcode < 0) { Py_DECREF(other); Py_DECREF((PyObject*)result); TYPE_ERROR("all items in iterable must be real numbers"); return NULL; } } /* create an array of pointers to the mpfr_t field of a Pympfr object */ if (!(tab = (mpfr_ptr *)GMPY_MALLOC((sizeof(mpfr_srcptr) * seq_length)))) { Py_DECREF(other); Py_DECREF((PyObject*)result); return PyErr_NoMemory(); } for (i=0; i < seq_length; i++) { temp = (MPFR_Object*)PyList_GET_ITEM(other, i); tab[i] = temp->f; } mpfr_clear_flags(); result->rc = mpfr_sum(result->f, tab, seq_length, GET_MPFR_ROUND(context)); Py_DECREF(other); GMPY_FREE(tab); _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * GMPy_Real_DivMod_1(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *tempx = NULL, *tempy = NULL, *quo = NULL, *rem = NULL; PyObject *result = NULL; CHECK_CONTEXT(context); if (!(result = PyTuple_New(2)) || !(rem = GMPy_MPFR_New(0, context)) || !(quo = GMPy_MPFR_New(0, context))) { /* LCOV_EXCL_START */ goto error; /* LCOV_EXCL_STOP */ } if (IS_REAL(x) && IS_REAL(y)) { if (!(tempx = GMPy_MPFR_From_Real(x, 1, context)) || !(tempy = GMPy_MPFR_From_Real(y, 1, context))) { /* LCOV_EXCL_START */ goto error; /* LCOV_EXCL_STOP */ } if (mpfr_zero_p(tempy->f)) { context->ctx.divzero = 1; if (context->ctx.traps & TRAP_DIVZERO) { GMPY_DIVZERO("divmod() division by zero"); goto error; } } if (mpfr_nan_p(tempx->f) || mpfr_nan_p(tempy->f) || mpfr_inf_p(tempx->f)) { context->ctx.invalid = 1; if (context->ctx.traps & TRAP_INVALID) { GMPY_INVALID("divmod() invalid operation"); goto error; } else { mpfr_set_nan(quo->f); mpfr_set_nan(rem->f); } } else if (mpfr_inf_p(tempy->f)) { context->ctx.invalid = 1; if (context->ctx.traps & TRAP_INVALID) { GMPY_INVALID("divmod() invalid operation"); goto error; } if (mpfr_zero_p(tempx->f)) { mpfr_set_zero(quo->f, mpfr_sgn(tempy->f)); mpfr_set_zero(rem->f, mpfr_sgn(tempy->f)); } else if ((mpfr_signbit(tempx->f)) != (mpfr_signbit(tempy->f))) { mpfr_set_si(quo->f, -1, MPFR_RNDN); mpfr_set_inf(rem->f, mpfr_sgn(tempy->f)); } else { mpfr_set_si(quo->f, 0, MPFR_RNDN); rem->rc = mpfr_set(rem->f, tempx->f, MPFR_RNDN); } } else { MPFR_Object *temp; if (!(temp = GMPy_MPFR_New(0, context))) { /* LCOV_EXCL_START */ goto error; /* LCOV_EXCL_STOP */ } mpfr_fmod(rem->f, tempx->f, tempy->f, MPFR_RNDN); mpfr_sub(temp->f, tempx->f, rem->f, MPFR_RNDN); mpfr_div(quo->f, temp->f, tempy->f, MPFR_RNDN); if (!mpfr_zero_p(rem->f)) { if ((mpfr_sgn(tempy->f) < 0) != (mpfr_sgn(rem->f) < 0)) { mpfr_add(rem->f, rem->f, tempy->f, MPFR_RNDN); mpfr_sub_ui(quo->f, quo->f, 1, MPFR_RNDN); } } else { mpfr_copysign(rem->f, rem->f, tempy->f, MPFR_RNDN); } if (!mpfr_zero_p(quo->f)) { mpfr_round(quo->f, quo->f); } else { mpfr_setsign(quo->f, quo->f, mpfr_sgn(tempx->f) * mpfr_sgn(tempy->f) - 1, MPFR_RNDN); } Py_DECREF((PyObject*)temp); } GMPY_MPFR_CHECK_RANGE(quo, context); GMPY_MPFR_CHECK_RANGE(rem, context); GMPY_MPFR_SUBNORMALIZE(quo, context); GMPY_MPFR_SUBNORMALIZE(rem, context); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); PyTuple_SET_ITEM(result, 0, (PyObject*)quo); PyTuple_SET_ITEM(result, 1, (PyObject*)rem); return (PyObject*)result; } /* LCOV_EXCL_START */ SYSTEM_ERROR("Internal error in GMPy_Real_DivMod_1()."); 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_Real_DivMod_2(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *tempx = NULL, *tempy = NULL, *quo = NULL, *rem = NULL; PyObject *result = NULL; CHECK_CONTEXT(context); if (!(result = PyTuple_New(2)) || !(rem = GMPy_MPFR_New(0, context)) || !(quo = GMPy_MPFR_New(0, context))) { /* LCOV_EXCL_START */ goto error; /* LCOV_EXCL_STOP */ } if (IS_REAL(x) && IS_REAL(y)) { if (!(tempx = GMPy_MPFR_From_Real(x, 1, context)) || !(tempy = GMPy_MPFR_From_Real(y, 1, context))) { /* LCOV_EXCL_START */ goto error; /* LCOV_EXCL_STOP */ } if (mpfr_zero_p(tempy->f)) { context->ctx.divzero = 1; if (context->ctx.traps & TRAP_DIVZERO) { GMPY_DIVZERO("divmod() division by zero"); goto error; } } if (mpfr_nan_p(tempx->f) || mpfr_nan_p(tempy->f) || mpfr_inf_p(tempx->f)) { context->ctx.invalid = 1; if (context->ctx.traps & TRAP_INVALID) { GMPY_INVALID("divmod() invalid operation"); goto error; } else { mpfr_set_nan(quo->f); mpfr_set_nan(rem->f); } } else if (mpfr_inf_p(tempy->f)) { context->ctx.invalid = 1; if (context->ctx.traps & TRAP_INVALID) { GMPY_INVALID("divmod() invalid operation"); goto error; } if (mpfr_zero_p(tempx->f)) { mpfr_set_zero(quo->f, mpfr_sgn(tempy->f)); mpfr_set_zero(rem->f, mpfr_sgn(tempy->f)); } else if ((mpfr_signbit(tempx->f)) != (mpfr_signbit(tempy->f))) { mpfr_set_si(quo->f, -1, MPFR_RNDN); mpfr_set_inf(rem->f, mpfr_sgn(tempy->f)); } else { mpfr_set_si(quo->f, 0, MPFR_RNDN); rem->rc = mpfr_set(rem->f, tempx->f, MPFR_RNDN); } } else { MPQ_Object *mpqx = NULL, *mpqy = NULL, *temp_rem = NULL; MPZ_Object *temp_quo = NULL; if (!(mpqx = GMPy_MPQ_From_MPFR(tempx, context)) || !(mpqy = GMPy_MPQ_From_MPFR(tempy, context))) { /* LCOV_EXCL_START */ Py_XDECREF((PyObject*)mpqx); Py_XDECREF((PyObject*)mpqy); goto error; /* LCOV_EXCL_STOP */ } if (!(temp_rem = GMPy_MPQ_New(context)) || !(temp_quo = GMPy_MPZ_New(context))) { /* LCOV_EXCL_START */ Py_XDECREF((PyObject*)temp_rem); Py_XDECREF((PyObject*)temp_quo); Py_XDECREF((PyObject*)mpqx); Py_XDECREF((PyObject*)mpqy); goto error; /* LCOV_EXCL_STOP */ } Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); mpq_div(temp_rem->q, mpqx->q, mpqy->q); mpz_fdiv_q(temp_quo->z, mpq_numref(temp_rem->q), mpq_denref(temp_rem->q)); /* Need to calculate x - quo * y. */ mpq_set_z(temp_rem->q, temp_quo->z); mpq_mul(temp_rem->q, temp_rem->q, mpqy->q); mpq_sub(temp_rem->q, mpqx->q, temp_rem->q); Py_DECREF((PyObject*)mpqx); Py_DECREF((PyObject*)mpqy); quo->rc = mpfr_set_z(quo->f, temp_quo->z, MPFR_RNDD); rem->rc = mpfr_set_q(rem->f, temp_rem->q, MPFR_RNDN); Py_DECREF((PyObject*)temp_rem); Py_DECREF((PyObject*)temp_quo); GMPY_MPFR_CHECK_RANGE(quo, context); GMPY_MPFR_CHECK_RANGE(rem, context); GMPY_MPFR_SUBNORMALIZE(quo, context); GMPY_MPFR_SUBNORMALIZE(rem, context); 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_Real_DivMod_2()."); 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_Real_Pow(PyObject *base, PyObject *exp, PyObject *mod, CTXT_Object *context) { MPFR_Object *tempb = NULL, *tempe = NULL, *result = NULL; MPZ_Object *tempz = NULL; MPC_Object *mpc_result = NULL; if (mod != Py_None) { TYPE_ERROR("pow() 3rd argument not allowed unless all arguments are integers"); return NULL; } CHECK_CONTEXT(context); result = GMPy_MPFR_New(0, context); tempb = GMPy_MPFR_From_Real(base, 1, context); if (!result || !tempb) { goto err; } mpfr_clear_flags(); if (PyIntOrLong_Check(exp)) { int error; long temp_exp = GMPy_Integer_AsLongAndError(exp, &error); if (!error) { result->rc = mpfr_pow_si(result->f, tempb->f, temp_exp, GET_MPFR_ROUND(context)); } else { mpz_t tempzz; mpz_inoc(tempzz); mpz_set_PyIntOrLong(tempzz, exp); result->rc = mpfr_pow_z(result->f, tempb->f, tempzz, GET_MPFR_ROUND(context)); mpz_cloc(tempzz); } } else if (IS_INTEGER(exp)) { if (!(tempz = GMPy_MPZ_From_Integer(exp, context))) { goto err; } result->rc = mpfr_pow_z(result->f, tempb->f, tempz->z, GET_MPFR_ROUND(context)); } else { if (!(tempe = GMPy_MPFR_From_Real(exp, 1, context))) { goto err; } result->rc = mpfr_pow(result->f, tempb->f, tempe->f, GET_MPFR_ROUND(context)); } /* If the result is NaN, check if a complex result works. */ if (result && mpfr_nanflag_p() && context->ctx.allow_complex) { mpc_result = (MPC_Object*)GMPy_Complex_Pow(base, exp, Py_None, context); if (!mpc_result || MPC_IS_NAN_P(mpc_result)) { Py_XDECREF((PyObject*)mpc_result); context->ctx.invalid = 1; GMPY_INVALID("pow() invalid operation"); goto err; } /* return a valid complex result */ Py_XDECREF((PyObject*)tempe); Py_XDECREF((PyObject*)tempz); Py_XDECREF((PyObject*)tempb); Py_XDECREF((PyObject*)result); return (PyObject*)mpc_result; } GMPY_MPFR_CLEANUP(result, context, "pow()"); Py_XDECREF((PyObject*)tempz); Py_XDECREF((PyObject*)tempe); Py_XDECREF((PyObject*)tempb); return (PyObject*)result; err: Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)tempz); Py_XDECREF((PyObject*)tempe); Py_XDECREF((PyObject*)tempb); return NULL; }
static PyObject * GMPy_Real_FloorDiv(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *result; CHECK_CONTEXT(context); if (!(result = GMPy_MPFR_New(0, context))) { /* LCOV_EXCL_START */ return NULL; /* LCOV_EXCL_STOP */ } if (MPFR_Check(x)) { if (MPFR_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_div(result->f, MPFR(x), MPFR(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } if (PyIntOrLong_Check(y)) { int error; long tempi = GMPy_Integer_AsLongAndError(y, &error); if (!error) { mpfr_clear_flags(); result->rc = mpfr_div_si(result->f, MPFR(x), tempi, GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } else { mpz_set_PyIntOrLong(global.tempz, y); mpfr_clear_flags(); result->rc = mpfr_div_z(result->f, MPFR(x), global.tempz, GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } } if (CHECK_MPZANY(y)) { mpfr_clear_flags(); result->rc = mpfr_div_z(result->f, MPFR(x), MPZ(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } if (IS_RATIONAL(y)) { MPQ_Object *tempy; if (!(tempy = GMPy_MPQ_From_Number(y, context))) { Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_div_q(result->f, MPFR(x), tempy->q, GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); Py_DECREF((PyObject*)tempy); goto done; } if (PyFloat_Check(y)) { mpfr_clear_flags(); result->rc = mpfr_div_d(result->f, MPFR(x), PyFloat_AS_DOUBLE(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } } if (MPFR_Check(y)) { if (PyIntOrLong_Check(x)) { int error; long tempi = GMPy_Integer_AsLongAndError(x, &error); if (!error) { mpfr_clear_flags(); result->rc = mpfr_si_div(result->f, tempi, MPFR(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } } /* Since mpfr_z_div does not exist, this combination is handled at the * end by converting x to an mpfr. Ditto for rational.*/ if (PyFloat_Check(x)) { mpfr_clear_flags(); result->rc = mpfr_d_div(result->f, PyFloat_AS_DOUBLE(x), MPFR(y), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); goto done; } } /* Handle the remaining cases. * Note: verify that MPZ if converted at full precision! */ if (IS_REAL(x) && IS_REAL(y)) { MPFR_Object *tempx, *tempy; tempx = GMPy_MPFR_From_Real(x, 1, context); tempy = GMPy_MPFR_From_Real(y, 1, context); if (!tempx || !tempy) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)result); return NULL; } mpfr_clear_flags(); result->rc = mpfr_div(result->f, MPFR(tempx), MPFR(tempy), GET_MPFR_ROUND(context)); result->rc = mpfr_floor(result->f, result->f); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); goto done; } Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; done: _GMPy_MPFR_Cleanup(&result, context); return (PyObject*)result; }
static PyObject * GMPy_Real_Mod(PyObject *x, PyObject *y, CTXT_Object *context) { MPFR_Object *tempx = NULL, *tempy = NULL, *temp, *result; CHECK_CONTEXT(context); result = GMPy_MPFR_New(0, context); temp = GMPy_MPFR_New(0, context); if (!result || !temp) { Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)temp); return NULL; } if (IS_REAL(x) && IS_REAL(y)) { tempx = GMPy_MPFR_From_Real(x, 1, context); tempy = GMPy_MPFR_From_Real(y, 1, context); if (!tempx || !tempy) { SYSTEM_ERROR("could not convert Real to mpfr"); goto error; } if (mpfr_zero_p(tempy->f)) { context->ctx.divzero = 1; if (context->ctx.traps & TRAP_DIVZERO) { GMPY_DIVZERO("mod() modulo by zero"); goto error; } } if (mpfr_nan_p(tempx->f) || mpfr_nan_p(tempy->f) || mpfr_inf_p(tempx->f)) { context->ctx.invalid = 1; if (context->ctx.traps & TRAP_INVALID) { GMPY_INVALID("mod() invalid operation"); goto error; } else { mpfr_set_nan(result->f); } } else if (mpfr_inf_p(tempy->f)) { context->ctx.invalid = 1; if (context->ctx.traps & TRAP_INVALID) { GMPY_INVALID("mod() invalid operation"); goto error; } if (mpfr_signbit(tempy->f)) { mpfr_set_inf(result->f, -1); } else { result->rc = mpfr_set(result->f, tempx->f, GET_MPFR_ROUND(context)); } } else { mpfr_fmod(result->f, tempx->f, tempy->f, GET_MPFR_ROUND(context)); if (!mpfr_zero_p(result->f)) { if ((mpfr_sgn(tempy->f) < 0) != (mpfr_sgn(result->f) < 0)) { mpfr_add(result->f, result->f, tempy->f, GET_MPFR_ROUND(context)); } } else { mpfr_copysign(result->f, result->f, tempy->f, GET_MPFR_ROUND(context)); } Py_DECREF((PyObject*)temp); } GMPY_MPFR_CHECK_RANGE(result, context); GMPY_MPFR_SUBNORMALIZE(result, context); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return (PyObject*)result; } Py_DECREF((PyObject*)temp); Py_DECREF((PyObject*)result); Py_RETURN_NOTIMPLEMENTED; error: Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); Py_DECREF((PyObject*)temp); Py_DECREF((PyObject*)result); return NULL; }