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); }
void fmpz_poly_disc_gauss_rounding(fmpz_poly_t rop, const fmpq_poly_t x, const mpfr_t r_f, gmp_randstate_t randstate) { mpfr_t xi; mpfr_init2(xi, mpfr_get_prec(r_f)); mpf_t xi_f; mpf_init2(xi_f, mpfr_get_prec(r_f)); mpq_t xi_q; mpq_init(xi_q); mpz_t s_z; mpz_init(s_z); const long n = fmpq_poly_length(x); const size_t tau = (ceil(2*sqrt(log2((double)n))) > 3) ? ceil(2*sqrt(log2((double)n))) : 3; fmpz_poly_zero(rop); for(int i=0; i<n; i++) { fmpq_poly_get_coeff_mpq(xi_q, x, i); mpf_set_q(xi_f, xi_q); mpfr_set_f(xi, xi_f, MPFR_RNDN); dgs_disc_gauss_mp_t *D = dgs_disc_gauss_mp_init(r_f, xi, tau, DGS_DISC_GAUSS_UNIFORM_ONLINE); D->call(s_z, D, randstate); dgs_disc_gauss_mp_clear(D); fmpz_poly_set_coeff_mpz(rop, i, s_z); } mpz_clear(s_z); mpq_clear(xi_q); mpf_clear(xi_f); mpfr_clear(xi); }
static void test_cmp_f (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpf_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, pmax+GMP_NUMB_BITS); mpf_init2 (y, MPFR_PREC_MIN); /* check the erange flag when x is NaN */ mpfr_set_nan (x); mpf_set_ui (y, 17); mpfr_clear_erangeflag (); res1 = mpfr_cmp_f (x, y); if (res1 != 0 || mpfr_erangeflag_p () == 0) { printf ("Error for mpfr_cmp_f (NaN, 17)\n"); printf ("Return value: expected 0, got %d\n", res1); printf ("Erange flag: expected set, got %d\n", mpfr_erangeflag_p ()); exit (1); } for(p=pmin ; p < pmax ; p+=3) { mpfr_set_prec (x, p); mpf_set_prec (y, p); for ( n = 0; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpf_urandomb (y, RANDS, p); if (!MPFR_IS_SINGULAR (x)) { mpfr_set_f (z, y, MPFR_RNDN); mpfr_sub (z, x, z, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_f (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_f: res=%d sub gives %d\n", res2, res1); exit (1); } } } } mpf_clear (y); mpfr_clear (x); mpfr_clear (z); }
static void test_cmp_f (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpf_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, pmax+GMP_NUMB_BITS); mpf_init2 (y, MPFR_PREC_MIN); for(p=pmin ; p < pmax ; p+=3) { mpfr_set_prec (x, p); mpf_set_prec (y, p); for ( n = 0; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpf_urandomb (y, RANDS, p); if (!MPFR_IS_SINGULAR (x)) { mpfr_set_f (z, y, MPFR_RNDN); mpfr_sub (z, x, z, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_f (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_f: res=%d sub gives %d\n", res2, res1); exit (1); } } } } mpf_clear (y); mpfr_clear (x); mpfr_clear (z); }
static void check_mixed (FILE *fout) { int ch = 'a'; signed char sch = -1; unsigned char uch = 1; short sh = -1; unsigned short ush = 1; int i = -1; int j = 1; unsigned int ui = 1; long lo = -1; unsigned long ulo = 1; float f = -1.25; double d = -1.25; long double ld = -1.25; ptrdiff_t p = 1; size_t sz = 1; mpz_t mpz; mpq_t mpq; mpf_t mpf; mp_rnd_t rnd = GMP_RNDN; mp_size_t limb_size = 3; mp_limb_t limb[3]; mpfr_t mpfr; mpfr_prec_t prec = 53; mpz_init (mpz); mpz_set_ui (mpz, ulo); mpq_init (mpq); mpq_set_si (mpq, lo, ulo); mpf_init (mpf); mpf_set_q (mpf, mpq); mpfr_init2 (mpfr, prec); mpfr_set_f (mpfr, mpf, GMP_RNDN); limb[0] = limb[1] = limb[2] = ~ (mp_limb_t) 0; check_vfprintf (fout, "a. %Ra, b. %u, c. %lx%n", mpfr, ui, ulo, &j); check_length (1, j, 22, d); check_vfprintf (fout, "a. %c, b. %Rb, c. %u, d. %li%ln", i, mpfr, i, lo, &ulo); check_length (2, ulo, 36, lu); check_vfprintf (fout, "a. %hi, b. %*f, c. %Re%hn", ush, 3, f, mpfr, &ush); check_length (3, ush, 29, hu); check_vfprintf (fout, "a. %hi, b. %f, c. %#.2Rf%n", sh, d, mpfr, &i); check_length (4, i, 29, d); check_vfprintf (fout, "a. %R*A, b. %Fe, c. %i%zn", rnd, mpfr, mpf, sz, &sz); check_length (5, (unsigned long) sz, 34, lu); /* no format specifier "%zu" in C89 */ check_vfprintf (fout, "a. %Pu, b. %c, c. %Zi%Zn", prec, ch, mpz, &mpz); check_length_with_cmp (6, mpz, 17, mpz_cmp_ui (mpz, 17), Zi); check_vfprintf (fout, "%% a. %#.0RNg, b. %Qx%Rn, c. %p", mpfr, mpq, &mpfr, (void *) &i); check_length_with_cmp (7, mpfr, 15, mpfr_cmp_ui (mpfr, 15), Rg); #ifndef NPRINTF_T check_vfprintf (fout, "%% a. %RNg, b. %Qx, c. %td%tn", mpfr, mpq, p, &p); check_length (8, (long) p, 20, ld); /* no format specifier "%td" in C89 */ #endif #ifndef NPRINTF_L check_vfprintf (fout, "a. %RA, b. %Lf, c. %QX%zn", mpfr, ld, mpq, &sz); check_length (9, (unsigned long) sz, 30, lu); /* no format specifier "%zu" in C89 */ #endif #ifndef NPRINTF_HH check_vfprintf (fout, "a. %hhi, b.%RA, c. %hhu%hhn", sch, mpfr, uch, &uch); check_length (10, (unsigned int) uch, 21, u); /* no format specifier "%hhu" in C89 */ #endif #if (__GNU_MP_VERSION * 10 + __GNU_MP_VERSION_MINOR) >= 42 /* The 'M' specifier was added in gmp 4.2.0 */ check_vfprintf (fout, "a. %Mx b. %Re%Mn", limb[0], mpfr, &limb[0]); if (limb[0] != 14 + BITS_PER_MP_LIMB / 4 || limb[1] != ~ (mp_limb_t) 0 || limb[2] != ~ (mp_limb_t) 0) { printf ("Error in test #11: mpfr_vfprintf did not print %d characters" " as expected\n", 14 + (int) BITS_PER_MP_LIMB / 4); exit (1); } limb[0] = ~ (mp_limb_t) 0; /* we tell vfprintf that limb array is 2 cells wide and check it doesn't go through */ check_vfprintf (fout, "a. %Re .b %Nx%Nn", mpfr, limb, limb_size, limb, limb_size - 1); if (limb[0] != 14 + 3 * BITS_PER_MP_LIMB / 4 || limb[1] != (mp_limb_t) 0 || limb[2] != ~ (mp_limb_t) 0) { printf ("Error in test #12: mpfr_vfprintf did not print %d characters" " as expected\n", 14 + (int) BITS_PER_MP_LIMB / 4); exit (1); } #endif #if defined(HAVE_LONG_LONG) && !defined(NPRINTF_LL) { long long llo = -1; unsigned long long ullo = 1; check_vfprintf (fout, "a. %Re, b. %llx%Qn", mpfr, ullo, &mpq); check_length_with_cmp (21, mpq, 16, mpq_cmp_ui (mpq, 16, 1), Qu); check_vfprintf (fout, "a. %lli, b. %Rf%Fn", llo, mpfr, &mpf); check_length_with_cmp (22, mpf, 19, mpf_cmp_ui (mpf, 19), Fg); } #endif #if defined(_MPFR_H_HAVE_INTMAX_T) && !defined(NPRINTF_J) { intmax_t im = -1; uintmax_t uim = 1; check_vfprintf (fout, "a. %*RA, b. %ji%Qn", 10, mpfr, im, &mpq); check_length_with_cmp (31, mpq, 20, mpq_cmp_ui (mpq, 20, 1), Qu); check_vfprintf (fout, "a. %.*Re, b. %jx%Fn", 10, mpfr, uim, &mpf); check_length_with_cmp (32, mpf, 25, mpf_cmp_ui (mpf, 25), Fg); } #endif mpfr_clear (mpfr); mpf_clear (mpf); mpq_clear (mpq); mpz_clear (mpz); }
static void check_mixed (void) { int ch = 'a'; #ifndef NPRINTF_HH signed char sch = -1; unsigned char uch = 1; #endif short sh = -1; unsigned short ush = 1; int i = -1; int j = 1; unsigned int ui = 1; long lo = -1; unsigned long ulo = 1; float f = -1.25; double d = -1.25; #if !defined(NPRINTF_T) || !defined(NPRINTF_L) long double ld = -1.25; #endif #ifndef NPRINTF_T ptrdiff_t p = 1, saved_p; #endif size_t sz = 1; mpz_t mpz; mpq_t mpq; mpf_t mpf; mpfr_rnd_t rnd = MPFR_RNDN; mpfr_t mpfr; mpfr_prec_t prec; mpz_init (mpz); mpz_set_ui (mpz, ulo); mpq_init (mpq); mpq_set_si (mpq, lo, ulo); mpf_init (mpf); mpf_set_q (mpf, mpq); mpfr_init (mpfr); mpfr_set_f (mpfr, mpf, MPFR_RNDN); prec = mpfr_get_prec (mpfr); check_vprintf ("a. %Ra, b. %u, c. %lx%n", mpfr, ui, ulo, &j); check_length (1, j, 22, d); check_vprintf ("a. %c, b. %Rb, c. %u, d. %li%ln", i, mpfr, i, lo, &ulo); check_length (2, ulo, 36, lu); check_vprintf ("a. %hi, b. %*f, c. %Re%hn", ush, 3, f, mpfr, &ush); check_length (3, ush, 29, hu); check_vprintf ("a. %hi, b. %f, c. %#.2Rf%n", sh, d, mpfr, &i); check_length (4, i, 29, d); check_vprintf ("a. %R*A, b. %Fe, c. %i%zn", rnd, mpfr, mpf, sz, &sz); check_length (5, (unsigned long) sz, 34, lu); /* no format specifier '%zu' in C89 */ check_vprintf ("a. %Pu, b. %c, c. %RUG, d. %Zi%Zn", prec, ch, mpfr, mpz, &mpz); check_length_with_cmp (6, mpz, 24, mpz_cmp_ui (mpz, 24), Zi); check_vprintf ("%% a. %#.0RNg, b. %Qx%Rn c. %p", mpfr, mpq, &mpfr, (void *) &i); check_length_with_cmp (7, mpfr, 15, mpfr_cmp_ui (mpfr, 15), Rg); #ifndef NPRINTF_T saved_p = p; check_vprintf ("%% a. %RNg, b. %Qx, c. %td%tn", mpfr, mpq, p, &p); if (p != 20) mpfr_fprintf (stderr, "Error in test 8, got '%% a. %RNg, b. %Qx, c. %td'\n", mpfr, mpq, saved_p); check_length (8, (long) p, 20, ld); /* no format specifier '%td' in C89 */ #endif #ifndef NPRINTF_L check_vprintf ("a. %RA, b. %Lf, c. %QX%zn", mpfr, ld, mpq, &sz); check_length (9, (unsigned long) sz, 30, lu); /* no format specifier '%zu' in C89 */ #endif #ifndef NPRINTF_HH check_vprintf ("a. %hhi, b. %Ra, c. %hhu%hhn", sch, mpfr, uch, &uch); check_length (10, (unsigned int) uch, 22, u); /* no format specifier '%hhu' in C89 */ #endif #if defined(HAVE_LONG_LONG) && !defined(NPRINTF_LL) { long long llo = -1; unsigned long long ullo = 1; check_vprintf ("a. %Re, b. %llx%Qn", mpfr, ullo, &mpq); check_length_with_cmp (11, mpq, 16, mpq_cmp_ui (mpq, 16, 1), Qu); check_vprintf ("a. %lli, b. %Rf%lln", llo, mpfr, &ullo); check_length (12, ullo, 19, llu); } #endif #if defined(_MPFR_H_HAVE_INTMAX_T) && !defined(NPRINTF_J) { intmax_t im = -1; uintmax_t uim = 1; check_vprintf ("a. %*RA, b. %ji%Fn", 10, mpfr, im, &mpf); check_length_with_cmp (31, mpf, 20, mpf_cmp_ui (mpf, 20), Fg); check_vprintf ("a. %.*Re, b. %jx%jn", 10, mpfr, uim, &im); check_length (32, (long) im, 25, li); /* no format specifier "%ji" in C89 */ } #endif mpfr_clear (mpfr); mpf_clear (mpf); mpq_clear (mpq); mpz_clear (mpz); }
/* 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); } }
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 mpfr_stats (unsigned long num, mpfr_prec_t prec_a, mpfr_prec_t prec_b, mpfr_prec_t prec_c, unsigned long seed) { mpf_t xt[num],yt[num],zt[num]; unsigned long long mc[num][MAX_OP], m; mpfr_t a, b, c; mpf_t x, y, z; mpz_t zz; unsigned long long min,max,moy; gmp_randstate_t state; int i,j=0, op, cont; int imin=0, imax=0; mpf_init2(x, prec_a); mpf_init2(y, prec_b); mpf_init2(z, prec_c); mpfr_init2(a, prec_a); mpfr_init2(b, prec_b); mpfr_init2(c, prec_c); gmp_randinit_lc_2exp_size (state, 128); gmp_randseed_ui (state, seed); mpz_init (zz); mpz_urandomb (zz, state, 2*prec_b); TIMP_OVERHEAD (); for(i = 0 ; i < num ; i++) { mpf_init2(xt[i], prec_a); mpf_init2(yt[i], prec_b); mpf_init2(zt[i], prec_c); mpf_urandomb(yt[i], state, prec_b); yt[i][0]._mp_exp += (rand() % prec_b) / GMP_NUMB_BITS; mpf_urandomb(zt[i], state, prec_c); /* zt[i][0]._mp_exp += (rand() % prec_c) / GMP_NUMB_BITS; */ for(op = 0 ; op < MAX_OP ; op++) mc[i][op] = 0xFFFFFFFFFFFFFFFLL; } for(j = 0, cont = 5 ; cont ; j++, cont--) { printf("Pass %d...\n", j+1); for(i = 0 ; i < num ; i++) { op = 0; mpf_set(y,yt[i]); mpf_set(z,zt[i]); mpfr_set_f(b, yt[i], MPFR_RNDN); mpfr_set_f(c, zt[i], MPFR_RNDN); #undef BENCH #define BENCH(TEST_STR, TEST) \ m = TIMP_MEASURE(TEST); if (m < mc[i][op]) {mc[i][op] = m; cont = 4;} op++; TEST_LIST; } #undef BENCH #define BENCH(TEST_STR, TEST) \ min = 0xFFFFFFFFFFFFFFFLL; max = 0LL; moy = 0LL; \ for(i = 0 ; i < num ; i++) { \ if (mc[i][op] < min) imin = i, min = mc[i][op]; \ if (mc[i][op] > max) imax = i, max = mc[i][op]; \ moy += mc[i][op]; \ } \ printf(" %s: %Lu / %Lu.%02Lu / %Lu", TEST_STR, min, \ (unsigned long long) moy/num, (moy*100LL/num)%100LL, max); \ if (verbose) printf ("\tMIN:%e,%e\tMAX:%e,%e", mpf_get_d(yt[imin]),\ mpf_get_d(zt[imin]), mpf_get_d(yt[imax]), \ mpf_get_d(zt[imax])); \ putchar ('\n'); \ op++; op =0; TEST_LIST; } printf("End\n"); mpz_clear (zz); mpfr_clear(a); mpfr_clear(b); mpfr_clear(c); mpf_clear(x); mpf_clear(y); mpf_clear(z); for(i = 0 ; i < num ; i++) { mpf_clear(xt[i]); mpf_clear(yt[i]); mpf_clear(zt[i]); } gmp_randclear(state); }
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; }
static void check_set (void) { long int lo; mpz_t mpz; mpq_t mpq; mpf_t mpf; mpfr_t fr; mpc_t x, z; mpfr_prec_t prec; mpz_init (mpz); mpq_init (mpq); mpf_init2 (mpf, 1000); mpfr_init2 (fr, 1000); mpc_init2 (x, 1000); mpc_init2 (z, 1000); mpz_set_ui (mpz, 0x4217); mpq_set_si (mpq, -1, 0x4321); mpf_set_q (mpf, mpq); for (prec = 2; prec <= 1000; prec++) { unsigned long int u = (unsigned long int) prec; mpc_set_prec (z, prec); mpfr_set_prec (fr, prec); lo = -prec; mpfr_set_d (fr, 1.23456789, GMP_RNDN); mpc_set_d (z, 1.23456789, MPC_RNDNN); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp_si (MPC_IM(z), 0) != 0) PRINT_ERROR ("mpc_set_d", prec, z); #if defined _MPC_H_HAVE_COMPLEX mpc_set_dc (z, I*1.23456789+1.23456789, MPC_RNDNN); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0) PRINT_ERROR ("mpc_set_c", prec, z); #endif mpc_set_ui (z, u, MPC_RNDNN); if (mpfr_cmp_ui (MPC_RE(z), u) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0) PRINT_ERROR ("mpc_set_ui", prec, z); mpc_set_d_d (z, 1.23456789, 1.23456789, MPC_RNDNN); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0) PRINT_ERROR ("mpc_set_d_d", prec, z); mpc_set_si (z, lo, MPC_RNDNN); if (mpfr_cmp_si (MPC_RE(z), lo) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0) PRINT_ERROR ("mpc_set_si", prec, z); mpfr_set_ld (fr, 1.23456789L, GMP_RNDN); mpc_set_ld_ld (z, 1.23456789L, 1.23456789L, MPC_RNDNN); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0) PRINT_ERROR ("mpc_set_ld_ld", prec, z); #if defined _MPC_H_HAVE_COMPLEX mpc_set_ldc (z, I*1.23456789L+1.23456789L, MPC_RNDNN); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0) PRINT_ERROR ("mpc_set_lc", prec, z); #endif mpc_set_ui_ui (z, u, u, MPC_RNDNN); if (mpfr_cmp_ui (MPC_RE(z), u) != 0 || mpfr_cmp_ui (MPC_IM(z), u) != 0) PRINT_ERROR ("mpc_set_ui_ui", prec, z); mpc_set_ld (z, 1.23456789L, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_ld", prec, z); mpc_set_prec (x, prec); mpfr_set_ui(fr, 1, GMP_RNDN); mpfr_div_ui(fr, fr, 3, GMP_RNDN); mpfr_set(MPC_RE(x), fr, GMP_RNDN); mpfr_set(MPC_IM(x), fr, GMP_RNDN); mpc_set (z, x, MPC_RNDNN); mpfr_clear_flags (); /* mpc_cmp set erange flag when an operand is a NaN */ if (mpc_cmp (z, x) != 0 || mpfr_erangeflag_p()) { printf ("Error in mpc_set for prec = %lu\n", (unsigned long int) prec); MPC_OUT(z); MPC_OUT(x); exit (1); } mpc_set_si_si (z, lo, lo, MPC_RNDNN); if (mpfr_cmp_si (MPC_RE(z), lo) != 0 || mpfr_cmp_si (MPC_IM(z), lo) != 0) PRINT_ERROR ("mpc_set_si_si", prec, z); mpc_set_fr (z, fr, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_fr", prec, z); mpfr_set_z (fr, mpz, GMP_RNDN); mpc_set_z_z (z, mpz, mpz, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_z_z", prec, z); mpc_set_fr_fr (z, fr, fr, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_fr_fr", prec, z); mpc_set_z (z, mpz, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_z", prec, z); mpfr_set_q (fr, mpq, GMP_RNDN); mpc_set_q_q (z, mpq, mpq, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_q_q", prec, z); mpc_set_ui_fr (z, u, fr, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp_ui (MPC_RE (z), u) != 0 || mpfr_cmp (MPC_IM (z), fr) != 0 || mpfr_erangeflag_p ()) PRINT_ERROR ("mpc_set_ui_fr", prec, z); mpc_set_fr_ui (z, fr, u, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE (z), fr) != 0 || mpfr_cmp_ui (MPC_IM (z), u) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_fr_ui", prec, z); mpc_set_q (z, mpq, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_q", prec, z); mpfr_set_f (fr, mpf, GMP_RNDN); mpc_set_f_f (z, mpf, mpf, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp (MPC_IM(z), fr) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_f_f", prec, z); mpc_set_f (z, mpf, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE(z), fr) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0 || mpfr_erangeflag_p()) PRINT_ERROR ("mpc_set_f", prec, z); mpc_set_f_si (z, mpf, lo, MPC_RNDNN); mpfr_clear_flags (); if (mpfr_cmp (MPC_RE (z), fr) != 0 || mpfr_cmp_si (MPC_IM (z), lo) != 0 || mpfr_erangeflag_p ()) PRINT_ERROR ("mpc_set_f", prec, z); mpc_set_nan (z); if (!mpfr_nan_p (MPC_RE(z)) || !mpfr_nan_p (MPC_IM(z))) PRINT_ERROR ("mpc_set_nan", prec, z); #ifdef _MPC_H_HAVE_INTMAX_T { uintmax_t uim = (uintmax_t) prec; intmax_t im = (intmax_t) prec; mpc_set_uj (z, uim, MPC_RNDNN); if (mpfr_cmp_ui (MPC_RE(z), u) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0) PRINT_ERROR ("mpc_set_uj", prec, z); mpc_set_sj (z, im, MPC_RNDNN); if (mpfr_cmp_ui (MPC_RE(z), u) != 0 || mpfr_cmp_ui (MPC_IM(z), 0) != 0) PRINT_ERROR ("mpc_set_sj (1)", prec, z); mpc_set_uj_uj (z, uim, uim, MPC_RNDNN); if (mpfr_cmp_ui (MPC_RE(z), u) != 0 || mpfr_cmp_ui (MPC_IM(z), u) != 0) PRINT_ERROR ("mpc_set_uj_uj", prec, z); mpc_set_sj_sj (z, im, im, MPC_RNDNN); if (mpfr_cmp_ui (MPC_RE(z), u) != 0 || mpfr_cmp_ui (MPC_IM(z), u) != 0) PRINT_ERROR ("mpc_set_sj_sj (1)", prec, z); im = LONG_MAX; if (sizeof (intmax_t) == 2 * sizeof (unsigned long)) im = 2 * im * im + 4 * im + 1; /* gives 2^(2n-1)-1 from 2^(n-1)-1 */ mpc_set_sj (z, im, MPC_RNDNN); if (mpfr_get_sj (MPC_RE(z), GMP_RNDN) != im || mpfr_cmp_ui (MPC_IM(z), 0) != 0) PRINT_ERROR ("mpc_set_sj (2)", im, z); mpc_set_sj_sj (z, im, im, MPC_RNDNN); if (mpfr_get_sj (MPC_RE(z), GMP_RNDN) != im || mpfr_get_sj (MPC_IM(z), GMP_RNDN) != im) PRINT_ERROR ("mpc_set_sj_sj (2)", im, z); } #endif /* _MPC_H_HAVE_INTMAX_T */ #if defined _MPC_H_HAVE_COMPLEX { double _Complex c = 1.0 - 2.0*I; long double _Complex lc = c; mpc_set_dc (z, c, MPC_RNDNN); if (mpc_get_dc (z, MPC_RNDNN) != c) PRINT_ERROR ("mpc_get_c", prec, z); mpc_set_ldc (z, lc, MPC_RNDNN); if (mpc_get_ldc (z, MPC_RNDNN) != lc) PRINT_ERROR ("mpc_get_lc", prec, z); } #endif } mpz_clear (mpz); mpq_clear (mpq); mpf_clear (mpf); mpfr_clear (fr); mpc_clear (x); mpc_clear (z); }