void my_init(unsigned long prec) { mpf_init2(t1, prec); mpf_init2(t2, prec); mpf_init2(d1, prec); mpf_init2(d2, prec); }
static void precision_init(int prec) { int i; mpf_t f0; mpf_set_default_prec(prec); mpf_init2(epsilon, 2); mpf_init2(negepsilon, 2); mpf_init(recipeulere); mpf_init(pi); mpf_init(eulere); mpf_set_ui(epsilon, 1); mpf_div_2exp(epsilon, epsilon, prec); mpf_neg(negepsilon, epsilon); mpf_init(f0); mpf_set_ui(eulere, 1); mpf_set_ui(f0, 1); for (i=1;; i++) { mpf_div_ui(f0, f0, i); if (mpf_cmp(f0, epsilon) < 0) { break; } mpf_add(eulere, eulere, f0); } mpf_clear(f0); mpf_ui_div(recipeulere, 1, eulere); compute_pi(prec); }
/** * rasqal_xsd_decimal_round: * @result: result variable * @a: argment decimal * * Return the number with no fractional part closes to argument for an XSD Decimal * * Return value: non-0 on failure **/ int rasqal_xsd_decimal_round(rasqal_xsd_decimal* result, rasqal_xsd_decimal* a) { int rc = 0; #ifdef RASQAL_DECIMAL_GMP mpf_t b; mpf_t c; #endif rasqal_xsd_decimal_clear_string(result); #if defined(RASQAL_DECIMAL_C99) || defined(RASQAL_DECIMAL_NONE) result->raw = round(a->raw); #endif #ifdef RASQAL_DECIMAL_MPFR mpfr_round(result->raw, a->raw); #endif #ifdef RASQAL_DECIMAL_GMP /* GMP has no mpf_round so use result := floor(a + 0.5) */ mpf_init2(b, a->precision_bits); mpf_init2(c, a->precision_bits); mpf_set_d(b, 0.5); mpf_add(c, a->raw, b); mpf_floor(result->raw, c); mpf_clear(b); mpf_clear(c); #endif return rc; }
void init() { mpf_init2(mandelRange.minRe,gmpBit); mpf_init2(mandelRange.maxRe,gmpBit); mpf_init2(mandelRange.minIm,gmpBit); mpf_init2(mandelRange.maxIm,gmpBit); mpf_set_d(mandelRange.minRe,-2.0); mpf_set_d(mandelRange.maxRe,1.0); mpf_set_d(mandelRange.minIm,-1.2); }
void mpfr_bench(mpfr_prec_t prec_a, mpfr_prec_t prec_b, mpfr_prec_t prec_c, const char *b_str, const char *c_str, unsigned long seed) { mpfr_t a,b,c; mpf_t x,y,z; mpz_t zz; gmp_randstate_t state; gmp_randinit_lc_2exp_size (state, 128); gmp_randseed_ui (state, seed); mpfr_init2(a, prec_a); mpfr_init2(b, prec_b); mpfr_init2(c, prec_c); mpf_init2(x, prec_a); mpf_init2(y, prec_b); mpf_init2(z, prec_c); if (b_str) mpf_set_str(y, b_str, 10); else mpf_urandomb(y, state, prec_b); if (c_str) mpf_set_str(z, c_str, 10); else mpf_urandomb(z, state, prec_c); mpfr_set_f(b, y, MPFR_RNDN); mpfr_set_f(c, z, MPFR_RNDN); mpz_init (zz); mpz_urandomb (zz, state, 2*prec_b); if (verbose) { printf("B="); mpfr_out_str(stdout, 10, 0, b, MPFR_RNDD); printf("\nC="); mpfr_out_str(stdout, 10, 0, c, MPFR_RNDD); putchar('\n'); } TIMP_OVERHEAD (); #undef BENCH #define BENCH(TEST_STR, TEST) printf(" "TEST_STR": %Lu\n", TIMP_MEASURE(TEST)) TEST_LIST; mpz_clear (zz); mpfr_clear (a); mpfr_clear (b); mpfr_clear (c); mpf_clear (x); mpf_clear (y); mpf_clear (z); gmp_randclear (state); }
/** * @brief Print a float to stdout (or whatever the output stream is * atm) respecting the given options, and only with the significant * digits. * * @param s A pointer to the current mps_context. * @param f The float approximation that should be printed. * @param rad The current inclusion radius for that approximation. * @param out_digit The number of output digits required. * @param sign The sign of the approximation. */ MPS_PRIVATE void mps_outfloat (mps_context * s, mpf_t f, rdpe_t rad, long out_digit, mps_boolean sign) { mpf_t t; rdpe_t r, ro; double d; long l, digit, true_digit; if (s->output_config->format == MPS_OUTPUT_FORMAT_FULL) { mpf_init2 (t, mpf_get_prec (f)); mpf_set (t, f); mpf_out_str (s->outstr, 10, 0, t); mpf_clear (t); return; } mpf_init2 (t, s->output_config->prec); mpf_get_rdpe (ro, f); if (s->output_config->format == MPS_OUTPUT_FORMAT_GNUPLOT || s->output_config->format == MPS_OUTPUT_FORMAT_GNUPLOT_FULL) rdpe_out_str_u (s->outstr, ro); else { rdpe_abs_eq (ro); if (rdpe_ne (ro, rdpe_zero)) rdpe_div (r, rad, ro); else rdpe_set_d (r, 1.0e-10); digit = (long)(-rdpe_log10 (r) - 0.5); if (digit <= 0) { rdpe_get_dl (&d, &l, ro); fprintf (s->outstr, "0.e%ld", l); } else { true_digit = (long)(LOG10_2 * mpf_get_prec (f)); true_digit = MIN (digit, true_digit); true_digit = MIN (true_digit, out_digit); if (sign) mpf_set (t, f); else mpf_abs (t, f); mpf_out_str (s->outstr, 10, true_digit, t); } } mpf_clear (t); }
void recalcZoom() { int32_t diffX = selectZoomW - selectZoomX; int32_t diffY = selectZoomH - selectZoomY; if(abs(diffX) >= 10 || abs(diffY) >= 10) { long double x = sizeX; long double y = sizeY; long double factorLeft = ((selectZoomX*100.0)/x)/100.0; long double factorRight = 1.0-((selectZoomW*100.0)/x)/100.0; long double factorBottom = 1.0-((selectZoomH*100.0)/y)/100.0; mpf_t facLeft; mpf_init2(facLeft,gmpBit); mpf_set_d(facLeft,factorLeft); mpf_t facRight; mpf_init2(facRight,gmpBit); mpf_set_d(facRight,factorRight); mpf_t facBottom; mpf_init2(facBottom,gmpBit); mpf_set_d(facBottom,factorBottom); mpf_t tmpDiff; mpf_init2(tmpDiff,gmpBit); mpf_t tmp; mpf_init2(tmp,gmpBit); //(maxRe - minRe) mpf_sub(tmpDiff,mandelRange.maxRe,mandelRange.minRe); //minRe+=(maxRe - minRe)*factorLeft; mpf_mul(tmp,tmpDiff,facLeft); mpf_add(mandelRange.minRe,mandelRange.minRe,tmp); //maxRe-=(maxRe - minRe)*factorRight; mpf_mul(tmp,tmpDiff,facRight); mpf_sub(mandelRange.maxRe,mandelRange.maxRe,tmp); //minIm+=(maxIm - minIm)*factorBottom; mpf_sub(tmpDiff,mandelRange.maxIm,mandelRange.minIm); mpf_mul(tmp,tmpDiff,facBottom); mpf_add(mandelRange.minIm,mandelRange.minIm,tmp); mpf_clear(tmp); mpf_clear(tmpDiff); mpf_clear(facLeft); mpf_clear(facRight); mpf_clear(facBottom); restart(); } }
unsigned char matrix_sub_d_d_mpf(double***** omatrix, unsigned long long* omsize, double**** imatrix, unsigned long long* imsize, double t) { unsigned long long n,m,p; unsigned char flag; mpf_t z1,tmp1,tmp2; unsigned char prec=21; if (imsize[1]==2) { mpf_init2(z1,prec); mpf_set_d(z1,t); omsize[1]=imsize[1]-1; } else { printf("ERROR: No free variables to use for substitution\n"); return 3; } omsize[0]=imsize[0]; flag = matrix_alloc_d(omatrix,omsize,1); //allocate each row to previous row allocation if (flag!=0) { mpf_clear(z1); return flag; } mpf_init2(tmp1,prec); mpf_init2(tmp2,prec); for (n=0ULL;n<omsize[0];n++) { for (m=0ULL;m<omsize[0];m++) { (*omatrix)[n][m][0][0]=1; for (p=0;p<imatrix[n][m][0][0];p++) { mpf_pow_ui(tmp1,z1,(unsigned long int)imatrix[n][m][1][imsize[1]*p]); mpf_set_d(tmp2,imatrix[n][m][1][imsize[1]*p+1]); mpf_mul(tmp1,tmp1,tmp2); (*omatrix)[n][m][1][0] = (*omatrix)[n][m][1][0] + mpf_get_d(tmp1); } } } mpf_clears(z1,tmp1,tmp2,NULL); return 0; }
/* * call-seq: * rand_state.mpfr_urandomb() * * From the MPFR Manual: * * Generate a uniformly distributed random float in the interval 0 <= rop < 1. */ VALUE r_gmprandstate_mpfr_urandomb(int argc, VALUE *argv, VALUE self) { MP_RANDSTATE *self_val; MP_FLOAT *res_val; VALUE res; unsigned long prec = 0; if (argc > 1) rb_raise (rb_eArgError, "wrong # of arguments(%d for 0 or 1)", argc); mprandstate_get_struct (self,self_val); if (argc == 1) { if (FIXNUM_P (argv[0])) { if (FIX2INT (argv[0]) >= 0) prec = FIX2INT (argv[0]); else rb_raise (rb_eRangeError, "prec must be non-negative"); } else { rb_raise (rb_eTypeError, "prec must be a Fixnum"); } } mpf_make_struct (res, res_val); if (prec == 0) { mpf_init (res_val); } else { mpf_init2 (res_val, prec); } mpfr_urandomb (res_val, self_val); return res; }
static void mps_context_init (mps_context * s) { mpf_t test; /* Set default streams */ s->instr = stdin; s->outstr = stdout; s->logstr = stdout; /* Allocate space for the configurations */ s->input_config = (mps_input_configuration*)mps_malloc (sizeof(mps_input_configuration)); s->output_config = (mps_output_configuration*)mps_malloc (sizeof(mps_output_configuration)); mps_set_default_values (s); /* Find minimum GMP supported precision */ mpf_init2 (test, 1); s->minimum_gmp_precision = mpf_get_prec (test); mpf_clear (test); /* Set standard precision */ s->output_config->prec = (int)(0.9 * DBL_DIG * LOG2_10); MPS_DEBUG (s, "Setting prec_out to %ld digits", s->output_config->prec); mps_mp_set_prec (s, DBL_DIG * LOG2_10 + 1); s->initialized = false; s->exit_required = false; }
static void rasqal_xsd_decimal_init(rasqal_xsd_decimal* dec) { /* XSD wants min of 18 decimal (base 10) digits * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#decimal */ #ifdef RASQAL_DECIMAL_NONE /* double has a restricted range */ dec->precision_digits = DBL_DIG; #else dec->precision_digits = 18; #endif /* over-estimate bits since log(10)/log(2) = 3.32192809488736234789 < 4 */ dec->precision_bits = dec->precision_digits*4; #ifdef RASQAL_DECIMAL_C99 dec->raw = 0DD; #endif #ifdef RASQAL_DECIMAL_MPFR mpfr_init2(dec->raw, dec->precision_bits); /* GMP_RNDD, GMP_RNDU, GMP_RNDN, GMP_RNDZ */ dec->rounding = mpfr_get_default_rounding_mode(); #endif #ifdef RASQAL_DECIMAL_GMP mpf_init2(dec->raw, dec->precision_bits); #endif #ifdef RASQAL_DECIMAL_NONE dec->raw = 0e0; #endif dec->string = NULL; dec->string_len = 0; }
void coords_set_precision(coords* c, mpfr_prec_t precision) { mpf_t test; if (precision < DEFAULT_PRECISION) precision = DEFAULT_PRECISION; mpf_init2(test, precision); c->gmp_precision = mpf_get_prec(test); if ((unsigned long)c->gmp_precision > (unsigned long)precision) precision = c->gmp_precision; precision_change(c->xmin, precision); precision_change(c->xmax, precision); precision_change(c->ymin, precision); precision_change(c->ymax, precision); precision_change(c->cx, precision); precision_change(c->cy, precision); precision_change(c->width, precision); precision_change(c->height, precision); precision_change(c->_size, precision); c->precision = precision; c->size = (c->aspect > 1.0) ? &c->width : &c->height; mpf_clear(test); }
void extrapolate(index_t num_samples, mpf_t *samples, mpf_t ans) { // The Richardson extrapolation recursive formula is // // A_n+1(x) = (2^n A_n(2x) - A_n(x)) / (2^n - 1) mpf_t mult; // mult = 2^n mpf_init_set_d(mult, 1); mpf_t denom; // denom = 1 / (mult - 1) mp_bitcnt_t precision = mpf_get_prec(ans); mpf_init2(denom, precision); mpf_t *end = samples + num_samples; for (mpf_t *lim = samples; lim < end; lim++) { // lim - samples = n mpf_mul_ui(mult, mult, 2); mpf_set(denom, mult); mpf_sub_ui(denom, denom, 1); mpf_ui_div(denom, 1, denom); // evaluate all extrapolations for (mpf_t *ptr = end; --ptr > lim; ) { // A_n+1(x) = (mult A_n(2x) - A_n(x)) * denom mpf_mul(*ptr, *ptr, mult); mpf_sub(*ptr, *ptr, *(ptr - 1)); mpf_mul(*ptr, *ptr, denom); } } mpf_set(ans, *(end - 1)); // move to ans // Clean mpf_clear(mult); mpf_clear(denom); }
void check_max (void) { mpf_t f; long want; long got; mpf_init2 (f, 200L); #define CHECK_MAX(name) \ if (got != want) \ { \ printf ("mpf_get_si wrong on %s\n", name); \ printf (" f "); \ mpf_out_str (stdout, 10, 0, f); printf (", hex "); \ mpf_out_str (stdout, 16, 0, f); printf ("\n"); \ printf (" got %ld, hex %lX\n", got, got); \ printf (" want %ld, hex %lX\n", want, want); \ abort(); \ } want = LONG_MAX; mpf_set_si (f, want); got = mpf_get_si (f); CHECK_MAX ("LONG_MAX"); want = LONG_MIN; mpf_set_si (f, want); got = mpf_get_si (f); CHECK_MAX ("LONG_MIN"); mpf_clear (f); }
void fmpz_poly_disc_gauss_rounding(fmpz_poly_t rop, const fmpq_poly_t x, const mpfr_t r_f, gmp_randstate_t randstate) { mpfr_t xi; mpfr_init2(xi, mpfr_get_prec(r_f)); mpf_t xi_f; mpf_init2(xi_f, mpfr_get_prec(r_f)); mpq_t xi_q; mpq_init(xi_q); mpz_t s_z; mpz_init(s_z); const long n = fmpq_poly_length(x); const size_t tau = (ceil(2*sqrt(log2((double)n))) > 3) ? ceil(2*sqrt(log2((double)n))) : 3; fmpz_poly_zero(rop); for(int i=0; i<n; i++) { fmpq_poly_get_coeff_mpq(xi_q, x, i); mpf_set_q(xi_f, xi_q); mpfr_set_f(xi, xi_f, MPFR_RNDN); dgs_disc_gauss_mp_t *D = dgs_disc_gauss_mp_init(r_f, xi, tau, DGS_DISC_GAUSS_UNIFORM_ONLINE); D->call(s_z, D, randstate); dgs_disc_gauss_mp_clear(D); fmpz_poly_set_coeff_mpz(rop, i, s_z); } mpz_clear(s_z); mpq_clear(xi_q); mpf_clear(xi_f); mpfr_clear(xi); }
Real mkReal(void){ Real result = VG_(malloc)("real", sizeof(struct _RealStruct)); #ifdef USE_MPFR mpfr_init2(result->mpfr_val, precision); #else mpf_init2(result->mpf_val, precision); #endif return result; }
void check_data (void) { static const struct { int base; const char *f; long want; } data[] = { { 10, "0", 0L }, { 10, "1", 1L }, { 10, "-1", -1L }, { 10, "2", 2L }, { 10, "-2", -2L }, { 10, "12345", 12345L }, { 10, "-12345", -12345L }, /* fraction bits ignored */ { 10, "0.5", 0L }, { 10, "-0.5", 0L }, { 10, "1.1", 1L }, { 10, "-1.1", -1L }, { 10, "1.9", 1L }, { 10, "-1.9", -1L }, { 16, "1.000000000000000000000000000000000000000000000000001", 1L }, { 16, "-1.000000000000000000000000000000000000000000000000001", -1L }, /* low bits extracted (this is undocumented) */ { 16, "1000000000000000000000000000000000000000000000000001", 1L }, { 16, "-1000000000000000000000000000000000000000000000000001", -1L }, }; int i; mpf_t f; long got; mpf_init2 (f, 2000L); for (i = 0; i < numberof (data); i++) { mpf_set_str_or_abort (f, data[i].f, data[i].base); got = mpf_get_si (f); if (got != data[i].want) { printf ("mpf_get_si wrong at data[%d]\n", i); printf (" f \"%s\"\n", data[i].f); printf (" dec "); mpf_out_str (stdout, 10, 0, f); printf ("\n"); printf (" hex "); mpf_out_str (stdout, 16, 0, f); printf ("\n"); printf (" size %ld\n", (long) SIZ(f)); printf (" exp %ld\n", (long) EXP(f)); printf (" got %ld (0x%lX)\n", got, got); printf (" want %ld (0x%lX)\n", data[i].want, data[i].want); abort(); } } mpf_clear (f); }
void recalcView() { long double x = sizeX; long double y = sizeY; int32_t diffX = selectMoveW - selectMoveX; int32_t diffY = selectMoveH - selectMoveY; if(abs(diffX) >= 1 || abs(diffY) >= 1) { long double factorX = ((diffX*100.0)/x)/100.0; long double factorY = ((diffY*100.0)/y)/100.0; mpf_t facX; mpf_init2(facX,gmpBit); mpf_set_d(facX,factorX); mpf_t facY; mpf_init2(facY,gmpBit); mpf_set_d(facY,factorY); mpf_t tmpDiff; mpf_init2(tmpDiff,gmpBit); mpf_t tmp; mpf_init2(tmp,gmpBit); mpf_sub(tmpDiff,mandelRange.maxRe,mandelRange.minRe); //minRe-=(maxRe - minRe)*factorX; mpf_mul(tmp,tmpDiff,facX); mpf_sub(mandelRange.maxRe,mandelRange.maxRe,tmp); //maxRe-=(maxRe - minRe)*factorX; mpf_sub(mandelRange.minRe,mandelRange.minRe,tmp); //minIm+=(maxIm - minIm)*factorY; mpf_sub(tmpDiff,mandelRange.maxIm,mandelRange.minIm); mpf_mul(tmp,tmpDiff,facY); mpf_add(mandelRange.minIm,mandelRange.minIm,tmp); mpf_clear(tmp); mpf_clear(tmpDiff); mpf_clear(facX); mpf_clear(facY); restart(); } }
void precision_change_gmp(mpf_t x, mp_bitcnt_t p) { mpf_t tmp; mpf_init2(tmp, p); mpf_set(tmp, x); mpf_set_prec( x, p); mpf_set( x, tmp); mpf_clear(tmp); }
static void test_cmp_f (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpf_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, pmax+GMP_NUMB_BITS); mpf_init2 (y, MPFR_PREC_MIN); /* check the erange flag when x is NaN */ mpfr_set_nan (x); mpf_set_ui (y, 17); mpfr_clear_erangeflag (); res1 = mpfr_cmp_f (x, y); if (res1 != 0 || mpfr_erangeflag_p () == 0) { printf ("Error for mpfr_cmp_f (NaN, 17)\n"); printf ("Return value: expected 0, got %d\n", res1); printf ("Erange flag: expected set, got %d\n", mpfr_erangeflag_p ()); exit (1); } for(p=pmin ; p < pmax ; p+=3) { mpfr_set_prec (x, p); mpf_set_prec (y, p); for ( n = 0; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpf_urandomb (y, RANDS, p); if (!MPFR_IS_SINGULAR (x)) { mpfr_set_f (z, y, MPFR_RNDN); mpfr_sub (z, x, z, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_f (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_f: res=%d sub gives %d\n", res2, res1); exit (1); } } } } mpf_clear (y); mpfr_clear (x); mpfr_clear (z); }
void UniRootF_Newton() { poly_f f,fd; mpf_t x,y,den,num; mpf_t prec; mpf_init2(den,DigitisToBits(FC_DEFAULT_PREC)); mpf_init2(num,DigitisToBits(FC_DEFAULT_PREC)); mpf_init2(y,DigitisToBits(FC_DEFAULT_PREC)); mpf_init2(x,DigitisToBits(FC_DEFAULT_PREC)); mpf_init2(prec,1); f.resize(3); mpf_set_str(prec,"1e-50",10); mpf_set_si(f[0],-2); mpf_set_si(f[1],0); mpf_set_si(f[2],1); mpf_set_str(x,"1",10); UniDFormF(fd,f); while(1) { UniEvalF(num,f,x); UniEvalF(den,fd,x); mpf_div(y,num,den); mpf_abs(num,y); if(mpf_cmp(num,prec)<0)break; mpf_sub(x,x,y); } mpf_sub(y,x,y); mpf_out_str(0,10,FC_DEFAULT_PREC,y);std::cout<<"\n"; mpf_clear(prec); mpf_clear(den); mpf_clear(num); mpf_clear(y); mpf_clear(x); f.resize(0); fd.resize(0); }
void check_various (void) { mpf_t u, got, want; char *s; mpf_init2 (u, 2*8*sizeof(long)); mpf_init2 (got, 2*8*sizeof(long)); mpf_init2 (want, 2*8*sizeof(long)); s = "0 * GMP_UI_MAX"; mpf_set_ui (u, 0L); mpf_mul_ui (got, u, GMP_UI_MAX); MPF_CHECK_FORMAT (got); mpf_set_ui (want, 0L); if (mpf_cmp (got, want) != 0) { error: printf ("Wrong result from %s\n", s); mpf_trace ("u ", u); mpf_trace ("got ", got); mpf_trace ("want", want); abort (); } s = "1 * GMP_UI_MAX"; mpf_set_ui (u, 1L); mpf_mul_ui (got, u, GMP_UI_MAX); MPF_CHECK_FORMAT (got); mpf_set_ui (want, GMP_UI_MAX); if (mpf_cmp (got, want) != 0) goto error; mpf_clear (u); mpf_clear (got); mpf_clear (want); }
void my_out_str(FILE *fp, unsigned long base, unsigned long digits, mpf_t f) { unsigned long num_units = (digits + UNIT_SIZE-1)/UNIT_SIZE; unsigned long block_size = (num_units + NUM_BLOCKS-1)/NUM_BLOCKS*UNIT_SIZE; mpf_init2(trunk, (int)(block_size*BITS_PER_DIGIT+1)); unsigned long i = mpf_get_ui(f); fprintf(fp, "%lu.\n", i); mpf_sub_ui(f, f, i); my_out_str_raw(fp, digits, f, 0); if (out_ptr != out_buf) flush_out(fp); mpf_clear(trunk); }
void check_one (mpf_srcptr src, mpf_srcptr trunc, mpf_srcptr ceil, mpf_srcptr floor) { mpf_t got; mpf_init2 (got, mpf_get_prec (trunc)); ASSERT_ALWAYS (PREC(got) == PREC(trunc)); ASSERT_ALWAYS (PREC(got) == PREC(ceil)); ASSERT_ALWAYS (PREC(got) == PREC(floor)); #define CHECK_SEP(name, fun, want) \ mpf_set_ui (got, 54321L); /* initial junk */ \ fun (got, src); \ MPF_CHECK_FORMAT (got); \ if (mpf_cmp (got, want) != 0) \ { \ printf ("%s wrong\n", name); \ check_print (src, got, want); \ abort (); \ } CHECK_SEP ("mpf_trunc", mpf_trunc, trunc); CHECK_SEP ("mpf_ceil", mpf_ceil, ceil); CHECK_SEP ("mpf_floor", mpf_floor, floor); #define CHECK_INPLACE(name, fun, want) \ mpf_set (got, src); \ fun (got, got); \ MPF_CHECK_FORMAT (got); \ if (mpf_cmp (got, want) != 0) \ { \ printf ("%s wrong\n", name); \ check_print (src, got, want); \ abort (); \ } CHECK_INPLACE ("mpf_trunc", mpf_trunc, trunc); /* Can't do these unconditionally in case truncation by mpf_set strips some low non-zero limbs which would have rounded the result. */ if (ABSIZ(src) <= PREC(trunc)+1) { CHECK_INPLACE ("mpf_ceil", mpf_ceil, ceil); CHECK_INPLACE ("mpf_floor", mpf_floor, floor); } mpf_clear (got); }
void check_reuse (void) { /* Try mpf_set(f,f) when f is bigger than prec. In the past this had resulted in an MPN_COPY with invalid operand overlap. */ mpf_t f; mp_size_t limbs = 20; unsigned long bits = limbs * GMP_NUMB_BITS; mpf_init2 (f, bits); refmpf_fill (f, limbs, GMP_NUMB_MAX); mpf_set_prec_raw (f, bits / 2); mpf_set (f, f); MPF_CHECK_FORMAT (f); mpf_set_prec_raw (f, bits); mpf_clear (f); }
void extrapolate(index_t num_samples, index_t start_index, SequenceFunc f, mpf_t ans) { // Calculate the desired samples, then pass to the other extrapolate // function. mpf_t *samples = (mpf_t*)malloc(sizeof(mpf_t) * num_samples); mpf_t *lim = samples + num_samples; mp_bitcnt_t precision = mpf_get_prec(ans); for (mpf_t *ptr = samples; ptr < lim; ptr++, start_index <<= 1) { mpf_init2(*ptr, precision); f(start_index, *ptr); } extrapolate(num_samples, samples, ans); // Clean for (mpf_t *ptr = samples; ptr < lim; ptr++) mpf_clear(*ptr); free(samples); }
void mpf_pow_ui (mpf_ptr r, mpf_srcptr b, unsigned long int e) { mpf_t b2; mpf_init2 (b2, mpf_get_prec (r)); mpf_set (b2, b); if ((e & 1) != 0) mpf_set (r, b); else mpf_set_ui (r, 1); while (e >>= 1) { mpf_mul (b2, b2, b2); if ((e & 1) != 0) mpf_mul (r, r, b2); } mpf_clear (b2); }
static void print_mpf(const char *mpfstr, unsigned base, mp_bitcnt_t precision) { const unsigned formatprec = (precision >= 4 ? precision - 4 : 0) / 4; mpf_t num; mpf_init2(num, precision); if (mpf_set_str(num, mpfstr, base) < 0) { (void) fprintf(stderr, "GMP mpf_set_str() failed for the number '%s'\n", mpfstr); exit(EX_DATAERR); } if (gmp_printf("%.*Fa\n", formatprec, num) < 0) { (void) fprintf(stderr, "GMP gmp_printf() failed for the number '%s'\n", mpfstr); exit(EX_IOERR); } mpf_clear(num); }
void mpf_pow_ui (mpf_ptr r, mpf_srcptr b, unsigned long int e) { mpf_t b2; unsigned long int e2; mpf_init2 (b2, mpf_get_prec (r)); mpf_set (b2, b); mpf_set_ui (r, 1); if ((e & 1) != 0) mpf_set (r, b2); for (e2 = e >> 1; e2 != 0; e2 >>= 1) { mpf_mul (b2, b2, b2); if ((e2 & 1) != 0) mpf_mul (r, r, b2); } mpf_clear (b2); }
static void test_cmp_f (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpf_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, pmax+GMP_NUMB_BITS); mpf_init2 (y, MPFR_PREC_MIN); for(p=pmin ; p < pmax ; p+=3) { mpfr_set_prec (x, p); mpf_set_prec (y, p); for ( n = 0; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpf_urandomb (y, RANDS, p); if (!MPFR_IS_SINGULAR (x)) { mpfr_set_f (z, y, MPFR_RNDN); mpfr_sub (z, x, z, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_f (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_f: res=%d sub gives %d\n", res2, res1); exit (1); } } } } mpf_clear (y); mpfr_clear (x); mpfr_clear (z); }