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); }
/* 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 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); }
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); }
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"); } }
/** * rasqal_xsd_decimal_set_string: * @dec: XSD Decimal * @string: lexical form * * Set an XSD Decimal value from a string lexical form * * Return value: non-0 on failure **/ int rasqal_xsd_decimal_set_string(rasqal_xsd_decimal* dec, const char* string) { int rc=0; size_t len; if(!string) return 1; rasqal_xsd_decimal_clear_string(dec); len = strlen(string); dec->string = (char*)RASQAL_MALLOC(cstring, len+1); if(!dec->string) return 1; memcpy(dec->string, string, len + 1); dec->string_len = len; #if defined(RASQAL_DECIMAL_C99) || defined(RASQAL_DECIMAL_NONE) dec->raw = strtod(string, NULL); #endif #ifdef RASQAL_DECIMAL_MPFR if(*string == '+') string++; rc = mpfr_set_str(dec->raw, string, 10, dec->rounding); #endif #ifdef RASQAL_DECIMAL_GMP if(*string == '+') string++; rc = mpf_set_str(dec->raw, string, 10); #endif return rc; }
int main(void) { int i ; char buf[4096] ; mpf_t x, y , result ; mpf_set_default_prec(LIMIT); mpf_init ( x ); mpf_init ( y ); mpf_init (result) ; mpf_set_str(result, "1" , 10 ); for ( i = 1 ; i < LIMIT ; i++ ) { sprintf ( buf , "%d" , (2*i + 1) ) ; mpf_set_str(x, buf , 10 ); mpf_ui_div (y, 1, x) ; if ( i % 2 ) { mpf_sub ( x , result , y ) ; mpf_set (result , x) ; } else { mpf_add ( x , result , y ) ; mpf_set (result , x) ; } } mpf_mul_ui (x, result, 4 ) ; printf("\n pi = " ) ; mpf_out_str (stdout , 10, 0, x) ; printf ("\n") ; mpf_clear (x); mpf_clear (y); mpf_clear (result); return EXIT_SUCCESS; }
void FractalTexture::setPosition(const std::string& x, const std::string& y, int base) { #ifdef ROE_FRACTAL_USE_GMP #ifdef ROE_FRACTAL_GMP_USE_C int xreturn = mpf_set_str(m_xCenter, x.c_str(), base); int yreturn = mpf_set_str(m_yCenter, y.c_str(), base); #else int xreturn = m_xCenter.set_str(x,base); int yreturn = m_yCenter.set_str(y,base); #endif if(xreturn==-1) roe_except("bad x-string("+x+") or bad base("+util::toString(base)+")", "setPosition"); if(yreturn==-1) roe_except("bad y-string("+y+") or bad base("+util::toString(base)+")", "setPosition"); #else if (base != 10) roe_except("bad base("+util::toString(base)+"), only base 10 allowed", "setPosition"); m_xCenter = (type)(util::parseDouble(x)); m_yCenter = (type)(util::parseDouble(y)); #endif updateCorners(); }
size_t mpf_inp_str (mpf_ptr rop, FILE *stream, int base) { char *str; size_t alloc_size, str_size; int c; int res; size_t nread; if (stream == 0) stream = stdin; alloc_size = 100; str = (char *) (*__gmp_allocate_func) (alloc_size); str_size = 0; nread = 0; /* Skip whitespace. */ do { c = getc (stream); nread++; } while (isspace (c)); for (;;) { if (str_size >= alloc_size) { size_t old_alloc_size = alloc_size; alloc_size = alloc_size * 3 / 2; str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size); } if (c == EOF || isspace (c)) break; str[str_size++] = c; c = getc (stream); } ungetc (c, stream); nread--; if (str_size >= alloc_size) { size_t old_alloc_size = alloc_size; alloc_size = alloc_size * 3 / 2; str = (char *) (*__gmp_reallocate_func) (str, old_alloc_size, alloc_size); } str[str_size] = 0; res = mpf_set_str (rop, str, base); (*__gmp_free_func) (str, alloc_size); if (res == -1) return 0; /* error */ return str_size + nread; }
int sg_big_float_set_str(sg_big_float_t *dst, const char *num_str, enum sg_num_sys sys) { int base = SG_COMPUTE_BASE(sys); if (!dst || !num_str || base <= 0) return -1; mpf_set_str(dst->mpf, num_str, base); return 0; }
void FractalTexture::setZoom(const std::string& w, int base) { #ifdef ROE_FRACTAL_USE_GMP #ifdef ROE_FRACTAL_GMP_USE_C int wreturn = mpf_set_str(m_width , w.c_str(), base); int hreturn = mpf_set_str(m_height, w.c_str(), base); #else int wreturn = m_width.set_str (w,base); int hreturn = m_height.set_str(w,base); #endif if(wreturn==-1) roe_except("bad w-string("+w+") or bad base("+util::toString(base)+")", "setZoom"); if(hreturn==-1) roe_except("bad w-string("+w+") or bad base("+util::toString(base)+")", "setZoom"); #else if (base != 10) roe_except("bad base("+util::toString(base)+"), only base 10 allowed", "setZoom"); m_width = (type)(util::parseDouble(w)); m_height = (type)(util::parseDouble(w)); #endif updateCorners(); }
void sphere_eval_data_mp::init() { this->is_solution_checker_d = &check_issoln_sphere_d; this->is_solution_checker_mp = &check_issoln_sphere_mp; this->evaluator_function_d = &sphere_eval_d; this->evaluator_function_mp = &sphere_eval_mp; this->precision_changer = &change_sphere_eval_prec; this->dehomogenizer = &sphere_dehom; this->num_static_linears = 0; // we will copy these out of the witness set. static_linear = static_linear_full_prec = NULL; starting_linear = (vec_mp *) br_malloc(2*sizeof(vec_mp));; for (int ii=0; ii<2; ii++) { init_vec_mp(starting_linear[ii],0); } init_vec_mp(center, 0); init_mp(radius); if (MPType==2) { init_vec_mp2(center_full_prec,0,1024); init_mp2(radius_full_prec,1024); starting_linear_full_prec = (vec_mp *) br_malloc(2*sizeof(vec_mp)); for (int ii=0; ii<2; ii++) { init_vec_mp2(starting_linear_full_prec[ii],0,1024); } init_mp2(two_full_prec,1024); set_zero_mp(two_full_prec); mpf_set_str(two_full_prec->r, "2.0", 10); } init_mp(two); set_zero_mp(two); mpf_set_str(two->r, "2.0", 10); num_natural_vars = 0; }
int mpf_init_set_str (mpf_ptr r, const char *s, int base) { mp_size_t prec = __gmp_default_fp_limb_precision; r->_mp_size = 0; r->_mp_exp = 0; r->_mp_prec = prec; r->_mp_d = (mp_ptr) (*__gmp_allocate_func) ((prec + 1) * BYTES_PER_MP_LIMB); return mpf_set_str (r, s, base); }
int mpf_init_set_str (mpf_ptr r, const char *s, int base) { mp_size_t prec = __gmp_default_fp_limb_precision; r->_mp_size = 0; r->_mp_exp = 0; r->_mp_prec = prec; r->_mp_d = __GMP_ALLOCATE_FUNC_LIMBS (prec + 1); return mpf_set_str (r, s, base); }
void mpf_set_str_or_abort (mpf_ptr f, const char *str, int base) { if (mpf_set_str (f, str, base) != 0) { fprintf (stderr, "ERROR mpf_set_str failed\n"); fprintf (stderr, " str = \"%s\"\n", str); fprintf (stderr, " base = %d\n", base); abort(); } }
void getParams(char **argv, int *flag, mpf_t *cr, mpf_t *ci, mpf_t *x, mpf_t *y, mpf_t *xr, mpf_t *yr, unsigned long int *height, unsigned long int *width, int *maxiter, char **image) { char data[SIZE]; char filename[SIZE]; FILE *params; params = fopen(argv[1], "r"); if (params != NULL) { if (fgets(data, SIZE, params) != NULL) *flag = strtol(data, NULL, 0); if (fgets(data, SIZE, params) != NULL) mpf_set_str(*cr, data, 10); if (fgets(data, SIZE, params) != NULL) mpf_set_str(*ci, data, 10); if (fgets(data, SIZE, params) != NULL) mpf_set_str(*x, data, 10); if (fgets(data, SIZE, params) != NULL) mpf_set_str(*y, data, 10); if (fgets(data, SIZE, params) != NULL) mpf_set_str(*xr, data, 10); if (fgets(data, SIZE, params) != NULL) mpf_set_str(*yr, data, 10); if (fgets(data, SIZE, params) != NULL) *height = strtol(data, NULL, 0); if (fgets(data, SIZE, params) != NULL) *width = strtol(data, NULL, 0); if (fgets(data, SIZE, params) != NULL) *maxiter = strtol(data, NULL, 0); if (fgets(data, SIZE, params) != NULL) { sscanf(data,"%s", filename); *image = malloc(strlen(filename)); strcpy(*image, filename); } fclose(params); } else perror("Error opening file\n"); return; }
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); }
int main (int argc, char *argv[]) { mpf_t sq_me, sq_out, test; mpf_set_default_prec (10000); mpf_init(sq_me); mpf_init(sq_out); mpf_init(test); mpf_set_str (sq_me, argv[1], 10); mpf_sqrt(sq_out, sq_me); mpf_mul(test,sq_out,sq_out); gmp_printf ("Input: %Ff\n\n", sq_me); gmp_printf ("Square root: %.200Ff\n\n", sq_out); gmp_printf ("Re-squared: %Ff\n\n", test); return 0; }
void calculate_term(unsigned int n,double x, mpf_t term, mpf_t a, mpf_t b) { /* a = ((-1)^n) */ mpf_set_str(a,"-1.0",10); mpf_pow_ui(a,a,n); /* b = (x^(2*n)) */ mpf_set_d(b,x); mpf_pow_ui(b,b,2*n); /* a = a*b (liberamos o uso de b para contas futuras) */ mpf_mul(a,a,b); /* fatorial(2*n) */ mpf_fac_ui(b,2*n); /* a = a/b */ mpf_div(term,a,b); }
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 set_gmp_precision(int precision) { mpf_t value; mpf_t log_2; unsigned int bits_precision_size; /* Inicializações */ mpf_init(value); mpf_init(log_2); /* Calculo do valor da precisao e faço a transformação para numero de bits */ mpf_set_ui(value, (unsigned int) precision); /* log de 10 na base 2 */ mpf_set_str(log_2,"3.3219280949",10); mpf_mul(value,value,log_2); bits_precision_size = mpf_get_ui(value) + 1; mpf_set_default_prec(bits_precision_size); /* Debug gmp_printf("###-Precision: %F.f\n", precision_gmp); */ /* Limpeza (frees) */ mpf_clear(log_2); mpf_clear(value); }
int main (void) { mpf_t f; tests_start (); mpf_init2 (f, 200L); mpf_set_ui (f, 0L); one (f, 1); mpf_set_ui (f, 1L); all (f, 1); mpf_set_ui (f, 1L); mpf_div_2exp (f, f, 1L); all (f, 0); mpf_set_ui (f, 1L); mpf_div_2exp (f, f, 5000L); all (f, 0); mpf_set_ui (f, 1L); mpf_mul_2exp (f, f, 5000L); all (f, 1); mpf_set_str (f, "0.5", 10); all (f, 0); mpf_set_ui (f, 1L); mpf_div_ui (f, f, 3L); all (f, 0); mpf_clear (f); tests_end (); exit (0); }
void log10(const mpf_t n, mpf_t l, int prec) { // Approximate log10 to prec digits. // Ref.: http://www.brics.dk/RS/04/17/BRICS-RS-04-17.pdf mpf_t tmp, tmp2; mpf_inits(tmp, tmp2, NULL); mpf_set(tmp, n); stringstream res; for (int i = 0; i < prec + 1; i++) { int d = digits(tmp); // m_(i+1) = (m_i / (10 ^ a_i)) ^ 10 mpf_set_ui(tmp2, 10); mpf_pow_ui(tmp2, tmp2, d); mpf_div(tmp2, tmp, tmp2); mpf_pow_ui(tmp, tmp2, 10); res << d; if (i == 0) res << "."; } mpf_set_str(l, res.str().c_str(), 10); mpf_clears(tmp, tmp2, NULL); }
int main (int argc, char **argv) { mpf_t x, y; int reps = 20000; int i; mp_size_t bprec = 100; mpf_t d, rerr, max_rerr, limit_rerr; char *str; mp_exp_t bexp; long size, exp; int base; char buf[SIZE * GMP_LIMB_BITS + 5]; tests_start (); if (argc > 1) { reps = strtol (argv[1], 0, 0); if (argc > 2) bprec = strtol (argv[2], 0, 0); } mpf_set_default_prec (bprec); mpf_init_set_ui (limit_rerr, 1); mpf_div_2exp (limit_rerr, limit_rerr, bprec); #if VERBOSE mpf_dump (limit_rerr); #endif mpf_init (rerr); mpf_init_set_ui (max_rerr, 0); mpf_init (x); mpf_init (y); mpf_init (d); /* First test some specific values. */ mpf_set_str (y, "1.23456e1000", 0); mpf_set_str (x, "1.23456e1000", 10); if (mpf_cmp (x, y) != 0) abort (); mpf_set_str (x, "1.23456e+1000", 0); if (mpf_cmp (x, y) != 0) abort (); mpf_set_str (x, "1.23456e+1000", 10); if (mpf_cmp (x, y) != 0) abort (); /* Now test random values. */ for (i = 0; i < reps; i++) { if (i == 0) { /* exercise the special case in get_str for for x==0 */ mpf_set_ui (x, 0L); base = 10; } else { size = urandom () % (2 * SIZE) - SIZE; exp = urandom () % EXPO; mpf_random2 (x, size, exp); base = urandom () % 61 + 2; } str = mpf_get_str (0, &bexp, base, 0, x); if (str[0] == '-') sprintf (buf, "-0.%s@%ld", str + 1, bexp); else sprintf (buf, "0.%s@%ld", str, bexp); mpf_set_str_or_abort (y, buf, -base); (*__gmp_free_func) (str, strlen (str) + 1); mpf_reldiff (rerr, x, y); if (mpf_cmp (rerr, max_rerr) > 0) { mpf_set (max_rerr, rerr); #if VERBOSE mpf_dump (max_rerr); #endif if (mpf_cmp (rerr, limit_rerr) > 0) { printf ("ERROR after %d tests\n", i); printf ("base = %d\n", base); printf (" x = "); mpf_dump (x); printf (" y = "); mpf_dump (y); abort (); } } } mpf_clear (limit_rerr); mpf_clear (rerr); mpf_clear (max_rerr); mpf_clear (x); mpf_clear (y); mpf_clear (d); tests_end (); exit (0); }
extern void _jl_mpf_set_string(mpf_t* rop, char* s) { mpf_set_str(*rop, s, 0); }
void integrator_generate_constants(void) { printf("Generaring constants.\n\n"); mpf_set_default_prec(512); mpf_t* _h = malloc(sizeof(mpf_t)*8); for (int i=0; i<8; i++) { mpf_init(_h[i]); } mpf_t* _r = malloc(sizeof(mpf_t)*28); for (int i=0; i<28; i++) { mpf_init(_r[i]); } mpf_t* _c = malloc(sizeof(mpf_t)*21); mpf_t* _d = malloc(sizeof(mpf_t)*21); for (int i=0; i<21; i++) { mpf_init(_c[i]); mpf_init(_d[i]); } mpf_set_str(_h[0],"0.0",10); mpf_set_str(_h[1],"0.0562625605369221464656521910",10); mpf_set_str(_h[2],"0.1802406917368923649875799428",10); mpf_set_str(_h[3],"0.3526247171131696373739077702",10); mpf_set_str(_h[4],"0.5471536263305553830014485577",10); mpf_set_str(_h[5],"0.7342101772154105410531523211",10); mpf_set_str(_h[6],"0.8853209468390957680903597629",10); mpf_set_str(_h[7],"0.9775206135612875018911745004",10); int l=0; for (int j=1; j<8; ++j) { for(int k=0; k<j; ++k) { // r[l] = h[j] - h[k]; mpf_sub(_r[l],_h[j],_h[k]); ++l; } } //c[0] = -h[1]; mpf_neg(_c[0],_h[1]); //d[0] = h[1]; mpf_set(_d[0],_h[1]); l=0; for (int j=2; j<7; ++j) { ++l; // c[l] = -h[j] * c[l-j+1]; mpf_mul(_c[l], _h[j], _c[l-j+1]); mpf_neg(_c[l], _c[l]); //d[l] = h[1] * d[l-j+1]; mpf_mul(_d[l], _h[1], _d[l-j+1]); for(int k=2; k<j; ++k) { ++l; //c[l] = c[l-j] - h[j] * c[l-j+1]; mpf_mul(_c[l], _h[j], _c[l-j+1]); mpf_sub(_c[l], _c[l-j], _c[l]); //d[l] = d[l-j] + h[k] * d[l-j+1]; mpf_mul(_d[l], _h[k], _d[l-j+1]); mpf_add(_d[l], _d[l-j], _d[l]); } ++l; //c[l] = c[l-j] - h[j]; mpf_sub(_c[l], _c[l-j], _h[j]); //d[l] = d[l-j] + h[j]; mpf_add(_d[l], _d[l-j], _h[j]); } // Output printf("double r[28] = {"); for (int i=0; i<28; i++) { gmp_printf ("%.*Ff", 25, _r[i]); if (i!=27) printf(", "); } printf("};\n"); printf("double c[21] = {"); for (int i=0; i<21; i++) { gmp_printf ("%.*Ff", 25, _c[i]); if (i!=20) printf(", "); } printf("};\n"); printf("double d[21] = {"); for (int i=0; i<21; i++) { gmp_printf ("%.*Ff", 25, _d[i]); if (i!=20) printf(", "); } printf("};\n"); exit(0); }
//------------------------------------------------------------------------------ // Name: //------------------------------------------------------------------------------ knumber_float::knumber_float(const QString &s) { mpf_init(mpf_); mpf_set_str(mpf_, s.toAscii(), 10); }
static size_t e_mpf_number (mpf_ptr res, const char *e, size_t elen, int base) { char *edup; size_t i, ret, extra=0; int mant_base, exp_base; void *(*allocate_func) (size_t); void (*free_func) (void *, size_t); TRACE (printf ("mpf_number base=%d \"%.*s\"\n", base, (int) elen, e)); /* mpf_set_str doesn't currently accept 0x for hex in base==0, so do it here instead. FIXME: Would prefer to let mpf_set_str handle this. */ if (base == 0 && elen >= 2 && e[0] == '0' && (e[1] == 'x' || e[1] == 'X')) { base = 16; extra = 2; e += extra; elen -= extra; } if (base == 0) mant_base = 10; else if (base < 0) mant_base = -base; else mant_base = base; /* exponent in decimal if base is negative */ if (base < 0) exp_base = 10; else if (base == 0) exp_base = 10; else exp_base = base; #define IS_EXPONENT(c) \ (c == '@' || (base <= 10 && base >= -10 && (e[i] == 'e' || e[i] == 'E'))) i = 0; for (;;) { if (i >= elen) goto parsed; if (e[i] == '.') break; if (IS_EXPONENT (e[i])) goto exponent; if (! isasciidigit_in_base (e[i], mant_base)) goto parsed; i++; } /* fraction */ i++; for (;;) { if (i >= elen) goto parsed; if (IS_EXPONENT (e[i])) goto exponent; if (! isasciidigit_in_base (e[i], mant_base)) goto parsed; i++; } exponent: i++; if (i >= elen) goto parsed; if (e[i] == '-') i++; for (;;) { if (i >= elen) goto parsed; if (! isasciidigit_in_base (e[i], exp_base)) break; i++; } parsed: TRACE (printf (" parsed i=%u \"%.*s\"\n", i, (int) i, e)); mp_get_memory_functions (&allocate_func, NULL, &free_func); edup = (*allocate_func) (i+1); memcpy (edup, e, i); edup[i] = '\0'; if (mpf_set_str (res, edup, base) == 0) ret = i + extra; else ret = 0; (*free_func) (edup, i+1); return ret; }
int main (void) { mpfr_t x, u; mpf_t y, z; mpfr_exp_t emax; unsigned long k, pr; int r, inexact; tests_start_mpfr (); mpf_init (y); mpf_init (z); mpf_set_d (y, 0.0); /* check prototype of mpfr_init_set_f */ mpfr_init_set_f (x, y, MPFR_RNDN); mpfr_set_prec (x, 100); mpfr_set_f (x, y, MPFR_RNDN); mpf_urandomb (y, RANDS, 10 * GMP_NUMB_BITS); mpfr_set_f (x, y, RND_RAND ()); /* bug found by Jean-Pierre Merlet */ mpfr_set_prec (x, 256); mpf_set_prec (y, 256); mpfr_init2 (u, 256); mpfr_set_str (u, "7.f10872b020c49ba5e353f7ced916872b020c49ba5e353f7ced916872b020c498@2", 16, MPFR_RNDN); mpf_set_str (y, "2033033E-3", 10); /* avoid 2033.033 which is locale-sensitive */ mpfr_set_f (x, y, MPFR_RNDN); if (mpfr_cmp (x, u)) { printf ("mpfr_set_f failed for y=2033033E-3\n"); exit (1); } mpf_set_str (y, "-2033033E-3", 10); /* avoid -2033.033 which is locale-sensitive */ mpfr_set_f (x, y, MPFR_RNDN); mpfr_neg (u, u, MPFR_RNDN); if (mpfr_cmp (x, u)) { printf ("mpfr_set_f failed for y=-2033033E-3\n"); exit (1); } mpf_set_prec (y, 300); mpf_set_str (y, "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", -2); mpf_mul_2exp (y, y, 600); mpfr_set_prec (x, 300); mpfr_set_f (x, y, MPFR_RNDN); if (mpfr_check (x) == 0) { printf ("Error in mpfr_set_f: corrupted result\n"); mpfr_dump (x); exit (1); } MPFR_ASSERTN(mpfr_cmp_ui_2exp (x, 1, 901) == 0); /* random values */ for (k = 1; k <= 1000; k++) { pr = 2 + (randlimb () & 255); mpf_set_prec (z, pr); mpf_urandomb (z, RANDS, z->_mp_prec); mpfr_set_prec (u, ((pr / GMP_NUMB_BITS + 1) * GMP_NUMB_BITS)); mpfr_set_f (u, z, MPFR_RNDN); if (mpfr_cmp_f (u , z) != 0) { printf ("Error in mpfr_set_f:\n"); printf ("mpf (precision=%lu)=", pr); mpf_out_str (stdout, 16, 0, z); printf ("\nmpfr(precision=%lu)=", ((pr / GMP_NUMB_BITS + 1) * GMP_NUMB_BITS)); mpfr_out_str (stdout, 16, 0, u, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_set_prec (x, pr); mpfr_set_f (x, z, MPFR_RNDN); mpfr_sub (u, u, x, MPFR_RNDN); mpfr_abs (u, u, MPFR_RNDN); if (mpfr_cmp_ui_2exp (u, 1, -pr - 1) > 0) { printf ("Error in mpfr_set_f: precision=%lu\n", pr); printf ("mpf ="); mpf_out_str (stdout, 16, 0, z); printf ("\nmpfr="); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } } /* Check for +0 */ mpfr_set_prec (x, 53); mpf_set_prec (y, 53); mpf_set_ui (y, 0); for (r = 0 ; r < MPFR_RND_MAX ; r++) { int i; for (i = -1; i <= 1; i++) { if (i) mpfr_set_si (x, i, MPFR_RNDN); inexact = mpfr_set_f (x, y, (mpfr_rnd_t) r); if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact) { printf ("mpfr_set_f(x,0) failed for %s, i = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), i); exit (1); } } } /* coverage test */ mpf_set_prec (y, 2); mpfr_set_prec (x, 3 * mp_bits_per_limb); mpf_set_ui (y, 1); for (r = 0; r < mp_bits_per_limb; r++) { mpfr_urandomb (x, RANDS); /* to fill low limbs with random data */ inexact = mpfr_set_f (x, y, MPFR_RNDN); MPFR_ASSERTN(inexact == 0 && mpfr_cmp_ui_2exp (x, 1, r) == 0); mpf_mul_2exp (y, y, 1); } mpf_set_ui (y, 1); mpf_mul_2exp (y, y, ULONG_MAX); mpfr_set_f (x, y, MPFR_RNDN); mpfr_set_ui (u, 1, MPFR_RNDN); mpfr_mul_2ui (u, u, ULONG_MAX, MPFR_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^ULONG_MAX\n"); exit (1); } emax = mpfr_get_emax (); /* For mpf_mul_2exp, emax must fit in an unsigned long! */ if (emax >= 0 && emax <= ULONG_MAX) { mpf_set_ui (y, 1); mpf_mul_2exp (y, y, emax); mpfr_set_f (x, y, MPFR_RNDN); mpfr_set_ui_2exp (u, 1, emax, MPFR_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^emax\n"); exit (1); } } /* For mpf_mul_2exp, emax - 1 must fit in an unsigned long! */ if (emax >= 1 && emax - 1 <= ULONG_MAX) { mpf_set_ui (y, 1); mpf_mul_2exp (y, y, emax - 1); mpfr_set_f (x, y, MPFR_RNDN); mpfr_set_ui_2exp (u, 1, emax - 1, MPFR_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^(emax-1)\n"); exit (1); } } mpfr_clear (x); mpfr_clear (u); mpf_clear (y); mpf_clear (z); tests_end_mpfr (); return 0; }
int main (int argc, char *argv[]) { const char usage[] = "usage: findlc [-dv] m2exp [low_merit [high_merit]]\n"; int f; int v_lose, m_lose, v_best, m_best; int c; int debug = 1; int cnt_high_merit; mpz_t m; unsigned long int m2exp; #define DIMS 6 /* dimensions run in spectral test */ mpf_t v[DIMS-1]; /* spectral test result (there's no v for 1st dimension */ mpf_t f_merit, low_merit, high_merit; mpz_t acc, minus8; mpz_t min, max; mpz_t s; mpz_init (m); mpz_init (a); for (f = 0; f < DIMS-1; f++) mpf_init (v[f]); mpf_init (f_merit); mpf_init_set_d (low_merit, .1); mpf_init_set_d (high_merit, .1); while ((c = getopt (argc, argv, "a:di:hv")) != -1) switch (c) { case 'd': /* debug */ g_debug++; break; case 'v': /* print version */ puts (rcsid[1]); exit (0); case 'h': case '?': default: fputs (usage, stderr); exit (1); } argc -= optind; argv += optind; if (argc < 1) { fputs (usage, stderr); exit (1); } /* Install signal handler. */ if (SIG_ERR == signal (SIGSEGV, sh_status)) { perror ("signal (SIGSEGV)"); exit (1); } if (SIG_ERR == signal (SIGHUP, sh_status)) { perror ("signal (SIGHUP)"); exit (1); } printf ("findlc: version: %s\n", rcsid[1]); m2exp = atol (argv[0]); mpz_init_set_ui (m, 1); mpz_mul_2exp (m, m, m2exp); printf ("m = 0x"); mpz_out_str (stdout, 16, m); puts (""); if (argc > 1) /* have low_merit */ mpf_set_str (low_merit, argv[1], 0); if (argc > 2) /* have high_merit */ mpf_set_str (high_merit, argv[2], 0); if (debug) { fprintf (stderr, "low_merit = "); mpf_out_str (stderr, 10, 2, low_merit); fprintf (stderr, "; high_merit = "); mpf_out_str (stderr, 10, 2, high_merit); fputs ("\n", stderr); } mpz_init (minus8); mpz_set_si (minus8, -8L); mpz_init_set_ui (acc, 0); mpz_init (s); mpz_init_set_d (min, 0.01 * pow (2.0, (double) m2exp)); mpz_init_set_d (max, 0.99 * pow (2.0, (double) m2exp)); mpz_true_random (s, m2exp); /* Start. */ mpz_setbit (s, 0); /* Make it odd. */ v_best = m_best = 2*(DIMS-1); for (;;) { mpz_add (acc, acc, s); mpz_mod_2exp (acc, acc, m2exp); #if later mpz_and_si (a, acc, -8L); #else mpz_and (a, acc, minus8); #endif mpz_add_ui (a, a, 5); if (mpz_cmp (a, min) <= 0 || mpz_cmp (a, max) >= 0) continue; spectral_test (v, DIMS, a, m); for (f = 0, v_lose = m_lose = 0, cnt_high_merit = DIMS-1; f < DIMS-1; f++) { merit (f_merit, f + 2, v[f], m); if (mpf_cmp_ui (v[f], 1 << (30 / (f + 2) + (f == 2))) < 0) v_lose++; if (mpf_cmp (f_merit, low_merit) < 0) m_lose++; if (mpf_cmp (f_merit, high_merit) >= 0) cnt_high_merit--; } if (0 == v_lose && 0 == m_lose) { mpz_out_str (stdout, 10, a); puts (""); fflush (stdout); if (0 == cnt_high_merit) break; /* leave loop */ } if (v_lose < v_best) { v_best = v_lose; printf ("best (v_lose=%d; m_lose=%d): ", v_lose, m_lose); mpz_out_str (stdout, 10, a); puts (""); fflush (stdout); } if (m_lose < m_best) { m_best = m_lose; printf ("best (v_lose=%d; m_lose=%d): ", v_lose, m_lose); mpz_out_str (stdout, 10, a); puts (""); fflush (stdout); } } mpz_clear (m); mpz_clear (a); for (f = 0; f < DIMS-1; f++) mpf_clear (v[f]); mpf_clear (f_merit); mpf_clear (low_merit); mpf_clear (high_merit); printf ("done.\n"); return 0; }