static void mpfr_set_double_range (void) { mpfr_set_default_prec (54); if (mpfr_get_default_prec () != 54) ERROR ("get_default_prec failed (1)"); mpfr_set_default_prec (53); if ((mpfr_get_default_prec) () != 53) ERROR ("get_default_prec failed (2)"); /* in double precision format, the unbiased exponent is between 0 and 2047, where 0 is used for subnormal numbers, and 2047 for special numbers (infinities, NaN), and the bias is 1023, thus "normal" numbers have an exponent between -1022 and 1023, corresponding to numbers between 2^(-1022) and previous(2^(1024)). (The smallest subnormal number is 0.(0^51)1*2^(-1022)= 2^(-1074).) The smallest normal power of two is 1.0*2^(-1022). The largest normal power of two is 2^1023. (We have to add one for mpfr since mantissa are between 1/2 and 1.) */ set_emin (-1021); set_emax (1024); }
void bessel_tests() { // // 50 digits first: // std::cout << "Testing Bessel Functions at 50 digits....." << std::endl; #if defined(TEST_MPFR) || defined(TEST_MPFR_CLASS) mpfr_set_default_prec(50 * 1000L / 301L); #endif #ifdef TEST_MPREAL mpfr::mpreal::set_default_prec(50 * 1000L / 301L); #endif bessel_tests_1(); bessel_tests_2(); bessel_tests_3(); // // Then 100 digits: // std::cout << "Testing Bessel Functions at 100 digits....." << std::endl; #if defined(TEST_MPFR) || defined(TEST_MPFR_CLASS) mpfr_set_default_prec(100 * 1000L / 301L); #endif bessel_tests_4(); bessel_tests_5(); bessel_tests_6(); }
void poly_tests() { // // 50 digits first: // std::cout << "Testing Polynomial Evaluation at 50 digits....." << std::endl; #if defined(TEST_MPFR) || defined(TEST_MPFR_CLASS) mpfr_set_default_prec(50 * 1000L / 301L); #endif #ifdef TEST_MPREAL mpfr::mpreal::set_default_prec(50 * 1000L / 301L); #endif #ifdef TEST_MPFR time_proc("mpfr_float_50", test_polynomial<mpfr_float_50>); time_proc("mpfr_float_50 (no expression templates", test_polynomial<number<mpfr_float_backend<50>, et_off> >); time_proc("static_mpfr_float_50", test_polynomial<static_mpfr_float_50>); #endif #ifdef TEST_MPF time_proc("mpf_float_50", test_polynomial<mpf_float_50>); time_proc("mpf_float_50 (no expression templates", test_polynomial<number<gmp_float<50>, et_off> >); #endif #ifdef TEST_CPP_DEC_FLOAT time_proc("cpp_dec_float_50", test_polynomial<cpp_dec_float_50>); #endif #ifdef TEST_MPFR_CLASS time_proc("mpfr_class", test_polynomial<mpfr_class>); #endif #ifdef TEST_MPREAL time_proc("mpfr::mpreal", test_polynomial<mpfr::mpreal>); #endif // // Then 100 digits: // std::cout << "Testing Polynomial Evaluation at 100 digits....." << std::endl; #ifdef TEST_MPFR_CLASS mpfr_set_default_prec(100 * 1000L / 301L); #endif #ifdef TEST_MPREAL mpfr::mpreal::set_default_prec(100 * 1000L / 301L); #endif #ifdef TEST_MPFR time_proc("mpfr_float_100", test_polynomial<mpfr_float_100>); time_proc("mpfr_float_100 (no expression templates", test_polynomial<number<mpfr_float_backend<100>, et_off> >); time_proc("static_mpfr_float_100", test_polynomial<static_mpfr_float_100>); #endif #ifdef TEST_MPF time_proc("mpf_float_100", test_polynomial<mpf_float_100>); time_proc("mpf_float_100 (no expression templates", test_polynomial<number<gmp_float<100>, et_off> >); #endif #ifdef TEST_CPP_DEC_FLOAT time_proc("cpp_dec_float_100", test_polynomial<cpp_dec_float_100>); #endif #ifdef TEST_MPFR_CLASS time_proc("mpfr_class", test_polynomial<mpfr_class>); #endif #ifdef TEST_MPREAL time_proc("mpfr::mpreal", test_polynomial<mpfr::mpreal>); #endif }
static bool jit_langhook_init (void) { gcc_assert (gcc::jit::active_playback_ctxt); JIT_LOG_SCOPE (gcc::jit::active_playback_ctxt->get_logger ()); static bool registered_root_tab = false; if (!registered_root_tab) { ggc_register_root_tab (jit_root_tab); registered_root_tab = true; } build_common_tree_nodes (false, false); /* I don't know why this has to be done explicitly. */ void_list_node = build_tree_list (NULL_TREE, void_type_node); build_common_builtin_nodes (); /* The default precision for floating point numbers. This is used for floating point constants with abstract type. This may eventually be controllable by a command line option. */ mpfr_set_default_prec (256); return true; }
int main () { mpfr_set_default_prec (60); mp_interval_t x, y, z; mpfr_t c; mpfr_init (x.a); mpfr_init (x.b); mpfr_init (y.a); mpfr_init (y.b); mpfr_init (z.a); mpfr_init (z.b); mpfr_init (c); mpfr_set_str (x.a, "-1.99999999999999999999999999999999999999999999999999999999999999", 10, MPFR_RNDD); mpfr_set_str (x.b, "2.99999999999999999999999999999999999999999999999999999999999999", 10, MPFR_RNDU); mpfr_set_str (y.a, "2.33", 10, MPFR_RNDD); mpfr_set_str (y.b, "3.66", 10, MPFR_RNDD); mpfr_set_str (c, "-1.35", 10, MPFR_RNDN); printf ("---------------------\n"); mp_Iprintf (x, "x "); printf ("---------------------\n"); mp_Iprintf (y, "y "); printf ("---------------------\n"); printf ("c = -1.35\n"); printf ("---------------------\n"); mp_Idiv (&z, x, y); mp_Iprintf (z, "x/y"); printf ("---------------------\n"); mp_Idiv (&z, y, x); mp_Iprintf (z, "y/x"); printf ("---------------------\n"); }
/// @brief Calculate A1 /// /// Calculate the probability of making a move divided by the /// probability of its reverse, that is: /// \f[a_1=\frac{move[i]}{\sum\limits_{i}move[i]}\f] /// /// @param move The type of move /// @return \f$a_1=\frac{move[i]}{\sum\limits_{i}move[i]}\f$ auto CalculateA1(const move_type move) const noexcept { auto total_moves = this->TotalMoves(); auto this_move = attempted_moves_[to_integral(move)]; // Set precision for initialization and assignment functions mpfr_set_default_prec(PRECISION); // Initialize for MPFR mpfr_t r1, r2, a1; mpfr_inits2(PRECISION, r1, r2, a1, nullptr); mpfr_init_set_ui(r1, this_move, MPFR_RNDD); // r1 = this_move mpfr_init_set_ui(r2, total_moves, MPFR_RNDD); // r2 = total_moves // The result mpfr_div(a1, r1, r2, MPFR_RNDD); // a1 = r1/r2 // std::cout << "A1 is " << mpfr_out_str(stdout, 10, 0, a1, MPFR_RNDD) // Convert mpfr_t total to Gmpzf result by using Gmpzf(double d) // Gmpzf result = Gmpzf(mpfr_get_d(a1, MPFR_RNDD)); // MP_Float result = MP_Float(mpfr_get_ld(a1, MPFR_RNDD)); auto result = mpfr_get_d(a1, MPFR_RNDD); // Free memory mpfr_clears(r1, r2, a1, nullptr); #ifndef NDEBUG std::cout << "TotalMoves() = " << total_moves << "\n"; std::cout << "A1 is " << result << "\n"; #endif return result; } // CalculateA1()
double run_mp(double sigma_, double c_, int tau, dgs_disc_gauss_alg_t alg, size_t ntrials, unsigned long long *t) { mpfr_set_default_prec(80); mpfr_t sigma; mpfr_init_set_d(sigma, sigma_, MPFR_RNDN); gmp_randstate_t state; gmp_randinit_default(state); mpfr_t c; mpfr_init_set_d(c, c_, MPFR_RNDN); dgs_disc_gauss_mp_t *gen = dgs_disc_gauss_mp_init(sigma, c, tau, alg); double variance = 0.0; mpz_t r; mpz_init(r); *t = walltime(0); for(size_t i=0; i<ntrials; i++) { gen->call(r, gen, state); variance += mpz_get_d(r)*mpz_get_d(r); } *t = walltime(*t); dgs_disc_gauss_mp_clear(gen); mpfr_clear(sigma); mpz_clear(r); mpfr_clear(c); gmp_randclear(state); variance /= ntrials; return sqrt(variance); }
static void check1 (void) { mpfr_t x; int i, j; mpfr_set_default_prec (9); mpfr_set_emin (-10); mpfr_set_emax (10); mpfr_init (x); for (i = 0; i < (sizeof (tab) / sizeof (tab[0])); i++) { mpfr_set_str (x, tab[i].in, 2, GMP_RNDN); j = mpfr_subnormalize (x, tab[i].i, tab[i].rnd); if (mpfr_cmp_str (x, tab[i].out, 2, GMP_RNDN) != 0) { printf ("Error for i=%d\nFor:%s\nExpected:%s\nGot:", i, tab[i].in, tab[i].out); mpfr_dump (x); exit (1); } } mpfr_clear (x); }
static awk_bool_t init_my_module(void) { load_vars(); mpfr_set_default_prec((int) NUMVAL(MPFR_PRECISION)); return 1; }
static bool go_langhook_init (void) { build_common_tree_nodes (false, false); /* I don't know why this has to be done explicitly. */ void_list_node = build_tree_list (NULL_TREE, void_type_node); /* We must create the gogo IR after calling build_common_tree_nodes (because Gogo::define_builtin_function_trees refers indirectly to, e.g., unsigned_char_type_node) but before calling build_common_builtin_nodes (because it calls, indirectly, go_type_for_size). */ go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix, go_relative_import_path); build_common_builtin_nodes (); /* The default precision for floating point numbers. This is used for floating point constants with abstract type. This may eventually be controllable by a command line option. */ mpfr_set_default_prec (256); /* Go uses exceptions. */ using_eh_for_cleanups (); return true; }
static void round_str (FILE *fout, const char *s, int prec, int emin, int emax, bool ibm_ld) { mpfr_t f; mpfr_set_default_prec (prec); mpfr_set_emin (emin); mpfr_set_emax (emax); mpfr_init (f); int r = string_to_fp (f, s, MPFR_RNDD); if (ibm_ld) { assert (prec == 106 && emin == -1073 && emax == 1024); /* The maximum value in IBM long double has discontiguous mantissa bits. */ mpfr_t max_value; mpfr_init2 (max_value, 107); mpfr_set_str (max_value, "0x1.fffffffffffff7ffffffffffffcp+1023", 0, MPFR_RNDN); if (mpfr_cmpabs (f, max_value) > 0) r = 1; mpfr_clear (max_value); } mpfr_fprintf (fout, "\t%s,\n", r ? "false" : "true"); print_fp (fout, f, ",\n"); string_to_fp (f, s, MPFR_RNDN); print_fp (fout, f, ",\n"); string_to_fp (f, s, MPFR_RNDZ); print_fp (fout, f, ",\n"); string_to_fp (f, s, MPFR_RNDU); print_fp (fout, f, ""); mpfr_clear (f); }
void num_init(void) { init_safe_mem_bucket(BUCKET_NUM, NULL, num_dtor); init_safe_mem_bucket(BUCKET_NUM_TEMP, NULL, num_dtor); mpfr_set_default_prec(256); }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *prec,*eout; int mrows,ncols; char *input_buf; char *w1,*w2; int buflen,status; mpfr_t x,y,z; mp_exp_t expptr; /* Check for proper number of arguments. */ if(nrhs!=1) { mexErrMsgTxt("1 inputs required."); } else if(nlhs>2) { mexErrMsgTxt("Too many output arguments"); } /* The input must be a noncomplex scalar double.*/ mrows = mxGetM(prhs[0]); ncols = mxGetN(prhs[0]); if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || !(mrows==1 && ncols==1) ) { mexErrMsgTxt("Input must be a noncomplex scalar double."); } /* Set precision and initialize mpfr variables */ prec = mxGetPr(prhs[0]); mpfr_set_default_prec(*prec); mpfr_init(x); mpfr_init(y); mpfr_init(z); /* Mathematical operation */ mpfr_const_pi(z,GMP_RNDN); /* Retrieve results */ input_buf=mpfr_get_str (NULL, &expptr, 10, 0, z, GMP_RNDN); w1=malloc(strlen(input_buf)+20); w2=malloc(strlen(input_buf)+20); if (strncmp(input_buf, "-", 1)==0){ strcpy(w2,&input_buf[1]); sprintf(w1,"-.%se%i",w2,expptr); } else { strcpy(w2,&input_buf[0]); sprintf(w1,"+.%se%i",w2,expptr); } plhs[0] = mxCreateString(w1); /* plhs[1] = mxCreateDoubleMatrix(mrows,ncols, mxREAL); */ /* eout=mxGetPr(plhs[1]); */ /* *eout=expptr; */ mpfr_clear(x); mpfr_clear(y); mpfr_clear(z); mpfr_free_str(input_buf); free(w1); free(w2); }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *prec,*eout; int mrows,ncols; char *input_buf; int buflen,status; mpfr_t x,y,z; mp_exp_t expptr; /* Check for proper number of arguments. */ if(nrhs!=3) { mexErrMsgTxt("Three inputs required."); } else if(nlhs>1) { mexErrMsgTxt("Too many output arguments"); } /* The input must be a noncomplex scalar double.*/ mrows = mxGetM(prhs[0]); ncols = mxGetN(prhs[0]); if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || !(mrows==1 && ncols==1) ) { mexErrMsgTxt("Input must be a noncomplex scalar double."); } /* Set precision and initialize mpfr variables */ prec = mxGetPr(prhs[0]); mpfr_set_default_prec(*prec); mpfr_init(x); mpfr_init(y); mpfr_init(z); /* Read the input strings into mpfr x and y */ buflen = (mxGetM(prhs[1]) * mxGetN(prhs[1])) + 1; input_buf=mxCalloc(buflen, sizeof(char)); status = mxGetString(prhs[1], input_buf, buflen); mpfr_set_str(x,input_buf,10,GMP_RNDN); buflen = (mxGetM(prhs[2]) * mxGetN(prhs[2])) + 1; input_buf=mxCalloc(buflen, sizeof(char)); status = mxGetString(prhs[2], input_buf, buflen); mpfr_set_str(y,input_buf,10,GMP_RNDN); /* Retrieve results */ mxFree(input_buf); plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL); eout=mxGetPr(plhs[0]); *eout=(double)mpfr_cmp(x,y); mpfr_clear(x); mpfr_clear(y); mpfr_clear(z); }
int main(int argc, char**argv) { itv_t a,b,c; bound_t bound; itv_internal_t* intern; ap_fpu_init(); mpfr_set_default_prec(4046); intern = itv_internal_alloc(); itv_init(a); itv_init(b); itv_init(c); bound_init(bound); /* Positive or negative intervals */ bound_set_int(b->inf,-3); bound_set_int(b->sup,5); bound_set_int(c->inf,-1); bound_set_int(c->sup,5); bound_set_int(bound,4); arith(intern,a,b,c,bound); itv_neg(c,c); arith(intern,a,b,c,bound); itv_neg(b,b); arith(intern,a,b,c,bound); itv_neg(c,c); arith(intern,a,b,c,bound); /* general intervals */ bound_set_int(b->inf,3); bound_set_int(b->sup,5); bound_set_int(c->inf,7); bound_set_int(c->sup,11); bound_set_int(bound,3); arith(intern,a,b,c,bound); bound_set_int(bound,-3); arith(intern,a,b,c,bound); /* aliases */ bound_set_int(b->inf,3); bound_set_int(b->sup,5); bound_set_int(c->inf,7); bound_set_int(c->sup,11); bound_set_int(bound,3); arith(intern,b,b,b,bound); bound_set_int(bound,-3); arith(intern,b,b,b,bound); itv_clear(a); itv_clear(b); itv_clear(c); bound_clear(bound); itv_internal_free(intern); }
void real::begin(int precision, int mmax_reals) { max_reals = mmax_reals; n_reals = 0; mpfr_set_default_prec(precision); mpfr_clear(NAN.r); mpfr_init(NAN.r); mpfr_set_nan(NAN.r); mpfr_clear(INF.r); mpfr_init(INF.r); mpfr_set_inf(INF.r, 0); mpfr_clear(ZERO.r); mpfr_init(ZERO.r); ZERO = real("0.0"); mpfr_clear(ONE.r); mpfr_init(ONE.r); ONE = real("1.0"); mpfr_clear(TWO.r); mpfr_init(TWO.r); TWO = real("2.0"); mpfr_clear(THREE.r); mpfr_init(THREE.r); THREE = real("3.0"); mpfr_clear(FOUR.r); mpfr_init(FOUR.r); FOUR = real("4.0"); mpfr_clear(FIVE.r); mpfr_init(FIVE.r); FIVE = real("5.0"); mpfr_clear(PI.r); mpfr_init(PI.r); mpfr_const_pi(PI.r, MPFR_RNDN); mpfr_clear(EPS.r); mpfr_init(EPS.r); EPS = ONE; while (ONE + EPS != ONE) { EPS = EPS / TWO; } mpfr_clear(SMALL_ENOUGH.r); mpfr_init(SMALL_ENOUGH.r); SMALL_ENOUGH = EPS; }
/* Language hooks. */ static bool gpy_langhook_init (void) { build_common_tree_nodes (false, false); // build_common_builtin_nodes (); // shouldnt have to do this... void_list_node = build_tree_list (NULL_TREE, void_type_node); // init some internal gccpy types gpy_dot_types_init (); /* The default precision for floating point numbers. This is used for floating point constants with abstract type. This may eventually be controllable by a command line option. */ mpfr_set_default_prec (128); // for exceptions using_eh_for_cleanups (); return true; }
static awk_value_t * convert_base(int nargs, awk_value_t *resval, int to_internal_base) { awk_value_t number_node, base_node; mpfr_t val; char * result; size_t len; int from_base, to_base; if (do_lint && nargs != 2) lintwarn(ext_id, _("convert_base: called with incorrect number of arguments")); mpfr_set_default_prec((int) NUMVAL(MPFR_PRECISION)); if (!get_argument(0, AWK_STRING, &number_node)) fatal(ext_id, _("first argument must be a string")); if (!get_argument(1, AWK_NUMBER, &base_node)) fatal(ext_id, _("second argument must be a number")); if (to_internal_base) { from_base = base_node.num_value; to_base = NUMVAL(MPFR_BASE); } else { from_base = NUMVAL(MPFR_BASE); to_base = base_node.num_value; } mpfr_init_set_str(val, number_node.str_value.str, from_base, (int) NUMVAL(MPFR_ROUND)); /* Set the return value */ result = malloc(10*(int) NUMVAL(MPFR_PRECISION)); len = mpfr_out_string(result, to_base, 0, val, (int) NUMVAL(MPFR_ROUND)); make_string_malloc(result, len, resval); free(result); mpfr_clear(val); return resval; }
SEXP R_mpfr_set_default_prec(SEXP prec) { // return the previous value int prev = (int) mpfr_get_default_prec(); mpfr_set_default_prec((mpfr_prec_t) asInteger(prec)); return ScalarInteger(prev); }
int main(int argc, char **argv) { double x; unsigned int n, D, p; mpfr_t X, R; mpz_t N; char sf[50]; if(argc > 1) { switch(argv[1][0]) { case 'a': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("(%.*lf)^(%d) = %.*lf (Naive)\n", d(D), x, n, D, naive_int_exp(x, n)); else printf("Uasge: %s a <x=Base for exp> " "<n=Exponent for exp> " "<D=Number of digits to display>\n", argv[0]); break; case 'b': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("(%.*lf)^(%d) = %.*lf (Squaring)\n", d(D), x, n, D, squaring_int_exp(x, n)); else printf("Uasge: %s b <x=Base for exp> " "<n=Exponent for exp> " "<D=Number of digits to display>\n", argv[0]); break; case 'c': if (argc == 6 && sscanf(argv[4], "%u", &D) == 1 && sscanf(argv[5], "%u" , &p) == 1) { mpfr_set_default_prec(p); if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN)==0 && mpz_init_set_str(N, argv[3], 10) == 0) { mpfr_init(R); sprintf(sf, "(%%.%uRNf)^%%Zd =~\t(Naive)" "\n\t%%.%uRNf\n", d(D), D); mpfr_naive_int_exp(R, X, N); mpfr_printf(sf, X, N, R); } else printf("Usage: %s c <X=Base for exp> " "<N=Exponent for exp> " "<D=Number of digitsto calculate to> " "<p=bits of precision>\n", argv[0]); } else printf("Usage: %s c <X=Base for exp> " "<N=Exponent for exp> " "<D=Number of digitsto calculate to> " "<p=bits of precision>\n", argv[0]); break; case 'd': if (argc == 6 && sscanf(argv[4], "%u", &D) == 1 && sscanf(argv[5], "%u" , &p) == 1) { mpfr_set_default_prec(p); if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN)==0 && mpz_init_set_str(N, argv[3], 10) == 0) { mpfr_init(R); sprintf(sf, "(%%.%uRNf)^%%Zd =~\t(Squaring)" "\n\t%%.%uRNf\n", d(D), D); mpfr_squaring_int_exp(R, X, N); mpfr_printf(sf, X, N, R); } else printf("Usage: %s d <X=Base for exp> " "<N=Exponent for exp> " "<D=Number of digitsto calculate to> " "<p=bits of precision>\n", argv[0]); } else printf("Usage: %s d <X=Base for exp> " "<N=Exponent for exp> " "<D=Number of digitsto calculate to> " "<p=bits of precision>\n", argv[0]); break; default: printf("Usage: %s <a/b/c/d> <arguments>\n", argv[0]); } } else printf("Usage: %s <a/b/c/d> <arguments>\n", argv[0]); }
/** *\brief set the precision for multiple precision floating-point type * \param prec required precision of computation * * \code * mp_precision_type prec = 200; * setMpPrecision (prec); * \endcode */ inline void setMpPrecision( mp_precision_type __prec ) { mpfr_set_default_prec ( __prec ); }
int main(int argc, char **argv) { double x, y; unsigned int n, p, D; mpfr_t R, X; char sf[50]; FILE *in; if(argc > 1) { switch(argv[1][0]) { case 'a': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("Cos(%.*lf) = %.*lf\n", d(D), x, D, taylor_cos(x, n)); else printf("Usage: %s a <x=value for Cos(x)> <n> " "<D=Number of digits to display>\n", argv[0]); break; case 'b': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("Sin(%.*lf) = %.*lf\n", d(D), x, D, taylor_sin(x, n)); else printf("Usage: %s b <x=value for Sin(x)> <n> " "<D=Number of digits to display>\n", argv[0]); break; case 'c': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u" , &n) == 1 && sscanf(argv[4], "%u" , &D) == 1) printf("Tan(%.*lf) = %.*lf\n", d(D), x, D, taylor_tan(x, n)); else printf("Usage: %s a <x=value for Tan(x)> <n> " "<D=Number of digits to display>\n", argv[0]); break; case 'd': if(argc == 6 && sscanf(argv[3], "%u", &D) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0) { mpfr_init(R); sprintf(sf, "cos(%%.%uRNf) =~\n\t%%.%uRNf\n", d(D), D); mpfr_taylor_cos(R, X, n); mpfr_printf(sf, X, R); } else printf("Usage: %s d <x=value for Cos(x)> " "<D=Number of digits to display> " "<n> <p=bits of precision to use>\n", argv[0]); } else printf("Usage: %s d <x=value for Cos(x)> " "<D=Number of digits to display> " "<n> <p=bits of precision to use>\n", argv[0]); break; case 'e': if(argc == 6 && sscanf(argv[3], "%u", &D) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if (mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0) { mpfr_init(R); sprintf(sf, "sin(%%.%uRNf) =~\n\t%%.%uRNf\n", d(D), D); mpfr_taylor_sin(R, X, n); mpfr_printf(sf, X, R); } else printf("Usage: %s d <x=value for Sin(x)> " "<D=Number of digits to display> " "<n> <p=bits of precision to use>\n", argv[0]); }
int main(int argc, char **argv) { double x, y; unsigned int n, D, p; mpfr_t X, Y, R; mpz_t N; char sf[50]; FILE *in; if(argc > 1) { switch(argv[1][0]) { case 'a': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u", &n) == 1 && sscanf(argv[4], "%u", &D) == 1) printf("exp(%.*lf) ~= %.*lf (naive)\n", d(D), x, D, naive_exp(x, n)); else printf("Usage: %s a <x=Value for exp(x)> <n> " "<D=digits to display>\n", argv[0]); break; case 'b': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u", &n) == 1 && sscanf(argv[4], "%u", &D) == 1) printf("exp(%.*lf) ~= %.*lf\n", d(D), x, D, taylor_exp(x, n)); else printf("Usage: %s b <x=Value for exp(x)> <n> " "<D=digits to display>\n", argv[0]); break; case 'c': if(argc == 5 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%u", &n) == 1 && sscanf(argv[4], "%u", &D) == 1) printf("ln(%.*lf) ~= %.*lf\n", d(D), x, D, taylor_nat_log(x, n)); else printf("Usage: %s c <x=Value for ln(x)> <n> " "<D=digits to display>\n", argv[0]); break; case 'd': if(argc == 6 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%lf", &y) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &D) == 1) printf("pow(%.*lf, %.*lf) ~= %.*lf\n", d(D), x, d(D), y, D, taylor_pow(x, y, n)); else printf("Usage: %s d <x=Value for pow(x,y)> " "<y=Value for pow(x,y)> <n> " "<D=digits to display>\n", argv[0]); break; case 'e': if(argc == 6 && sscanf(argv[2], "%lf", &x) == 1 && sscanf(argv[3], "%lf", &y) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &D) == 1) printf("log(%.*lf, %.*lf) ~= %.*lf\n", d(D), x, d(D), y, D, taylor_log(x, y, n)); else printf("Usage: %s e <x=Value for log(x, y)> " "<y=Value for log(x, y)> <n> " "<D=digits to display>\n", argv[0]); break; case 'f': if(argc == 6 && sscanf(argv[4], "%u", &D) == 1 && sscanf(argv[5], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if(mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0 && mpz_init_set_str(N, argv[3], 10) == 0) { mpfr_init(R); sprintf(sf, "exp(%%.%uRNf) =~\t(naive)" "\n\t%%.%uRNf\n", d(D), D); mpfr_naive_exp(R, X, N); mpfr_printf(sf, X, R); } else printf("Usage: %s f <X=value for exp(X)> <N> " "<D=Digits to display> " "<p=bits of precision>\n", argv[0]); } else printf("Usage: %s f <X=value for exp(X)> <N> " "<D=Digits to display> " "<p=bits of precision>\n", argv[0]); break; case 'g': if(argc == 6 && sscanf(argv[4], "%u", &D) == 1 && sscanf(argv[5], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if(mpfr_init_set_str(X, argv[2], 10, MPFR_RNDN) == 0 && mpz_init_set_str(N, argv[3], 10) == 0) { mpfr_init(R); sprintf(sf, "exp(%%.%uRNf) =~" "\n\t%%.%uRNf\n", d(D), D); mpfr_taylor_exp(R, X, N); mpfr_printf(sf, X, R); } else printf("Usage: %s g <X=value for exp(X)> <N> " "<D=Digits to display> " "<p=bits of precision>\n", argv[0]); }
/// @brief Calculate A2 /// /// Calculate \f$a_2=e^{\Delta S}\f$ /// /// @param move The type of move /// @return \f$a_2=e^{-\Delta S}\f$ auto CalculateA2(const move_type move) const noexcept { auto currentS3Action = S3_bulk_action(N1_TL_, N3_31_13_, N3_22_, Alpha_, K_, Lambda_); auto newS3Action = static_cast<Gmpzf>(0); // auto newS3Action = static_cast<MP_Float>(0); switch (move) { case move_type::TWO_THREE: // A (2,3) move adds a timelike edge // and a (2,2) simplex newS3Action = S3_bulk_action(N1_TL_ + 1, N3_31_13_, N3_22_ + 1, Alpha_, K_, Lambda_); break; case move_type::THREE_TWO: // A (3,2) move removes a timelike edge // and a (2,2) simplex newS3Action = S3_bulk_action(N1_TL_ - 1, N3_31_13_, N3_22_ - 1, Alpha_, K_, Lambda_); break; case move_type::TWO_SIX: // A (2,6) move adds 2 timelike edges and // 2 (1,3) and 2 (3,1) simplices newS3Action = S3_bulk_action(N1_TL_ + 2, N3_31_13_ + 4, N3_22_, Alpha_, K_, Lambda_); break; case move_type::SIX_TWO: // A (6,2) move removes 2 timelike edges and // 2 (1,3) and 2 (3,1) simplices newS3Action = S3_bulk_action(N1_TL_ - 2, N3_31_13_, N3_22_ - 4, Alpha_, K_, Lambda_); break; case move_type::FOUR_FOUR: // A (4,4) move changes nothing with respect to the action, // and e^0==1 #ifndef NDEBUG std::cout << "A2 is 1\n"; #endif return static_cast<double>(1); } // auto exponent = newS3Action - currentS3Action; auto exponent = currentS3Action - newS3Action; auto exponent_double = Gmpzf_to_double(exponent); // if exponent > 0 then e^exponent >=1 so according to Metropolis // algorithm return A2=1 if (exponent >= 0) return static_cast<double>(1); // Set precision for initialization and assignment functions mpfr_set_default_prec(PRECISION); // Initialize for MPFR mpfr_t r1, a2; mpfr_inits2(PRECISION, r1, a2, nullptr); // Set input parameters and constants to mpfr_t equivalents mpfr_init_set_d(r1, exponent_double, MPFR_RNDD); // r1 = exponent // e^exponent mpfr_exp(a2, r1, MPFR_RNDD); // Convert mpfr_t total to Gmpzf result by using Gmpzf(double d) // Gmpzf result = Gmpzf(mpfr_get_d(a2, MPFR_RNDD)); auto result = mpfr_get_d(a2, MPFR_RNDD); // Free memory mpfr_clears(r1, a2, nullptr); #ifndef NDEBUG std::cout << "A2 is " << result << "\n"; #endif return result; } // CalculateA2()
int main(int argc, char **argv) { mpfr_t tmp1; mpfr_t tmp2; mpfr_t tmp3; mpfr_t s1; mpfr_t s2; mpfr_t r; mpfr_t a1; mpfr_t a2; time_t start_time; time_t end_time; // Parse command line opts int hide_pi = 0; if(argc == 2) { if(strcmp(argv[1], "--hide-pi") == 0) { hide_pi = 1; } else if((precision = atoi(argv[1])) == 0) { fprintf(stderr, "Invalid precision specified. Aborting.\n"); return 1; } } else if(argc == 3) { if(strcmp(argv[1], "--hide-pi") == 0) { hide_pi = 1; } if((precision = atoi(argv[2])) == 0) { fprintf(stderr, "Invalid precision specified. Aborting.\n"); return 1; } } // If the precision was not specified, default it if(precision == 0) { precision = DEFAULT_PRECISION; } // Actual number of correct digits is roughly 3.35 times the requested precision precision *= 3.35; mpfr_set_default_prec(precision); mpfr_inits(tmp1, tmp2, tmp3, s1, s2, r, a1, a2, NULL); start_time = time(NULL); // a0 = 1/3 mpfr_set_ui(a1, 1, MPFR_RNDN); mpfr_div_ui(a1, a1, 3, MPFR_RNDN); // s0 = (3^.5 - 1) / 2 mpfr_sqrt_ui(s1, 3, MPFR_RNDN); mpfr_sub_ui(s1, s1, 1, MPFR_RNDN); mpfr_div_ui(s1, s1, 2, MPFR_RNDN); unsigned long i = 0; while(i < MAX_ITERS) { // r = 3 / (1 + 2(1-s^3)^(1/3)) mpfr_pow_ui(tmp1, s1, 3, MPFR_RNDN); mpfr_ui_sub(r, 1, tmp1, MPFR_RNDN); mpfr_root(r, r, 3, MPFR_RNDN); mpfr_mul_ui(r, r, 2, MPFR_RNDN); mpfr_add_ui(r, r, 1, MPFR_RNDN); mpfr_ui_div(r, 3, r, MPFR_RNDN); // s = (r - 1) / 2 mpfr_sub_ui(s2, r, 1, MPFR_RNDN); mpfr_div_ui(s2, s2, 2, MPFR_RNDN); // a = r^2 * a - 3^i(r^2-1) mpfr_pow_ui(tmp1, r, 2, MPFR_RNDN); mpfr_mul(a2, tmp1, a1, MPFR_RNDN); mpfr_sub_ui(tmp1, tmp1, 1, MPFR_RNDN); mpfr_ui_pow_ui(tmp2, 3UL, i, MPFR_RNDN); mpfr_mul(tmp1, tmp1, tmp2, MPFR_RNDN); mpfr_sub(a2, a2, tmp1, MPFR_RNDN); // s1 = s2 mpfr_set(s1, s2, MPFR_RNDN); // a1 = a2 mpfr_set(a1, a2, MPFR_RNDN); i++; } // pi = 1/a mpfr_ui_div(a2, 1, a2, MPFR_RNDN); end_time = time(NULL); mpfr_clears(tmp1, tmp2, tmp3, s1, s2, r, a1, NULL); // Write the digits to a string for accuracy comparison char *pi = malloc(precision + 100); if(pi == NULL) { fprintf(stderr, "Failed to allocated memory for output string.\n"); return 1; } mpfr_sprintf(pi, "%.*R*f", precision, MPFR_RNDN, a2); // Check out accurate we are unsigned long accuracy = check_digits(pi); // Print the results (only print the digits that are accurate) if(!hide_pi) { // Plus two for the "3." at the beginning for(unsigned long i=0; i<(unsigned long)(precision/3.35)+2; i++) { printf("%c", pi[i]); } printf("\n"); } // Send the time and accuracy to stderr so pi can be redirected to a file if necessary fprintf(stderr, "Time: %d seconds\nAccuracy: %lu digits\n", (int)(end_time - start_time), accuracy); mpfr_clear(a2); free(pi); pi = NULL; return 0; }
static awk_value_t * mpfr_ordinary_op (int argc, awk_value_t *result, int arity, int is_predicate, void * ordinary_op) { mpfr_t number_mpfr[10]; char * result_func; int result_pred; size_t len; int i, base, precision; mp_rnd_t round; base = NUMVAL(MPFR_BASE); round = NUMVAL(MPFR_ROUND); precision = NUMVAL(MPFR_PRECISION); if (argc < arity) fatal(ext_id, _("too few arguments to MPFR function")); if (argc > 5) fatal(ext_id, _("too many arguments to MPFR function")); /* First optional argument is rounding mode. */ if (argc > arity) { awk_value_t val; if (!get_argument(arity+1, AWK_STRING, &val)) fatal(ext_id, _("optional round argument must be a string")); round = mpfr_get_round(val.str_value.str); } mpfr_set_default_prec(precision); /* Second optional argument is precision mode. */ if (argc > arity+1) { awk_value_t val; if (!get_argument(arity+2, AWK_NUMBER, &val)) fatal(ext_id, _("optional precision argument must be numeric")); precision = val.num_value; } for (i=0; i < arity; i++) { awk_value_t val; if (!get_argument(i, AWK_STRING, &val)) fatal(ext_id, _("missing required argument")); mpfr_init2(number_mpfr[i], precision); mpfr_set_str(number_mpfr[i], val.str_value.str, base, round); } switch (arity) { case 0: mpfr_init_set_str(number_mpfr[0], "0", base, round); if (is_predicate) { result_pred = ((constpred_t) ordinary_op) (); } else { set_exact(((constop_t) ordinary_op) (number_mpfr[0], round)); } break; case 1: if (is_predicate) { result_pred = ((unpred_t ) ordinary_op) (number_mpfr[0]); } else { set_exact(((unop_t ) ordinary_op) (number_mpfr[0], number_mpfr[0], round)); } break; case 2: if (is_predicate) { result_pred = ((binpred_t ) ordinary_op) (number_mpfr[0], number_mpfr[1]); } else { set_exact(((binop_t ) ordinary_op) (number_mpfr[0], number_mpfr[0], number_mpfr[1], round)); } break; } if (is_predicate) { make_number(result_pred, result); } else { result_func = malloc(10*(int) NUMVAL(MPFR_PRECISION)); len = mpfr_out_string(result_func, base, 0, number_mpfr[0], round); make_string_malloc(result_func, len, result); free(result_func); } for (i=0; i < arity; i++) mpfr_clear(number_mpfr[i]); return result; }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *prec,*eoutr,*eouti; int mrows,ncols; char *input_buf; char *w1,*w2; int buflen,status; mpfr_t xr,xi,yr,yi,zr,zi,temp,temp1; mp_exp_t expptr; /* Check for proper number of arguments. */ if(nrhs!=5) { mexErrMsgTxt("5 inputs required."); } else if(nlhs>4) { mexErrMsgTxt("Too many output arguments"); } /* The input must be a noncomplex scalar double.*/ mrows = mxGetM(prhs[0]); ncols = mxGetN(prhs[0]); if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) || !(mrows==1 && ncols==1) ) { mexErrMsgTxt("Input must be a noncomplex scalar double."); } /* Set precision and initialize mpfr variables */ prec = mxGetPr(prhs[0]); mpfr_set_default_prec(*prec); mpfr_init(xr); mpfr_init(xi); mpfr_init(yr); mpfr_init(yi); mpfr_init(zr); mpfr_init(zi); mpfr_init(temp); mpfr_init(temp1); /* Read the input strings into mpfr x real */ buflen = (mxGetM(prhs[1]) * mxGetN(prhs[1])) + 1; input_buf=mxCalloc(buflen, sizeof(char)); status = mxGetString(prhs[1], input_buf, buflen); mpfr_set_str(xr,input_buf,10,GMP_RNDN); /* Read the input strings into mpfr x imag */ buflen = (mxGetM(prhs[2]) * mxGetN(prhs[2])) + 1; input_buf=mxCalloc(buflen, sizeof(char)); status = mxGetString(prhs[2], input_buf, buflen); mpfr_set_str(xi,input_buf,10,GMP_RNDN); /* Read the input strings into mpfr y real */ buflen = (mxGetM(prhs[3]) * mxGetN(prhs[3])) + 1; input_buf=mxCalloc(buflen, sizeof(char)); status = mxGetString(prhs[3], input_buf, buflen); mpfr_set_str(yr,input_buf,10,GMP_RNDN); /* Read the input strings into mpfr y imag */ buflen = (mxGetM(prhs[4]) * mxGetN(prhs[4])) + 1; input_buf=mxCalloc(buflen, sizeof(char)); status = mxGetString(prhs[4], input_buf, buflen); mpfr_set_str(yi,input_buf,10,GMP_RNDN); /* Mathematical operation */ /* denominator */ mpfr_mul(temp,yr,yr,GMP_RNDN); mpfr_mul(temp1,yi,yi,GMP_RNDN); mpfr_add(temp,temp,temp1,GMP_RNDN); /* real part */ mpfr_mul(temp1,xr,yr,GMP_RNDN); mpfr_mul(zr,xi,yi,GMP_RNDN); mpfr_add(zr,temp1,zr,GMP_RNDN); /* imag part */ mpfr_mul(temp1,xi,yr,GMP_RNDN); mpfr_mul(zi,xr,yi,GMP_RNDN); mpfr_sub(zi,temp1,zi,GMP_RNDN); /* divide by denominator */ mpfr_div(zr,zr,temp,GMP_RNDN); mpfr_div(zi,zi,temp,GMP_RNDN); /* Retrieve results */ mxFree(input_buf); input_buf=mpfr_get_str (NULL, &expptr, 10, 0, zr, GMP_RNDN); w1=malloc(strlen(input_buf)+20); w2=malloc(strlen(input_buf)+20); if (strncmp(input_buf, "-", 1)==0){ strcpy(w2,&input_buf[1]); sprintf(w1,"-.%se%i",w2,expptr); } else { strcpy(w2,&input_buf[0]); sprintf(w1,"+.%se%i",w2,expptr); } plhs[0] = mxCreateString(w1); /* plhs[1] = mxCreateDoubleMatrix(mrows,ncols, mxREAL); */ /* eoutr=mxGetPr(plhs[1]); */ /* *eoutr=expptr; */ mpfr_free_str(input_buf); input_buf=mpfr_get_str (NULL, &expptr, 10, 0, zi, GMP_RNDN); free(w1); free(w2); w1=malloc(strlen(input_buf)+20); w2=malloc(strlen(input_buf)+20); if (strncmp(input_buf, "-", 1)==0){ strcpy(w2,&input_buf[1]); sprintf(w1,"-.%se%i",w2,expptr); } else { strcpy(w2,&input_buf[0]); sprintf(w1,"+.%se%i",w2,expptr); } plhs[1] = mxCreateString(w1); /* plhs[3] = mxCreateDoubleMatrix(mrows,ncols, mxREAL); */ /* eouti=mxGetPr(plhs[3]); */ /* *eouti=expptr; */ mpfr_clear(xr); mpfr_clear(xi); mpfr_clear(yr); mpfr_clear(yi); mpfr_clear(zr); mpfr_clear(zi); mpfr_clear(temp); mpfr_clear(temp1); mpfr_free_str(input_buf); free(w1); free(w2); }
static void check1 (void) { mpfr_t x; int i, j, k, s, old_inex, tiny, expj; mpfr_exp_t emin, emax; unsigned int expflags, flags; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_set_default_prec (9); mpfr_set_emin (-10); mpfr_set_emax (10); mpfr_init (x); for (i = 0; i < numberof (tab); i++) for (s = 0; s <= (tab[i].rnd == MPFR_RNDN); s++) for (k = 0; k <= 1; k++) { mpfr_set_str (x, tab[i].in, 2, MPFR_RNDN); old_inex = tab[i].i; expj = tab[i].j; if (s) { mpfr_neg (x, x, MPFR_RNDN); old_inex = - old_inex; expj = - expj; } if (k && old_inex) old_inex = old_inex < 0 ? INT_MIN : INT_MAX; tiny = MPFR_GET_EXP (x) <= -3; mpfr_clear_flags (); j = mpfr_subnormalize (x, old_inex, tab[i].rnd); expflags = (tiny ? MPFR_FLAGS_UNDERFLOW : 0) | (expj ? MPFR_FLAGS_INEXACT : 0); flags = __gmpfr_flags; if (s) mpfr_neg (x, x, MPFR_RNDN); if (mpfr_cmp_str (x, tab[i].out, 2, MPFR_RNDN) != 0 || flags != expflags || ! SAME_SIGN (j, expj)) { const char *sgn = s ? "-" : ""; printf ("Error for i = %d (old_inex = %d), k = %d, x = %s%s\n" "Expected: %s%s\nGot: ", i, old_inex, k, sgn, tab[i].in, sgn, tab[i].out); if (s) mpfr_neg (x, x, MPFR_RNDN); mpfr_dump (x); printf ("Expected flags = %u, got %u\n", expflags, flags); printf ("Expected ternary value = %d, got %d\n", expj, j); exit (1); } } mpfr_clear (x); MPFR_ASSERTN (mpfr_get_emin () == -10); MPFR_ASSERTN (mpfr_get_emax () == 10); set_emin (emin); set_emax (emax); }
int main(int argc, char** argv) { double N, T; unsigned int n, D, p; mpfr_t Nr, Tr, R; int c; char sf[50]; if (argc == 1) { printf("Usage: %s [a/b/c/d/e] [arguments]\n", argv[0]); exit(1); } switch(argv[1][0]) { case 'a': if (argc == 5 && sscanf(argv[2], "%lf", &N) == 1 && sscanf(argv[3], "%lf", &T) == 1 && sscanf(argv[4], "%u", &D) == 1) printf("sqrt(%.*lf) =~ %.*lf\n", d(D), N, D, bisect_sqrt(N, T)); else printf("Usage: %s a <N=Value to sqrt> " "<T=Tolerance> <D=Number of digits to display>\n", argv[0]); break; case 'b': if (argc == 6 && sscanf(argv[2], "%lf", &N) == 1 && sscanf(argv[3], "%lf", &T) == 1 && sscanf(argv[4], "%u", &n) == 1 && sscanf(argv[5], "%u", &D) == 1) printf("%u_Root(%.*lf) =~ %.*lf\n", n, d(D), N, D, bisect_nRoot(N, T, n)); else printf("Usage: %s b <N=Value to root> <T=Tolerance>" "<n=nth Root> <D=Number of digits to display>\n", argv[0]); break; case 'c': if (argc == 5 && sscanf(argv[3], "%u", &D) == 1 && sscanf(argv[4], "%u", &p) == 1) { mpfr_set_default_prec(p); INIT_CONSTANTS if (mpfr_init_set_str(Nr, argv[2], 10, MPFR_RNDN) == 0) { mpfr_init(R); //Sets the tolerance to Tr = 10^-D mpfr_digits_to_tolerance(D, Tr); //Generates the required format string sprintf(sf, "sqrt(%%.%uRNf) =~\n\t%%.%uRNf\n", d(D), D); mpfr_bisect_sqrt(R, Nr, Tr); mpfr_printf(sf, Nr, R); } else printf("Usage: %s c <N=Value to sqrt> " "<D=Number of digits to calculate to> " "<p=bits of precision>\n", argv[0]); } else