static PyObject * Pympany_pow(PyObject *base, PyObject *exp, PyObject *mod) { #ifndef WITHMPFR PyObject *result = 0, *temp; #endif if (isInteger(base) && isInteger(exp)) return Pympz_pow(base, exp, mod); else if (isRational(base) && isRational(exp)) return Pympq_pow(base, exp, mod); #ifdef WITHMPFR else if (isReal(base) && isReal(exp)); return Pympfr2_pow(base, exp, mod); #else /* Support mpz**float and float**mpz. */ if (CHECK_MPZANY(base) && PyFloat_Check(exp)) { temp = PyFloat_FromDouble(mpz_get_d(Pympz_AS_MPZ(base))); if (temp) { result = PyNumber_Power(temp, exp, mod); Py_DECREF(temp); } return result; } if (CHECK_MPZANY(exp) && PyFloat_Check(base)) { temp = PyFloat_FromDouble(mpz_get_d(Pympz_AS_MPZ(exp))); if (temp) { result = PyNumber_Power(base, temp, mod); Py_DECREF(temp); } return result; } #endif Py_RETURN_NOTIMPLEMENTED; }
static PyObject * Pympany_sign(PyObject *self, PyObject *other) { if (isInteger(other)) return Pympz_sign(self, other); else if (isRational(other)) return Pympq_sign(self, other); #ifdef WITHMPFR else if (isReal(other)) return Pympfr_sign(self, other); #endif TYPE_ERROR("sign() not supported"); return NULL; }
// =============================================================================================================== // ====================== Override Methods ========================== // =============================================================================================================== string NumericLiteral::toString() const { ostringstream sting_stream; if (isInteger()) { sting_stream << (int) numerator; return sting_stream.str(); } if (isRational()) { sting_stream << numerator << "/" << denominator; return sting_stream.str(); } if (isReal()) { sting_stream << numerator; return sting_stream.str(); } throw UTComputerException("Error NumericLiteral::toString() : literal isn't integer nor rational nor real"); }
static PyObject * Pympany_square(PyObject *self, PyObject *other) { if (isInteger(other)) { return Pympz_square(self, other); } else if (isRational(other)) { return Pympq_square(self, other); } #ifdef WITHMPFR else if (isReal(other)) { return Pympfr_sqr(self, other); } #endif TYPE_ERROR("square() not supported"); return NULL; }
void NumericLiteral::simplification() { if (!isRational()) throw UTComputerException("Error NumericLiteral::simplification : simplifiaction on non retional literal is impossible."); // if numerator is 0, then set denominator to 1 if (numerator == 0) { denominator = 1; return; } // denominator can't be 0 if (denominator == 0) { throw UTComputerException("Error NumericLiteral::simplification : denominator can't be 0."); } /* Use of Euclide algorithm to find the greatest common divisor */ double a=numerator, b=denominator; // we only works with positive values if (a<0) a=-a; if (b<0) b=-b; while(a != b) { if (a>b) a = a - b; else b = b - a; } // division of numerator and denominator by a, the greatest common divisor numerator /= a; denominator /= a; // if denominator is negative, pass the negation to the numerator if (denominator < 0) { denominator = -denominator; numerator = -numerator; } }
static PyObject * Pympany_digits(PyObject *self, PyObject *args) { PyObject *temp; if (PyTuple_GET_SIZE(args) == 0) { TYPE_ERROR("digits() requires at least one argument"); return NULL; } temp = PyTuple_GET_ITEM(args, 0); if (isInteger(temp)) return Pympz_digits(self, args); else if (isRational(temp)) return Pympq_digits(self, args); #ifdef WITHMPFR else if (isReal(temp)) return Pympfr_digits(self, args); #endif TYPE_ERROR("digits() not supported"); return NULL; }
const CVC4::Rational& SExpr::getRationalValue() const { PrettyCheckArgument( isRational(), this ); return d_rationalValue; }
static PyObject * Pygmpy_xmpz(PyObject *self, PyObject *args, PyObject *keywds) { PyxmpzObject *result = 0; PyObject *n = 0; long base = 0; Py_ssize_t argc; static char *kwlist[] = {"n", "base", NULL }; /* Optimize the most common use case */ argc = PyTuple_Size(args); if (argc == 1) { n = PyTuple_GetItem(args, 0); #ifdef WITHMPFR if (isReal(n) && !keywds) { #else if ((isRational(n) || PyFloat_Check(n)) && !keywds) { #endif result = Pyxmpz_From_Number(n); if (!result && !PyErr_Occurred()) TYPE_ERROR("xmpz() requires numeric or string argument"); return (PyObject*)result; } } if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|l", kwlist, &n, &base)) return NULL; if ((base!=0) && ((base<2)||(base>62))) { VALUE_ERROR("base for xmpz() must be 0 or in the " "interval 2 ... 62"); return NULL; } if (PyStrOrUnicode_Check(n)) { /* build-from-string (ascii or unicode) */ result = Pyxmpz_From_PyStr(n, base); } else { if (argc==2 || (argc == 1 && keywds)) TYPE_ERROR("xmpz() with non-string argument needs exactly " "1 argument"); else { result = Pyxmpz_From_Number(n); if (!result && !PyErr_Occurred()) TYPE_ERROR("xmpz() requires numeric or string argument"); } } return (PyObject*)result; } /* For many xmpz_functions, the doc-strings are in gmpy_mpz.c. */ static PyObject * Pyxmpz_digits(PyObject *self, PyObject *args) { long base = 10; PyObject *result; PARSE_ONE_MPZ_OPT_CLONG(&base, "digits() requires 'int' argument for base"); result = Pyxmpz_To_PyStr((PyxmpzObject*)self, base, 0); Py_DECREF(self); return result; }
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 * 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; }