static void reuse_bug (void) { /* bug found by the automatic builds on http://hydra.nixos.org/build/1469029/log/raw */ mpc_t x, y, z; mp_prec_t prec = 2; for (prec = 2; prec <= 20; prec ++) { mpc_init2 (x, prec); mpc_init2 (y, prec); mpc_init2 (z, prec); mpfr_set_ui (mpc_realref (x), 0ul, MPFR_RNDN); mpfr_set_ui_2exp (mpc_imagref (x), 3ul, -2, MPFR_RNDN); mpc_set_ui (y, 8ul, MPC_RNDNN); mpc_pow (z, x, y, MPC_RNDNN); mpc_pow (y, x, y, MPC_RNDNN); if (mpfr_signbit (mpc_imagref (y)) != mpfr_signbit (mpc_imagref (z))) { printf ("Error: regression, reuse_bug reproduced\n"); exit (1); } mpc_clear (x); mpc_clear (y); mpc_clear (z); } }
SEXP R_mpc_pow(SEXP e1, SEXP e2) { mpc_t *z = (mpc_t *)malloc(sizeof(mpc_t)); if (Rf_inherits(e1, "mpc")) { mpc_t *z1 = (mpc_t *)R_ExternalPtrAddr(e1); if (Rf_inherits(e2, "mpc")) { mpc_t *z2 = (mpc_t *)R_ExternalPtrAddr(e2); mpc_init2(*z, max(mpc_get_prec(*z1), mpc_get_prec(*z2))); mpc_pow(*z, *z1, *z2, Rmpc_get_rounding()); } else if (Rf_isInteger(e2)) { mpc_init2(*z, mpc_get_prec(*z1)); mpc_pow_si(*z, *z1, INTEGER(e2)[0], Rmpc_get_rounding()); } else if (Rf_isNumeric(e2)) { mpc_init2(*z, mpc_get_prec(*z1)); mpc_pow_d(*z, *z1, REAL(e2)[0], Rmpc_get_rounding()); } else { Rf_error("Invalid second operand for mpc power."); } } else { Rf_error("Invalid first operand for MPC power."); } SEXP retVal = PROTECT(R_MakeExternalPtr((void *)z, Rf_install("mpc ptr"), R_NilValue)); Rf_setAttrib(retVal, R_ClassSymbol, Rf_mkString("mpc")); R_RegisterCFinalizerEx(retVal, mpcFinalizer, TRUE); UNPROTECT(1); return retVal; }
int mpc_pow_ui (mpc_ptr z, mpc_srcptr x, unsigned long y, mpc_rnd_t rnd) { mpc_t yy; int inex; mpc_init3 (yy, sizeof (unsigned long) * CHAR_BIT, MPFR_PREC_MIN); mpc_set_ui (yy, y, MPC_RNDNN); /* exact */ inex = mpc_pow (z, x, yy, rnd); mpc_clear (yy); return inex; }
static void compare_mpc_pow (mpfr_prec_t pmax, int iter, unsigned long nbits) /* copied from tpow_ui.c and replaced unsigned by signed */ { mpfr_prec_t p; mpc_t x, y, z, t; long n; int i, inex_pow, inex_pow_si; mpc_rnd_t rnd; mpc_init3 (y, sizeof (unsigned long) * CHAR_BIT, MPFR_PREC_MIN); for (p = MPFR_PREC_MIN; p <= pmax; p++) for (i = 0; i < iter; i++) { mpc_init2 (x, p); mpc_init2 (z, p); mpc_init2 (t, p); mpc_urandom (x, rands); n = (signed long) gmp_urandomb_ui (rands, nbits); mpc_set_si (y, n, MPC_RNDNN); for (rnd = 0; rnd < 16; rnd ++) { inex_pow = mpc_pow (z, x, y, rnd); inex_pow_si = mpc_pow_si (t, x, n, rnd); if (mpc_cmp (z, t) != 0) { printf ("mpc_pow and mpc_pow_si differ for x="); mpc_out_str (stdout, 10, 0, x, MPC_RNDNN); printf (" n=%li\n", n); printf ("mpc_pow gives "); mpc_out_str (stdout, 10, 0, z, MPC_RNDNN); printf ("\nmpc_pow_si gives "); mpc_out_str (stdout, 10, 0, t, MPC_RNDNN); printf ("\n"); exit (1); } if (inex_pow != inex_pow_si) { printf ("mpc_pow and mpc_pow_si give different flags for x="); mpc_out_str (stdout, 10, 0, x, MPC_RNDNN); printf (" n=%li\n", n); printf ("mpc_pow gives %d\n", inex_pow); printf ("mpc_pow_si gives %d\n", inex_pow_si); exit (1); } } mpc_clear (x); mpc_clear (z); mpc_clear (t); } mpc_clear (y); }
int mpc_pow_d (mpc_ptr z, mpc_srcptr x, double y, mpc_rnd_t rnd) { mpc_t yy; int inex; MPC_ASSERT(FLT_RADIX == 2); mpc_init3 (yy, DBL_MANT_DIG, MPFR_PREC_MIN); mpc_set_d (yy, y, MPC_RNDNN); /* exact */ inex = mpc_pow (z, x, yy, rnd); mpc_clear (yy); return inex; }
void ovm_dd_pow(oregister_t *l, oregister_t *r) { switch (r->t) { case t_void: l->t = t_float; l->v.d = 1.0; break; case t_word: real(r->v.dd) = r->v.w; imag(r->v.dd) = 0.0; goto cdd; case t_float: real(r->v.dd) = r->v.d; imag(r->v.dd) = 0.0; goto cdd; case t_mpz: real(r->v.dd) = mpz_get_d(ozr(r)); imag(r->v.dd) = 0.0; goto cdd; case t_rat: real(r->v.dd) = rat_get_d(r->v.r); imag(r->v.dd) = 0.0; goto cdd; case t_mpq: real(r->v.dd) = mpq_get_d(oqr(r)); imag(r->v.dd) = 0.0; goto cdd; case t_mpr: mpc_set_fr(occ(r), orr(r), thr_rndc); goto mpc; case t_cdd: cdd: l->v.dd = cpow(l->v.dd, r->v.dd); check_cdd(l); break; case t_cqq: real(r->v.dd) = mpq_get_d(oqr(r)); imag(r->v.dd) = mpq_get_d(oqi(r)); goto cdd; case t_mpc: mpc: l->t = t_mpc; mpc_set_d_d(occ(l), real(l->v.dd), imag(l->v.dd), thr_rndc); mpc_pow(occ(l), occ(l), occ(r), thr_rndc); check_mpc(l); break; default: ovm_raise(except_not_a_number); } }
int mpc_pow_fr (mpc_ptr z, mpc_srcptr x, mpfr_srcptr y, mpc_rnd_t rnd) { mpc_t yy; int inex; /* avoid copying the significand of y by copying only the struct */ MPC_RE(yy)[0] = y[0]; mpfr_init2 (MPC_IM(yy), MPFR_PREC_MIN); mpfr_set_ui (MPC_IM(yy), 0, GMP_RNDN); inex = mpc_pow (z, x, yy, rnd); mpfr_clear (MPC_IM(yy)); return inex; }
void ovm_q_pow(oregister_t *l, oregister_t *r) { switch (r->t) { case t_void: if (!cfg_float_format) { l->t = t_float; l->v.d = 1.0; } else { l->t = t_mpr; mpfr_set_ui(orr(l), 1, thr_rnd); } break; case t_word: if (!cfg_float_format) { l->t = t_float; l->v.d = pow(mpq_get_d(oqr(l)), r->v.w); } else { mpfr_set_si(orr(r), r->v.w, thr_rnd); goto mpr; } break; case t_float: if (mpq_sgn(oqr(l)) < 0 && finite(r->v.d) && rint(r->v.d) != r->v.d) { real(r->v.dd) = r->v.d; imag(r->v.dd) = 0.0; goto cdd; } l->t = t_float; l->v.d = pow(mpq_get_d(oqr(l)), r->v.d); break; case t_mpz: if (!cfg_float_format) { l->t = t_float; l->v.d = pow(mpq_get_d(oqr(l)), mpz_get_d(ozr(r))); } else { mpfr_set_z(orr(r), ozr(r), thr_rnd); goto mpr; } break; case t_rat: if (mpq_sgn(oqr(l)) < 0) { if (!cfg_float_format) { real(r->v.dd) = mpq_get_d(oqr(r)); imag(r->v.dd) = 0.0; goto cdd; } mpc_set_q(occ(r), oqr(r), thr_rndc); goto mpc; } else { if (!cfg_float_format) { l->t = t_float; l->v.d = pow(mpq_get_d(oqr(l)), rat_get_d(r->v.r)); } else { mpq_set_si(oqr(r), rat_num(r->v.r), rat_den(r->v.r)); mpfr_set_q(orr(r), oqr(r), thr_rnd); goto mpr; } } break; case t_mpq: if (mpq_sgn(oqr(r)) < 0) { if (!cfg_float_format) { real(r->v.dd) = mpq_get_d(oqr(r)); imag(r->v.dd) = 0.0; goto cdd; } mpc_set_q(occ(r), oqr(r), thr_rndc); goto mpc; } else { if (!cfg_float_format) { l->t = t_float; l->v.d = pow(mpq_get_d(oqr(l)), mpq_get_d(oqr(r))); } else { mpfr_set_q(orr(r), oqr(r), thr_rnd); goto mpr; } } break; case t_mpr: if (mpq_sgn(oqr(l)) < 0 && mpfr_number_p(orr(r)) && !mpfr_integer_p(orr(r))) { mpc_set_q(occ(r), oqr(r), thr_rndc); goto mpc; } mpr: l->t = t_mpr; mpfr_set_q(orr(l), oqr(l), thr_rnd); mpfr_pow(orr(l), orr(l), orr(r), thr_rnd); break; case t_cdd: cdd: l->t = t_cdd; real(l->v.dd) = mpq_get_d(oqr(l)); imag(l->v.dd) = 0.0; l->v.dd = cpow(l->v.dd, r->v.dd); check_cdd(l); break; case t_cqq: if (!cfg_float_format) { real(r->v.dd) = mpq_get_d(oqr(r)); imag(r->v.dd) = mpq_get_d(oqi(r)); goto cdd; } mpc_set_q_q(occ(r), oqr(r), oqi(r), thr_rndc); case t_mpc: mpc: l->t = t_mpc; mpc_set_q(occ(l), oqr(l), thr_rndc); mpc_pow(occ(l), occ(l), occ(r), thr_rndc); check_mpc(l); break; default: ovm_raise(except_not_a_number); } }
static PyObject * GMPy_Complex_Pow(PyObject *base, PyObject *exp, PyObject *mod, CTXT_Object *context) { MPC_Object *tempb = NULL, *tempe = NULL, *result= NULL; MPFR_Object *tempf = NULL; MPZ_Object *tempz = NULL; if (mod != Py_None) { TYPE_ERROR("pow() 3rd argument not allowed unless all arguments are integers"); return NULL; } CHECK_CONTEXT(context); result = GMPy_MPC_New(0, 0, context); tempb = GMPy_MPC_From_Complex(base, 1, 1, context); if (!result || !tempb) { goto err; } mpfr_clear_flags(); if (PyIntOrLong_Check(exp)) { int error; long temp_exp = GMPy_Integer_AsLongAndError(exp, &error); if (!error) { result->rc = mpc_pow_si(result->c, tempb->c, temp_exp, GET_MPC_ROUND(context)); } else { mpz_t tempzz; mpz_inoc(tempzz); mpz_set_PyIntOrLong(tempzz, exp); result->rc = mpc_pow_z(result->c, tempb->c, tempzz, GET_MPC_ROUND(context)); mpz_cloc(tempzz); } } else if (IS_INTEGER(exp)) { if (!(tempz = GMPy_MPZ_From_Integer(exp, context))) { goto err; } result->rc = mpc_pow_z(result->c, tempb->c, tempz->z, GET_MPC_ROUND(context)); } else if (IS_REAL(exp)) { if (!(tempf = GMPy_MPFR_From_Real(exp, 1, context))) { goto err; } result->rc = mpc_pow_fr(result->c, tempb->c, tempf->f, GET_MPC_ROUND(context)); } else { if (!(tempe = GMPy_MPC_From_Complex(exp, 1, 1, context))) { goto err; } result->rc = mpc_pow(result->c, tempb->c, tempe->c, GET_MPC_ROUND(context)); } GMPY_MPC_CLEANUP(result, context, "pow()"); Py_XDECREF((PyObject*)tempz); Py_XDECREF((PyObject*)tempf); Py_XDECREF((PyObject*)tempe); Py_XDECREF((PyObject*)tempb); return (PyObject*)result; err: Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)tempz); Py_XDECREF((PyObject*)tempf); Py_XDECREF((PyObject*)tempe); Py_XDECREF((PyObject*)tempb); return NULL; }
void Lib_Mpcr_Pow(MpcrPtr x, MpcrPtr y, MpcrPtr z, long rnd) { mpc_pow( (mpc_ptr) x, (mpc_ptr) y, (mpc_ptr) z, (mpc_rnd_t) rnd); }