예제 #1
0
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;
}
예제 #2
0
static MPC_Object *
GMPy_MPC_From_Decimal(PyObject *obj, mpfr_prec_t rprec, mpfr_prec_t iprec,
                      CTXT_Object *context)
{
    MPC_Object *result = NULL;
    MPFR_Object *tempf;
    mpfr_prec_t oldmpfr, oldreal;
    int oldmpfr_round, oldreal_round;

    assert(IS_DECIMAL(obj));

    CHECK_CONTEXT(context);

    oldmpfr = GET_MPFR_PREC(context);
    oldreal = GET_REAL_PREC(context);
    oldmpfr_round = GET_MPFR_ROUND(context);
    oldreal_round = GET_REAL_ROUND(context);

    context->ctx.mpfr_prec = oldreal;
    context->ctx.mpfr_round = oldreal_round;

    tempf = GMPy_MPFR_From_Decimal(obj, rprec, context);

    context->ctx.mpfr_prec = oldmpfr;
    context->ctx.mpfr_round = oldmpfr_round;

    result = GMPy_MPC_New(0, 0, context);
    if (!tempf || !result) {
        Py_XDECREF((PyObject*)tempf);
        Py_XDECREF((PyObject*)result);
        return NULL;
    }

    result->rc = MPC_INEX(tempf->rc, 0);
    mpfr_swap(mpc_realref(result->c), tempf->f);
    Py_DECREF(tempf);
    return result;
}