static PyObject * Pympany_copy(PyObject *self, PyObject *other) { if (self && Pympz_Check(self)) return (PyObject*)Pympz2Pympz(self); else if (self && Pyxmpz_Check(self)) return (PyObject*)Pyxmpz2Pyxmpz(self); else if (self && Pympq_Check(self)) return (PyObject*)Pympq2Pympq(self); #ifdef WITHMPFR else if (self && Pympfr_Check(self)) return (PyObject*)Pympfr2Pympfr(self, 0); #endif else if (Pympz_Check(other)) return (PyObject*)Pympz2Pympz(other); else if (Pyxmpz_Check(other)) return (PyObject*)Pyxmpz2Pyxmpz(other); else if (Pympq_Check(other)) return (PyObject*)Pympq2Pympq(other); #ifdef WITHMPFR else if (Pympfr_Check(other)) return (PyObject*)Pympfr2Pympfr(other, 0); #endif TYPE_ERROR("_copy() requires a gmpy2 object as argument"); return NULL; }
static PyObject * Pympany_binary(PyObject *self, PyObject *other) { if (self && Pympz_Check(self)) return Pympz2binary((PympzObject*)self); else if(self && Pyxmpz_Check(self)) return Pyxmpz2binary((PyxmpzObject*)self); else if(self && Pympq_Check(self)) return Pympq2binary((PympqObject*)self); #ifdef WITHMPFR else if(self && Pympfr_Check(self)) return Pympfr2binary((PympfrObject*)self); #endif else if(Pympz_Check(other)) return Pympz2binary((PympzObject*)other); else if(Pyxmpz_Check(other)) return Pyxmpz2binary((PyxmpzObject*)other); else if(Pympq_Check(other)) return Pympq2binary((PympqObject*)other); #ifdef WITHMPFR else if(Pympfr_Check(other)) return Pympfr2binary((PympfrObject*)other); #endif TYPE_ERROR("binary() requires a gmpy2 object as argument"); return NULL; }
static PyObject * Pympany_printf(PyObject *self, PyObject *args) { PyObject *result = 0, *x = 0; char *buffer = 0, *fmtcode = 0; int buflen; void *generic; if (!PyArg_ParseTuple(args, "sO", &fmtcode, &x)) return NULL; if (CHECK_MPZANY(x) || Pympq_Check(x)) { if (CHECK_MPZANY(x)) generic = Pympz_AS_MPZ(x); else generic = Pympq_AS_MPQ(x); buflen = gmp_asprintf(&buffer, fmtcode, generic); result = Py_BuildValue("s", buffer); PyMem_Free(buffer); return result; } #ifdef WITHMPFR else if(Pympfr_Check(x)) { generic = Pympfr_AS_MPFR(x); buflen = mpfr_asprintf(&buffer, fmtcode, generic); result = Py_BuildValue("s", buffer); PyMem_Free(buffer); return result; } #endif else { TYPE_ERROR("printf() requires a gmpy2 object as argument"); return NULL; } }
static int isOne(PyObject* obj) { int overflow = 0; long temp; if (!obj) return 1; if (Pympq_Check(obj)) { return (0==mpz_cmp_ui(mpq_denref(Pympq_AS_MPQ(obj)),1)) && (0==mpz_cmp_ui(mpq_numref(Pympq_AS_MPQ(obj)),1)); } else if (Pympz_Check(obj)) { return 0==mpz_cmp_ui(Pympz_AS_MPZ(obj),1); } else if (Pyxmpz_Check(obj)) { return 0==mpz_cmp_ui(Pyxmpz_AS_MPZ(obj),1); #ifdef PY2 } else if (PyInt_Check(obj)) { return PyInt_AS_LONG(obj)==1; #endif } #ifdef WITHMPFR else if (Pympfr_Check(obj)) { return mpfr_get_d(Pympfr_AS_MPFR(obj), context->ctx.mpfr_round)==1.0; } #endif else if (PyFloat_Check(obj)) { return PyFloat_AS_DOUBLE(obj)==1.0; } else if (PyLong_Check(obj)) { temp = PyLong_AsLongAndOverflow(obj, &overflow); if (!overflow && temp == 1) return 1; else return 0; } return 0; }