void init() { T_ = NULL; same_point_tolerance_ = 1e-5; num_projections_ = 0; projections_ = NULL; curr_projection_ = -1; curr_input_index_ = -2; this->num_vertices_ = 0; this->num_natural_variables_ = 0; init_vec_mp(checker_1_,0); init_vec_mp(checker_2_,0); init_mp(this->diff_); mpf_init(abs_); mpf_init(zerothresh_); mpf_set_d(zerothresh_, 1e-8); }
void check_various (void) { mpf_t got, u, v; mpf_init (got); mpf_init (u); mpf_init (v); /* 100/4 == 25 */ mpf_set_prec (got, 20L); mpf_set_ui (u, 100L); mpf_set_ui (v, 4L); mpf_div (got, u, v); MPF_CHECK_FORMAT (got); ASSERT_ALWAYS (mpf_cmp_ui (got, 25L) == 0); /* 1/(2^n+1), a case where truncating the divisor would be wrong */ mpf_set_prec (got, 500L); mpf_set_prec (v, 900L); mpf_set_ui (v, 1L); mpf_mul_2exp (v, v, 800L); mpf_add_ui (v, v, 1L); mpf_div (got, u, v); check_one ("1/2^n+1, separate", got, u, v); mpf_clear (got); mpf_clear (u); mpf_clear (v); }
float evaluateFitness(float position[]) { char stringFitness[28]; mpf_t mpfFitness, one; float ris; float fitness = formulae(position); if (fitness >= 0) { sprintf(stringFitness, "%.20f", fitness); mpf_init(mpfFitness); mpf_init(one); mpf_set_str (one, "1.0", 10); mpf_set_str(mpfFitness, stringFitness, 10); mpf_add(mpfFitness, mpfFitness, one); //printf("fitness: %.20e\t", fitness); //mpf_out_str (stdout, 10, 256, mpfFitness); mpf_div(mpfFitness, one, mpfFitness); printf("\nris %.20e\t", (float) mpf_get_d(mpfFitness)); ris = (float) mpf_get_d(mpfFitness); mpf_out_str (stdout, 10, 256, mpfFitness); printf("\n"); return ris; //return 1 / (1 + fitness); } return 1 + fabs(fitness); }
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); }
static void mpf_exp(mpf_t res, mpf_t pwr) { mpf_t a; mpf_t f0; int i; mpf_init(a); mpf_set(a, pwr); mpf_init(f0); mpf_set(f0, a); mpf_add_ui(res, a, 1); for (i=2;;i++) { mpf_mul(f0, f0, a); mpf_div_ui(f0, f0, i); if (mpf_sgn(f0) > 0) { if (mpf_cmp(f0, epsilon) < 0) break; } else { if (mpf_cmp(f0, negepsilon) > 0) break; } mpf_add(res, res, f0); } mpf_clear(f0); mpf_clear(a); }
int main (void) { mpf_set_default_prec (300000); mpf_t x0, y0, resA, resB, Z, var; mpf_init_set_ui (x0, 1); mpf_init_set_d (y0, 0.5); mpf_sqrt (y0, y0); mpf_init (resA); mpf_init (resB); mpf_init_set_d (Z, 0.25); mpf_init (var); int n = 1; for(int i=0; i<8; i++){ agm(x0, y0, resA, resB); mpf_sub(var, resA, x0); mpf_mul(var, var, var); mpf_mul_ui(var, var, n); mpf_sub(Z, Z, var); n += n; agm(resA, resB, x0, y0); mpf_sub(var, x0, resA); mpf_mul(var, var, var); mpf_mul_ui(var, var, n); mpf_sub(Z, Z, var); n += n; } mpf_mul(x0, x0, x0); mpf_div(x0, x0, Z); gmp_printf ("%.100000Ff\n", x0); return 0; }
void check_rand (void) { unsigned long min_prec = __GMPF_BITS_TO_PREC (1); gmp_randstate_t rands; mpf_t got, u; unsigned long prec, v; int i; /* The nails code in mpf_mul_ui currently isn't exact, so suppress these tests for now. */ if (BITS_PER_UI > GMP_NUMB_BITS) return; mpf_init (got); mpf_init (u); gmp_randinit_default(rands); for (i = 0; i < 200; i++) { /* got precision */ prec = min_prec + gmp_urandomm_ui (rands, 15L); refmpf_set_prec_limbs (got, prec); /* u precision */ prec = min_prec + gmp_urandomm_ui (rands, 15L); refmpf_set_prec_limbs (u, prec); /* u, possibly negative */ mpf_rrandomb (u, rands, PREC(u), (mp_exp_t) 20); if (gmp_urandomb_ui (rands, 1L)) mpf_neg (u, u); /* v, 0 to BITS_PER_ULONG bits (inclusive) */ prec = gmp_urandomm_ui (rands, BITS_PER_ULONG+1); v = gmp_urandomb_ui (rands, prec); if ((i % 2) == 0) { /* separate */ mpf_mul_ui (got, u, v); check_one ("separate", got, u, v); } else { /* overlap */ prec = refmpf_set_overlap (got, u); mpf_mul_ui (got, got, v); check_one ("overlap src==dst", got, u, v); mpf_set_prec_raw (got, prec); } } mpf_clear (got); mpf_clear (u); gmp_randclear(rands); }
void division(int var1, int var2, int digits) { mpf_t x; mpf_init(x); mpf_t y; mpf_init(y); mpf_set_d(x, var1); mpf_set_d(y, var2); mpf_div (x, x, y); gmp_printf ("%.*Ff\n", digits, x); }
/* Constructor and destructor for Lambda fractal. */ static lambda_t* constructor_lambda(const ordinal_number_t iteration_steps, long long int prec, const char args[]) { lambda_t* context; char* real_param; char* imaginary_param; char* args_help; /* Get memory for the fractal context. */ if (!(context=malloc(sizeof(lambda_t)))) return NULL; mpf_set_default_prec(sizeof(char)*prec); mpf_init(Re(context->lambda)); mpf_init(Im(context->lambda)); /* Set the fractal context. */ context->iteration_steps=iteration_steps; if(args!=NULL) { if (strchr(args, ',')==NULL) { real_param=malloc(sizeof(char)*(prec+1)); sscanf(args,"%s",real_param); mpf_set_str(Re(context->lambda),real_param,10); mpf_set_str(Im(context->lambda),"0",10); free(real_param); } else { args_help=malloc(sizeof(args)); strcpy(args_help,args); real_param=strtok(args_help,","); imaginary_param=strtok(NULL,"\0"); mpf_set_str(Re(context->lambda),real_param,10); mpf_set_str(Im(context->lambda),imaginary_param,10); free(args_help); } } else { mpf_set_si(Re(context->lambda),1); mpf_set_si(Im(context->lambda),0); } context->prec=prec; #ifdef DEBUG gmp_fprintf(stderr,"Lambda parameter: %F.10f,%F.10f\n",Re(context->lambda),Im(context->lambda)); #endif /* Return the handle. */ return context; }
void ComplexInitialize() { FC_DEFAULT_PREC=100; mpf_init(mpfc_mpf_temp[0]); mpf_init(mpfc_mpf_temp[1]); mpf_init(mpfc_mpf_temp[2]); mpf_init(mpfc_mpf_temp[3]); mpfc_init(mpfc_one);mpfc_set_ui(mpfc_one,1); mpfc_init(mpfc_zero);mpfc_set_ui(mpfc_zero,0); return ; }
/* New calculate function. */ ordinal_number_t cache_calculator(render_t* handle,const view_position_t render_position) { /* Volatile data. */ ordinal_number_t* help; complex_number_t complex_position; view_position_t shift; real_number_t scaling_factor; mpf_t help_mpf; mpf_t help_two; mpf_set_default_prec(sizeof(char)*handle->prec); mpf_init(help_mpf); mpf_init(Re(complex_position)); mpf_init(Im(complex_position)); mpf_init(scaling_factor); mpf_init(help_two); /* Check if the point has been calculated already. */ help=handle->points+render_position.y*handle->geometry.width+render_position.x; if(*help==0) { /* Has not been calculated till now, calculate the iteration. */ /* Precalculate scaling factor and center shift for speed reasons. */ mpf_div_ui(scaling_factor,handle->scale,handle->geometry.width); shift.x=handle->geometry.width/2; shift.y=handle->geometry.height/2; /* Calculate the iteration. */ mpf_set_si(help_two,(render_position.x-shift.x)); mpf_mul(help_mpf,scaling_factor,help_two); mpf_add(complex_position.real_part,help_mpf,handle->center.real_part); mpf_set_si(help_two,(render_position.y-shift.y)); mpf_mul(help_mpf,scaling_factor,help_two); mpf_sub(Im(complex_position),Im(handle->center),help_mpf); *help=(*handle->fractal_facility->facility.fractal.calculate_function)(handle->fractal,&complex_position); } mpf_clear(help_mpf); mpf_clear(Re(complex_position)); mpf_clear(Im(complex_position)); mpf_clear(scaling_factor); mpf_clear(help_two); /* Return the iteration. */ return(*help); //return(0); }
/* * Function: compute_bbp_first_sum_gmp * -------------------- * Computes the first summand in the BBP formula using GNU GMP. * * d: digit to be calculated * base: the base * c: a fixed positive integer * p: a simple polynomial like x or x^2 * start_at_0: start the summation at k=0, if true, at k=1, otherwise. Most * instances of the BBP formula, such as pi, have you start at 0. * But some, such as log(2), have you start at 1. * * returns: the value of the first sum */ void compute_bbp_first_sum_gmp(mpf_t sum, unsigned long int d, int base, long int c, void (*p)(mpz_t, mpz_t), bool start_at_0) { mpf_set_d(sum, 0.0); signed long int k_start = start_at_0 ? 0 : 1; mpz_t k; mpz_init_set_si(k, k_start); double upper = floor((double) d / (double) c); while (mpz_cmp_d(k, upper) <= 0) { mpz_t poly_result; mpz_init(poly_result); (*p)(poly_result, k); mpz_t num; mpz_init(num); mpz_t exponent; mpz_init_set(exponent, k); mpz_mul_si(exponent, exponent, c); mpz_mul_si(exponent, exponent, -1); mpz_add_ui(exponent, exponent, d); modular_pow_gmp(num, base, exponent, poly_result); mpf_t num_float; mpf_init(num_float); mpf_set_z(num_float, num); mpz_clear(num); mpz_clear(exponent); mpf_t denom; mpf_init_set_d(denom, mpz_get_d(poly_result)); mpz_clear(poly_result); mpf_t quotient; mpf_init(quotient); mpf_div(quotient, num_float, denom); mpf_clear(num_float); mpf_clear(denom); mpf_add(sum, sum, quotient); mpf_clear(quotient); mod_one_gmp(sum, sum); mpz_add_ui(k, k, 1); } mpz_clear(k); mod_one_gmp(sum, sum); }
/* Constructor and destructor for recurse render function. */ static render_t* constructor( const complex_number_t center, const view_dimension_t geometry, const real_number_t scale, const plugin_facility_t* fractal_facility, const plugin_facility_t* output_facility, const void* fractal, const void* output, const char args[], long long int prec) { render_t* context; /* Check if parameter was given, */ if(args==NULL) { fprintf(stderr,"You have to specify render parameters\n"); exit(EXIT_FAILURE); } #ifdef DEBUG fprintf(stderr,"Render parameter is: %s\n",args); #endif /* Get memory for the fractal context. */ if (!(context=malloc(sizeof(render_t)))) return NULL; VARCOPY(context->prec,prec); mpf_set_default_prec(sizeof(char)*prec); /* Set the fractal context. */ mpf_init(context->center.real_part); mpf_init(context->center.imaginary_part); mpf_init(context->scale); mpf_set(context->center.real_part,Re(center)); mpf_set(context->center.imaginary_part,Im(center)); VARCOPY(context->geometry,geometry); mpf_set(context->scale,scale); VARCOPY(context->fractal_facility,fractal_facility); VARCOPY(context->output_facility,output_facility); VARCOPY(context->fractal,fractal); VARCOPY(context->output,output); sscanf(args,"%d", &context->param); /* Return the handle. */ return context; }
static void mpc_cis(mpc_t res, mpf_t theta) { mpf_t a; mpf_init(a); mpf_set(a, theta); //res = exp(i a) // = cos a + i sin a //converges quickly near the origin mpf_t f0; mpf_ptr rx = mpc_re(res), ry = mpc_im(res); int i; int toggle = 1; mpf_init(f0); mpf_set(f0, a); mpf_set_ui(rx, 1); mpf_set(ry, f0); i = 1; for(;;) { toggle = !toggle; i++; mpf_div_ui(f0, f0, i); mpf_mul(f0, f0, a); if (toggle) { mpf_add(rx, rx, f0); } else { mpf_sub(rx, rx, f0); } i++; mpf_div_ui(f0, f0, i); mpf_mul(f0, f0, a); if (toggle) { mpf_add(ry, ry, f0); } else { mpf_sub(ry, ry, f0); } if (mpf_sgn(f0) > 0) { if (mpf_cmp(f0, epsilon) < 0) break; } else { if (mpf_cmp(f0, negepsilon) > 0) break; } } mpf_clear(f0); mpf_clear(a); }
// TODO: adjust sign int mpf_gamma(mp_float * a, mp_float * b) { int err; long oldeps, eps; mp_float t; err = MP_OKAY; oldeps = a->radix; eps = oldeps + MP_DIGIT_BIT; if ((err = mpf_init(&t, oldeps)) != MP_OKAY) { return err; } if ((err = mpf_copy(a, &t)) != MP_OKAY) { goto _ERR; } if ((err = mpf_normalize_to(&t, eps)) != MP_OKAY) { goto _ERR; } if ((err = mpf_lngamma(&t, &t)) != MP_OKAY) { goto _ERR; } if ((err = mpf_exp(&t, &t)) != MP_OKAY) { goto _ERR; } if ((err = mpf_normalize_to(&t, oldeps)) != MP_OKAY) { goto _ERR; } mpf_exch(&t, b); _ERR: mpf_clear(&t); return err; }
void check_consistency (void) { mpf_t x; unsigned long i, a, b; mpf_init (x); for (i = 1; i < 2000; i++) { mpf_set_prec (x, i); a = mpf_get_prec (x); mpf_set_prec (x, a); b = mpf_get_prec (x); if (a != b) { printf ("mpf_get_prec / mpf_set_prec inconsistent\n"); printf (" set %lu gives %lu, but then set %lu gives %lu\n", i, a, a, b); abort (); } } mpf_clear (x); }
inline Mtbdd readMPQAttribute(const TiXmlNode* node, const char* att) { const std::string numberString = readStringAttribute(node, att); mpq_t gmp_value; mpq_init(gmp_value); try { size_t pos = 0; if ((pos = numberString.find('.')) != std::string::npos) { mpf_t f_value; mpf_init(f_value); mpf_set_str(f_value, numberString.c_str(), 10); mpq_set_f(gmp_value, f_value); mpf_clear(f_value); } else { mpq_set_str(gmp_value, numberString.c_str(), 10); } if (mpq_sgn(gmp_value) == 0) { mpq_clear(gmp_value); return mtbdd_false; } MTBDD res = mtbdd_gmp(gmp_value); mpq_clear(gmp_value); return res; } catch(boost::bad_lexical_cast&) { throw ParseError("[ERROR] String " + numberString + " is not a number"); } }
int main (void) { /* The localeconv replacement breaks printf "%lu" on SunOS 4, so we can't print the seed in tests_rand_start(). Nothing random is used in this program though, so just use the memory tests alone. */ tests_memory_start (); { mpf_t f; char buf[128]; mpf_init (f); decimal_point = ","; mpf_set_d (f, 1.5); gmp_snprintf (buf, sizeof(buf), "%.1Ff", f); mpf_clear (f); if (strcmp (buf, "1,5") != 0) { printf ("Test skipped, replacing localeconv/nl_langinfo doesn't work\n"); goto done; } } check_input (); done: tests_memory_end (); exit (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; }
void test_denorms (int prc) { #ifdef _GMP_IEEE_FLOATS double d1, d2; mpf_t f; int i; mpf_set_default_prec (prc); mpf_init (f); d1 = 1.9; for (i = 0; i < 820; i++) { mpf_set_d (f, d1); d2 = mpf_get_d (f); if (d1 != d2) abort (); d1 *= 0.4; } mpf_clear (f); #endif }
void AssignValueToIdentifier(char *Id, number_t value) { symbol_value_t *s; s = (symbol_value_t *)symtab->get(symtab, (void *)Id); if (s) { mpf_init(s->value); mpf_set(s->value, value); } else { s = (symbol_value_t *)malloc(sizeof(symbol_value_t)); mpf_init(s->value); mpf_set(s->value, value); symtab->put(symtab, Id, s, NULL); } mpf_set(*fExpressionResult, value); }
vanilla::float_object::gmp_mpf_wrapper::gmp_mpf_wrapper(char* str, int base) : _mpf(), _valid(true) { mpf_init(_mpf); int result = mpf_set_str(_mpf, str, base); assert(result == 0); }
int main (int argc, char * argv[]) { mpf_t a_k; int prec, nbits; prec = 120; /* Set the precision (number of binary bits) */ /* We need more bits than what what is available, for intermediate calcs */ nbits = 3.3*prec; mpf_set_default_prec (nbits+200); mpf_init(a_k); printf("#\n# The topsin series a_k\n#\n"); int k; double akprev=0.0; for (k=0; k<95001; k++) { topsin_series(a_k, k, prec); double ak = mpf_get_d(a_k); printf("%d %20.16g %20.16g\n", k, ak, ak+akprev); akprev = ak; } return 0; }
int mpf_const_le10(mp_float * a) { int err; long eps; err = MP_OKAY; if (mpf_le10_precision > 0 && a == NULL) { mpf_clear(&mpf_le10); mpf_le10_precision = 0; return err; } if (mpf_le10_precision >= a->radix) { eps = a->radix; if ((err = mpf_copy(&mpf_le10, a)) != MP_OKAY) { return err; } return mpf_normalize_to(a, eps); } else { if (mpf_le10_precision == 0) { if ((err = mpf_init(&mpf_le10, a->radix)) != MP_OKAY) { return err; } } if ((err = machin_ln_10(&mpf_le10)) != MP_OKAY) { return err; } if ((err = mpf_copy(&mpf_le10, a)) != MP_OKAY) { return err; } } return MP_OKAY; }
void readfac (mpf_t *factoriales) { FILE *fichero; char *filename; int i; mpz_t intfac; filename = (char *) malloc(50*sizeof(char)); mpz_init(intfac); mpf_init_set_ui(factoriales[0], 1); for (i=1; i<=NFAC; i++) { sprintf(filename, "factoriales/%4.4d.dat", i); fichero = fopen(filename, "r"); mpz_inp_str(intfac, fichero, 10); /* printf("Ahi va: %d\n", i); * mpz_out_str(stdout, 10, intfac); * printf("\n\n"); */ mpf_init(factoriales[i]); mpf_set_z(factoriales[i], intfac); fclose(fichero); } }
void check_various (void) { mpf_t got; mpq_t q; mpf_init (got); mpq_init (q); /* 1/1 == 1 */ mpf_set_prec (got, 20L); mpq_set_ui (q, 1L, 1L); mpf_set_q (got, q); MPF_CHECK_FORMAT (got); ASSERT_ALWAYS (mpf_cmp_ui (got, 1L) == 0); /* 1/(2^n+1), a case where truncating the divisor would be wrong */ mpf_set_prec (got, 500L); mpq_set_ui (q, 1L, 1L); mpz_mul_2exp (mpq_denref(q), mpq_denref(q), 800L); mpz_add_ui (mpq_denref(q), mpq_denref(q), 1L); check_one (got, q); mpf_clear (got); mpq_clear (q); }
int main(int argc, char **argv) { unsigned bits; double start, end; mpf_t e; if(argc < 2) { usage(argv); } bits = atoi(argv[1]); if(bits == 0) { usage(argv); } printf("Calculating e..."); fflush(stdout); start = currentTime(); mpf_set_default_prec(bits); mpf_init(e); calculateE(bits, e); end = currentTime(); printf("\ne = "); mpf_out_str(stdout, 10, 0, e); printf("\n"); printf("total elapsed time : %.2f seconds\n", end - start); return 0; }
void log2(const mpf_t n, mpf_t l) { // l = log10(n)/log10(2) mpf_t two; mpf_init(two); mpf_set_ui(two, 2); log(n, l, two); mpf_clear(two); }
char* mpSum(float* input, size_t n){ int i; char* resStr = (char*)malloc(1024); char* tmp = (char*)malloc(1024); mpf_t sum; mpf_init(sum); mpf_t in; mpf_init(in); for(i=0; i < n; i++){ mpf_set_d(in, (float)input[i]); mpf_add(sum, sum, in); } mp_exp_t exp; mpf_get_str(tmp, &exp, 10, 1022, sum); strncpy(resStr, tmp, exp); resStr[exp]='.'; strcpy(resStr+exp+1, tmp+exp); free(tmp); return resStr; }
void w3j(mpf_t w, long j1, long j2, long j3, long m1, long m2, long m3) { mpq_t delta_sq,r; mpz_t i; mpf_t h; mpq_init(delta_sq); mpq_init(r); mpz_init(i); mpf_init(h); mpq_set_si(r,0,1); if(m1+m2+m3!=0) return; if((iabs(m1)>j1) || (iabs(m2)>j2) || (iabs(m3)>j3)) return; if((j3<iabs(j1-j2)) || ((j1+j2)<j3)) return; w3j_Delta_sq(delta_sq, j1, j2, j3); w3j_intterm(i, j1, j2, j3, m1, m2, m3); if(iabs(j1-j2-m3)%2 == 1) mpz_neg(i,i); w3j_sqrt_sq(r, j1, j2, j3, m1, m2, m3); mpq_mul(r,r,delta_sq); mpf_set_q(w,r); mpf_sqrt(w,w); mpf_set_z(h,i); mpf_mul(w,w,h); mpf_clear(h); mpz_clear(i); mpq_clear(r); mpq_clear(delta_sq); }