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_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_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_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_Context_Sign(PyObject *self, PyObject *other) { CTXT_Object *context = NULL; if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } return GMPy_Number_Sign(other, context); }
static PyObject * GMPy_Context_Add(PyObject *self, PyObject *args) { CTXT_Object *context = NULL; if (PyTuple_GET_SIZE(args) != 2) { TYPE_ERROR("add() requires 2 arguments."); return NULL; } if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } return GMPy_Number_Add(PyTuple_GET_ITEM(args, 0), PyTuple_GET_ITEM(args, 1), context); }
static PyObject * GMPy_Context_Round2(PyObject *self, PyObject *args) { CTXT_Object *context = NULL; if (PyTuple_GET_SIZE(args) < 1 || PyTuple_GET_SIZE(args) > 2) { TYPE_ERROR("round2() requires 1 or 2 arguments"); return NULL; } if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } if (PyTuple_GET_SIZE(args) == 1) { return GMPy_Number_Round2(PyTuple_GET_ITEM(args, 0), NULL, context); } else { return GMPy_Number_Round2(PyTuple_GET_ITEM(args, 0), PyTuple_GET_ITEM(args, 1), context); } }
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_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; }