static void check_round_p (void) { mp_limb_t buf[MAX_LIMB_SIZE]; mp_size_t n, i; mp_prec_t p; mp_exp_t err; int r1, r2; for (n = 2 ; n <= MAX_LIMB_SIZE ; n++) { /* avoid mpn_random which leaks memory */ for (i = 0; i < n; i++) buf[i] = randlimb (); p = (mp_prec_t) randlimb() % ((n-1) * BITS_PER_MP_LIMB) + MPFR_PREC_MIN; err = p + randlimb () % BITS_PER_MP_LIMB; r1 = mpfr_round_p (buf, n, err, p); r2 = mpfr_can_round_raw (buf, n, MPFR_SIGN_POS, err, GMP_RNDN, GMP_RNDZ, p); if (r1 != r2) { printf ("mpfr_round_p(%d) != mpfr_can_round(%d)!\n" "bn = %ld, err0 = %ld, prec = %lu\nbp = ", r1, r2, n, err, (unsigned long) p); gmp_printf ("%NX\n", buf, n); exit (1); } } }
/* pos is 512 times the proportion of negative numbers. If pos=256, half of the numbers are negative. If pos=0, all generated numbers are positive. */ void tests_default_random (mpfr_ptr x, int pos, mpfr_exp_t emin, mpfr_exp_t emax) { MPFR_ASSERTN (emin <= emax); MPFR_ASSERTN (emin >= MPFR_EMIN_MIN); MPFR_ASSERTN (emax <= MPFR_EMAX_MAX); /* but it isn't required that emin and emax are in the current exponent range (see below), so that underflow/overflow checks can be done on 64-bit machines. */ mpfr_urandomb (x, RANDS); if (MPFR_IS_PURE_FP (x) && (emin >= 1 || (randlimb () & 1))) { mpfr_exp_t e; e = MPFR_GET_EXP (x) + (emin + (long) (randlimb () % (emax - emin + 1))); /* Note: There should be no overflow here because both terms are between MPFR_EMIN_MIN and MPFR_EMAX_MAX, but the sum e isn't necessarily between MPFR_EMIN_MIN and MPFR_EMAX_MAX. */ if (mpfr_set_exp (x, e)) { /* The random number doesn't fit in the current exponent range. In this case, test the function in the extended exponent range, which should be restored by the caller. */ mpfr_set_emin (MPFR_EMIN_MIN); mpfr_set_emax (MPFR_EMAX_MAX); mpfr_set_exp (x, e); } } if (randlimb () % 512 < pos) mpfr_neg (x, x, MPFR_RNDN); }
static void cmp_tests (void) { mpfr_t x, y; long i; mpfr_inits (x, y, (mpfr_ptr) 0); for (i = 0; i < 80000; i++) { mpfr_prec_t precx, precy; int signx, signy, cmp; unsigned int cmpbool = 0; precx = (randlimb () % 17) * 11 + MPFR_PREC_MIN; precy = (randlimb () % 17) * 11 + MPFR_PREC_MIN; mpfr_set_prec (x, precx); mpfr_set_prec (y, precy); mpfr_urandomb (x, RANDS); mpfr_urandomb (y, RANDS); signx = randlimb () & 1; signy = randlimb () % 256 ? signx : 1 - signx; /* signy = signx most of the time (most interesting case) */ if (signx) mpfr_neg (x, x, MPFR_RNDN); if (signy) mpfr_neg (y, y, MPFR_RNDN); if (i <= 1) mpfr_set_nan (x); if (i == 0 || i == 2) mpfr_set_nan (y); if (mpfr_greater_p (x, y)) cmpbool |= 0x01; if (mpfr_greaterequal_p (x, y)) cmpbool |= 0x02; if (mpfr_less_p (x, y)) cmpbool |= 0x04; if (mpfr_lessequal_p (x, y)) cmpbool |= 0x08; if (mpfr_lessgreater_p (x, y)) cmpbool |= 0x10; if (mpfr_equal_p (x, y)) cmpbool |= 0x20; if (mpfr_unordered_p (x, y)) cmpbool |= 0x40; if ((i <= 2 && cmpbool != 0x40) || (i > 2 && (cmp = mpfr_cmp (x, y), (cmp == 0 && cmpbool != 0x2a) || (cmp < 0 && cmpbool != 0x1c) || (cmp > 0 && cmpbool != 0x13)))) { printf ("Error in cmp_tests for\nx = "); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); printf (" and\ny = "); mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN); printf ("\n"); exit (1); } } mpfr_clears (x, y, (mpfr_ptr) 0); }
/* pos is 512 times the proportion of negative numbers. If pos=256, half of the numbers are negative. If pos=0, all generated numbers are positive. */ void tests_default_random (mpfr_ptr x, int pos, mpfr_exp_t emin, mpfr_exp_t emax, int always_scale) { MPFR_ASSERTN (emin <= emax); MPFR_ASSERTN (emin >= MPFR_EMIN_MIN); MPFR_ASSERTN (emax <= MPFR_EMAX_MAX); /* but it isn't required that emin and emax are in the current exponent range (see below), so that underflow/overflow checks can be done on 64-bit machines without a manual change of the exponent range (well, this is a bit ugly...). */ mpfr_urandomb (x, RANDS); if (MPFR_IS_PURE_FP (x) && (emin >= 1 || always_scale || (randlimb () & 1))) { mpfr_exp_t e; e = emin + (mpfr_exp_t) (randlimb () % (emax - emin + 1)); /* Note: There should be no overflow here because both terms are between MPFR_EMIN_MIN and MPFR_EMAX_MAX. */ MPFR_ASSERTD (e >= emin && e <= emax); if (mpfr_set_exp (x, e)) { /* The random number doesn't fit in the current exponent range. In this case, test the function in the extended exponent range, which should be restored by the caller. */ mpfr_set_emin (MPFR_EMIN_MIN); mpfr_set_emax (MPFR_EMAX_MAX); mpfr_set_exp (x, e); } } if (randlimb () % 512 < pos) mpfr_neg (x, x, MPFR_RNDN); }
static void eq_tests (void) { mpfr_t x, y; long i; mpfr_inits (x, y, (mpfr_ptr) 0); for (i = 0; i < 20000; i++) { mpfr_prec_t precx; precx = (randlimb () % 17) * 11 + MPFR_PREC_MIN; mpfr_set_prec (x, precx); mpfr_set_prec (y, precx + (randlimb () % 64)); mpfr_urandomb (x, RANDS); if (randlimb () & 1) mpfr_neg (x, x, MPFR_RNDN); mpfr_set (y, x, MPFR_RNDN); /* exact -> x = y */ if (mpfr_greater_p (x, y) || !mpfr_greaterequal_p (x, y) || mpfr_less_p (x, y) || !mpfr_lessequal_p (x, y) || mpfr_lessgreater_p (x, y) || !mpfr_equal_p (x, y) || mpfr_unordered_p (x, y)) { printf ("Error in eq_tests for x = "); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); printf ("\n"); exit (1); } } mpfr_clears (x, y, (mpfr_ptr) 0); }
static void test_cmp_q (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpq_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, MPFR_PREC_MIN); mpq_init (y); /* check the erange flag when x is NaN */ mpfr_set_nan (x); mpq_set_ui (y, 17, 1); mpfr_clear_erangeflag (); res1 = mpfr_cmp_q (x, y); if (res1 != 0 || mpfr_erangeflag_p () == 0) { printf ("Error for mpfr_cmp_q (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++) { mpfr_set_prec (x, p); for (n = 0 ; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpq_set_ui (y, randlimb (), randlimb() ); if (!MPFR_IS_SINGULAR (x)) { mpfr_sub_q (z, x, y, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_q (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_q: res=%d sub_z gives %d\n", res2, res1); exit (1); } } } } mpq_clear (y); mpfr_clear (x); mpfr_clear (z); }
/* 1. compare expected string with the string BUFFER returned by mpfr_sprintf(buffer, fmt, x) 2. then test mpfr_snprintf (buffer, p, fmt, x) with a random p. */ static int check_sprintf (const char *expected, const char *fmt, mpfr_srcptr x) { int n0, n1, p; char buffer[BUF_SIZE]; /* test mpfr_sprintf */ n0 = mpfr_sprintf (buffer, fmt, x); if (strcmp (buffer, expected) != 0) { printf ("Error in mpfr_sprintf (s, \"%s\", x);\n", fmt); printf ("expected: \"%s\"\ngot: \"%s\"\n", expected, buffer); exit (1); } /* test mpfr_snprintf */ p = (int) (randlimb () % n0); if (p == 0 && (randlimb () & 1) == 0) { n1 = mpfr_snprintf (NULL, 0, fmt, x); } else { buffer[p] = 17; n1 = mpfr_snprintf (buffer, p, fmt, x); if (buffer[p] != 17) { printf ("Buffer overflow in mpfr_snprintf for p = %d!\n", p); exit (1); } } if (n0 != n1) { printf ("Error in mpfr_snprintf (s, %d, \"%s\", x) return value\n", p, fmt); printf ("expected: %d\ngot: %d\n", n0, n1); exit (1); } if ((p > 1 && strncmp (expected, buffer, p-1) != 0) || (p == 1 && buffer[0] != '\0')) { char part_expected[BUF_SIZE]; strncpy (part_expected, expected, p); part_expected[p-1] = '\0'; printf ("Error in mpfr_vsnprintf (s, %d, \"%s\", ...);\n", p, fmt); printf ("expected: \"%s\"\ngot: \"%s\"\n", part_expected, buffer); exit (1); } return n0; }
static void test_specialq (mpfr_prec_t p0, mpfr_prec_t p1, unsigned int N, int (*mpfr_func)(mpfr_ptr, mpfr_srcptr, mpq_srcptr, mpfr_rnd_t), void (*mpq_func)(mpq_ptr, mpq_srcptr, mpq_srcptr), const char *op) { mpfr_t fra, frb, frq; mpq_t q1, q2, qr; unsigned int n; mpfr_prec_t prec; for (prec = p0 ; prec < p1 ; prec++) { mpfr_inits2 (prec, fra, frb, frq, (mpfr_ptr) 0); mpq_init (q1); mpq_init(q2); mpq_init (qr); for( n = 0 ; n < N ; n++) { mpq_set_ui(q1, randlimb(), randlimb() ); mpq_set_ui(q2, randlimb(), randlimb() ); mpq_canonicalize (q1); mpq_canonicalize (q2); mpq_func (qr, q1, q2); mpfr_set_q (fra, q1, MPFR_RNDD); mpfr_func (fra, fra, q2, MPFR_RNDD); mpfr_set_q (frb, q1, MPFR_RNDU); mpfr_func (frb, frb, q2, MPFR_RNDU); mpfr_set_q (frq, qr, MPFR_RNDN); /* We should have fra <= qr <= frb */ if ( (mpfr_cmp(fra, frq) > 0) || (mpfr_cmp (frq, frb) > 0)) { printf("Range error for prec=%lu and %s", (unsigned long) prec, op); printf ("\nq1="); mpq_out_str(stdout, 2, q1); printf ("\nq2="); mpq_out_str(stdout, 2, q2); printf ("\nfr_dn="); mpfr_print_binary (fra); printf ("\nfr_q ="); mpfr_print_binary (frq); printf ("\nfr_up="); mpfr_print_binary (frb); putchar('\n'); exit (1); } } mpq_clear (q1); mpq_clear (q2); mpq_clear (qr); mpfr_clears (fra, frb, frq, (mpfr_ptr) 0); } }
static void consistency (void) { mpfr_t x, y, z1, z2; int i; mpfr_inits (x, y, z1, z2, (mpfr_ptr) 0); for (i = 0; i < 10000; i++) { mpfr_rnd_t rnd; mpfr_prec_t px, py, pz, p; int inex1, inex2; rnd = RND_RAND (); px = (randlimb () % 256) + 2; py = (randlimb () % 128) + 2; pz = (randlimb () % 256) + 2; mpfr_set_prec (x, px); mpfr_set_prec (y, py); mpfr_set_prec (z1, pz); mpfr_set_prec (z2, pz); mpfr_urandomb (x, RANDS); do mpfr_urandomb (y, RANDS); while (mpfr_zero_p (y)); inex1 = mpfr_div (z1, x, y, rnd); MPFR_ASSERTN (!MPFR_IS_NAN (z1)); p = MAX (MAX (px, py), pz); if (mpfr_prec_round (x, p, MPFR_RNDN) != 0 || mpfr_prec_round (y, p, MPFR_RNDN) != 0) { printf ("mpfr_prec_round error for i = %d\n", i); exit (1); } inex2 = mpfr_div (z2, x, y, rnd); MPFR_ASSERTN (!MPFR_IS_NAN (z2)); if (inex1 != inex2 || mpfr_cmp (z1, z2) != 0) { printf ("Consistency error for i = %d\n", i); exit (1); } } mpfr_clears (x, y, z1, z2, (mpfr_ptr) 0); }
static void test_sum (mpfr_prec_t f, unsigned long n) { mpfr_t sum, real_sum, real_non_rounded; mpfr_t *tab; unsigned long i; int rnd_mode; /* Init */ tab = (mpfr_t *) (*__gmp_allocate_func) (n * sizeof(mpfr_t)); for (i = 0; i < n; i++) mpfr_init2 (tab[i], f); mpfr_inits2 (f, sum, real_sum, real_non_rounded, (mpfr_ptr) 0); /* First Uniform */ for (i = 0; i < n; i++) mpfr_urandomb (tab[i], RANDS); algo_exact (real_non_rounded, tab, n, f); for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX; rnd_mode++) { sum_tab (sum, tab, n, (mpfr_rnd_t) rnd_mode); mpfr_set (real_sum, real_non_rounded, (mpfr_rnd_t) rnd_mode); if (mpfr_cmp (real_sum, sum) != 0) { printf ("mpfr_sum incorrect.\n"); mpfr_dump (real_sum); mpfr_dump (sum); exit (1); } } /* Then non uniform */ for (i = 0; i < n; i++) { mpfr_urandomb (tab[i], RANDS); if (! mpfr_zero_p (tab[i])) mpfr_set_exp (tab[i], randlimb () % 1000); } algo_exact (real_non_rounded, tab, n, f); for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX; rnd_mode++) { sum_tab (sum, tab, n, (mpfr_rnd_t) rnd_mode); mpfr_set (real_sum, real_non_rounded, (mpfr_rnd_t) rnd_mode); if (mpfr_cmp (real_sum, sum) != 0) { printf ("mpfr_sum incorrect.\n"); mpfr_dump (real_sum); mpfr_dump (sum); exit (1); } } /* Clear stuff */ for (i = 0; i < n; i++) mpfr_clear (tab[i]); mpfr_clears (sum, real_sum, real_non_rounded, (mpfr_ptr) 0); (*__gmp_free_func) (tab, n * sizeof(mpfr_t)); }
/* check case when c does not overlap with a, but both b and c count for rounding */ static void check_case_1b (void) { mpfr_t a, b, c; unsigned int prec_a, prec_b, prec_c, dif; mpfr_init (a); mpfr_init (b); mpfr_init (c); { prec_a = MPFR_PREC_MIN + (randlimb () % 63); mpfr_set_prec (a, prec_a); for (prec_b = prec_a + 2; prec_b <= 64; prec_b++) { dif = prec_b - prec_a; mpfr_set_prec (b, prec_b); /* b = 1 - 2^(-prec_a) + 2^(-prec_b) */ mpfr_set_ui (b, 1, MPFR_RNDN); mpfr_div_2exp (b, b, dif, MPFR_RNDN); mpfr_sub_ui (b, b, 1, MPFR_RNDN); mpfr_div_2exp (b, b, prec_a, MPFR_RNDN); mpfr_add_ui (b, b, 1, MPFR_RNDN); for (prec_c = dif; prec_c <= 64; prec_c++) { /* c = 2^(-prec_a) - 2^(-prec_b) */ mpfr_set_prec (c, prec_c); mpfr_set_si (c, -1, MPFR_RNDN); mpfr_div_2exp (c, c, dif, MPFR_RNDN); mpfr_add_ui (c, c, 1, MPFR_RNDN); mpfr_div_2exp (c, c, prec_a, MPFR_RNDN); test_add (a, b, c, MPFR_RNDN); if (mpfr_cmp_ui (a, 1) != 0) { printf ("case (1b) failed for prec_a=%u, prec_b=%u," " prec_c=%u\n", prec_a, prec_b, prec_c); printf ("b="); mpfr_print_binary(b); puts (""); printf ("c="); mpfr_print_binary(c); puts (""); printf ("a="); mpfr_print_binary(a); puts (""); exit (1); } } } } mpfr_clear (a); mpfr_clear (b); mpfr_clear (c); }
static void check_inexact (void) { mpfr_t x, y, z; mp_prec_t px, py; int inexact, cmp; unsigned long int u; int rnd; mpfr_init (x); mpfr_init (y); mpfr_init (z); for (px=2; px<300; px++) { mpfr_set_prec (x, px); do { mpfr_urandomb (x, RANDS); } while (mpfr_cmp_ui (x, 0) == 0); u = randlimb (); for (py=2; py<300; py++) { mpfr_set_prec (y, py); mpfr_set_prec (z, py + px); for (rnd = 0; rnd < GMP_RND_MAX; rnd++) { inexact = mpfr_ui_div (y, u, x, (mp_rnd_t) rnd); if (mpfr_mul (z, y, x, (mp_rnd_t) rnd)) { printf ("z <- y * x should be exact\n"); exit (1); } cmp = mpfr_cmp_ui (z, u); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { printf ("Wrong inexact flag for u=%lu, rnd=%s\n", u, mpfr_print_rnd_mode ((mp_rnd_t) rnd)); printf ("expected %d, got %d\n", cmp, inexact); printf ("x="); mpfr_print_binary (x); puts (""); printf ("y="); mpfr_print_binary (y); puts (""); printf ("y*x="); mpfr_print_binary (z); puts (""); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
static void check_inexact (void) { mpfr_t x, y, z; mpfr_prec_t px, py; int inexact, cmp; unsigned long int u; int rnd; mpfr_init (x); mpfr_init (y); mpfr_init (z); for (px=2; px<300; px++) { mpfr_set_prec (x, px); mpfr_urandomb (x, RANDS); do { u = randlimb (); } while (u == 0); for (py=2; py<300; py++) { mpfr_set_prec (y, py); mpfr_set_prec (z, py + mp_bits_per_limb); for (rnd = 0; rnd < MPFR_RND_MAX; rnd++) { inexact = mpfr_div_ui (y, x, u, (mpfr_rnd_t) rnd); if (mpfr_mul_ui (z, y, u, (mpfr_rnd_t) rnd)) { printf ("z <- y * u should be exact for u=%lu\n", u); printf ("y="); mpfr_print_binary (y); puts (""); printf ("z="); mpfr_print_binary (z); puts (""); exit (1); } cmp = mpfr_cmp (z, x); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { printf ("Wrong inexact flag for u=%lu, rnd=%s\n", u, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd)); printf ("x="); mpfr_print_binary (x); puts (""); printf ("y="); mpfr_print_binary (y); puts (""); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
static void check_set_uj (mpfr_prec_t pmin, mpfr_prec_t pmax, int N) { mpfr_t x, y; mpfr_prec_t p; int inex1, inex2, n; mp_limb_t limb; mpfr_inits2 (pmax, x, y, (mpfr_ptr) 0); for ( p = pmin ; p < pmax ; p++) { mpfr_set_prec (x, p); mpfr_set_prec (y, p); for (n = 0 ; n < N ; n++) { /* mp_limb_t may be unsigned long long */ limb = (unsigned long) randlimb (); inex1 = mpfr_set_uj (x, limb, MPFR_RNDN); inex2 = mpfr_set_ui (y, limb, MPFR_RNDN); if (mpfr_cmp (x, y)) { printf ("ERROR for mpfr_set_uj and j=%lu and p=%lu\n", (unsigned long) limb, (unsigned long) p); printf ("X="); mpfr_dump (x); printf ("Y="); mpfr_dump (y); exit (1); } if (inexact_sign (inex1) != inexact_sign (inex2)) { printf ("ERROR for inexact(set_uj): j=%lu p=%lu\n" "Inexact1= %d Inexact2= %d\n", (unsigned long) limb, (unsigned long) p, inex1, inex2); exit (1); } } } /* Special case */ mpfr_set_prec (x, sizeof(uintmax_t)*CHAR_BIT); inex1 = mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN); if (inex1 != 0 || mpfr_sgn(x) <= 0) ERROR ("inexact / UINTMAX_MAX"); inex1 = mpfr_add_ui (x, x, 1, MPFR_RNDN); if (inex1 != 0 || !mpfr_powerof2_raw (x) || MPFR_EXP (x) != (sizeof(uintmax_t)*CHAR_BIT+1) ) ERROR ("power of 2"); mpfr_set_uj (x, 0, MPFR_RNDN); if (!MPFR_IS_ZERO (x)) ERROR ("Setting 0"); mpfr_clears (x, y, (mpfr_ptr) 0); }
static void set_special (mpfr_ptr x, unsigned int select) { MPFR_ASSERTN (select < SPECIAL_MAX); switch (select) { case 0: MPFR_SET_NAN (x); break; case 1: MPFR_SET_INF (x); MPFR_SET_POS (x); break; case 2: MPFR_SET_INF (x); MPFR_SET_NEG (x); break; case 3: MPFR_SET_ZERO (x); MPFR_SET_POS (x); break; case 4: MPFR_SET_ZERO (x); MPFR_SET_NEG (x); break; case 5: mpfr_set_str_binary (x, "1"); break; case 6: mpfr_set_str_binary (x, "-1"); break; case 7: mpfr_set_str_binary (x, "1e-1"); break; case 8: mpfr_set_str_binary (x, "1e+1"); break; case 9: mpfr_const_pi (x, MPFR_RNDN); break; case 10: mpfr_const_pi (x, MPFR_RNDN); MPFR_SET_EXP (x, MPFR_GET_EXP (x)-1); break; default: mpfr_urandomb (x, RANDS); if (randlimb () & 1) mpfr_neg (x, x, MPFR_RNDN); break; } }
static void check_inexact (void) { mpfr_prec_t p, q; mpfr_t x, y, absx; int rnd; int inexact, cmp; mpfr_init (x); mpfr_init (y); mpfr_init (absx); for (p=2; p<500; p++) { mpfr_set_prec (x, p); mpfr_set_prec (absx, p); mpfr_urandomb (x, RANDS); if (randlimb () % 2) { mpfr_set (absx, x, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); } else mpfr_set (absx, x, MPFR_RNDN); for (q=2; q<2*p; q++) { mpfr_set_prec (y, q); RND_LOOP (rnd) { inexact = mpfr_abs (y, x, (mpfr_rnd_t) rnd); cmp = mpfr_cmp (y, absx); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { printf ("Wrong inexact flag: expected %d, got %d\n", cmp, inexact); printf ("x="); mpfr_dump (x); printf ("absx="); mpfr_dump (absx); printf ("y="); mpfr_dump (y); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (absx); }
static void test_cmp_q (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpq_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, MPFR_PREC_MIN); mpq_init (y); for(p=pmin ; p < pmax ; p++) { mpfr_set_prec (x, p); for (n = 0 ; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpq_set_ui (y, randlimb (), randlimb() ); if (!MPFR_IS_SINGULAR (x)) { mpfr_sub_q (z, x, y, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_q (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_q: res=%d sub_z gives %d\n", res2, res1); exit (1); } } } } mpq_clear (y); mpfr_clear (x); mpfr_clear (z); }
int main (int argc, char *argv[]) { long j; tests_start_mpfr (); check (0, MPFR_RNDN); for (j = 0; j < 200000; j++) check (randlimb () & LONG_MAX, RND_RAND ()); check0 (); tests_end_mpfr (); return 0; }
static void pow_int (mpfr_rnd_t rnd) { mpfr_t ref1, ref2, ref3; mpfr_t res1; int i; #ifdef DEBUG printf("pow_int\n"); #endif mpfr_inits2 ((randlimb () % 200) + MPFR_PREC_MIN, ref1, ref2, res1, (mpfr_ptr) 0); mpfr_init2 (ref3, 1005); for (i = 0; i <= 15; i++) { mpfr_urandomb (ref2, RANDS); if (i & 1) mpfr_neg (ref2, ref2, MPFR_RNDN); mpfr_set_ui (ref3, 20, MPFR_RNDN); /* We need to test huge integers because different algorithms/codes are used for not-too-large integers (mpfr_pow_z) and for general cases, in particular huge integers (mpfr_pow_general). [r7606] */ if (i & 2) mpfr_mul_2ui (ref3, ref3, 1000, MPFR_RNDN); if (i & 4) mpfr_add_ui (ref3, ref3, 1, MPFR_RNDN); /* odd integer */ /* reference call: pow(a, b, c) */ mpfr_pow (ref1, ref2, ref3, rnd); /* pow(a, a, c) */ mpfr_set (res1, ref2, rnd); /* exact operation */ mpfr_pow (res1, res1, ref3, rnd); if (mpfr_compare (res1, ref1)) { printf ("Error for pow_int(a, a, c) for "); DISP("a=",ref2); DISP2(", c=",ref3); printf ("expected "); mpfr_print_binary (ref1); puts (""); printf ("got "); mpfr_print_binary (res1); puts (""); exit (1); } } mpfr_clears (ref1, ref2, ref3, res1, (mpfr_ptr) 0); }
static void test2ui (int (*testfunc)(mpfr_ptr, mpfr_srcptr, unsigned long int, mp_rnd_t), char *foo, mp_prec_t prec, mp_rnd_t rnd) { mpfr_t ref1, ref2; unsigned int ref3; mpfr_t res1; int i; #ifdef DEBUG printf("checking %s\n", foo); #endif mpfr_init2 (ref1, prec); mpfr_init2 (ref2, prec); mpfr_init2 (res1, prec); /* ref2 can be NaN, +Inf, -Inf, +0, -0 or any number ref3 can be 0 or any number */ for (i=0; i<SPECIAL_MAX*2; i++) { set_special (ref2, i%SPECIAL_MAX); ref3 = i/SPECIAL_MAX == 0 ? 0 : randlimb (); /* reference call: foo(a, b, c) */ testfunc (ref1, ref2, ref3, rnd); /* foo(a, a, c) */ mpfr_set (res1, ref2, rnd); /* exact operation */ testfunc (res1, res1, ref3, rnd); if (mpfr_compare (res1, ref1)) { printf ("Error for %s(a, a, c) for c=%u\n", foo, ref3); DISP2("a=",ref2); printf ("expected "); mpfr_print_binary (ref1); puts (""); printf ("got "); mpfr_print_binary (res1); puts (""); exit (1); } } mpfr_clear (ref1); mpfr_clear (ref2); mpfr_clear (res1); }
static void testui2 (int (*testfunc)(mpfr_ptr, unsigned long int, mpfr_srcptr, mpfr_rnd_t), const char *foo, mpfr_prec_t prec, mpfr_rnd_t rnd) { mpfr_t ref1, ref3; unsigned int ref2; mpfr_t res1; int i; #ifdef DEBUG printf("checking %s\n", foo); #endif mpfr_init2 (ref1, prec); mpfr_init2 (ref3, prec); mpfr_init2 (res1, prec); for (i=0; i<SPECIAL_MAX*2; i++) { set_special (ref3, i%SPECIAL_MAX); ref2 = i/SPECIAL_MAX==0 ? 0 : randlimb (); /* reference call: foo(a, b, c) */ testfunc (ref1, ref2, ref3, rnd); /* foo(a, b, a) */ mpfr_set (res1, ref3, rnd); /* exact operation */ testfunc (res1, ref2, res1, rnd); if (mpfr_compare (res1, ref1)) { printf ("Error for %s(a, b, a) for b=%u \n", foo, ref2); DISP2("a=", ref3); DISP("expected", ref1); DISP2(", got ", res1); exit (1); } } mpfr_clear (ref1); mpfr_clear (ref3); mpfr_clear (res1); }
static void check_ternary_value (void) { int p, q, rnd; int inexact, cmp; mpfr_t x, y; mpfr_init (x); mpfr_init (y); for (p=2; p<500; p++) { mpfr_set_prec (x, p); mpfr_urandomb (x, RANDS); if (randlimb () % 2) mpfr_neg (x, x, MPFR_RNDN); for (q=2; q<2*p; q++) { mpfr_set_prec (y, q); for (rnd = 0; rnd < MPFR_RND_MAX; rnd++) { inexact = mpfr_set (y, x, (mpfr_rnd_t) rnd); cmp = mpfr_cmp (y, x); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { printf ("Wrong ternary value in mpfr_set: expected %d," " got %d\n", cmp, inexact); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); }
/* FastTwoSum: if EXP(x) >= EXP(y), u = o(x+y), v = o(u-x), w = o(y-v), then x + y = u + w thus if u = o(y-x), v = o(u+x), w = o(v-y), then y-x = u-w */ static void check_two_sum (mpfr_prec_t p) { unsigned int x; mpfr_t y, u, v, w; mpfr_rnd_t rnd; int inexact; mpfr_inits2 (p, y, u, v, w, (mpfr_ptr) 0); do { x = randlimb (); } while (x < 1); mpfr_urandomb (y, RANDS); rnd = MPFR_RNDN; inexact = mpfr_sub_ui (u, y, x, rnd); mpfr_add_ui (v, u, x, rnd); mpfr_sub (w, v, y, rnd); /* as u - (y-x) = w, we should have inexact and w of same sign */ if (((inexact == 0) && mpfr_cmp_ui (w, 0)) || ((inexact > 0) && (mpfr_cmp_ui (w, 0) <= 0)) || ((inexact < 0) && (mpfr_cmp_ui (w, 0) >= 0))) { printf ("Wrong inexact flag for prec=%u, rnd=%s\n", (unsigned int) p, mpfr_print_rnd_mode (rnd)); printf ("x=%u\n", x); printf ("y="); mpfr_print_binary(y); puts (""); printf ("u="); mpfr_print_binary(u); puts (""); printf ("v="); mpfr_print_binary(v); puts (""); printf ("w="); mpfr_print_binary(w); puts (""); printf ("inexact = %d\n", inexact); exit (1); } mpfr_clears (y, u, v, w, (mpfr_ptr) 0); }
static void check_inexact (void) { mpfr_t x, y, z, u; mpfr_prec_t px, py, pu; int inexact, cmp; mpfr_rnd_t rnd; mpfr_init (x); mpfr_init (y); mpfr_init (z); mpfr_init (u); mpfr_set_prec (x, 28); mpfr_set_prec (y, 28); mpfr_set_prec (z, 1023); mpfr_set_str_binary (x, "0.1000001001101101111100010011E0"); mpfr_set_str (z, "48284762641021308813686974720835219181653367326353400027913400579340343320519877153813133510034402932651132854764198688352364361009429039801248971901380781746767119334993621199563870113045276395603170432175354501451429471578325545278975153148347684600400321033502982713296919861760382863826626093689036010394", 10, MPFR_RNDN); mpfr_div (x, x, z, MPFR_RNDN); mpfr_set_str_binary (y, "0.1111001011001101001001111100E-1023"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_div for prec=28, RNDN\n"); printf ("Expected "); mpfr_dump (y); printf ("Got "); mpfr_dump (x); exit (1); } mpfr_set_prec (x, 53); mpfr_set_str_binary (x, "0.11101100110010100011011000000100001111011111110010101E0"); mpfr_set_prec (u, 127); mpfr_set_str_binary (u, "0.1000001100110110110101110110101101111000110000001111111110000000011111001010110100110010111111111101000001011011101011101101000E-2"); mpfr_set_prec (y, 95); inexact = test_div (y, x, u, MPFR_RNDN); if (inexact != (cmp = get_inexact (y, x, u))) { printf ("Wrong inexact flag (0): expected %d, got %d\n", cmp, inexact); printf ("x="); mpfr_out_str (stdout, 10, 99, x, MPFR_RNDN); printf ("\n"); printf ("u="); mpfr_out_str (stdout, 10, 99, u, MPFR_RNDN); printf ("\n"); printf ("y="); mpfr_out_str (stdout, 10, 99, y, MPFR_RNDN); printf ("\n"); exit (1); } mpfr_set_prec (x, 33); mpfr_set_str_binary (x, "0.101111100011011101010011101100001E0"); mpfr_set_prec (u, 2); mpfr_set_str_binary (u, "0.1E0"); mpfr_set_prec (y, 28); if ((inexact = test_div (y, x, u, MPFR_RNDN) >= 0)) { printf ("Wrong inexact flag (1): expected -1, got %d\n", inexact); exit (1); } mpfr_set_prec (x, 129); mpfr_set_str_binary (x, "0.111110101111001100000101011100101100110011011101010001000110110101100101000010000001110110100001101010001010100010001111001101010E-2"); mpfr_set_prec (u, 15); mpfr_set_str_binary (u, "0.101101000001100E-1"); mpfr_set_prec (y, 92); if ((inexact = test_div (y, x, u, MPFR_RNDN)) <= 0) { printf ("Wrong inexact flag for rnd=MPFR_RNDN(1): expected 1, got %d\n", inexact); mpfr_dump (x); mpfr_dump (u); mpfr_dump (y); exit (1); } for (px=2; px<MAX_PREC; px++) { mpfr_set_prec (x, px); mpfr_urandomb (x, RANDS); for (pu=2; pu<=MAX_PREC; pu++) { mpfr_set_prec (u, pu); do { mpfr_urandomb (u, RANDS); } while (mpfr_cmp_ui (u, 0) == 0); { py = MPFR_PREC_MIN + (randlimb () % (MAX_PREC - MPFR_PREC_MIN)); mpfr_set_prec (y, py); mpfr_set_prec (z, py + pu); { rnd = RND_RAND (); inexact = test_div (y, x, u, rnd); if (mpfr_mul (z, y, u, rnd)) { printf ("z <- y * u should be exact\n"); exit (1); } cmp = mpfr_cmp (z, x); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { printf ("Wrong inexact flag for rnd=%s\n", mpfr_print_rnd_mode(rnd)); printf ("expected %d, got %d\n", cmp, inexact); printf ("x="); mpfr_print_binary (x); puts (""); printf ("u="); mpfr_print_binary (u); puts (""); printf ("y="); mpfr_print_binary (y); puts (""); printf ("y*u="); mpfr_print_binary (z); puts (""); exit (1); } } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (u); }
int main (int argc, char *argv[]) { mpfr_t x; long k, z, d, N; unsigned long zl, dl; int inex; int r; mpfr_exp_t emin, emax; int flag; tests_start_mpfr (); mpfr_init2 (x, 100); N = (argc==1) ? 100000 : atol (argv[1]); for (k = 1; k <= N; k++) { z = (long) (randlimb () & LONG_MAX) + LONG_MIN / 2; inex = mpfr_set_si (x, z, MPFR_RNDZ); d = mpfr_get_si (x, MPFR_RNDZ); if (d != z) { printf ("Error in mpfr_set_si: expected %ld got %ld\n", z, d); exit (1); } if (inex) { printf ("Error in mpfr_set_si: inex value incorrect for %ld: %d\n", z, inex); exit (1); } } for (k = 1; k <= N; k++) { zl = randlimb (); inex = mpfr_set_ui (x, zl, MPFR_RNDZ); dl = mpfr_get_ui (x, MPFR_RNDZ); if (dl != zl) { printf ("Error in mpfr_set_ui: expected %lu got %lu\n", zl, dl); exit (1); } if (inex) { printf ("Error in mpfr_set_ui: inex value incorrect for %lu: %d\n", zl, inex); exit (1); } } mpfr_set_prec (x, 2); if (mpfr_set_si (x, 5, MPFR_RNDZ) >= 0) { printf ("Wrong inexact flag for x=5, rnd=MPFR_RNDZ\n"); exit (1); } mpfr_set_prec (x, 2); if (mpfr_set_si (x, -5, MPFR_RNDZ) <= 0) { printf ("Wrong inexact flag for x=-5, rnd=MPFR_RNDZ\n"); exit (1); } mpfr_set_prec (x, 3); inex = mpfr_set_si (x, 77617, MPFR_RNDD); /* should be 65536 */ if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1)) || inex >= 0) { printf ("Error in mpfr_set_si(x:3, 77617, MPFR_RNDD)\n"); mpfr_print_binary (x); puts (""); exit (1); } inex = mpfr_set_ui (x, 77617, MPFR_RNDD); /* should be 65536 */ if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1)) || inex >= 0) { printf ("Error in mpfr_set_ui(x:3, 77617, MPFR_RNDD)\n"); mpfr_print_binary (x); puts (""); exit (1); } mpfr_set_prec (x, 2); inex = mpfr_set_si (x, 33096, MPFR_RNDU); if (mpfr_get_si (x, MPFR_RNDZ) != 49152 || inex <= 0) { printf ("Error in mpfr_set_si, exp. 49152, got %ld, inex %d\n", mpfr_get_si (x, MPFR_RNDZ), inex); exit (1); } inex = mpfr_set_ui (x, 33096, MPFR_RNDU); if (mpfr_get_si (x, MPFR_RNDZ) != 49152) { printf ("Error in mpfr_set_ui, exp. 49152, got %ld, inex %d\n", mpfr_get_si (x, MPFR_RNDZ), inex); exit (1); } /* Also test the mpfr_set_ui function (instead of macro). */ inex = (mpfr_set_ui) (x, 33096, MPFR_RNDU); if (mpfr_get_si (x, MPFR_RNDZ) != 49152) { printf ("Error in mpfr_set_ui function, exp. 49152, got %ld, inex %d\n", mpfr_get_si (x, MPFR_RNDZ), inex); exit (1); } for (r = 0 ; r < MPFR_RND_MAX ; r++) { mpfr_set_si (x, -1, (mpfr_rnd_t) r); mpfr_set_ui (x, 0, (mpfr_rnd_t) r); if (MPFR_IS_NEG (x) || mpfr_get_ui (x, (mpfr_rnd_t) r) != 0) { printf ("mpfr_set_ui (x, 0) gives -0 for %s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); exit (1); } mpfr_set_si (x, -1, (mpfr_rnd_t) r); mpfr_set_si (x, 0, (mpfr_rnd_t) r); if (MPFR_IS_NEG (x) || mpfr_get_si (x, (mpfr_rnd_t) r) != 0) { printf ("mpfr_set_si (x, 0) gives -0 for %s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); exit (1); } } /* check potential bug in case mp_limb_t is unsigned */ emax = mpfr_get_emax (); set_emax (0); mpfr_set_si (x, -1, MPFR_RNDN); if (mpfr_sgn (x) >= 0) { printf ("mpfr_set_si (x, -1) fails\n"); exit (1); } set_emax (emax); emax = mpfr_get_emax (); set_emax (5); mpfr_set_prec (x, 2); mpfr_set_si (x, -31, MPFR_RNDN); if (mpfr_sgn (x) >= 0) { printf ("mpfr_set_si (x, -31) fails\n"); exit (1); } set_emax (emax); /* test for get_ui */ mpfr_set_ui (x, 0, MPFR_RNDN); MPFR_ASSERTN(mpfr_get_ui (x, MPFR_RNDN) == 0); mpfr_set_ui (x, ULONG_MAX, MPFR_RNDU); mpfr_nextabove (x); mpfr_get_ui (x, MPFR_RNDU); /* another test for get_ui */ mpfr_set_prec (x, 10); mpfr_set_str_binary (x, "10.101"); dl = mpfr_get_ui (x, MPFR_RNDN); MPFR_ASSERTN (dl == 3); mpfr_set_str_binary (x, "-1.0"); mpfr_get_ui (x, MPFR_RNDN); mpfr_set_str_binary (x, "0.1"); dl = mpfr_get_ui (x, MPFR_RNDN); MPFR_ASSERTN (dl == 0); dl = mpfr_get_ui (x, MPFR_RNDZ); MPFR_ASSERTN (dl == 0); dl = mpfr_get_ui (x, MPFR_RNDD); MPFR_ASSERTN (dl == 0); dl = mpfr_get_ui (x, MPFR_RNDU); MPFR_ASSERTN (dl == 1); /* coverage tests */ mpfr_set_prec (x, 2); mpfr_set_si (x, -7, MPFR_RNDD); MPFR_ASSERTN(mpfr_cmp_si (x, -8) == 0); mpfr_set_prec (x, 2); mpfr_set_ui (x, 7, MPFR_RNDU); MPFR_ASSERTN(mpfr_cmp_ui (x, 8) == 0); emax = mpfr_get_emax (); set_emax (3); mpfr_set_ui (x, 7, MPFR_RNDU); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); set_emax (1); MPFR_ASSERTN( mpfr_set_ui (x, 7, MPFR_RNDU) ); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); set_emax (emax); mpfr_set_ui_2exp (x, 17, -50, MPFR_RNDN); MPFR_ASSERTN (mpfr_get_ui (x, MPFR_RNDD) == 0); MPFR_ASSERTN (mpfr_get_si (x, MPFR_RNDD) == 0); /* Test for ERANGE flag + correct behaviour if overflow */ mpfr_set_prec (x, 256); mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN); mpfr_clear_erangeflag (); dl = mpfr_get_ui (x, MPFR_RNDN); if (dl != ULONG_MAX || mpfr_erangeflag_p ()) { printf ("ERROR for get_ui + ERANGE + ULONG_MAX (1)\n"); exit (1); } mpfr_add_ui (x, x, 1, MPFR_RNDN); dl = mpfr_get_ui (x, MPFR_RNDN); if (dl != ULONG_MAX || !mpfr_erangeflag_p ()) { printf ("ERROR for get_ui + ERANGE + ULONG_MAX (2)\n"); exit (1); } mpfr_set_si (x, -1, MPFR_RNDN); mpfr_clear_erangeflag (); dl = mpfr_get_ui (x, MPFR_RNDN); if (dl != 0 || !mpfr_erangeflag_p ()) { printf ("ERROR for get_ui + ERANGE + -1 \n"); exit (1); } mpfr_set_si (x, LONG_MAX, MPFR_RNDN); mpfr_clear_erangeflag (); d = mpfr_get_si (x, MPFR_RNDN); if (d != LONG_MAX || mpfr_erangeflag_p ()) { printf ("ERROR for get_si + ERANGE + LONG_MAX (1): %ld\n", d); exit (1); } mpfr_add_ui (x, x, 1, MPFR_RNDN); d = mpfr_get_si (x, MPFR_RNDN); if (d != LONG_MAX || !mpfr_erangeflag_p ()) { printf ("ERROR for get_si + ERANGE + LONG_MAX (2)\n"); exit (1); } mpfr_set_si (x, LONG_MIN, MPFR_RNDN); mpfr_clear_erangeflag (); d = mpfr_get_si (x, MPFR_RNDN); if (d != LONG_MIN || mpfr_erangeflag_p ()) { printf ("ERROR for get_si + ERANGE + LONG_MIN (1)\n"); exit (1); } mpfr_sub_ui (x, x, 1, MPFR_RNDN); d = mpfr_get_si (x, MPFR_RNDN); if (d != LONG_MIN || !mpfr_erangeflag_p ()) { printf ("ERROR for get_si + ERANGE + LONG_MIN (2)\n"); exit (1); } mpfr_set_nan (x); mpfr_clear_erangeflag (); d = mpfr_get_ui (x, MPFR_RNDN); if (d != 0 || !mpfr_erangeflag_p ()) { printf ("ERROR for get_ui + NaN\n"); exit (1); } mpfr_clear_erangeflag (); d = mpfr_get_si (x, MPFR_RNDN); if (d != 0 || !mpfr_erangeflag_p ()) { printf ("ERROR for get_si + NaN\n"); exit (1); } emin = mpfr_get_emin (); mpfr_set_prec (x, 2); mpfr_set_emin (4); mpfr_clear_flags (); mpfr_set_ui (x, 7, MPFR_RNDU); flag = mpfr_underflow_p (); mpfr_set_emin (emin); if (mpfr_cmp_ui (x, 8) != 0) { printf ("Error for mpfr_set_ui (x, 7, MPFR_RNDU), prec = 2, emin = 4\n"); exit (1); } if (flag) { printf ("mpfr_set_ui (x, 7, MPFR_RNDU) should not underflow " "with prec = 2, emin = 4\n"); exit (1); } mpfr_set_emin (4); mpfr_clear_flags (); mpfr_set_si (x, -7, MPFR_RNDD); flag = mpfr_underflow_p (); mpfr_set_emin (emin); if (mpfr_cmp_si (x, -8) != 0) { printf ("Error for mpfr_set_si (x, -7, MPFR_RNDD), prec = 2, emin = 4\n"); exit (1); } if (flag) { printf ("mpfr_set_si (x, -7, MPFR_RNDD) should not underflow " "with prec = 2, emin = 4\n"); exit (1); } mpfr_clear (x); test_2exp (); test_macros (); test_macros_keyword (); tests_end_mpfr (); return 0; }
static void check4 (const char *as, const char *bs, mpfr_rnd_t rnd_mode, const char *res, int inex) { mpfr_t ta, tb, tc, tres; mpfr_exp_t emin, emax; int i; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_inits2 (53, ta, tb, tc, tres, (mpfr_ptr) 0); for (i = 0; i <= 2; i++) { unsigned int expflags, newflags; int inex2; mpfr_set_str1 (ta, as); mpfr_set_str1 (tb, bs); mpfr_set_str1 (tc, res); if (i > 0) { mpfr_exp_t ea, eb, ec, e0; set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); ea = mpfr_get_exp (ta); eb = mpfr_get_exp (tb); ec = mpfr_get_exp (tc); e0 = i == 1 ? __gmpfr_emin : __gmpfr_emax; if ((i == 1 && ea < eb) || (i == 2 && ea > eb)) { mpfr_set_exp (ta, e0); mpfr_set_exp (tb, e0 + (eb - ea)); mpfr_set_exp (tc, e0 + (ec - ea)); } else { mpfr_set_exp (ta, e0 + (ea - eb)); mpfr_set_exp (tb, e0); mpfr_set_exp (tc, e0 + (ec - eb)); } } __gmpfr_flags = expflags = (randlimb () & 1) ? MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE : 0; inex2 = mpfr_agm (tres, ta, tb, rnd_mode); newflags = __gmpfr_flags; expflags |= MPFR_FLAGS_INEXACT; if (SIGN (inex2) != inex || newflags != expflags || ! mpfr_equal_p (tres, tc)) { printf ("mpfr_agm failed in rnd_mode=%s for\n", mpfr_print_rnd_mode (rnd_mode)); printf (" a = "); mpfr_out_str (stdout, 10, 0, ta, MPFR_RNDN); printf ("\n"); printf (" b = "); mpfr_out_str (stdout, 10, 0, tb, MPFR_RNDN); printf ("\n"); printf ("expected inex = %d, flags = %u,\n" " ", inex, expflags); mpfr_dump (tc); printf ("got inex = %d, flags = %u,\n" " ", inex2, newflags); mpfr_dump (tres); exit (1); } set_emin (emin); set_emax (emax); } mpfr_clears (ta, tb, tc, tres, (mpfr_ptr) 0); }
int main (int argc, char *argv[]) { int i, N=10000, p; mpfr_rnd_t rnd; double d; tests_start_mpfr (); /* with no argument: prints to /dev/null, tout_str N: prints N tests to stdout */ if (argc == 1) { fout = fopen ("/dev/null", "w"); /* If we failed to open this device, try with a dummy file */ if (fout == NULL) fout = fopen ("mpfrtest.txt", "w"); } else { fout = stdout; N = atoi (argv[1]); } if (fout == NULL) { printf ("Can't open /dev/null or stdout\n"); exit (1); } special (); check (-1.37247529013405550000e+15, MPFR_RNDN, 7); check (-1.5674376729569697500e+15, MPFR_RNDN, 19); check (-5.71262771772792640000e-79, MPFR_RNDU, 16); check (DBL_NEG_ZERO, MPFR_RNDU, 7); check (-4.5306392613572974756e-308, MPFR_RNDN, 8); check (-6.7265890111403371523e-165, MPFR_RNDN, 4); check (-1.3242553591261807653e+156, MPFR_RNDN, 16); check (-6.606499965302424244461355e233, MPFR_RNDN, 10); check4 (1.0, MPFR_RNDN, 10, 120); check (1.0, MPFR_RNDU, 10); check (4.059650008e-83, MPFR_RNDN, 10); check (-7.4, MPFR_RNDN, 10); check (0.997, MPFR_RNDN, 10); check (-4.53063926135729747564e-308, MPFR_RNDN, 10); check (2.14478198760196000000e+16, MPFR_RNDN, 10); check (7.02293374921793516813e-84, MPFR_RNDN, 10); /* random tests */ for (i=0;i<N;i++) { do { d = DBL_RAND (); } #ifdef HAVE_DENORMS while (0); #else while (ABS(d) < DBL_MIN); #endif rnd = RND_RAND (); p = 2 + randlimb () % 61; check (d, rnd, p); } fclose (fout); tests_end_mpfr (); return 0; }
int main (int argc, char *argv[]) { mpfr_t x, y, z, s; tests_start_mpfr (); mpfr_init (x); mpfr_init (s); mpfr_init (y); mpfr_init (z); /* check special cases */ mpfr_set_prec (x, 2); mpfr_set_prec (y, 2); mpfr_set_prec (z, 2); mpfr_set_prec (s, 2); mpfr_set_str (x, "-0.75", 10, GMP_RNDN); mpfr_set_str (y, "0.5", 10, GMP_RNDN); mpfr_set_str (z, "0.375", 10, GMP_RNDN); mpfr_fma (s, x, y, z, GMP_RNDU); /* result is 0 */ if (mpfr_cmp_ui(s, 0)) { printf("Error: -0.75 * 0.5 + 0.375 should be equal to 0 for prec=2\n"); exit(1); } mpfr_set_prec (x, 27); mpfr_set_prec (y, 27); mpfr_set_prec (z, 27); mpfr_set_prec (s, 27); mpfr_set_str_binary (x, "1.11111111111111111111111111e-1"); mpfr_set (y, x, GMP_RNDN); mpfr_set_str_binary (z, "-1.00011110100011001011001001e-1"); if (mpfr_fma (s, x, y, z, GMP_RNDN) >= 0) { printf ("Wrong inexact flag for x=y=1-2^(-27)\n"); exit (1); } mpfr_set_nan (x); mpfr_random (y); mpfr_random (z); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_nan_p (s)) { printf ("evaluation of function in x=NAN does not return NAN"); exit (1); } mpfr_set_nan (y); mpfr_random (x); mpfr_random (z); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_nan_p(s)) { printf ("evaluation of function in y=NAN does not return NAN"); exit (1); } mpfr_set_nan (z); mpfr_random (y); mpfr_random (x); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_nan_p (s)) { printf ("evaluation of function in z=NAN does not return NAN"); exit (1); } mpfr_set_inf (x, 1); mpfr_set_inf (y, 1); mpfr_set_inf (z, 1); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_inf_p (s) || mpfr_sgn (s) < 0) { printf ("Error for (+inf) * (+inf) + (+inf)\n"); exit (1); } mpfr_set_inf (x, -1); mpfr_set_inf (y, -1); mpfr_set_inf (z, 1); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_inf_p (s) || mpfr_sgn (s) < 0) { printf ("Error for (-inf) * (-inf) + (+inf)\n"); exit (1); } mpfr_set_inf (x, 1); mpfr_set_inf (y, -1); mpfr_set_inf (z, -1); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_inf_p (s) || mpfr_sgn (s) > 0) { printf ("Error for (+inf) * (-inf) + (-inf)\n"); exit (1); } mpfr_set_inf (x, -1); mpfr_set_inf (y, 1); mpfr_set_inf (z, -1); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_inf_p (s) || mpfr_sgn (s) > 0) { printf ("Error for (-inf) * (+inf) + (-inf)\n"); exit (1); } mpfr_set_inf (x, 1); mpfr_set_ui (y, 0, GMP_RNDN); mpfr_random (z); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_nan_p (s)) { printf ("evaluation of function in x=INF y=0 does not return NAN"); exit (1); } mpfr_set_inf (y, 1); mpfr_set_ui (x, 0, GMP_RNDN); mpfr_random (z); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_nan_p (s)) { printf ("evaluation of function in x=0 y=INF does not return NAN"); exit (1); } mpfr_set_inf (x, 1); mpfr_random (y); /* always positive */ mpfr_set_inf (z, -1); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_nan_p (s)) { printf ("evaluation of function in x=INF y>0 z=-INF does not return NAN"); exit (1); } mpfr_set_inf (y, 1); mpfr_random (x); mpfr_set_inf (z, -1); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_nan_p (s)) { printf ("evaluation of function in x>0 y=INF z=-INF does not return NAN"); exit (1); } mpfr_set_inf (x, 1); mpfr_random (y); mpfr_random (z); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_inf_p (s) || mpfr_sgn (s) < 0) { printf ("evaluation of function in x=INF does not return INF"); exit (1); } mpfr_set_inf (y, 1); mpfr_random (x); mpfr_random (z); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_inf_p (s) || mpfr_sgn (s) < 0) { printf ("evaluation of function in y=INF does not return INF"); exit (1); } mpfr_set_inf (z, 1); mpfr_random (x); mpfr_random (y); mpfr_fma (s, x, y, z, GMP_RNDN); if (!mpfr_inf_p (s) || mpfr_sgn (s) < 0) { printf ("evaluation of function in z=INF does not return INF"); exit (1); } mpfr_set_ui (x, 0, GMP_RNDN); mpfr_random (y); mpfr_random (z); mpfr_fma (s, x, y, z, GMP_RNDN); if (mpfr_cmp (s, z)) { printf ("evaluation of function in x=0 does not return z\n"); exit (1); } mpfr_set_ui (y, 0, GMP_RNDN); mpfr_random (x); mpfr_random (z); mpfr_fma (s, x, y, z, GMP_RNDN); if (mpfr_cmp (s, z)) { printf ("evaluation of function in y=0 does not return z\n"); exit (1); } { mp_prec_t prec; mpfr_t t, slong; mp_rnd_t rnd; int inexact, compare; unsigned int n; mp_prec_t p0=2, p1=200; unsigned int N=200; mpfr_init (t); mpfr_init (slong); /* generic test */ for (prec = p0; prec <= p1; prec++) { mpfr_set_prec (x, prec); mpfr_set_prec (y, prec); mpfr_set_prec (z, prec); mpfr_set_prec (s, prec); mpfr_set_prec (t, prec); for (n=0; n<N; n++) { mpfr_random (x); mpfr_random (y); mpfr_random (z); if (randlimb () % 2) mpfr_neg (x, x, GMP_RNDN); if (randlimb () % 2) mpfr_neg (y, y, GMP_RNDN); if (randlimb () % 2) mpfr_neg (z, z, GMP_RNDN); rnd = (mp_rnd_t) RND_RAND (); mpfr_set_prec (slong, 2 * prec); if (mpfr_mul (slong, x, y, rnd)) { printf ("x*y should be exact\n"); exit (1); } compare = mpfr_add (t, slong, z, rnd); inexact = mpfr_fma (s, x, y, z, rnd); if (mpfr_cmp (s, t)) { printf ("results differ for x="); mpfr_out_str (stdout, 2, prec, x, GMP_RNDN); printf (" y="); mpfr_out_str (stdout, 2, prec, y, GMP_RNDN); printf (" z="); mpfr_out_str (stdout, 2, prec, z, GMP_RNDN); printf (" prec=%u rnd_mode=%s\n", (unsigned int) prec, mpfr_print_rnd_mode (rnd)); printf ("got "); mpfr_out_str (stdout, 2, prec, s, GMP_RNDN); puts (""); printf ("expected "); mpfr_out_str (stdout, 2, prec, t, GMP_RNDN); puts (""); printf ("approx "); mpfr_print_binary (slong); puts (""); exit (1); } if (((inexact == 0) && (compare != 0)) || ((inexact < 0) && (compare >= 0)) || ((inexact > 0) && (compare <= 0))) { printf ("Wrong inexact flag for rnd=%s: expected %d, got %d\n", mpfr_print_rnd_mode (rnd), compare, inexact); printf (" x="); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf (" y="); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf (" z="); mpfr_out_str (stdout, 2, 0, z, GMP_RNDN); printf (" s="); mpfr_out_str (stdout, 2, 0, s, GMP_RNDN); printf ("\n"); exit (1); } } } mpfr_clear (t); mpfr_clear (slong); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (s); test_exact (); test_overflow1 (); test_overflow2 (); test_underflow1 (); test_underflow2 (); tests_end_mpfr (); return 0; }
int main (int argc, char *argv[]) { mpfr_t x, y; unsigned long k, bd, nc, i; char *str, *str2; mp_exp_t e; int base, logbase, prec, baseprec, ret; tests_start_mpfr (); if (argc >= 2) /* tset_str <string> [<prec>] [<base>] */ { prec = (argc >= 3) ? atoi (argv[2]) : 53; base = (argc >= 4) ? atoi (argv[3]) : 2; mpfr_init2 (x, prec); mpfr_set_str (x, argv[1], base, GMP_RNDN); mpfr_out_str (stdout, 10, 0, x, GMP_RNDN); puts (""); mpfr_clear (x); return 0; } mpfr_init2 (x, 2); nc = (argc > 1) ? atoi(argv[1]) : 53; if (nc < 100) nc = 100; bd = randlimb () & 8; str2 = str = (char*) (*__gmp_allocate_func) (nc * sizeof(char)); if (bd) { for(k = 1; k <= bd; k++) *(str2++) = (randlimb () & 1) + '0'; } else *(str2++) = '0'; *(str2++) = '.'; for (k = 1; k < nc - 17 - bd; k++) *(str2++) = '0' + (char) (randlimb () & 1); *(str2++) = 'e'; sprintf (str2, "%d", (int) (randlimb () & INT_MAX) + INT_MIN/2); mpfr_set_prec (x, nc + 10); mpfr_set_str_binary (x, str); mpfr_set_prec (x, 54); mpfr_set_str_binary (x, "0.100100100110110101001010010101111000001011100100101010E-529"); mpfr_init2 (y, 54); mpfr_set_str (y, "4.936a52bc17254@-133", 16, GMP_RNDN); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (1a):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str_binary (x, "0.111111101101110010111010100110000111011001010100001101E-529"); mpfr_set_str (y, "0.fedcba98765434P-529", 16, GMP_RNDN); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (1b):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } (*__gmp_free_func) (str, nc * sizeof(char)); mpfr_set_prec (x, 53); mpfr_set_str_binary (x, "+110101100.01010000101101000000100111001000101011101110E00"); mpfr_set_str_binary (x, "1.0"); if (mpfr_cmp_ui (x, 1)) { printf ("Error in mpfr_set_str_binary for s=1.0\n"); mpfr_clear(x); mpfr_clear(y); exit(1); } mpfr_set_str_binary (x, "+0000"); mpfr_set_str_binary (x, "+0000E0"); mpfr_set_str_binary (x, "0000E0"); if (mpfr_cmp_ui (x, 0)) { printf ("Error in mpfr_set_str_binary for s=0.0\n"); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (x, "+243495834958.53452345E1", 10, GMP_RNDN); mpfr_set_str (x, "9007199254740993", 10, GMP_RNDN); mpfr_set_str (x, "9007199254740992", 10, GMP_RNDU); mpfr_set_str (x, "9007199254740992", 10, GMP_RNDD); mpfr_set_str (x, "9007199254740992", 10, GMP_RNDZ); /* check a random number printed and read is not modified */ prec = 53; mpfr_set_prec (x, prec); mpfr_set_prec (y, prec); for (i=0;i<N;i++) { mpfr_random (x); k = RND_RAND (); logbase = (randlimb () % 5) + 1; base = 1 << logbase; /* Warning: the number of bits needed to print exactly a number of 'prec' bits in base 2^logbase may be greater than ceil(prec/logbase), for example 0.11E-1 in base 2 cannot be written exactly with only one digit in base 4 */ if (base == 2) baseprec = prec; else baseprec = 1 + (prec - 2 + logbase) / logbase; str = mpfr_get_str (NULL, &e, base, baseprec, x, (mp_rnd_t) k); mpfr_set_str (y, str, base, (mp_rnd_t) k); MPFR_EXP(y) += logbase * (e - strlen (str)); if (mpfr_cmp (x, y)) { printf ("mpfr_set_str o mpfr_get_str <> id for rnd_mode=%s\n", mpfr_print_rnd_mode ((mp_rnd_t) k)); printf ("x="); mpfr_print_binary (x); puts (""); printf ("s=%s, exp=%d, base=%d\n", str, (int) e, base); printf ("y="); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } (*__gmp_free_func) (str, strlen (str) + 1); } for (i = 2; i <= 36; i++) { if (mpfr_set_str (x, "@NaN@(garbage)", i, GMP_RNDN) != 0 || !mpfr_nan_p(x)) { printf ("mpfr_set_str failed on @NaN@(garbage)\n"); exit (1); } /* if (mpfr_set_str (x, "@Inf@garbage", i, GMP_RNDN) != 0 || !mpfr_inf_p(x) || MPFR_SIGN(x) < 0) { printf ("mpfr_set_str failed on @Inf@garbage\n"); exit (1); } if (mpfr_set_str (x, "-@Inf@garbage", i, GMP_RNDN) != 0 || !mpfr_inf_p(x) || MPFR_SIGN(x) > 0) { printf ("mpfr_set_str failed on -@Inf@garbage\n"); exit (1); } if (mpfr_set_str (x, "+@Inf@garbage", i, GMP_RNDN) != 0 || !mpfr_inf_p(x) || MPFR_SIGN(x) < 0) { printf ("mpfr_set_str failed on +@Inf@garbage\n"); exit (1); } */ if (i > 16) continue; if (mpfr_set_str (x, "NaN", i, GMP_RNDN) != 0 || !mpfr_nan_p(x)) { printf ("mpfr_set_str failed on NaN\n"); exit (1); } if (mpfr_set_str (x, "Inf", i, GMP_RNDN) != 0 || !mpfr_inf_p(x) || MPFR_SIGN(x) < 0) { printf ("mpfr_set_str failed on Inf\n"); exit (1); } if (mpfr_set_str (x, "-Inf", i, GMP_RNDN) != 0 || !mpfr_inf_p(x) || MPFR_SIGN(x) > 0) { printf ("mpfr_set_str failed on -Inf\n"); exit (1); } if (mpfr_set_str (x, "+Inf", i, GMP_RNDN) != 0 || !mpfr_inf_p(x) || MPFR_SIGN(x) < 0) { printf ("mpfr_set_str failed on +Inf\n"); exit (1); } } /* check that mpfr_set_str works for uppercase letters too */ mpfr_set_prec (x, 10); mpfr_set_str (x, "B", 16, GMP_RNDN); if (mpfr_cmp_ui (x, 11) != 0) { printf ("mpfr_set_str does not work for uppercase letters\n"); exit (1); } /* start of tests added by Alain Delplanque */ /* in this example an overflow can occur */ mpfr_set_prec (x, 64); mpfr_set_prec (y, 64); mpfr_set_str_binary (x, "1.0E-532"); mpfr_set_str (y, "0.71128279983522479470@-160", 10, GMP_RNDU); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (2):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } /* in this example, I think there was a pb in the old function : result of mpfr_set_str_old for the same number , but with more precision is: 1.111111111110000000000000000111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100111000100001100000010101100111010e184 this result is the same as mpfr_set_str */ mpfr_set_prec (x, 64); mpfr_set_prec (y, 64); mpfr_set_str_binary (x, "1.111111111110000000000000000111111111111111111111111110000000001E184"); mpfr_set_str (y, "0.jo08hg31hc5mmpj5mjjmgn55p2h35g@39", 27, GMP_RNDU); /* y = 49027884868983130654865109690613178467841148597221480052 */ if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (3):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } /* not exact rounding in mpfr_set_str same number with more precision is : 1.111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011011111101000001101110110010101101000010100110011101110010001110e195 this result is the same as mpfr_set_str */ /* problem was : can_round was call with GMP_RNDN round mode, so can_round use an error : 1/2 * 2^err * ulp(y) instead of 2^err * ulp(y) I have increase err by 1 */ mpfr_set_prec (x, 64); /* it was round down instead of up */ mpfr_set_prec (y, 64); mpfr_set_str_binary (x, "1.111111111111111111111111111000000000000000000000000000000000001e195"); mpfr_set_str (y, "0.6e23ekb6acgh96abk10b6c9f2ka16i@45", 21, GMP_RNDU); /* y = 100433627392042473064661483711179345482301462325708736552078 */ if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (4):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } /* may be an error in mpfr_set_str_old with more precision : 1.111111100000001111110000000000011111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111101010001110111011000010111001011100110110e180 */ mpfr_set_prec (x, 64); /* it was round down instead of up */ mpfr_set_prec (y, 64); mpfr_set_str_binary (x, "1.111111100000001111110000000000011111011111111111111111111111111e180"); mpfr_set_str (y, "0.10j8j2k82ehahha56390df0a1de030@41", 23, GMP_RNDZ); /* y = 3053110535624388280648330929253842828159081875986159414 */ if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (5):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_prec (x, 64); mpfr_set_prec (y, 64); mpfr_set_str (y, "0.jrchfhpp9en7hidqm9bmcofid9q3jg@39", 28, GMP_RNDU); /* y = 196159429139499688661464718784226062699788036696626429952 */ mpfr_set_str_binary (x, "0.1111111111111111111111111111111000000000000011100000001111100001E187"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (6):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_prec (x, 64); mpfr_set_prec (y, 64); mpfr_set_str (y, "0.h148m5ld5cf8gk1kd70b6ege92g6ba@47", 24, GMP_RNDZ); /* y = 52652933527468502324759448399183654588831274530295083078827114496 */ mpfr_set_str_binary (x, "0.1111111111111100000000001000000000000000000011111111111111101111E215"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (7):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } /* worst cases for rounding to nearest in double precision */ mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str (y, "5e125", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.10111101000101110110011000100000101001010000000111111E418"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (8):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "69e267", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.10000101101111100101101100000110010011001010011011010E894"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (9):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "623e100", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.10110010000001010011000101111001110101000001111011111E342"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (10):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "3571e263", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.10110001001100100010011000110000111010100000110101010E886"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (11):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "75569e-254", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.10101101001000110001011011001000111000110101010110011E-827"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (12):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "920657e-23", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.10101001110101001100110000101110110111101111001101100E-56"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (13):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "9210917e80", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.11101101000100011001000110100011111100110000000110010E289"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (14):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "87575437e-309", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.11110000001110011001000000110000000100000010101101100E-1000"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (15):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "245540327e122", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.10001101101100010001100011110000110001100010111001011E434"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (16):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "491080654e122", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.10001101101100010001100011110000110001100010111001011E435"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (17):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } mpfr_set_str (y, "83356057653e193", 10, GMP_RNDN); mpfr_set_str_binary (x, "0.10101010001001110011011011010111011100010101000011000E678"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_set_str (18):\n"); mpfr_print_binary (x); puts (""); mpfr_print_binary (y); puts (""); mpfr_clear (x); mpfr_clear (y); exit (1); } CHECK53(y, "83356057653e193", GMP_RNDN, x, "0.10101010001001110011011011010111011100010101000011000E678", 18); CHECK53(y, "619534293513e124", GMP_RNDN, x, "0.10001000011000010000000110000001111111110000011110001e452", 19); CHECK53(y, "3142213164987e-294", GMP_RNDN, x, "0.11101001101000000100111011111101111001010001001101111e-935", 20); CHECK53(y, "36167929443327e-159", GMP_RNDN, x, "0.11100111001110111110000101011001100110010100011111100e-483", 21); CHECK53(y, "904198236083175e-161", GMP_RNDN, x, "0.11100111001110111110000101011001100110010100011111100e-485", 22); CHECK53(y, "3743626360493413e-165", GMP_RNDN, x, "0.11000100000100011101001010111101011011011111011111001e-496", 23); CHECK53(y, "94080055902682397e-242", GMP_RNDN, x, "0.10110010010011000000111100011100111100110011011001010e-747", 24); CHECK53(y, "7e-303", GMP_RNDD, x, "0.10011001100111001000100110001110001000110111110001011e-1003", 25); CHECK53(y, "7e-303", GMP_RNDU, x, "0.10011001100111001000100110001110001000110111110001100e-1003", 26); CHECK53(y, "93e-234", GMP_RNDD, x, "0.10010011110110010111001001111001000010000000001110101E-770", 27); CHECK53(y, "93e-234", GMP_RNDU, x, "0.10010011110110010111001001111001000010000000001110110E-770", 28); CHECK53(y, "755e174", GMP_RNDD, x, "0.10111110110010011000110010011111101111000111111000101E588", 29); CHECK53(y, "755e174", GMP_RNDU, x, "0.10111110110010011000110010011111101111000111111000110E588", 30); CHECK53(y, "8699e-276", GMP_RNDD, x, "0.10010110100101101111100100100011011101100110100101100E-903", 31); CHECK53(y, "8699e-276", GMP_RNDU, x, "0.10010110100101101111100100100011011101100110100101101E-903", 32); CHECK53(y, "82081e41", GMP_RNDD, x, "0.10111000000010000010111011111001111010100011111001011E153", 33); CHECK53(y, "82081e41", GMP_RNDU, x, "0.10111000000010000010111011111001111010100011111001100E153", 34); CHECK53(y, "584169e229", GMP_RNDD, x, "0.11101011001010111000001011001110111000111100110101010E780", 35); CHECK53(y, "584169e229", GMP_RNDU, x, "0.11101011001010111000001011001110111000111100110101011E780", 36); CHECK53(y, "5783893e-128", GMP_RNDD, x, "0.10011000111100000110011110000101100111110011101110100E-402", 37); CHECK53(y, "5783893e-128", GMP_RNDU, x, "0.10011000111100000110011110000101100111110011101110101E-402", 38); CHECK53(y, "87575437e-310", GMP_RNDD, x, "0.11000000001011100000110011110011010000000010001010110E-1003", 39); CHECK53(y, "87575437e-310", GMP_RNDU, x, "0.11000000001011100000110011110011010000000010001010111E-1003", 40); CHECK53(y, "245540327e121", GMP_RNDD, x, "0.11100010101101001111010010110100011100000100101000100E430", 41); CHECK53(y, "245540327e121", GMP_RNDU, x, "0.11100010101101001111010010110100011100000100101000101E430", 42); CHECK53(y, "9078555839e-109", GMP_RNDD, x, "0.11111110001010111010110000110011100110001010011101101E-329", 43); CHECK53(y, "9078555839e-109", GMP_RNDU, x, "0.11111110001010111010110000110011100110001010011101110E-329", 44); CHECK53(y, "42333842451e201", GMP_RNDD, x, "0.10000000110001001101000100110110111110101011101011111E704", 45); CHECK53(y, "42333842451e201", GMP_RNDU, x, "0.10000000110001001101000100110110111110101011101100000E704", 46); CHECK53(y, "778380362293e218", GMP_RNDD, x, "0.11001101010111000001001100001100110010000001010010010E764", 47); CHECK53(y, "778380362293e218", GMP_RNDU, x, "0.11001101010111000001001100001100110010000001010010011E764", 48); CHECK53(y, "7812878489261e-179", GMP_RNDD, x, "0.10010011011011010111001111011101111101101101001110100E-551", 49); CHECK53(y, "7812878489261e-179", GMP_RNDU, x, "0.10010011011011010111001111011101111101101101001110101E-551", 50); CHECK53(y, "77003665618895e-73", GMP_RNDD, x, "0.11000101111110111111001111111101001101111000000101001E-196", 51); CHECK53(y, "77003665618895e-73", GMP_RNDU, x, "0.11000101111110111111001111111101001101111000000101010E-196", 52); CHECK53(y, "834735494917063e-300", GMP_RNDD, x, "0.11111110001101100001001101111100010011001110111010001E-947", 53); CHECK53(y, "834735494917063e-300", GMP_RNDU, x, "0.11111110001101100001001101111100010011001110111010010E-947", 54); CHECK53(y, "6182410494241627e-119", GMP_RNDD, x, "0.10001101110010110010001011000010001000101110100000111E-342", 55); CHECK53(y, "6182410494241627e-119", GMP_RNDU, x, "0.10001101110010110010001011000010001000101110100001000E-342", 56); CHECK53(y, "26153245263757307e49", GMP_RNDD, x, "0.10011110111100000000001011011110101100010000011011110E218", 57); CHECK53(y, "26153245263757307e49", GMP_RNDU, x, "0.10011110111100000000001011011110101100010000011011111E218", 58); /* to check this problem : I convert limb (10--0 or 101--1) into base b with more than mp_bits_per_limb digits, so when convert into base 2 I should have the limb that I have choose */ /* this use mpfr_get_str */ { size_t nb_digit = mp_bits_per_limb; mp_limb_t check_limb[2] = {MPFR_LIMB_HIGHBIT, ~(MPFR_LIMB_HIGHBIT >> 1)}; int base[3] = {10, 16, 19}; mp_rnd_t rnd[3] = {GMP_RNDU, GMP_RNDN, GMP_RNDD}; int cbase, climb, crnd; char *str; mpfr_set_prec (x, mp_bits_per_limb); /* x and y have only one limb */ mpfr_set_prec (y, mp_bits_per_limb); str = (char*) (*__gmp_allocate_func) (N + 20); mpfr_set_ui (x, 1, GMP_RNDN); /* ensures that x is not NaN or Inf */ for (; nb_digit < N; nb_digit *= 10) for (cbase = 0; cbase < 3; cbase++) for (climb = 0; climb < 2; climb++) for (crnd = 0; crnd < 3; crnd++) { char *str1; mp_exp_t exp; *(MPFR_MANT(x)) = check_limb[climb]; MPFR_EXP(x) = 0; mpfr_get_str (str + 2, &exp, base[cbase], nb_digit, x, rnd[crnd]); str[0] = '-'; str[(str[2] == '-')] = '0'; str[(str[2] == '-') + 1] = '.'; for (str1 = str; *str1 != 0; str1++) ; sprintf (str1, "@%i", (int) exp); mpfr_set_str (y, str, base[cbase], rnd[2 - crnd]); if (mpfr_cmp (x, y) != 0) { printf ("Error in mpfr_set_str for nb_digit=%u, base=%d, " "rnd=%s:\n", (unsigned int) nb_digit, base[cbase], mpfr_print_rnd_mode (rnd[crnd])); printf ("instead of: "); mpfr_print_binary (x); puts (""); printf ("return : "); mpfr_print_binary (y); puts (""); exit (1); } } (*__gmp_free_func) (str, N + 20); } /* end of tests added by Alain Delplanque */ /* check that flags are correctly cleared */ mpfr_set_nan (x); mpfr_set_str (x, "+0.0", 10, GMP_RNDN); if (!mpfr_number_p(x) || mpfr_cmp_ui (x, 0) != 0 || mpfr_sgn (x) < 0) { printf ("x <- +0.0 failed after x=NaN\n"); exit (1); } mpfr_set_str (x, "-0.0", 10, GMP_RNDN); if (!mpfr_number_p(x) || mpfr_cmp_ui (x, 0) != 0 || mpfr_sgn (x) > 0) { printf ("x <- -0.0 failed after x=NaN\n"); exit (1); } /* check invalid input */ ret = mpfr_set_str (x, "1E10toto", 10, GMP_RNDN); MPFR_ASSERTN (ret == -1); ret = mpfr_set_str (x, "1p10toto", 16, GMP_RNDN); MPFR_ASSERTN (ret == -1); ret = mpfr_set_str (x, "", 16, GMP_RNDN); MPFR_ASSERTN (ret == -1); ret = mpfr_set_str (x, "+", 16, GMP_RNDN); MPFR_ASSERTN (ret == -1); ret = mpfr_set_str (x, "-", 16, GMP_RNDN); MPFR_ASSERTN (ret == -1); ret = mpfr_set_str (x, "this_is_an_invalid_number_in_base_36", 36, GMP_RNDN); MPFR_ASSERTN (ret == -1); ret = mpfr_set_str (x, "1.2.3", 10, GMP_RNDN); MPFR_ASSERTN (ret == -1); mpfr_set_prec (x, 135); ret = mpfr_set_str (x, "thisisavalidnumberinbase36", 36, GMP_RNDN); mpfr_set_prec (y, 135); mpfr_set_str (y, "23833565676460972739462619524519814462546", 10, GMP_RNDN); MPFR_ASSERTN (mpfr_cmp (x, y) == 0 && ret == 0); /* coverage test for set_str_binary */ mpfr_set_str_binary (x, "NaN"); MPFR_ASSERTN(mpfr_nan_p (x)); mpfr_set_str_binary (x, "Inf"); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); mpfr_set_str_binary (x, "+Inf"); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0); mpfr_set_str_binary (x, "-Inf"); MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) < 0); mpfr_set_prec (x, 3); mpfr_set_str_binary (x, "0.01E2"); MPFR_ASSERTN(mpfr_cmp_ui (x, 1) == 0); mpfr_set_str_binary (x, "-0.01E2"); MPFR_ASSERTN(mpfr_cmp_si (x, -1) == 0); mpfr_clear (x); mpfr_clear (y); check_underflow (); tests_end_mpfr (); return 0; }
int main (int argc, char *argv[]) { mpfr_t x, y; unsigned long int n; tests_start_mpfr (); mpfr_init (x); mpfr_init (y); n = randlimb (); MPFR_SET_INF(x); mpfr_ui_pow (y, n, x, MPFR_RNDN); if(!MPFR_IS_INF(y)) { printf ("evaluation of function in INF does not return INF\n"); exit (1); } MPFR_CHANGE_SIGN(x); mpfr_ui_pow (y, n, x, MPFR_RNDN); if(!MPFR_IS_ZERO(y)) { printf ("evaluation of function in -INF does not return 0"); exit (1); } MPFR_SET_NAN(x); mpfr_ui_pow (y, n, x, MPFR_RNDN); if(!MPFR_IS_NAN(y)) { printf ("evaluation of function in NAN does not return NAN"); exit (1); } test1 (); { mpfr_t z, t; mpfr_prec_t prec; mpfr_rnd_t rnd; unsigned int n; mpfr_prec_t p0=2, p1=100; unsigned int N=20; mpfr_init2 (z, 38); mpfr_init2 (t, 6); /* check exact power */ mpfr_set_str_binary (t, "0.110000E5"); mpfr_ui_pow (z, 3, t, MPFR_RNDN); mpfr_set_prec (x, 2); mpfr_set_prec (y, 2); mpfr_set_str (x, "-0.5", 10, MPFR_RNDZ); mpfr_ui_pow (y, 4, x, MPFR_RNDD); if (mpfr_cmp_ui_2exp(y, 1, -1)) { fprintf (stderr, "Error for 4^(-0.5), prec=2, MPFR_RNDD\n"); fprintf (stderr, "expected 0.5, got "); mpfr_out_str (stderr, 2, 0, y, MPFR_RNDN); fprintf (stderr, "\n"); exit (1); } /* problem found by Kevin on spe175.testdrive.compaq.com (03 Sep 2003), ia64 under HP-UX */ mpfr_set_prec (x, 2); mpfr_set_prec (y, 2); mpfr_set_str (x, "0.5", 10, MPFR_RNDN); mpfr_ui_pow (y, 398441521, x, MPFR_RNDN); if (mpfr_cmp_ui_2exp(y, 1, 14)) { fprintf (stderr, "Error for 398441521^(0.5), prec=2, MPFR_RNDN\n"); fprintf (stderr, "expected 1.0e14, got "); mpfr_out_str (stderr, 2, 0, y, MPFR_RNDN); fprintf (stderr, "\n"); exit (1); } mpfr_clear (z); mpfr_clear (t); mpfr_set_prec (x, 2); mpfr_set_str (x, "0.5", 10, MPFR_RNDN); check1 (x, 2, 398441521, MPFR_RNDN); /* 398441521 = 19961^2 */ /* generic test */ for (prec = p0; prec <= p1; prec++) { mpfr_set_prec (x, prec); for (n=0; n<N; n++) { int nt; nt = randlimb () & INT_MAX; mpfr_urandomb (x, RANDS); rnd = RND_RAND (); check1 (x, prec, nt, rnd); } } } mpfr_clear (x); mpfr_clear (y); tests_end_mpfr (); return 0; }