void mpfc_out_str(FILE * stream,int base, size_t n_digits,mpfc_ptr x) { std::cout<<"("; mpf_out_str(stream,base,n_digits,x->Re); std::cout<<","; mpf_out_str(stream,base,n_digits,x->Im); std::cout<<")"; }
void check_data (void) { static const struct { int base; const char *f; long want; } data[] = { { 10, "0", 0L }, { 10, "1", 1L }, { 10, "-1", -1L }, { 10, "2", 2L }, { 10, "-2", -2L }, { 10, "12345", 12345L }, { 10, "-12345", -12345L }, /* fraction bits ignored */ { 10, "0.5", 0L }, { 10, "-0.5", 0L }, { 10, "1.1", 1L }, { 10, "-1.1", -1L }, { 10, "1.9", 1L }, { 10, "-1.9", -1L }, { 16, "1.000000000000000000000000000000000000000000000000001", 1L }, { 16, "-1.000000000000000000000000000000000000000000000000001", -1L }, /* low bits extracted (this is undocumented) */ { 16, "1000000000000000000000000000000000000000000000000001", 1L }, { 16, "-1000000000000000000000000000000000000000000000000001", -1L }, }; int i; mpf_t f; long got; mpf_init2 (f, 2000L); for (i = 0; i < numberof (data); i++) { mpf_set_str_or_abort (f, data[i].f, data[i].base); got = mpf_get_si (f); if (got != data[i].want) { printf ("mpf_get_si wrong at data[%d]\n", i); printf (" f \"%s\"\n", data[i].f); printf (" dec "); mpf_out_str (stdout, 10, 0, f); printf ("\n"); printf (" hex "); mpf_out_str (stdout, 16, 0, f); printf ("\n"); printf (" size %ld\n", (long) SIZ(f)); printf (" exp %ld\n", (long) EXP(f)); printf (" got %ld (0x%lX)\n", got, got); printf (" want %ld (0x%lX)\n", data[i].want, data[i].want); abort(); } } mpf_clear (f); }
/** * @brief Print a float to stdout (or whatever the output stream is * atm) respecting the given options, and only with the significant * digits. * * @param s A pointer to the current mps_context. * @param f The float approximation that should be printed. * @param rad The current inclusion radius for that approximation. * @param out_digit The number of output digits required. * @param sign The sign of the approximation. */ MPS_PRIVATE void mps_outfloat (mps_context * s, mpf_t f, rdpe_t rad, long out_digit, mps_boolean sign) { mpf_t t; rdpe_t r, ro; double d; long l, digit, true_digit; if (s->output_config->format == MPS_OUTPUT_FORMAT_FULL) { mpf_init2 (t, mpf_get_prec (f)); mpf_set (t, f); mpf_out_str (s->outstr, 10, 0, t); mpf_clear (t); return; } mpf_init2 (t, s->output_config->prec); mpf_get_rdpe (ro, f); if (s->output_config->format == MPS_OUTPUT_FORMAT_GNUPLOT || s->output_config->format == MPS_OUTPUT_FORMAT_GNUPLOT_FULL) rdpe_out_str_u (s->outstr, ro); else { rdpe_abs_eq (ro); if (rdpe_ne (ro, rdpe_zero)) rdpe_div (r, rad, ro); else rdpe_set_d (r, 1.0e-10); digit = (long)(-rdpe_log10 (r) - 0.5); if (digit <= 0) { rdpe_get_dl (&d, &l, ro); fprintf (s->outstr, "0.e%ld", l); } else { true_digit = (long)(LOG10_2 * mpf_get_prec (f)); true_digit = MIN (digit, true_digit); true_digit = MIN (true_digit, out_digit); if (sign) mpf_set (t, f); else mpf_abs (t, f); mpf_out_str (s->outstr, 10, true_digit, t); } } mpf_clear (t); }
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); }
void print(char *s, mpf_t x){ printf("%s: ", s); mpf_out_str (stdout, 10, 1000, x); printf("\n"); fflush(stdout); }
int main( int argc, char** argv ) { unsigned bits; double start, end; if ( argc < 2 ) { usage( argv ); } bits = atoi( argv[1] ); if ( bits == 0 ) { usage( argv ); } printf( "Calculating pi..." ); fflush( stdout ); start = currentTime( ); mpf_set_default_prec( bits ); mpf_class pi; calculatePi( bits, pi ); end = currentTime( ); printf( "\npi = " ); mpf_out_str( stdout, 10, 0, pi.get_mpf_t( ) ); printf( "\n" ); printf( "total elapsed time : %.2f seconds\n", end - start ); return 0; }
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 filePrint() * * Descricao: * Imprime a cada iteracao o valor do "pi" num arquivo de nome "gauss_legendre_sequencial_X.txt", onde * X é o numero da iteracao, comecando em 1. * * Parametros de entrada: * - * * Parametros de retorno: * - */ void filePrint(){ char filename[30]; sprintf(filename, "gauss_legendre_sequencial_%ld.txt", n_count+1); FILE *output = fopen(filename, "w"); mpf_out_str(output, 10, 0, pi[n_count]); fclose(output); }
void print_vector_coordinate(FILE *OUT, int digits, complex_vector z) /***************************************************************\ * USAGE: prints z * \***************************************************************/ { int i, size = z->size, base = 10; if (size > 0) { // print each coordinate for (i = 0; i < size; i++) { mpf_out_str(OUT, base, digits, z->coord[i]->re); fprintf(OUT, " "); mpf_out_str(OUT, base, digits, z->coord[i]->im); fprintf(OUT, "\n"); } } return; }
void print_number(FILE *OUT, int digits, complex_number z) /***************************************************************\ * USAGE: prints z to OUT * \***************************************************************/ { int base = 10; // verify that z is a valid number if (mpfr_number_p(z->re) && mpfr_number_p(z->im)) { // print to OUT mpf_out_str(OUT, base, digits, z->re); if (mpfr_sgn(z->im) >= 0) fprintf(OUT, "+"); mpf_out_str(OUT, base, digits, z->im); fprintf(OUT, "*I"); } else fprintf(OUT, "NaN+NaN*I"); return; }
void gmp_print_mpq(FILE* fp, const mpq_t qval) { mpf_t fval; mpf_init(fval); mpf_set_q(fval, qval); mpf_out_str(fp, 10, 32, fval); fprintf(fp, " = "); mpq_out_str(fp, 10, qval); fputc('\n', fp); mpf_clear(fval); }
void x2 (mpf_t V, /* result */ unsigned long int X[], /* data */ unsigned int k, /* #of categories */ void (P) (mpf_t, unsigned long int, void *), /* probability func */ void *x, /* extra user data passed to P() */ unsigned long int n) /* #of samples */ { unsigned int f; mpf_t f_t, f_t2; /* temp floats */ mpf_init (f_t); mpf_init (f_t2); mpf_set_ui (V, 0); for (f = 0; f < k; f++) { if (g_debug > DEBUG_2) fprintf (stderr, "%u: P()=", f); mpf_set_ui (f_t, X[f]); mpf_mul (f_t, f_t, f_t); /* f_t = X[f]^2 */ P (f_t2, f, x); /* f_t2 = Pr(f) */ if (g_debug > DEBUG_2) mpf_out_str (stderr, 10, 2, f_t2); mpf_div (f_t, f_t, f_t2); mpf_add (V, V, f_t); if (g_debug > DEBUG_2) { fprintf (stderr, "\tV="); mpf_out_str (stderr, 10, 2, V); fprintf (stderr, "\t"); } } if (g_debug > DEBUG_2) fprintf (stderr, "\n"); mpf_div_ui (V, V, n); mpf_sub_ui (V, V, n); mpf_clear (f_t); mpf_clear (f_t2); }
void gmp_monte_carlo_par (int numthr, int numpts) { /*Para realizar o metodo Monte Carlo foi utilizado uma circunferencia de raio igual a 1 (para facilitar a comparacao que verifica se o ponto esta dentro da circunferencia) inscrita em uma quadrado de lado igual a 2. Foi considerado apenas o primeiro quadrante, pois assim as coordenadas dos pontos variam de 0 a 1. */ int i =0; mpf_t pi; mpf_t intern_points, total_points, ratio; pthread_t tabela_thr[1000]; /*Variavel que armazena quantos pontos vao ficar dentro da circunferencia*/ /*A precisao padrao sera no minimo 10.000.000*log10.*/ mpf_set_default_prec (34000000); /*O objeto mpf_t deve ser inicializado antes de receber algum valor.*/ mpf_init(pi),mpf_init(intern_points),mpf_init(total_points),mpf_init(ratio); /* Inicializa a semente para calcular o valor aleatorio*/ srand (time(NULL)); /* Para uma quantidade de quase 1 bi de pontos*/ i=0; if(numthr>1000) numthr=1000; while(i<numthr) { //printf("\nIniciando Thread: %d", i); mpf_init(param[i].intern_points); mpf_init(param[i].total_points); param[i].numpoints=numpts; pthread_create(&tabela_thr[i], NULL, (void*)&montecarlo_thr, (void*) & param[i]); i++; } i=0; while(i<numthr){ pthread_join(tabela_thr[i], NULL); mpf_add(intern_points,intern_points,param[i].intern_points); mpf_add(total_points,total_points,param[i].total_points); i++; } /*calculando o valor de pi pi = 4* (double) intern_points /total_points */ mpf_div (ratio,intern_points,total_points); /*ratio = intern_points / total_points*/ mpf_mul_ui (pi,ratio,4); /* pi = 4*ratio*/ mpf_out_str (stdout,10,10000000,pi); }
/* Print "name=value\n" to stdout for an mpf_t value. */ void mpf_trace (const char *name, mpf_srcptr f) { mp_trace_start (name); if (f == NULL) { printf ("NULL\n"); return; } mpf_out_str (stdout, ABS (mp_trace_base), 0, f); printf ("\n"); }
ats_void_type atslib_mpf_out_str_exn ( ats_ptr_type file , ats_int_type base , ats_size_type ndigit , const ats_mpf_ptr_type x ) { size_t n = mpf_out_str((FILE*)file, base, ndigit, (mpf_ptr)x) ; if (n == 0) { ats_exit_errmsg (1, "exit(ATS): [mpf_out_str] failed.\n") ; } // end of [if] return ; } // end of [atslib_mpf_out_str_exn]
void poly_f::print() { if(this->size()==0) { std::cout<<"0\n"; return ; } std::cout<<"("; for(size_t i=0;i<this->size();i++) { mpf_out_str(0,10,FC_DEFAULT_PREC,this->rep[i]); std::cout<<" "; } std::cout<<")\n"; return ; }
// 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(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; }
int main(void) { clock_t begin, end; mpf_set_default_prec(BITS_PER_DIGIT*DIGITS); //mpf_set_default_prec(4096); begin = clock(); mpf_init(x); mpf_init(y); mpf_init(p); mpf_init(aux1); mpf_init(aux2); mpf_init(sqrtx); mpf_init(invsqrtx); /* x = sqrt(2)*/ mpf_set_ui(x, 2); mpf_sqrt(x, x); /* y = sqrt(sqrt(2)) = sqrt(x)*/ mpf_sqrt(y, x); /* p = 2 + sqrt(2) = 2 + x*/ mpf_add_ui(p, x, 2); for (i=0; i<24; i++) { mpf_sqrt(sqrtx, x); mpf_ui_div(invsqrtx, 1, sqrtx); pthread_create(&t1, NULL, thread1, NULL); pthread_create(&t2, NULL, thread2, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); mpf_div(p, aux1, aux2); //Para ver os valores de pi a cada iteracao //mpf_out_str(stdout, 10, DIGITS, p); } mpf_out_str(stdout, 10, DIGITS, p); mpf_clear(x); mpf_clear(y); mpf_clear(p); mpf_clear(aux1); mpf_clear(aux2); mpf_clear(sqrtx); mpf_clear(invsqrtx); end = clock(); printf("Took %lfs\n", (double)(end-begin)/CLOCKS_PER_SEC); pthread_exit(0); }
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); } }
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(void){ /*inicializa as variaveis*/ int cont, numThread[NUMTHREADS]; mpf_set_default_prec((long)(DIGITS*BITS_PER_DIGIT+16)); mpf_t PI, dentroCircunferencia, TOTAL; pthread_t tID[NUMTHREADS]; // ID das threads mpf_init(PI); mpf_init_set_d(dentroCircunferencia, 0.0); mpf_init_set_d(TOTAL, 0.0); srand48(time(NULL)); // Para todas as threads, cria a i-esima thread for (cont = 0; cont< NUMTHREADS ; cont++){ numThread[cont] = cont; pthread_create (&tID[cont], NULL, calcula, &numThread[cont]); } // Para cada thread, espera que as threads terminem for (cont = 0; cont< NUMTHREADS ; cont++) pthread_join (tID[cont], NULL); //Para cada thread, soma a sua parcela na conta total for (cont = 0; cont< NUMTHREADS ; cont++){ mpf_add_ui(TOTAL, TOTAL, TOTALParcial[cont]); mpf_add_ui(dentroCircunferencia, dentroCircunferencia, dentroCircunferenciaParcial[cont]); } mpf_div (PI, dentroCircunferencia, TOTAL); mpf_mul_ui (PI, PI, 4); mpf_out_str(stdout, 10, DIGITS, PI); printf("\n"); mpf_clear (PI); mpf_clear (TOTAL); mpf_clear (dentroCircunferencia); 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; }
int main (int argc, char **argv) { #if GMP_NAIL_BITS == 0 static const struct { int f_base; const char *f; int z_base; const char *want_num; const char *want_den; } data[] = { { -2, "0", 16, "0", "1" }, { -2, "1", 16, "1", "1" }, { -2, "1@1", 16, "2", "1" }, { -2, "1@2", 16, "4", "1" }, { -2, "1@3", 16, "8", "1" }, { -2, "1@30", 16, "40000000", "1" }, { -2, "1@31", 16, "80000000", "1" }, { -2, "1@32", 16, "100000000", "1" }, { -2, "1@33", 16, "200000000", "1" }, { -2, "1@34", 16, "400000000", "1" }, { -2, "1@62", 16, "4000000000000000", "1" }, { -2, "1@63", 16, "8000000000000000", "1" }, { -2, "1@64", 16, "10000000000000000", "1" }, { -2, "1@65", 16, "20000000000000000", "1" }, { -2, "1@66", 16, "40000000000000000", "1" }, { -2, "1@126", 16, "40000000000000000000000000000000", "1" }, { -2, "1@127", 16, "80000000000000000000000000000000", "1" }, { -2, "1@128", 16, "100000000000000000000000000000000", "1" }, { -2, "1@129", 16, "200000000000000000000000000000000", "1" }, { -2, "1@130", 16, "400000000000000000000000000000000", "1" }, { -2, "1@-1", 16, "1", "2" }, { -2, "1@-2", 16, "1", "4" }, { -2, "1@-3", 16, "1", "8" }, { -2, "1@-30", 16, "1", "40000000" }, { -2, "1@-31", 16, "1", "80000000" }, { -2, "1@-32", 16, "1", "100000000" }, { -2, "1@-33", 16, "1", "200000000" }, { -2, "1@-34", 16, "1", "400000000" }, { -2, "1@-62", 16, "1", "4000000000000000" }, { -2, "1@-63", 16, "1", "8000000000000000" }, { -2, "1@-64", 16, "1", "10000000000000000" }, { -2, "1@-65", 16, "1", "20000000000000000" }, { -2, "1@-66", 16, "1", "40000000000000000" }, { -2, "1@-126", 16, "1", "40000000000000000000000000000000" }, { -2, "1@-127", 16, "1", "80000000000000000000000000000000" }, { -2, "1@-128", 16, "1", "100000000000000000000000000000000" }, { -2, "1@-129", 16, "1", "200000000000000000000000000000000" }, { -2, "1@-130", 16, "1", "400000000000000000000000000000000" }, { -2, "1@-30", 16, "1", "40000000" }, { -2, "1@-31", 16, "1", "80000000" }, { -2, "1@-32", 16, "1", "100000000" }, { -2, "1@-33", 16, "1", "200000000" }, { -2, "1@-34", 16, "1", "400000000" }, { -2, "11@-62", 16, "3", "4000000000000000" }, { -2, "11@-63", 16, "3", "8000000000000000" }, { -2, "11@-64", 16, "3", "10000000000000000" }, { -2, "11@-65", 16, "3", "20000000000000000" }, { -2, "11@-66", 16, "3", "40000000000000000" }, { 16, "80000000.00000001", 16, "8000000000000001", "100000000" }, { 16, "80000000.00000008", 16, "1000000000000001", "20000000" }, { 16, "80000000.8", 16, "100000001", "2" }, }; mpf_t f; mpq_t got; mpz_t want_num, want_den; int i, neg; tests_start (); mpf_init2 (f, 1024L); mpq_init (got); mpz_init (want_num); mpz_init (want_den); for (i = 0; i < numberof (data); i++) { for (neg = 0; neg <= 1; neg++) { mpf_set_str_or_abort (f, data[i].f, data[i].f_base); mpz_set_str_or_abort (want_num, data[i].want_num, data[i].z_base); mpz_set_str_or_abort (want_den, data[i].want_den, data[i].z_base); if (neg) { mpf_neg (f, f); mpz_neg (want_num, want_num); } mpq_set_f (got, f); MPQ_CHECK_FORMAT (got); if (mpz_cmp (mpq_numref(got), want_num) != 0 || mpz_cmp (mpq_denref(got), want_den) != 0) { printf ("wrong at data[%d]\n", i); printf (" f_base %d, z_base %d\n", data[i].f_base, data[i].z_base); printf (" f \"%s\" hex ", data[i].f); mpf_out_str (stdout, 16, 0, f); printf ("\n"); printf (" want num 0x"); mpz_out_str (stdout, 16, want_num); printf ("\n"); printf (" want den 0x"); mpz_out_str (stdout, 16, want_den); printf ("\n"); printf (" got num 0x"); mpz_out_str (stdout, 16, mpq_numref(got)); printf ("\n"); printf (" got den 0x"); mpz_out_str (stdout, 16, mpq_denref(got)); printf ("\n"); abort (); } } } mpf_clear (f); mpq_clear (got); mpz_clear (want_num); mpz_clear (want_den); tests_end (); #endif exit (0); }
int main(int argc,char *argv[]) { mpf_t pi,qi,ci; mpz_t pstack,qstack,gstack; long d=100,out=0,threads=1,depth,psize,qsize; double begin, mid0, mid3, mid4, end; double wbegin, wmid0, wmid3, wmid4, wend; prog_name = argv[0]; if (argc==1) { fprintf(stderr,"\nSyntax: %s <digits> <option> <threads>\n",prog_name); fprintf(stderr," <digits> digits of pi to output\n"); fprintf(stderr," <option> 0 - just run (default)\n"); fprintf(stderr," 1 - output digits\n"); fprintf(stderr," <threads> number of threads (default 1)\n"); exit(1); } if (argc>1) d = strtoul(argv[1],0,0); if (argc>2) out = atoi(argv[2]); if (argc>3) threads = atoi(argv[3]); terms = d/DIGITS_PER_ITER; depth = 0; while ((1L<<depth)<terms) depth++; depth++; fprintf(stderr,"#terms=%ld, depth=%ld, threads=%ld cores=%d\n", terms, depth, threads, get_nprocs()); begin = cpu_time(); wbegin = wall_clock(); mpz_init(pstack); mpz_init(qstack); mpz_init(gstack); /* begin binary splitting process */ if (terms<=0) { mpz_set_ui(pstack,1); mpz_set_ui(qstack,0); mpz_set_ui(gstack,1); } else { #ifdef _OPENMP #pragma omp parallel num_threads(threads) #pragma omp single nowait { bs(0,terms,1,pstack,qstack,gstack); } #else bs(0,terms,1,pstack,qstack,gstack); #endif } mid0 = cpu_time(); wmid0 = wall_clock(); fprintf(stderr,"bs cputime = %6.2f wallclock = %6.2f factor = %6.1f\n", mid0-begin,wmid0-wbegin,(mid0-begin)/(wmid0-wbegin)); fflush(stderr); mpz_clear(gstack); /* prepare to convert integers to floats */ mpf_set_default_prec((long)(d*BITS_PER_DIGIT+16)); /* p*(C/D)*sqrt(C) pi = ----------------- (q+A*p) */ psize = mpz_sizeinbase(pstack,10); qsize = mpz_sizeinbase(qstack,10); mpz_addmul_ui(qstack,pstack,A); mpz_mul_ui(pstack,pstack,C/D); mpf_init(pi); mpf_set_z(pi,pstack); mpz_clear(pstack); mpf_init(qi); mpf_set_z(qi,qstack); mpz_clear(qstack); /* final step */ mid3 = cpu_time(); wmid3 = wall_clock(); #ifdef _OPENMP #pragma omp parallel num_threads(threads) #pragma omp single nowait { #pragma omp task shared(qi,pi) { mpf_div(qi,pi,qi); mpf_clear(pi); } #pragma omp task shared(ci) { mpf_init(ci); mpf_sqrt_ui(ci,C); } #pragma omp taskwait } #else mpf_div(qi, pi, qi); mpf_clear(pi); mpf_init(ci); mpf_sqrt_ui(ci, C); #endif mid4 = cpu_time(); wmid4 = wall_clock(); fprintf(stderr,"div/sqrt cputime = %6.2f wallclock = %6.2f factor = %6.1f\n", mid4-mid3,wmid4-wmid3,(mid4-mid3)/(wmid4-wmid3)); mpf_mul(qi,qi,ci); mpf_clear(ci); end = cpu_time(); wend = wall_clock(); fprintf(stderr,"mul cputime = %6.2f wallclock = %6.2f factor = %6.1f\n", end-mid4,wend-wmid4,(end-mid4)/(wend-wmid4)); fprintf(stderr,"total cputime = %6.2f wallclock = %6.2f factor = %6.1f\n", end-begin,wend-wbegin,(end-begin)/(wend-wbegin)); fflush(stderr); fprintf(stderr," P size=%ld digits (%f)\n" " Q size=%ld digits (%f)\n", psize, (double)psize/d, qsize, (double)qsize/d); /* output Pi and timing statistics */ if (out&1) { fprintf(stdout,"pi(0,%ld)=\n", terms); mpf_out_str(stdout,10,d,qi); fprintf(stdout,"\n"); } /* free float resources */ mpf_clear(qi); exit (0); }
int main (void) { mpf_t x; mpfr_t y, z; unsigned long i; mpfr_exp_t e; int inex; tests_start_mpfr (); mpfr_init (y); mpfr_init (z); mpf_init (x); i = 1; while (i) { mpfr_set_ui (y, i, MPFR_RNDN); if (mpfr_get_f (x, y, MPFR_RNDN) != 0 || mpf_cmp_ui (x, i)) { printf ("Error: mpfr_get_f(%lu) fails\n", i); exit (1); } if (i <= - (unsigned long) LONG_MIN) { long j = i < - (unsigned long) LONG_MIN ? - (long) i : LONG_MIN; mpfr_set_si (y, j, MPFR_RNDN); if (mpfr_get_f (x, y, MPFR_RNDN) != 0 || mpf_cmp_si (x, j)) { printf ("Error: mpfr_get_f(-%lu) fails\n", i); exit (1); } } i *= 2; } /* same tests, but with a larger precision for y, which requires to round it */ mpfr_set_prec (y, 100); i = 1; while (i) { mpfr_set_ui (y, i, MPFR_RNDN); inex = mpfr_get_f (x, y, MPFR_RNDN); if (! SAME_SIGN (inex, - mpfr_cmp_f (y, x)) || mpf_cmp_ui (x, i)) { printf ("Error: mpfr_get_f(%lu) fails\n", i); exit (1); } mpfr_set_si (y, (signed long) -i, MPFR_RNDN); inex = mpfr_get_f (x, y, MPFR_RNDN); if (! SAME_SIGN (inex, - mpfr_cmp_f (y, x)) || mpf_cmp_si (x, (signed long) -i)) { printf ("Error: mpfr_get_f(-%lu) fails\n", i); exit (1); } i *= 2; } /* bug reported by Jim White */ for (e = 0; e <= 2 * GMP_NUMB_BITS; e++) { /* test with 2^(-e) */ mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_div_2exp (y, y, e, MPFR_RNDN); inex = mpfr_get_f (x, y, MPFR_RNDN); mpf_mul_2exp (x, x, e); if (inex != 0 || mpf_cmp_ui (x, 1) != 0) { printf ("Error: mpfr_get_f(x,y,MPFR_RNDN) fails\n"); printf ("y="); mpfr_dump (y); printf ("x="); mpf_div_2exp (x, x, e); mpf_out_str (stdout, 2, 0, x); exit (1); } /* test with 2^(e) */ mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_mul_2exp (y, y, e, MPFR_RNDN); inex = mpfr_get_f (x, y, MPFR_RNDN); mpf_div_2exp (x, x, e); if (inex != 0 || mpf_cmp_ui (x, 1) != 0) { printf ("Error: mpfr_get_f(x,y,MPFR_RNDN) fails\n"); printf ("y="); mpfr_dump (y); printf ("x="); mpf_mul_2exp (x, x, e); mpf_out_str (stdout, 2, 0, x); exit (1); } } /* Bug reported by Yury Lukach on 2006-04-05 */ mpfr_set_prec (y, 32); mpfr_set_prec (z, 32); mpf_set_prec (x, 32); mpfr_set_ui_2exp (y, 0xc1234567, -30, MPFR_RNDN); mpfr_get_f (x, y, MPFR_RNDN); inex = mpfr_set_f (z, x, MPFR_RNDN); if (inex != 0 || ! mpfr_equal_p (y, z)) { printf ("Error in mpfr_get_f:\n inex = %d, y = ", inex); mpfr_dump (z); printf ("Expected:\n inex = 0, y = "); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (z); mpf_clear (x); special_test (); prec_test (); ternary_test (); tests_end_mpfr (); return 0; }
void check_data (void) { static const struct { int a_base; const char *a; const char *b; int want; } data[] = { { 10, "0", "1", -1 }, { 10, "0", "0", 0 }, { 10, "0", "-1", 1 }, { 10, "1", "1", 0 }, { 10, "1", "0", 1 }, { 10, "1", "-1", 1 }, { 10, "-1", "1", -1 }, { 10, "-1", "0", -1 }, { 10, "-1", "-1", 0 }, { 16, "0", "-0x80000000", 1 }, { 16, "80000000", "-0x80000000", 1 }, { 16, "80000001", "-0x80000000", 1 }, { 16, "-80000000", "-0x80000000", 0 }, { 16, "-80000001", "-0x80000000", -1 }, { 16, "-FF0080000001", "-0x80000000", -1 }, { 16, "0", "-0x8000000000000000", 1 }, { 16, "8000000000000000", "-0x8000000000000000", 1 }, { 16, "8000000000000001", "-0x8000000000000000", 1 }, { 16, "-8000000000000000", "-0x8000000000000000", 0 }, { 16, "-8000000000000001", "-0x8000000000000000", -1 }, { 16, "-FF008000000000000001", "-0x8000000000000000", -1 }, }; mpf_t a; mpz_t bz; long b; int got; int i; mpf_init (a); mpz_init (bz); for (i = 0; i < numberof (data); i++) { mpf_set_str_or_abort (a, data[i].a, data[i].a_base); mpz_set_str_or_abort (bz, data[i].b, 0); if (mpz_fits_slong_p (bz)) { b = mpz_get_si (bz); got = mpf_cmp_si (a, b); if (SGN (got) != data[i].want) { printf ("mpf_cmp_si wrong on data[%d]\n", i); printf (" a="); mpf_out_str (stdout, 10, 0, a); printf (" (%s)\n", data[i].a); printf (" b=%ld (%s)\n", b, data[i].b); printf (" got=%d\n", got); printf (" want=%d\n", data[i].want); abort(); } } } mpf_clear (a); mpz_clear (bz); }
/* Test that there is no lost of accuracy when converting a mpfr_t number into a mpf_t number (test with various precisions and exponents). */ static void prec_test (void) { int px, py; for (py = 3; py <= 136; py++) { mpfr_t y1, y2, y3; mpfr_init2 (y1, py); mpfr_init2 (y2, py); mpfr_init2 (y3, py); for (px = 32; px <= 160; px += 32) { mpf_t x1, x2, x3; int e; mpf_init (x1); mpf_init (x2); mpf_init (x3); mpfr_set_ui_2exp (y1, 1, py - 1, MPFR_RNDN); mpfr_get_f (x1, y1, MPFR_RNDN); /* exact (power of 2) */ mpf_set (x2, x1); mpfr_set (y2, y1, MPFR_RNDN); for (e = py - 2; e >= 0; e--) { int inex; mpf_div_2exp (x2, x2, 1); mpf_add (x1, x1, x2); mpfr_div_2exp (y2, y2, 1, MPFR_RNDN); inex = mpfr_add (y1, y1, y2, MPFR_RNDN); MPFR_ASSERTN (inex == 0); mpfr_set_f (y3, x1, MPFR_RNDN); if (! mpfr_equal_p (y1, y3)) break; inex = mpfr_get_f (x3, y3, MPFR_RNDN); if (mpf_cmp (x1, x3) != 0) { printf ("Error in prec_test (px = %d, py = %d, e = %d)\n", px, py, e); printf ("x1 = "); mpf_out_str (stdout, 16, 0, x1); printf ("\nx2 = "); mpf_out_str (stdout, 16, 0, x2); printf ("\n"); exit (1); } if (inex != 0) { printf ("Error in prec_test (px = %d, py = %d, e = %d)\n", px, py, e); printf ("wrong ternary value got: %+d, expected: 0\n", inex); exit (1); } } mpf_clear (x1); mpf_clear (x2); mpf_clear (x3); } mpfr_clear (y1); mpfr_clear (y2); mpfr_clear (y3); } }
void ks (mpf_t Kp, mpf_t Km, mpf_t X[], void (P) (mpf_t, mpf_t), unsigned long int n) { mpf_t Kt; /* temp */ mpf_t f_x; mpf_t f_j; /* j */ mpf_t f_jnq; /* j/n or (j-1)/n */ unsigned long int j; /* Sort the vector in ascending order. */ qsort (X, n, sizeof (__mpf_struct), mpf_cmp); /* K-S test. */ /* Kp = sqr(n) * max(j/n - F(Xj)) for all 1<=j<=n Km = sqr(n) * max(F(Xj) - (j-1)/n)) for all 1<=j<=n */ mpf_init (Kt); mpf_init (f_x); mpf_init (f_j); mpf_init (f_jnq); mpf_set_ui (Kp, 0); mpf_set_ui (Km, 0); for (j = 1; j <= n; j++) { P (f_x, X[j-1]); mpf_set_ui (f_j, j); mpf_div_ui (f_jnq, f_j, n); mpf_sub (Kt, f_jnq, f_x); if (mpf_cmp (Kt, Kp) > 0) mpf_set (Kp, Kt); if (g_debug > DEBUG_2) { printf ("j=%lu ", j); printf ("P()="); mpf_out_str (stdout, 10, 2, f_x); printf ("\t"); printf ("jnq="); mpf_out_str (stdout, 10, 2, f_jnq); printf (" "); printf ("diff="); mpf_out_str (stdout, 10, 2, Kt); printf (" "); printf ("Kp="); mpf_out_str (stdout, 10, 2, Kp); printf ("\t"); } mpf_sub_ui (f_j, f_j, 1); mpf_div_ui (f_jnq, f_j, n); mpf_sub (Kt, f_x, f_jnq); if (mpf_cmp (Kt, Km) > 0) mpf_set (Km, Kt); if (g_debug > DEBUG_2) { printf ("jnq="); mpf_out_str (stdout, 10, 2, f_jnq); printf (" "); printf ("diff="); mpf_out_str (stdout, 10, 2, Kt); printf (" "); printf ("Km="); mpf_out_str (stdout, 10, 2, Km); printf (" "); printf ("\n"); } } mpf_sqrt_ui (Kt, n); mpf_mul (Kp, Kp, Kt); mpf_mul (Km, Km, Kt); mpf_clear (Kt); mpf_clear (f_x); mpf_clear (f_j); mpf_clear (f_jnq); }
static void ternary_test (void) { int prec; int rnd; int inex, expected_inex; mpf_t x; mpfr_t y; mpf_init2 (x, 256); mpfr_init2 (y, 256); for (prec = 2; prec <= 256; prec++) { mpf_set_prec (x, prec); mpfr_set_prec (y, PREC (x) * GMP_NUMB_BITS + 1); /* y == 1 */ mpfr_set_ui_2exp (y, 1, prec, MPFR_RNDN); RND_LOOP (rnd) { inex = mpfr_get_f (x, y, (mpfr_rnd_t) rnd); if (inex != 0 || mpfr_cmp_f (y, x) !=0) { printf ("Error in mpfr_get_f (x, y, %s)\nx = ", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); mpf_out_str (stdout, 2, 0, x); printf ("\ny = "); mpfr_dump (y); if (inex != 0) printf ("got ternary value = %+d, expected: 0\n", inex); exit (1); } } /* y == 1 + epsilon */ mpfr_nextbelow (y); RND_LOOP (rnd) { switch (rnd) { case MPFR_RNDU: case MPFR_RNDA: case MPFR_RNDN: expected_inex = +1; break; default : expected_inex = -1; } inex = mpfr_get_f (x, y, (mpfr_rnd_t) rnd); if (! SAME_SIGN (expected_inex, inex) || SAME_SIGN (expected_inex, mpfr_cmp_f (y, x))) { printf ("Error in mpfr_get_f (x, y, %s)\nx = ", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); mpf_out_str (stdout, 2, 0, x); printf ("\ny = "); mpfr_dump (y); if (! SAME_SIGN (expected_inex, inex)) printf ("got ternary value = %+d, expected: %+d\n", inex, expected_inex); exit (1); } } /* y == positive random float */ mpfr_random2 (y, MPFR_LIMB_SIZE (y), 1024, RANDS); RND_LOOP (rnd) { inex = mpfr_get_f (x, y, (mpfr_rnd_t) rnd); if (! SAME_SIGN (inex, -mpfr_cmp_f (y, x))) { printf ("Error in mpfr_get_f (x, y, %s)\nx = ", mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); mpf_out_str (stdout, 2, 0, x); printf ("\ny = "); mpfr_dump (y); printf ("got ternary value = %+d, expected: %+d\n", inex, -mpfr_cmp_f (y, x)); exit (1); } } } mpf_clear (x); mpfr_clear (y); }