/* * Evaluate the series 1/0! + 1/1! + 1/2! + 1/3! + 1/4! ... * On input, 'bits' is the desired precision in bits. * On output, e' contains the calculated value. */ static void calculateE( unsigned bits, mpf_t e) { mpf_t lastE; mpf_t invFact; /* 1/2!, 1/3!, 1/4!, etc. */ unsigned term; /* 2, 3, 4... */ /* initial conditions, including the first two terms */ mpf_init_set_ui(lastE, 0); mpf_set_ui(e, 2); mpf_init_set_ui(invFact, 1); /* 1/1! */ term = 2; for(;;) { /* invFact /= (term) */ mpf_div_ui(invFact, invFact, term); /* e += 1 / (term!) */ mpf_add(e, e, invFact); /* if e == lastE, within the requested precision, we're done */ if(mpf_eq(e, lastE, bits)) { break; } mpf_set(lastE, e); term++; } /* free memory associated with mpf_t's we allocated */ mpf_clear(lastE); mpf_clear(invFact); }
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); } }
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; }
// The Brent-Salamin algorithm int main(int argc, char* argv[]) { if (argc < 2) return -1; int n = (int)strtol(argv[1], NULL, 10); mpf_set_default_prec(1000); mpf_t a, b, t, c, sum; // a=1 mpf_init_set_ui(a, 1); mpf_init_set_ui(sum, 0); mpf_init(b); mpf_init(t); mpf_init(c); mpf_init(sum); // b=1/sqrt(2) mpf_sqrt_ui(b, 2); mpf_ui_div(b, 1, b); // n次迭代的误差小于\frac{2^{n+9}}{20^{2n+1}} for (int i = 1; i <= n; ++i) { // t=(a+b)/2 mpf_add(t, a, b); mpf_div_ui(t, t, 2); // b=sqrt(a*b); mpf_mul(b, a, b); mpf_sqrt(b, b); // a=t mpf_swap(t, a); mpf_mul(t, a, a); mpf_mul(c, b, b); mpf_sub(c, t, c); mpf_mul_2exp(c, c, i + 1); mpf_add(sum, sum, c); } mpf_mul(t, a, a); mpf_mul_ui(t, t, 4); mpf_ui_sub(sum, 1, sum); mpf_div(t, t, sum); mpf_out_str(stdout, 10, 0, t); printf("\n"); mpf_clear(a); mpf_clear(b); mpf_clear(t); mpf_clear(c); mpf_clear(sum); return 0; }
int main() { char function; mpf_t number1, number2, output; mpf_init_set_ui(number1, 0); mpf_init_set_ui(number2, 0); mpf_init_set_ui(output, 0); while(true) { printf("enter a calculation using A +,-,*,/ B format.\n"); gmp_scanf("%Ff%c%Ff", number1, &function, number2); if (function == '+') { mpf_add(output, number1, number2); gmp_printf("%Ff\n", output); } if (function == '-') { mpf_sub(output, number1, number2); gmp_printf("%Ff\n", output); } if (function == 'x' || function == '*') { mpf_mul(output, number1, number2); gmp_printf("%Ff\n", output); } if (function == '/') { mpf_div(output, number1, number2); gmp_printf("%Ff\n", output); } } mpf_clear(number1); mpf_clear(number2); mpf_clear(output); }
void my_out_str_raw(FILE *fp, unsigned long digits, mpf_t f, unsigned long offset) { unsigned long d; if (digits <= LINE_SIZE*NUM_BLOCKS) { unsigned long cursor = offset % LINE_SIZE; for (d = 0; d < digits; ) { mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1)); mpf_mul_ui(f, f, UNIT_MOD); unsigned long i = mpf_get_ui(f); mpf_sub_ui(f, f, i); utoa(i, UNIT_SIZE); *out_ptr++ = ' '; d += UNIT_SIZE; cursor += UNIT_SIZE; if (cursor == LINE_SIZE) { cursor = 0; *out_ptr++ = ':'; *out_ptr++ = ' '; utoa(offset + d, 0); *out_ptr++ = '\n'; if ((offset + d) % (LINE_SIZE*10) == 0) flush_out(fp); } } } else { mpf_t block, mod; unsigned long num_units = (digits + UNIT_SIZE-1)/UNIT_SIZE; unsigned long block_size = (num_units + NUM_BLOCKS-1)/NUM_BLOCKS*UNIT_SIZE; mpf_set_default_prec((int)(block_size*BITS_PER_DIGIT+1)); mpf_init(block); mpf_init_set_ui(mod, 10); mpf_pow_ui(mod, mod, block_size); for (d = 0; d < digits; d += block_size) { unsigned long size = block_size < digits - d ? block_size : digits - d; mpf_set_prec_raw(block, (int)(size*BITS_PER_DIGIT+1)); mpf_set(block, f); my_out_str_raw(fp, size, block, offset+d); if (block_size < digits - d) { mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1)); mpf_mul(f, f, mod); mpf_floor(trunk, f); mpf_sub(f, f, trunk); } } mpf_clear(block); mpf_clear(mod); } }
int main(int argc,char *argv[]) { mpf_t mpfVal,mpf1; unsigned long i; mpf_set_default_prec(9000); mpf_init_set_ui(mpf1,1); printf ("%ld\n",mpf_get_default_prec()); for (i=997; i>=997; i--) { mpf_init(mpfVal); mpf_div_ui(mpfVal,mpf1,i); printf("%ld: ",i); (void) mpf_out_str(stdout,10,2000,mpfVal); mpf_clear(mpfVal); } }
int main (void) { mpf_set_default_prec (65568); mpf_t x0, y0, resA, resB; mpf_init_set_ui (y0, 1); mpf_init_set_d (x0, 0.5); mpf_sqrt (x0, x0); mpf_init (resA); mpf_init (resB); for(int i=0; i<7; i++){ agm(x0, y0, resA, resB); agm(resA, resB, x0, y0); } gmp_printf ("%.20000Ff\n", x0); gmp_printf ("%.20000Ff\n\n", y0); return 0; }
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); }
void check_rand (int argc, char **argv) { mp_size_t size; mp_exp_t exp; int reps = 20000; int i; mpf_t u, v, w, wref; mp_size_t bprec = 100; mpf_t rerr, max_rerr, limit_rerr; gmp_randstate_t rands; if (argc > 1) { reps = strtol (argv[1], 0, 0); if (argc > 2) bprec = strtol (argv[2], 0, 0); } mpf_set_default_prec (bprec); gmp_randinit_default(rands); 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 (u); mpf_init (v); mpf_init (w); mpf_init (wref); for (i = 0; i < reps; i++) { size = urandom (rands) % (2 * SIZE) - SIZE; exp = urandom (rands) % SIZE; mpf_rrandomb (u, rands, size, exp); size = urandom (rands) % (2 * SIZE) - SIZE; exp = urandom (rands) % SIZE; mpf_rrandomb (v, rands, size, exp); if ((urandom (rands) & 1) != 0) mpf_add_ui (u, v, 1); else if ((urandom (rands) & 1) != 0) mpf_sub_ui (u, v, 1); mpf_sub (w, u, v); refmpf_sub (wref, u, v); mpf_reldiff (rerr, w, wref); 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 (" u = "); mpf_dump (u); printf (" v = "); mpf_dump (v); printf ("wref = "); mpf_dump (wref); printf (" w = "); mpf_dump (w); abort (); } } } mpf_clear (limit_rerr); mpf_clear (rerr); mpf_clear (max_rerr); mpf_clear (u); mpf_clear (v); mpf_clear (w); mpf_clear (wref); gmp_randclear(rands); }
int main (int argc, char *argv[]) { int type = 'z'; int base = 0; mp_size_t prec = 64; int obase, opt, i, ret; while ((opt = getopt (argc, argv, "b:fp:qrz")) != EOF) { switch (opt) { case 'f': case 'q': case 'r': case 'z': type = opt; break; case 'b': base = atoi (optarg); break; case 'p': prec = atoi (optarg); break; case '?': default: abort (); } } obase = (base == 0 ? 10 : base); if (optind >= argc) { printf ("Usage: %s [-z] [-q] [-f] [-r] [-p prec] [-b base] expression...\n", argv[0]); exit (1); } switch (type) { case 'z': default: { mpz_t res, foo, bar; mpz_init (res); mpz_init_set_ui (foo, 55L); mpz_init_set_ui (bar, 99L); for (i = optind; i < argc; i++) TRY (mpz_expr, mpz_out_str (stdout, obase, res), argv[i]); mpz_clear (res); mpz_clear (foo); mpz_clear (bar); } break; case 'q': { mpq_t res, foo, bar; mpq_init (res); mpq_init (foo); mpq_init (bar); mpq_set_ui (foo, 55L, 1); mpq_set_ui (bar, 99L, 1); for (i = optind; i < argc; i++) TRY (mpq_expr, mpq_out_str (stdout, obase, res), argv[i]); mpq_clear (res); mpq_clear (foo); mpq_clear (bar); } break; case 'f': { mpf_t res, foo, bar; mpf_init2 (res, prec); mpf_init_set_ui (foo, 55L); mpf_init_set_ui (bar, 99L); for (i = optind; i < argc; i++) TRY (mpf_expr, mpf_out_str (stdout, obase, 0, res), argv[i]); mpf_clear (res); mpf_clear (foo); mpf_clear (bar); } break; case 'r': #if HAVE_MPFR { mpfr_t res, foo, bar; mpfr_init2 (res, prec); mpfr_init_set_ui (foo, 55L, GMP_RNDZ); mpfr_init_set_ui (bar, 99L, GMP_RNDZ); for (i = optind; i < argc; i++) TRY (mpfr_expr, mpfr_out_str (stdout, obase, 0, res, GMP_RNDZ), argv[i]); mpfr_clear (res); mpfr_clear (foo); mpfr_clear (bar); } #else printf ("mpfr not compiled in\n"); exit (1); #endif break; } return 0; }
void check_rand1 (int argc, char **argv) { mp_size_t size; mp_exp_t exp; int reps = 20000; int i; mpf_t x, y, y2; mp_size_t bprec = 100; mpf_t rerr, max_rerr, limit_rerr; 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 (y2); for (i = 0; i < reps; i++) { size = urandom () % SIZE; exp = urandom () % SIZE; mpf_random2 (x, size, exp); mpf_sqrt (y, x); MPF_CHECK_FORMAT (y); mpf_mul (y2, y, y); mpf_reldiff (rerr, x, y2); 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 (" x = "); mpf_dump (x); printf (" y = "); mpf_dump (y); printf (" y2 = "); mpf_dump (y2); printf (" rerr = "); mpf_dump (rerr); printf (" limit_rerr = "); mpf_dump (limit_rerr); printf ("in hex:\n"); mp_trace_base = 16; mpf_trace (" x ", x); mpf_trace (" y ", y); mpf_trace (" y2 ", y2); mpf_trace (" rerr ", rerr); mpf_trace (" limit_rerr", limit_rerr); abort (); } } } mpf_clear (limit_rerr); mpf_clear (rerr); mpf_clear (max_rerr); mpf_clear (x); mpf_clear (y); mpf_clear (y2); }
void jarvis_hypercube_approximation (int C, /* Number of types of customers*/ int N, /* Number of servers */ mpf_t *lambda, /* arrive rate according to a Poisson process per type */ mpf_t **Tao, /* expected service time for service i and customer of node m */ int **a, /* for customers of type m, the list of preferred servers */ mpf_t **f) { mpf_t Lambda; mpf_t Rho; mpf_t tao; mpf_t *rho; mpf_t *new_rho; mpf_t *Q_N_rho; mpf_t max_change; mpf_t tmp; mpf_init(Lambda); rho = new mpf_t [N]; for (int i = 0; i < N;i++) mpf_init(rho[i]); mpf_init(tao); mpf_init_set_ui(P__0,1); mpf_init_set_ui(P__N,1); Q_N_rho = new mpf_t [N]; for (int i = 0;i < N;i++) mpf_init(Q_N_rho[i]); new_rho = new mpf_t [N]; for (int i = 0;i < N;i++) mpf_init(new_rho[i]); mpf_init(Rho); mpf_init(max_change); mpf_init(tmp); /* INITIALIZE: */ for (int m = 0;m < C;m++) mpf_add(Lambda,Lambda,lambda[m]); /* Busy probability rho_i */ for (int i = 0; i < N;i++) { for (int m = 0; m < C;m++) { if (a[m][0] == i) { mpf_mul(tmp,lambda[m],Tao[i][m]); mpf_add(rho[i],rho[i],tmp); } } } /* mean service time 'tao' */ for (int m = 0;m < C;m++) { mpf_mul(tmp,lambda[m],Tao[a[m][0]][m]); mpf_add(tao,tao,tmp); } mpf_div(tao,tao,Lambda); /* Define intial values for P__0 and P__N */ for (int i = 0;i < N;i++) { mpf_ui_sub(tmp,1,rho[i]); mpf_mul(P__0,P__0,tmp); } for (int i = 0;i < N;i++) mpf_mul(P__N,P__N,rho[i]); /* ITERATION: */ do { /* cout << "'rho_i':"; for (int i = 0;i < N;i++) cout << " " << mpf_get_d(rho[i]); cout << endl; */ /* traffic intensity */ mpf_mul(Rho,Lambda,tao); mpf_div_ui(Rho,Rho,N); /* Compute Q(N,'rho',k) */ for (int k = 0;k < N;k++) correction_factor_Q(Q_N_rho[k],N,Rho,k); /* Compute f_{im} */ mpf_t rho_a_ml; mpf_init(rho_a_ml); for (int i = 0;i < N;i++) { for (int m = 0;m < C;m++) { mpf_set_ui(rho_a_ml,1); for (int k = 0;k < N;k++) { if (a[m][k] == i) { mpf_ui_sub(tmp,1,rho[i]); mpf_mul(tmp,tmp,rho_a_ml); mpf_mul(f[i][m],tmp,Q_N_rho[k]); break; } mpf_mul(rho_a_ml,rho_a_ml,rho[a[m][k]]); } } } mpf_clear(rho_a_ml); /* Aproximation of 'rho'_i */ mpf_t Vi; mpf_init(Vi); mpf_init(rho_a_ml); for (int i = 0;i < N;i++) { mpf_set_ui(Vi,0); for (int m = 0;m < C;m++) { mpf_set_ui(rho_a_ml,1); for (int k = 0;k < N;k++) { if (a[m][k] == i) { mpf_mul(tmp,lambda[m],Tao[i][m]); mpf_mul(tmp,tmp,Q_N_rho[k]); mpf_mul(tmp,tmp,rho_a_ml); mpf_add(Vi,Vi,tmp); /* Vi += lambda[m] * Tao[i][m] * Q_N_rho[k] * rho_a_ml */ break; } mpf_mul(rho_a_ml,rho_a_ml,rho[a[m][k]]); } } mpf_add_ui(tmp,Vi,1); mpf_div(new_rho[i],Vi,tmp); } mpf_clear(rho_a_ml); mpf_clear(Vi); /* Convergence criterion */ mpf_set_ui(max_change,0); for (int i = 0;i < N;i++) { mpf_sub(tmp,rho[i],new_rho[i]); mpf_abs(tmp,tmp); if (mpf_cmp(tmp,max_change) > 0) mpf_set(max_change,tmp); } /* cout << "max change = " << mpf_get_d(max_change); if (mpf_cmp_d(max_change,JARVIS_EPSILON) < 0) cout << " **STOP**" << endl; else cout << "\r"; */ if (mpf_cmp_d(max_change,JARVIS_EPSILON) < 0) break; /* STOP */ for (int i = 0;i < N;i++) mpf_set(rho[i],new_rho[i]); /* Compute P__0 */ mpf_set_ui(P__0,1); for (int i = 0;i < N;i++) { mpf_ui_sub(tmp,1,rho[i]); mpf_mul(P__0,P__0,tmp); } /* Compute P__N */ mpf_t s_rho; mpf_init(s_rho); for (int i = 0;i < N;i++) mpf_add(s_rho,s_rho,rho[i]); mpf_div_ui(tmp,s_rho,N); mpf_div(tmp,tmp,Rho); mpf_ui_sub(P__N,1,tmp); /* Compute mean service time 'tao' */ mpf_t aux; mpf_set_ui(tao,0); mpf_init(aux); for (int m = 0;m < C;m++) { mpf_set_ui(tmp,0); for (int i = 0;i < N;i++) { mpf_mul(aux,Tao[i][m],f[i][m]); mpf_add(tmp,tmp,aux); } mpf_mul(tmp,lambda[m],tmp); mpf_add(tao,tao,tmp); } mpf_div(tao,tao,Lambda); // / Lambda mpf_ui_sub(aux,1,P__N); // 1 - P__N mpf_div(tao,tao,aux); // / (1 - P__N) mpf_clear(aux); } while (1); /* cout << "finsh jarvis" << endl; for (int i = 0;i < N;i++) { for (int m = 0;m < C;m++) { cout << mpf_get_d(f[i][m]) << " "; } cout << endl; } */ mpf_clear(tmp); mpf_clear(max_change); mpf_clear(Rho); for (int i = 0;i < N;i++) mpf_clear(new_rho[i]); delete [] new_rho; for (int i = 0;i < N;i++) mpf_clear(Q_N_rho[i]); delete [] Q_N_rho; mpf_clear(P__N); mpf_clear(P__0); mpf_clear(tao); for (int i = 0;i < N;i++) mpf_clear(rho[i]); delete [] rho; mpf_clear(Lambda); }
int main (int argc, char **argv) { mp_size_t size; mp_exp_t exp; int reps = 20000; int i; mpf_t u, v, w, wref; mp_size_t bprec = 100; mpf_t rerr, max_rerr, limit_rerr; 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 (u); mpf_init (v); mpf_init (w); mpf_init (wref); for (i = 0; i < reps; i++) { size = urandom () % (2 * SIZE) - SIZE; exp = urandom () % SIZE; mpf_random2 (u, size, exp); size = urandom () % (2 * SIZE) - SIZE; exp = urandom () % SIZE; mpf_random2 (v, size, exp); mpf_add (w, u, v); refmpf_add (wref, u, v); mpf_reldiff (rerr, w, wref); 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 (" u = "); mpf_dump (u); printf (" v = "); mpf_dump (v); printf ("wref = "); mpf_dump (wref); printf (" w = "); mpf_dump (w); abort (); } } } mpf_clear (limit_rerr); mpf_clear (rerr); mpf_clear (max_rerr); mpf_clear (u); mpf_clear (v); mpf_clear (w); mpf_clear (wref); tests_end (); exit (0); }
// // The core Gauss-Legendre routine. // On input, 'bits' is the desired precision in bits. // On output, 'pi' contains the calculated value. // static void calculatePi( unsigned bits, mpf_t pi) { mpf_t lastPi; mpf_t scratch; // variables per the formal Gauss-Legendre formulae mpf_t a; mpf_t b; mpf_t t; mpf_t x; mpf_t y; unsigned p = 1; mpf_init_set_ui(lastPi, 0); mpf_init(x); mpf_init(y); mpf_init(scratch); // initial conditions mpf_init_set_ui(a, 1); // a := 1 mpf_init(b); // b := 1 / sqrt(2) mpf_sqrt_ui(b, 2); mpf_ui_div(b, 1, b); mpf_init_set_ui(t, 4); // t := 1/4 mpf_ui_div(t, 1, t); for(;;) { // x := (a+b)/2 mpf_add(x, a, b); mpf_div_ui(x, x, 2); // y := sqrt(a*b) mpf_mul(y, a, b); mpf_sqrt(y, y); // t := t - p * (a-x)**2 mpf_sub(scratch, a, x); mpf_pow_ui(scratch, scratch, 2); mpf_mul_ui(scratch, scratch, p); mpf_sub(t, t, scratch); // a := x // b := y // p := 2p mpf_set(a, x); mpf_set(b, y); p <<= 1; // pi := ((a + b)**2) / 4t mpf_add(pi, a, b); mpf_pow_ui(pi, pi, 2); mpf_mul_ui(scratch, t, 4); mpf_div(pi, pi, scratch); // if pi == lastPi, within the requested precision, we're done if(mpf_eq(pi, lastPi, bits)) { break; } mpf_set(lastPi, pi); } // free memory associated with mpf_t's we allocated mpf_clear(a); mpf_clear(b); mpf_clear(t); mpf_clear(x); mpf_clear(y); mpf_clear(lastPi); mpf_clear(scratch); }