mpcomplex operator-(const mpcomplex& a, const long int& b) { mpc_t value; mpc_init3( value , a.mpc_prec, a.mpc_prec ); mpc_sub_ui(value, a.mpc_val, b, a.default_rnd); return mpcomplex(value); }
SEXP R_mpc_sub(SEXP e1, SEXP e2) { mpc_t *z = (mpc_t *)malloc(sizeof(mpc_t)); if (z == NULL) { Rf_error("Could not allocate memory for MPC type."); } if (Rf_inherits(e1, "mpc")) { Rprintf("It's an 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_sub(*z, *z1, *z2, Rmpc_get_rounding()); } else if (Rf_isInteger(e2)) { mpc_init2(*z, mpc_get_prec(*z1)); mpc_sub_ui(*z, *z1, INTEGER(e2)[0], Rmpc_get_rounding()); } else if (Rf_isNumeric(e2)) { mpfr_t x; mpfr_init2(x, 53); mpfr_set_d(x, REAL(e2)[0], GMP_RNDN); Rprintf("Precision: %d\n", mpc_get_prec(*z1)); mpc_init2(*z, max(mpc_get_prec(*z1), 53)); mpc_sub_fr(*z, *z1, x, Rmpc_get_rounding()); } else { Rf_error("Unsupported type for operand 2 of MPC subtraction."); } } else if (Rf_isInteger(e1)) { if (Rf_inherits(e2, "mpc")) { mpc_t *z2 = (mpc_t *)R_ExternalPtrAddr(e2); mpc_init2(*z, mpc_get_prec(*z2)); mpc_ui_sub(*z, INTEGER(e1)[0], *z2, Rmpc_get_rounding()); } else { Rf_error("Unsupported type for operands for MPC subtraction."); } } else if (Rf_isNumeric(e1)) { if (Rf_inherits(e2, "mpc")) { mpc_t *z2 = (mpc_t *)R_ExternalPtrAddr(e2); mpc_init2(*z, mpc_get_prec(*z2)); mpfr_t x; mpfr_init2(x, 53); mpfr_set_d(x, REAL(e1)[0], GMP_RNDN); mpc_fr_sub(*z, x, *z2, Rmpc_get_rounding()); } else { Rf_error("Unsupported type for operands for MPC subtraction."); } } else { /* TODO(mstokely): Add support for mpfr types here. */ Rprintf("It's unknown"); free(z); Rf_error("Invalid second operand for mpc subtraction."); } 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; }
void Lib_Mpcr_Sub_Ui(MpcrPtr f, MpcrPtr g, unsigned int x, long rnd) { mpc_sub_ui( (mpc_ptr) f, (mpc_ptr) g, x, (mpc_rnd_t) rnd); }