void test_gc_get_floating (void* data) { mpfr_t* num = GC_NEW_FLOATING(runtime); mpfr_t* num2 = GC_NEW_FLOATING(runtime); mpfr_set_d(*num, 2.3, MPFR_RNDN); mpfr_set_d(*num, 4.2, MPFR_RNDN); mpfr_add(*num, *num, *num2, MPFR_RNDN); tt_assert(mpfr_cmp_d(*num, 4.2) == 0); end:; }
static void init(int n0, int m0) { int i, j; m = m0; n = n0; mpfr_init(zero); mpfr_set_d(zero, 0, GMP_RNDN); mpfr_init(one); mpfr_set_d(one, 1, GMP_RNDN); mpfr_init(eps); mpfr_set_d(eps, EPS, GMP_RNDN); mpfr_init(minuseps); mpfr_set_d(minuseps, -EPS, GMP_RNDN); mpfr_init(large); mpfr_set_d(large, 1.0 / EPS, GMP_RNDN); a = malloc(sizeof(mpfr_t *) * (m + 1)); for(i=0;i < m+1;i++) { a[i] = malloc(sizeof(mpfr_t) * (n + 1)); for(j=0;j < (n+1);j++) { mpfr_zinit(a[i][j]); } } q = malloc(sizeof(mpfr_t *) * (m + 1)); for(i=0;i < m+1;i++) { q[i] = malloc(sizeof(mpfr_t) * (m + 1)); for(j=0;j < m+1;j++) { mpfr_zinit(q[i][j]); } } c = malloc(sizeof(mpfr_t) * (n + 1)); for(j=0;j < (n+1);j++) { mpfr_zinit(c[j]); } pivotcolumn = malloc(sizeof(mpfr_t) * (m + 1)); for(j=0;j < (m+1);j++) { mpfr_zinit(pivotcolumn[j]); } col = calloc(m+1, sizeof(int)); row = calloc(n+2*m+1, sizeof(int)); nonzero_row = calloc(n+2*m+1, sizeof(int)); inequality = calloc(m+1, sizeof(int)); }
int coords_calculate_precision(coords* c) { int l, p; mpfr_t tmp; mpfr_t bail; mpfr_t px_size; mpfr_t precision; mpfr_init2(tmp, c->precision); mpfr_init2(bail, c->precision); mpfr_init2(px_size, c->precision); mpfr_init2(precision, c->precision); mpfr_set_d( bail, 4.0, GMP_RNDN); mpfr_div_si(px_size, c->width, c->img_width, GMP_RNDN); mpfr_div( tmp, bail, px_size, GMP_RNDN); l = mpfr_log2( precision, tmp, GMP_RNDN); p = (int)mpfr_get_si( precision, GMP_RNDN); if (l < 0) /* precision was rounded down */ ++p; c->recommend = p; mpfr_clear(tmp); mpfr_clear(bail); mpfr_clear(px_size); mpfr_clear(precision); mpfr_free_cache(); /* <-- keep valgrind happy over mpfr_log2 */ return c->recommend; }
int mpfr_mul_d (mpfr_ptr a, mpfr_srcptr b, double c, mpfr_rnd_t rnd_mode) { int inexact; mpfr_t d; mp_limb_t tmp_man[MPFR_LIMBS_PER_DOUBLE]; MPFR_SAVE_EXPO_DECL (expo); MPFR_LOG_FUNC (("b[%Pu]=%.*Rg c=%.20g rnd=%d", mpfr_get_prec(b), mpfr_log_prec, b, c, rnd_mode), ("a[%Pu]=%.*Rg inexact=%d", mpfr_get_prec (a), mpfr_log_prec, a, inexact)); MPFR_SAVE_EXPO_MARK (expo); MPFR_TMP_INIT1(tmp_man, d, IEEE_DBL_MANT_DIG); inexact = mpfr_set_d (d, c, rnd_mode); MPFR_ASSERTD (inexact == 0); MPFR_CLEAR_FLAGS (); inexact = mpfr_mul (a, b, d, rnd_mode); MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags); MPFR_SAVE_EXPO_FREE (expo); return mpfr_check_range (a, inexact, rnd_mode); }
int main(int argc, char *argv[]) { assert(argc>=3); const long n = atol(argv[1]); const long prec = atol(argv[2]); flint_rand_t randstate; flint_randinit_seed(randstate, 0x1337, 1); mpfr_t sigma; mpfr_init2(sigma, prec); mpfr_set_d(sigma, _gghlite_sigma(n), MPFR_RNDN); fmpz_poly_t g; fmpz_poly_init(g); fmpz_poly_sample_sigma(g, n, sigma, randstate); fmpq_poly_t gq; fmpq_poly_init(gq); fmpq_poly_set_fmpz_poly(gq, g); fmpq_poly_t g_inv; fmpq_poly_init(g_inv); fmpq_poly_t modulus; fmpq_poly_init_oz_modulus(modulus, n); printf(" n: %4ld, log σ': %7.2f, ",n, log2(_gghlite_sigma(n))); uint64_t t = ggh_walltime(0); fmpq_poly_invert_mod(g_inv, gq, modulus); t = ggh_walltime(t); printf("xgcd: %7.3f, ", t/1000000.0); fflush(0); t = ggh_walltime(0); _fmpq_poly_oz_invert_approx(g_inv, gq, n, 160); t = ggh_walltime(t); printf("%4ld: %7.3f, ", prec, t/1000000.0); fflush(0); t = ggh_walltime(0); fmpq_poly_oz_invert_approx(g_inv, gq, n, 160, 0); t = ggh_walltime(t); printf("%4lditer: %7.3f, ", prec, t/1000000.0); fflush(0); t = ggh_walltime(0); _fmpq_poly_oz_invert_approx(g_inv, gq, n, 0); t = ggh_walltime(t); printf("∞: %7.3f.", t/1000000.0); printf("\n"); mpfr_clear(sigma); fmpz_poly_clear(g); fmpq_poly_clear(gq); fmpq_poly_clear(g_inv); fmpq_poly_clear(modulus); flint_randclear(randstate); flint_cleanup(); return 0; }
int mpfr_d_div (mpfr_ptr a, double b, mpfr_srcptr c, mpfr_rnd_t rnd_mode) { int inexact; mpfr_t d; MPFR_SAVE_EXPO_DECL (expo); MPFR_LOG_FUNC ( ("b=%.20g c[%Pu]=%*.Rg rnd=%d", b, mpfr_get_prec (c), mpfr_log_prec, c, rnd_mode), ("a[%Pu]=%*.Rg", mpfr_get_prec (a), mpfr_log_prec, a)); MPFR_SAVE_EXPO_MARK (expo); mpfr_init2 (d, IEEE_DBL_MANT_DIG); inexact = mpfr_set_d (d, b, rnd_mode); MPFR_ASSERTN (inexact == 0); mpfr_clear_flags (); inexact = mpfr_div (a, d, c, rnd_mode); MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags); mpfr_clear(d); MPFR_SAVE_EXPO_FREE (expo); return mpfr_check_range (a, inexact, rnd_mode); }
static void prepare() { int i; n1 = n; for (i = 1; i <= m; i++) if (inequality[i] == GEQ) { n1++; nonzero_row[n1] = i; } n2 = n1; for (i = 1; i <= m; i++) if (inequality[i] == LEQ) { n2++; col[i] = n2; nonzero_row[n2] = row[n2] = i; } n3 = n2; for (i = 1; i <= m; i++) if (inequality[i] != LEQ) { n3++; col[i] = n3; nonzero_row[n3] = row[n3] = i; } for (i = 0; i <= m; i++) { mpfr_set_d(q[i][i], 1, GMP_RNDN); } }
static void bug20080610 (void) { /* bug on icc found on June 10, 2008 */ /* this is not a bug but a different implementation choice: ISO C99 doesn't specify the sign of a zero exponent (see note in random_double above). */ mpfr_t x; double y; int xi; char *xs; int yi; char *ys; mpfr_init2 (x, MPFR_LDBL_MANT_DIG); y = -9.95645044213728791504536275169812142849e-01; mpfr_set_d (x, y, MPFR_RNDN); xi = mpfr_asprintf (&xs, "%- #0.*Re", 1, x); yi = mpfr_asprintf (&ys, "%- #0.*e", 1, y); if (xi != yi || strcmp (xs, ys) != 0) { printf ("Error in bug20080610\n"); printf ("expected: %s\n", ys); printf (" got: %s\n", xs); printf ("xi=%d yi=%d\n", xi, yi); exit (1); } mpfr_free_str (xs); mpfr_free_str (ys); mpfr_clear (x); }
SEXP R_mpc_mul(SEXP e1, SEXP e2) { /* N.B. We always use signed integers for e2 given R's type system. */ 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_mul(*z, *z1, *z2, Rmpc_get_rounding()); } else if (Rf_isInteger(e2)) { mpc_init2(*z, mpc_get_prec(*z1)); mpc_mul_si(*z, *z1, INTEGER(e2)[0], Rmpc_get_rounding()); } else if (Rf_isNumeric(e2)) { mpc_init2(*z, mpc_get_prec(*z1)); mpfr_t x; mpfr_init2(x, 53); mpfr_set_d(x, REAL(e2)[0], GMP_RNDN); mpc_mul_fr(*z, *z1, x, Rmpc_get_rounding()); } else { Rf_error("Invalid second operand for mpc multiplication."); } } else { Rf_error("Invalid first operand for MPC multiplication."); } 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; }
number::number(double v) { number_base *d = new number_base; int r = mpfr_set_d(d->val, v, GMP_RNDN); assert(r == 0); (void)r; data = d; }
static void bug20081214 (void) { /* problem with glibc 2.3.6, December 14, 2008: the system asprintf outputs "-1.0" instead of "-1.". */ mpfr_t x; double y; int xi; char *xs; int yi; char *ys; mpfr_init2 (x, MPFR_LDBL_MANT_DIG); y = -9.90597761233942053494e-01; mpfr_set_d (x, y, MPFR_RNDN); xi = mpfr_asprintf (&xs, "%- #0.*RG", 1, x); yi = mpfr_asprintf (&ys, "%- #0.*G", 1, y); if (xi != yi || strcmp (xs, ys) != 0) { mpfr_printf ("Error in bug20081214\n" "mpfr_asprintf(\"%- #0.*Re\", 1, %Re)\n", x); printf ("expected: %s\n", ys); printf (" got: %s\n", xs); printf ("xi=%d yi=%d\n", xi, yi); exit (1); } mpfr_free_str (xs); mpfr_free_str (ys); mpfr_clear (x); }
gmp_RRorNull IM2_RingElement_to_BigReal(const RingElement *a) { const Ring *R = a->get_ring(); gmp_RR result; void *b; double *c; const M2::ConcreteRing<M2::ARingRRR> *R1; switch (R->ringID()) { case M2::ring_RR: result = getmemstructtype(gmp_RR); mpfr_init2(result, 53); b = static_cast<void *>(a->get_value().poly_val); c = static_cast<double *>(b); mpfr_set_d(result, *c, GMP_RNDN); return result; case M2::ring_RRR: R1 = dynamic_cast<const M2::ConcreteRing<M2::ARingRRR> *>(a->get_ring()); result = getmemstructtype(gmp_RR); mpfr_init2(result, R1->get_precision()); b = a->get_value().poly_val; mpfr_set(result, static_cast<gmp_RR>(b), GMP_RNDN); return result; default: if (!a->get_ring()->is_RRR()) { ERROR("expected an element of RRR"); return 0; } return a->get_value().mpfr_val; } }
coords* coords_new( int img_width, int img_height, double init_cx, double init_cy, double init_size) { coords* c = malloc(sizeof(*c)); if (!c) return 0; c->precision = 0; c->precision = DEFAULT_PRECISION; mpfr_init2(c->xmin, c->precision); mpfr_init2(c->xmax, c->precision); mpfr_init2(c->ymin, c->precision); mpfr_init2(c->ymax, c->precision); mpfr_init2(c->width, c->precision); mpfr_init2(c->height, c->precision); mpfr_init2(c->cx, c->precision); mpfr_init2(c->cy, c->precision); mpfr_init2(c->_size, c->precision); coords_set(c, img_width, img_height); c->init_cx = init_cx; c->init_cy = init_cy; c->init_size = init_size; mpfr_set_d( c->_size, init_size, GMP_RNDN); return c; }
int mpfr_mul_d (mpfr_ptr a, mpfr_srcptr b, double c, mpfr_rnd_t rnd_mode) { int inexact; mpfr_t d; MPFR_SAVE_EXPO_DECL (expo); MPFR_LOG_FUNC (("b[%Pu]=%.*Rg c=%.20g rnd=%d", mpfr_get_prec(b), mpfr_log_prec, b, c, rnd_mode), ("a[%Pu]=%.*Rg inexact=%d", mpfr_get_prec (a), mpfr_get_prec, a, inexact)); MPFR_SAVE_EXPO_MARK (expo); mpfr_init2 (d, IEEE_DBL_MANT_DIG); inexact = mpfr_set_d (d, c, rnd_mode); MPFR_ASSERTN (inexact == 0); mpfr_clear_flags (); inexact = mpfr_mul (a, b, d, rnd_mode); MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags); mpfr_clear(d); MPFR_SAVE_EXPO_FREE (expo); return mpfr_check_range (a, inexact, rnd_mode); }
void increase_norm(gmp_RR& norm, const ElementType& a) const { double d; abs(d,a); if (mpfr_cmp_d(norm, d) < 0) mpfr_set_d(norm, d, GMP_RNDN); }
void setReal(Real r, double bytes){ #ifdef USE_MPFR mpfr_set_d(r->mpfr_val, bytes, MPFR_RNDN); #else mpf_set_d(r->mpf_val, bytes); #endif }
SeedValue seed_mpfr_sub (SeedContext ctx, SeedObject function, SeedObject this_object, gsize argument_count, const SeedValue args[], SeedException *exception) { mpfr_rnd_t rnd; mpfr_ptr rop, op1, op2; gdouble dop1, dop2; gint ret; seed_mpfr_t argt1, argt2; /* only want 1 double argument. alternatively, could accept 2, add those, and set from the result*/ CHECK_ARG_COUNT("mpfr.sub", 3); rop = seed_object_get_private(this_object); rnd = seed_value_to_mpfr_rnd_t(ctx, args[2], exception); argt1 = seed_mpfr_arg_type(ctx, args[0], exception); argt2 = seed_mpfr_arg_type(ctx, args[1], exception); if ( (argt1 & argt2) == SEED_MPFR_MPFR ) { /* both mpfr_t */ op1 = seed_object_get_private(args[0]); op2 = seed_object_get_private(args[1]); ret = mpfr_sub(rop, op1, op2, rnd); } else if ( (argt1 | argt2) == (SEED_MPFR_MPFR | SEED_MPFR_DOUBLE) ) { /* a double and an mpfr_t. Figure out the order */ if ( argt1 == SEED_MPFR_MPFR ) { op1 = seed_object_get_private(args[0]); dop2 = seed_value_to_double(ctx, args[1], exception); mpfr_sub_d(rop, op1, dop2, rnd); } else { dop1 = seed_value_to_double(ctx, args[0], exception); op2 = seed_object_get_private(args[1]); mpfr_d_sub(rop, dop1, op2, rnd); } } else if ( (argt1 & argt2) == SEED_MPFR_DOUBLE ) { /* 2 doubles. hopefully doesn't happen */ dop1 = seed_value_to_double(ctx, args[0], exception); dop2 = seed_value_to_double(ctx, args[1], exception); ret = mpfr_set_d(rop, dop1 - dop2, rnd); } else { TYPE_EXCEPTION("mpfr.sub", "double or mpfr_t"); } return seed_value_from_int(ctx, ret, exception); }
void doubledouble_to_mpfr(mpfr_t rop, double dh, double dm) { mpfr_t temp; mp_prec_t prec; prec = mpfr_get_prec(rop); if (prec < 53) prec = 53; mpfr_init2(temp, prec); mpfr_set_d(rop, dh, GMP_RNDN); mpfr_set_d(temp, dm, GMP_RNDN); mpfr_add(rop, rop, temp, GMP_RNDN); mpfr_clear(temp); }
int mpfr_set_flt (mpfr_ptr r, float f, mpfr_rnd_t rnd_mode) { /* we convert f to double precision and use mpfr_set_d; NaN and infinities should be preserved, and all single precision numbers are exactly representable in the double format, thus the conversion is always exact */ return mpfr_set_d (r, (double) f, rnd_mode); }
int mpfr_d_div(mpfr_ptr z, double a, mpfr_srcptr x, mpfr_rnd_t r) { if (!initialized) { mpfr_init2(y, 53); initialized = 1; } mpfr_set_d(y, a, GMP_RNDN); return mpfr_div(z, y, x, r); }
/* Function to generate the binomial pdf */ void inpdf(mpfr_t *out,mpz_t n,int psize,double pini,double pinc,unsigned long mu,mpfr_t bcs[],mpfr_prec_t prec) { mpfr_t s; mpfr_init2(s,prec); mpfr_t f; mpfr_init2(f,prec); mpfr_t unity; mpfr_init2(unity,prec); mpfr_set_ui(unity,(unsigned long int) 1,MPFR_RNDN); mpfr_t Z; mpfr_init2(Z,prec); mpfr_set_d(Z,0,MPFR_RNDN); mpfr_t pfr; mpfr_init2(pfr,prec); int pdex; int odex; int odn; int i; for(pdex=0;pdex<=psize-1;pdex=pdex++) { mpfr_set_d(pfr,pini+pdex*pinc,MPFR_RNDN); for(i=0;i<=mu;i++) { odex=pdex*(mu+1)+i; mpfr_pow_ui(s, pfr,(unsigned long int)i,MPFR_RNDN); mpfr_sub(f, unity, pfr,MPFR_RNDN); mpfr_pow_ui(f,f,(unsigned long int)mu-i,MPFR_RNDN); mpfr_mul(*(out+odex),s,f,MPFR_RNDN); mpfr_mul(*(out+odex),bcs[i],*(out+odex),MPFR_RNDN); mpfr_add(Z,Z,*(out+odex),MPFR_RNDN); } for(i=0;i<=mu;i++) odn=pdex*(mu+1)+i; mpfr_div(*(out+odn), *(out+odn),Z,MPFR_RNDN); } mpfr_clear(pfr); mpfr_clear(s); mpfr_clear(f); mpfr_clear(unity); mpfr_clear(Z); }
int mpfr_mul_d(mpfr_t rop, mpfr_t op1, double _op2, mpfr_rnd_t rnd) { int r; mpfr_t op2; mpfr_init2( op2, mpfr_get_prec(op1)); mpfr_set_d( op2, _op2, rnd); r = mpfr_mul( rop, op1, op2, rnd); mpfr_clear(op2); return r; }
void mpfr_to_doubledouble(double *dh, double *dm, mpfr_t op) { mpfr_t rest, temp; mpfr_init2(rest,mpfr_get_prec(op)); mpfr_init2(temp,mpfr_get_prec(op)); mpfr_set(rest,op,GMP_RNDN); *dh = mpfr_get_d(rest, GMP_RNDN); mpfr_set_d(temp,*dh,GMP_RNDN); mpfr_sub(rest,rest,temp,GMP_RNDN); *dm = mpfr_get_d(rest, GMP_RNDN); mpfr_set_d(temp,*dm,GMP_RNDN); mpfr_sub(rest,rest,temp,GMP_RNDN); mpfr_clear(rest); mpfr_clear(temp); }
mpz_class FirstLogTable::function(int x) { mpz_class result; double apprinv; mpfr_t i,l; mpz_t r; mpfr_init(i); mpfr_init2(l,wOut); mpz_init2(r,400); apprinv = fit->output2double(fit->function(x));; // result = double2output(log(apprinv)); mpfr_set_d(i, apprinv, GMP_RNDN); mpfr_log(l, i, GMP_RNDN); mpfr_neg(l, l, GMP_RNDN); // Remove the sum of small offsets that are added to the other log tables for(int j=1; j<=op->stages; j++){ mpfr_set_d(i, 1.0, GMP_RNDN); int pi=op->p[j]; mpfr_mul_2si(i, i, -2*pi, GMP_RNDN); mpfr_sub(l, l, i, GMP_RNDN); } // code the log in 2's compliment mpfr_mul_2si(l, l, wOut, GMP_RNDN); mpfr_get_z(r, l, GMP_RNDN); result = mpz_class(r); // signed // This is a very inefficient way of converting mpz_class t = mpz_class(1) << wOut; result = t+result; if(result>t) result-=t; // cout << "x="<<x<<" apprinv="<<apprinv<<" logapprinv="<<log(apprinv)<<" result="<<result<<endl; mpfr_clear(i); mpfr_clear(l); mpz_clear(r); return result; }
real::real(double a) { mpfr_init(r); mpfr_set_d(r, a, MPFR_RNDN); ++n_reals; if (max_reals != -1 && n_reals > max_reals) { std::cout << "error: program has reached memory capacity" << std::endl; exit(1); } }
Floating* Floating_set_double (Floating* self, double number) { assert(self); mpfr_set_d(*self->value, number, MPFR_RNDN); invalidate_cache(self); return self; }
static void check4 (double d, mpfr_rnd_t rnd, int base, int prec) { mpfr_t x; mpfr_init2 (x, prec); mpfr_set_d (x, d, rnd); fprintf (fout, "%1.19e base %d rnd %d:\n ", d, base, rnd); mpfr_out_str (fout, base, (base == 2) ? prec : 0, x, rnd); fputc ('\n', fout); mpfr_clear (x); }
void check2a (double x, int px, double y, int py, int pz, mp_rnd_t rnd_mode, char *res) { mpfr_t xx, yy, zz; mpfr_init2(xx,px); mpfr_init2(yy,py); mpfr_init2(zz,pz); mpfr_set_d(xx, x, rnd_mode); mpfr_set_d(yy, y, rnd_mode); mpfr_add(zz, xx, yy, rnd_mode); mpfr_set_prec(xx, pz); mpfr_set_str(xx, res, 16, GMP_RNDN); if (mpfr_cmp(xx, zz)) { printf("x=%1.20e,%d y=%1.20e,%d pz=%d,rnd=%s\n", x,px,y,py,pz,mpfr_print_rnd_mode(rnd_mode)); printf("got "); mpfr_print_binary(zz); putchar('\n'); printf("instead of "); mpfr_print_binary(xx); putchar('\n'); exit(1); } mpfr_clear(xx); mpfr_clear(yy); mpfr_clear(zz); }
/* checks when source and destination are equal */ void check_same (void) { mpfr_t x; mpfr_init(x); mpfr_set_d(x, 1.0, GMP_RNDZ); mpfr_add(x, x, x, GMP_RNDZ); if (mpfr_get_d1 (x) != 2.0) { printf("Error when all 3 operands are equal\n"); exit(1); } mpfr_clear(x); }
void checknan (double x, double y, mp_rnd_t rnd_mode, unsigned int px, unsigned int py, unsigned int pz) { double z2; mpfr_t xx, yy, zz; mpfr_init2(xx, px); mpfr_init2(yy, py); mpfr_init2(zz, pz); mpfr_set_d(xx, x, rnd_mode); mpfr_set_d(yy, y, rnd_mode); mpfr_add(zz, xx, yy, rnd_mode); #ifdef MPFR_HAVE_FESETROUND mpfr_set_machine_rnd_mode(rnd_mode); #endif if (MPFR_IS_NAN(zz) == 0) { printf("Error, not an MPFR_NAN for xx = %1.20e, y = %1.20e\n", x, y); exit(1); } z2 = mpfr_get_d1 (zz); if (!isnan(z2)) { printf("Error, not a NaN after conversion, xx = %1.20e yy = %1.20e, got %1.20e\n", x, y, z2); exit(1); } mpfr_clear(xx); mpfr_clear(yy); mpfr_clear(zz); }