static MPC_Object * GMPy_MPC_From_MPQ(MPQ_Object *obj, mpfr_prec_t rprec, mpfr_prec_t iprec, CTXT_Object *context) { MPC_Object *result = NULL; assert(MPQ_Check(obj)); CHECK_CONTEXT(context); if (rprec == 0 || rprec == 1) rprec = GET_REAL_PREC(context) + rprec * GET_GUARD_BITS(context); if (iprec == 0 || iprec == 1) iprec = GET_IMAG_PREC(context) + iprec * GET_GUARD_BITS(context); if ((result = GMPy_MPC_New(rprec, iprec, context))) { result->rc = mpc_set_q(result->c, obj->q, GET_MPC_ROUND(context)); if (rprec != 1) { GMPY_MPC_CHECK_RANGE(result, context); } GMPY_MPC_SUBNORMALIZE(result, context); GMPY_MPC_EXCEPTIONS(result, context); } return result; }
static MPFR_Object * GMPy_MPFR_New(mpfr_prec_t bits, CTXT_Object *context) { MPFR_Object *result; if (bits == 0 || bits == 1) bits = GET_MPFR_PREC(context) + bits * GET_GUARD_BITS(context); if (bits < MPFR_PREC_MIN || bits > MPFR_PREC_MAX) { VALUE_ERROR("invalid value for precision"); return NULL; } if (in_gmpympfrcache) { result = gmpympfrcache[--in_gmpympfrcache]; /* Py_INCREF does not set the debugging pointers, so need to use _Py_NewReference instead. */ _Py_NewReference((PyObject*)result); mpfr_set_prec(result->f, bits); } else { if (!(result = PyObject_New(MPFR_Object, &MPFR_Type))) return NULL; mpfr_init2(result->f, bits); } result->hash_cache = -1; result->rc = 0; return result; }
static MPC_Object * GMPy_MPC_New(mpfr_prec_t rprec, mpfr_prec_t iprec, CTXT_Object *context) { MPC_Object *self; CHECK_CONTEXT(context); if (rprec == 0 || rprec == 1) rprec = GET_REAL_PREC(context) + rprec * GET_GUARD_BITS(context); if (iprec == 0 || iprec == 1) iprec = GET_IMAG_PREC(context) + iprec * GET_GUARD_BITS(context); if (rprec < MPFR_PREC_MIN || rprec > MPFR_PREC_MAX || iprec < MPFR_PREC_MIN || iprec > MPFR_PREC_MAX) { VALUE_ERROR("invalid value for precision"); return NULL; } if (in_gmpympccache) { self = gmpympccache[--in_gmpympccache]; /* Py_INCREF does not set the debugging pointers, so need to use _Py_NewReference instead. */ _Py_NewReference((PyObject*)self); if (rprec == iprec) { mpc_set_prec(self->c, rprec); } else { mpc_clear(self->c); mpc_init3(self->c, rprec, iprec); } } else { if (!(self = PyObject_New(MPC_Object, &MPC_Type))) { /* LCOV_EXCL_START */ return NULL; /* LCOV_EXCL_STOP */ } mpc_init3(self->c, rprec, iprec); } self->hash_cache = -1; self->rc = 0; return self; }