static void consistency (void) { mpfr_t x, y, z1, z2; int i; mpfr_inits (x, y, z1, z2, (void *) 0); for (i = 0; i < 10000; i++) { mp_rnd_t rnd; mp_prec_t px, py, pz, p; int inex1, inex2; rnd = (mp_rnd_t) 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_random (x); do mpfr_random (y); 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, GMP_RNDN) != 0 || mpfr_prec_round (y, p, GMP_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, (void *) 0); }
/* worst cases communicated by Jean-Michel Muller and Vincent Lefevre */ static int check_worst_cases (void) { mpfr_t x; mpfr_t y; mpfr_init(x); mpfr_set_prec (x, 53); check_worst_case("4.44089209850062517562e-16", "1.00000000000000022204"); check_worst_case("6.39488462184069720009e-14", "1.00000000000006372680"); check_worst_case("1.84741111297455401935e-12", "1.00000000000184718907"); check_worst_case("1.76177628026265550074e-10", "1.00000000017617751702"); check3("1.76177628026265550074e-10", GMP_RNDN, "1.00000000017617773906"); check_worst_case("7.54175277499595900852e-10", "1.00000000075417516676"); check3("7.54175277499595900852e-10", GMP_RNDN, "1.00000000075417538881"); /* bug found by Vincent Lefe`vre on December 8, 1999 */ check3("-5.42410311287441459172e+02", GMP_RNDN, "2.7176584868845723e-236"); /* further cases communicated by Vincent Lefe`vre on January 27, 2000 */ check3("-1.32920285897904911589e-10", GMP_RNDN, "0.999999999867079769622"); check3("-1.44037948245738330735e-10", GMP_RNDN, "0.9999999998559621072757"); check3("-1.66795910430705305937e-10", GMP_RNDZ, "0.9999999998332040895832"); check3("-1.64310953745426656203e-10", GMP_RNDN, "0.9999999998356891017792"); check3("-1.38323574826034659172e-10", GMP_RNDZ, "0.9999999998616764251835"); check3("-1.23621668465115401498e-10", GMP_RNDZ, "0.9999999998763783315425"); mpfr_set_prec (x, 601); mpfr_set_str (x, "0.88b6ba510e10450edc258748bc9dfdd466f21b47ed264cdf24aa8f64af1f3fad9ec2301d43c0743f534b5aa20091ff6d352df458ef1ba519811ef6f5b11853534fd8fa32764a0a6d2d0dd20@0", 16, GMP_RNDZ); mpfr_init2 (y, 601); mpfr_exp_2 (y, x, GMP_RNDD); mpfr_exp_3 (x, x, GMP_RNDD); if (mpfr_cmp (x, y)) { printf ("mpfr_exp_2 and mpfr_exp_3 differ for prec=601\n"); printf ("mpfr_exp_2 gives "); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\nmpfr_exp_3 gives "); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf ("\n"); exit (1); } mpfr_set_prec (x, 13001); mpfr_set_prec (y, 13001); mpfr_random (x); mpfr_exp_3 (y, x, GMP_RNDN); mpfr_exp_2 (x, x, GMP_RNDN); if (mpfr_cmp (x, y)) { printf ("mpfr_exp_2 and mpfr_exp_3 differ for prec=13001\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); return 0; }
int main (void) { mp_prec_t p, q; mpfr_t x, y, z, u; mp_rnd_t rnd; int inexact, cmp; /* check prototypes of mpfr_init_set_* */ inexact = mpfr_init_set_si (x, -1, GMP_RNDN); inexact = mpfr_init_set (y, x, GMP_RNDN); inexact = mpfr_init_set_ui (z, 1, GMP_RNDN); inexact = mpfr_init_set_d (u, 1.0, GMP_RNDN); mpfr_set_prec (y, 11); mpfr_set_str_raw (y, "0.11111111100E-8"); mpfr_set_prec (x, 2); mpfr_set (x, y, GMP_RNDN); mpfr_set_str_raw (y, "1.0E-8"); if (mpfr_cmp (x, y)) { fprintf (stderr, "Error for y=0.11111111100E-8, prec=2, rnd=GMP_RNDN\n"); exit (1); } for (p=2; p<500; p++) { mpfr_set_prec (x, p); mpfr_random (x); if (LONG_RAND () % 2) mpfr_neg (x, x, GMP_RNDN); for (q=2; q<2*p; q++) { mpfr_set_prec (y, q); for (rnd=0; rnd<4; rnd++) { inexact = mpfr_set (y, x, rnd); cmp = mpfr_cmp (y, x); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { fprintf (stderr, "Wrong inexact flag in mpfr_set: expected %d, got %d\n", cmp, inexact); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (u); return 0; }
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); mpfr_random (x); 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 < GMP_RND_MAX; rnd++) { inexact = mpfr_div_ui (y, x, u, (mp_rnd_t) rnd); if (mpfr_mul_ui (z, y, u, (mp_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 ((mp_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); }
void check_inexact (mp_prec_t p) { mpfr_t x, y, z, t; unsigned long u; mp_prec_t q; int inexact, cmp; mp_rnd_t rnd; mpfr_init2 (x, p); mpfr_init (y); mpfr_init (z); mpfr_init (t); mpfr_random (x); u = LONG_RAND() % 2; for (q=2; q<=p; q++) for (rnd=0; rnd<4; rnd++) { mpfr_set_prec (y, q); mpfr_set_prec (z, q + 10); mpfr_set_prec (t, q); inexact = mpfr_pow_ui (y, x, u, rnd); cmp = mpfr_pow_ui (z, x, u, rnd); if (mpfr_can_round (z, q + 10, rnd, rnd, q)) { cmp = mpfr_set (t, z, rnd) || cmp; if (mpfr_cmp (y, t)) { fprintf (stderr, "results differ for u=%lu rnd=%s\n", u, mpfr_print_rnd_mode(rnd)); printf ("x="); mpfr_print_binary (x); putchar ('\n'); printf ("y="); mpfr_print_binary (y); putchar ('\n'); printf ("t="); mpfr_print_binary (t); putchar ('\n'); printf ("z="); mpfr_print_binary (z); putchar ('\n'); exit (1); } if (((inexact == 0) && (cmp != 0)) || ((inexact != 0) && (cmp == 0))) { fprintf (stderr, "Wrong inexact flag for p=%u, q=%u, rnd=%s\n", (unsigned) p, (unsigned) q, mpfr_print_rnd_mode (rnd)); printf ("expected %d, got %d\n", cmp, inexact); printf ("u=%lu x=", u); mpfr_print_binary (x); putchar ('\n'); printf ("y="); mpfr_print_binary (y); putchar ('\n'); exit (1); } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (t); }
static void check_inexact (void) { mp_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_random (x); if (randlimb () % 2) { mpfr_set (absx, x, GMP_RNDN); mpfr_neg (x, x, GMP_RNDN); } else mpfr_set (absx, x, GMP_RNDN); for (q=2; q<2*p; q++) { mpfr_set_prec (y, q); for (rnd = 0; rnd < GMP_RND_MAX; rnd++) { inexact = mpfr_abs (y, x, (mp_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_print_binary (x); puts (""); printf ("absx="); mpfr_print_binary (absx); puts (""); printf ("y="); mpfr_print_binary (y); puts (""); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (absx); }
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, GMP_RNDN); break; case 10: mpfr_const_pi (x, GMP_RNDN); MPFR_SET_EXP (x, MPFR_GET_EXP (x)-1); break; default: mpfr_random (x); break; } }
static void check_sgn(void) { mpfr_t x; int i, s1, s2; mpfr_init(x); for(i = 0 ; i < 100 ; i++) { mpfr_random(x); if (i&1) { MPFR_SET_POS(x); s2 = 1; } else { MPFR_SET_NEG(x); s2 = -1; } s1 = mpfr_sgn(x); if (s1 < -1 || s1 > 1) { printf("Error for sgn: out of range.\n"); goto lexit; } else if (MPFR_IS_NAN(x) || MPFR_IS_ZERO(x)) { if (s1 != 0) { printf("Error for sgn: Nan or Zero should return 0.\n"); goto lexit; } } else if (s1 != s2) { printf("Error for sgn. Return %d instead of %d.\n", s1, s2); goto lexit; } } mpfr_clear(x); return; lexit: mpfr_clear(x); exit(1); }
static void compare_exp2_exp3 (int n) { mpfr_t x, y, z; int prec; mp_rnd_t rnd; mpfr_init (x); mpfr_init (y); mpfr_init (z); for (prec = 20; prec <= n; prec++) { mpfr_set_prec (x, prec); mpfr_set_prec (y, prec); mpfr_set_prec (z, prec); mpfr_random (x); rnd = (mp_rnd_t) RND_RAND(); mpfr_exp_2 (y, x, rnd); mpfr_exp_3 (z, x, rnd); if (mpfr_cmp (y,z)) { printf ("mpfr_exp_2 and mpfr_exp_3 disagree for rnd=%s and\nx=", mpfr_print_rnd_mode (rnd)); mpfr_print_binary (x); puts (""); printf ("mpfr_exp_2 gives "); mpfr_print_binary (y); puts (""); printf ("mpfr_exp_3 gives "); mpfr_print_binary (z); puts (""); exit (1); } } /* bug found by Patrick Pe'lissier on 7 Jun 2004 */ prec = 203780; mpfr_set_prec (x, prec); mpfr_set_prec (z, prec); mpfr_set_d (x, 3.0, GMP_RNDN); mpfr_sqrt (x, x, GMP_RNDN); mpfr_sub_ui (x, x, 1, GMP_RNDN); mpfr_exp_3 (z, x, GMP_RNDN); mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
/* if u = o(x-y), v = o(u-x), w = o(v+y), then x-y = u-w */ static void check_two_sum (mp_prec_t p) { unsigned int x; mpfr_t y, u, v, w; mp_rnd_t rnd; int inexact; mpfr_inits2 (p, y, u, v, w, NULL); do { x = randlimb (); } while (x < 1); mpfr_random (y); rnd = GMP_RNDN; inexact = mpfr_ui_sub (u, x, y, rnd); mpfr_sub_ui (v, u, x, rnd); mpfr_add (w, v, y, rnd); /* as u = (x-y) + 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, NULL); }
static void special (void) { mpfr_t x, y, res; int inexact; mpfr_init (x); mpfr_init (y); mpfr_init (res); mpfr_set_prec (x, 24); mpfr_set_prec (y, 24); mpfr_set_str_binary (y, "0.111100110001011010111"); inexact = mpfr_ui_sub (x, 1, y, GMP_RNDN); if (inexact) { printf ("Wrong inexact flag: got %d, expected 0\n", inexact); exit (1); } mpfr_set_prec (x, 24); mpfr_set_prec (y, 24); mpfr_set_str_binary (y, "0.111100110001011010111"); if ((inexact = mpfr_ui_sub (x, 38181761, y, GMP_RNDN)) >= 0) { printf ("Wrong inexact flag: got %d, expected -1\n", inexact); exit (1); } mpfr_set_prec (x, 63); mpfr_set_prec (y, 63); mpfr_set_str_binary (y, "0.111110010010100100110101101010001001100101110001000101110111111E-1"); if ((inexact = mpfr_ui_sub (x, 1541116494, y, GMP_RNDN)) <= 0) { printf ("Wrong inexact flag: got %d, expected +1\n", inexact); exit (1); } mpfr_set_prec (x, 32); mpfr_set_prec (y, 32); mpfr_set_str_binary (y, "0.11011000110111010001011100011100E-1"); if ((inexact = mpfr_ui_sub (x, 2000375416, y, GMP_RNDN)) >= 0) { printf ("Wrong inexact flag: got %d, expected -1\n", inexact); exit (1); } mpfr_set_prec (x, 24); mpfr_set_prec (y, 24); mpfr_set_str_binary (y, "0.110011011001010011110111E-2"); if ((inexact = mpfr_ui_sub (x, 927694848, y, GMP_RNDN)) <= 0) { printf ("Wrong inexact flag: got %d, expected +1\n", inexact); exit (1); } /* bug found by Mathieu Dutour, 12 Apr 2001 */ mpfr_set_prec (x, 5); mpfr_set_prec (y, 5); mpfr_set_prec (res, 5); mpfr_set_str_binary (x, "1e-12"); mpfr_ui_sub (y, 1, x, GMP_RNDD); mpfr_set_str_binary (res, "0.11111"); if (mpfr_cmp (y, res)) { printf ("Error in mpfr_ui_sub (y, 1, x, GMP_RNDD) for x=2^(-12)\nexpected 1.1111e-1, got "); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\n"); exit (1); } mpfr_ui_sub (y, 1, x, GMP_RNDU); mpfr_set_str_binary (res, "1.0"); if (mpfr_cmp (y, res)) { printf ("Error in mpfr_ui_sub (y, 1, x, GMP_RNDU) for x=2^(-12)\n" "expected 1.0, got "); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\n"); exit (1); } mpfr_ui_sub (y, 1, x, GMP_RNDN); mpfr_set_str_binary (res, "1.0"); if (mpfr_cmp (y, res)) { printf ("Error in mpfr_ui_sub (y, 1, x, GMP_RNDN) for x=2^(-12)\n" "expected 1.0, got "); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\n"); exit (1); } mpfr_set_prec (x, 10); mpfr_set_prec (y, 10); mpfr_random (x); mpfr_ui_sub (y, 0, x, GMP_RNDN); if (MPFR_IS_ZERO(x)) MPFR_ASSERTN(MPFR_IS_ZERO(y)); else MPFR_ASSERTN(mpfr_cmpabs (x, y) == 0 && mpfr_sgn (x) != mpfr_sgn (y)); mpfr_set_prec (x, 73); mpfr_set_str_binary (x, "0.1101111010101011011011100011010000000101110001011111001011011000101111101E-99"); mpfr_ui_sub (x, 1, x, GMP_RNDZ); mpfr_nextabove (x); MPFR_ASSERTN(mpfr_cmp_ui (x, 1) == 0); mpfr_clear (x); mpfr_clear (y); mpfr_clear (res); }
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; }
static void check_inexact (void) { mpfr_t x, y, z, u; mp_prec_t px, py, pu; int inexact, cmp; mp_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, GMP_RNDN); mpfr_div (x, x, z, GMP_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, GMP_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, GMP_RNDN); printf ("\n"); printf ("u="); mpfr_out_str (stdout, 10, 99, u, GMP_RNDN); printf ("\n"); printf ("y="); mpfr_out_str (stdout, 10, 99, y, GMP_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, GMP_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, GMP_RNDN)) <= 0) { printf ("Wrong inexact flag for rnd=GMP_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_random (x); for (pu=2; pu<=MAX_PREC; pu++) { mpfr_set_prec (u, pu); do { mpfr_random (u); } 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 = (mp_rnd_t) 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 (void) { mp_prec_t p, q; mpfr_t x, y, z, u; int rnd; int inexact, cmp; mp_exp_t emax; tests_start_mpfr (); /* check prototypes of mpfr_init_set_* */ inexact = mpfr_init_set_si (x, -1, GMP_RNDN); inexact = mpfr_init_set (y, x, GMP_RNDN); inexact = mpfr_init_set_ui (z, 1, GMP_RNDN); inexact = mpfr_init_set_d (u, 1.0, GMP_RNDN); mpfr_set_nan (x); (mpfr_set) (y, x, GMP_RNDN); MPFR_ASSERTN(mpfr_nan_p (y)); mpfr_set_inf (x, 1); mpfr_set (y, x, GMP_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && mpfr_sgn (y) > 0); mpfr_set_inf (x, -1); mpfr_set (y, x, GMP_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && mpfr_sgn (y) < 0); mpfr_set_ui (x, 0, GMP_RNDN); mpfr_set (y, x, GMP_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS(y)); mpfr_set_ui (x, 0, GMP_RNDN); mpfr_neg (x, x, GMP_RNDN); mpfr_set (y, x, GMP_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_NEG(y)); emax = mpfr_get_emax (); set_emax (0); mpfr_set_prec (x, 3); mpfr_set_str_binary (x, "0.111"); mpfr_set_prec (y, 2); mpfr_set (y, x, GMP_RNDU); if (!(MPFR_IS_INF (y) && MPFR_SIGN (y) > 0)) { printf ("Error for y=x=0.111 with px=3, py=2 and emax=0\nx="); mpfr_dump (x); printf ("y="); mpfr_dump (y); exit (1); } MPFR_ASSERTN (MPFR_IS_INF (y) && MPFR_SIGN (y) > 0); set_emax (emax); mpfr_set_prec (y, 11); mpfr_set_str_binary (y, "0.11111111100E-8"); mpfr_set_prec (x, 2); mpfr_set (x, y, GMP_RNDN); mpfr_set_str_binary (y, "1.0E-8"); if (mpfr_cmp (x, y)) { printf ("Error for y=0.11111111100E-8, prec=2, rnd=GMP_RNDN\n"); exit (1); } for (p=2; p<500; p++) { mpfr_set_prec (x, p); mpfr_random (x); if (randlimb () % 2) mpfr_neg (x, x, GMP_RNDN); for (q=2; q<2*p; q++) { mpfr_set_prec (y, q); for (rnd = 0; rnd < GMP_RND_MAX; rnd++) { inexact = mpfr_set (y, x, (mp_rnd_t) rnd); cmp = mpfr_cmp (y, x); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { printf ("Wrong inexact flag in mpfr_set: expected %d," " got %d\n", cmp, inexact); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (u); check_neg_special (); tests_end_mpfr (); return 0; }
static void check_hard (void) { mpfr_t u, v, q, q2; mp_prec_t precu, precv, precq; int rnd; int inex, inex2, i, j; mpfr_init (q); mpfr_init (q2); mpfr_init (u); mpfr_init (v); for (precq = MPFR_PREC_MIN; precq <= 64; precq ++) { mpfr_set_prec (q, precq); mpfr_set_prec (q2, precq + 1); for (j = 0; j < 2; j++) { if (j == 0) { do { mpfr_random (q2); } while (mpfr_cmp_ui (q2, 0) == 0); } else /* use q2=1 */ mpfr_set_ui (q2, 1, GMP_RNDN); for (precv = precq; precv <= 10 * precq; precv += precq) { mpfr_set_prec (v, precv); do { mpfr_random (v); } while (mpfr_cmp_ui (v, 0) == 0); for (precu = precq; precu <= 10 * precq; precu += precq) { mpfr_set_prec (u, precu); mpfr_mul (u, v, q2, GMP_RNDN); mpfr_nextbelow (u); for (i = 0; i <= 2; i++) { for (rnd = 0; rnd < GMP_RND_MAX; rnd++) { inex = test_div (q, u, v, (mp_rnd_t) rnd); inex2 = get_inexact (q, u, v); if (inex_cmp (inex, inex2)) { printf ("Wrong inexact flag for rnd=%s: expected %d, got %d\n", mpfr_print_rnd_mode ((mp_rnd_t) rnd), inex2, inex); printf ("u= "); mpfr_dump (u); printf ("v= "); mpfr_dump (v); printf ("q= "); mpfr_dump (q); mpfr_set_prec (q2, precq + precv); mpfr_mul (q2, q, v, GMP_RNDN); printf ("q*v="); mpfr_dump (q2); exit (1); } } mpfr_nextabove (u); } } } } } mpfr_clear (q); mpfr_clear (q2); mpfr_clear (u); mpfr_clear (v); }
static void check_lowr (void) { mpfr_t x, y, z, z2, z3, tmp; int k, c, c2; mpfr_init2 (x, 1000); mpfr_init2 (y, 100); mpfr_init2 (tmp, 850); mpfr_init2 (z, 10); mpfr_init2 (z2, 10); mpfr_init2 (z3, 50); for (k = 1; k < KMAX; k++) { do { mpfr_random (z); } while (mpfr_cmp_ui (z, 0) == 0); do { mpfr_random (tmp); } while (mpfr_cmp_ui (tmp, 0) == 0); mpfr_mul (x, z, tmp, GMP_RNDN); /* exact */ c = test_div (z2, x, tmp, GMP_RNDN); if (c || mpfr_cmp (z2, z)) { printf ("Error in mpfr_div rnd=GMP_RNDN\n"); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d, expected 0\n", c); exit (1); } } /* x has still precision 1000, z precision 10, and tmp prec 850 */ mpfr_set_prec (z2, 9); for (k = 1; k < KMAX; k++) { mpfr_random (z); do { mpfr_random (tmp); } while (mpfr_cmp_ui (tmp, 0) == 0); mpfr_mul (x, z, tmp, GMP_RNDN); /* exact */ c = test_div (z2, x, tmp, GMP_RNDN); /* since z2 has one less bit that z, either the division is exact if z is representable on 9 bits, or we have an even round case */ c2 = get_inexact (z2, x, tmp); if ((mpfr_cmp (z2, z) == 0 && c) || inex_cmp (c, c2)) { printf ("Error in mpfr_div rnd=GMP_RNDN\n"); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d, expected %d\n", c, c2); exit (1); } else if (c == 2) { mpfr_nexttoinf (z); if (mpfr_cmp(z2, z)) { printf ("Error in mpfr_div [even rnd?] rnd=GMP_RNDN\n"); printf ("Dividing "); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d\n", 1); exit (1); } } else if (c == -2) { mpfr_nexttozero (z); if (mpfr_cmp(z2, z)) { printf ("Error in mpfr_div [even rnd?] rnd=GMP_RNDN\n"); printf ("Dividing "); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d\n", 1); exit (1); } } } mpfr_set_prec(x, 1000); mpfr_set_prec(y, 100); mpfr_set_prec(tmp, 850); mpfr_set_prec(z, 10); mpfr_set_prec(z2, 10); /* almost exact divisions */ for (k = 1; k < KMAX; k++) { do { mpfr_random(z); } while (mpfr_cmp_ui (z, 0) == 0); do { mpfr_random (tmp); } while (mpfr_cmp_ui (tmp, 0) == 0); mpfr_mul(x, z, tmp, GMP_RNDN); mpfr_set(y, tmp, GMP_RNDD); mpfr_nexttoinf (x); c = test_div(z2, x, y, GMP_RNDD); test_div(z3, x, y, GMP_RNDD); mpfr_set(z, z3, GMP_RNDD); if (c != -1 || mpfr_cmp(z2, z)) { printf ("Error in mpfr_div rnd=GMP_RNDD\n"); printf ("got "); mpfr_print_binary(z2); puts (""); printf ("instead of "); mpfr_print_binary(z); puts (""); printf ("inex flag = %d\n", c); exit (1); } mpfr_set (y, tmp, GMP_RNDU); test_div (z3, x, y, GMP_RNDU); mpfr_set (z, z3, GMP_RNDU); c = test_div (z2, x, y, GMP_RNDU); if (c != 1 || mpfr_cmp (z2, z)) { printf ("Error in mpfr_div rnd=GMP_RNDU\n"); printf ("u="); mpfr_dump (x); printf ("v="); mpfr_dump (y); printf ("got "); mpfr_print_binary (z2); puts (""); printf ("instead of "); mpfr_print_binary (z); puts (""); printf ("inex flag = %d\n", c); exit (1); } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (z2); mpfr_clear (z3); mpfr_clear (tmp); }
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 (void) { mpfr_t x, u; mpf_t y, z; mp_exp_t emax; unsigned long k, pr; int r, inexact; MPFR_TEST_USE_RANDS (); 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, GMP_RNDN); mpfr_set_prec (x, 100); mpfr_set_f (x, y, GMP_RNDN); mpf_random2 (y, 10, 0); mpfr_set_f (x, y, (mp_rnd_t) 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, GMP_RNDN); mpf_set_str (y, "2033033E-3", 10); /* avoid 2033.033 which is locale-sensitive */ mpfr_set_f (x, y, GMP_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, GMP_RNDN); mpfr_neg (u, u, GMP_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, GMP_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); for (k = 1; k <= 100000; k++) { pr = 2 + (randlimb () & 255); mpf_set_prec (z, pr); mpf_random2 (z, z->_mp_prec, 0); mpfr_set_prec (x, pr); mpfr_set_f (x, z, (mp_rnd_t) 0); } /* Check for +0 */ mpfr_set_prec (x, 53); mpf_set_prec (y, 53); mpf_set_ui (y, 0); for (r = 0 ; r < GMP_RND_MAX ; r++) { int i; for (i = -1; i <= 1; i++) { if (i) mpfr_set_si (x, i, GMP_RNDN); inexact = mpfr_set_f (x, y, (mp_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 ((mp_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_random (x); /* to fill low limbs with random data */ inexact = mpfr_set_f (x, y, GMP_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, GMP_RNDN); mpfr_set_ui (u, 1, GMP_RNDN); mpfr_mul_2ui (u, u, ULONG_MAX, GMP_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, GMP_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, GMP_RNDN); mpfr_set_ui_2exp (u, 1, emax, GMP_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, GMP_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, GMP_RNDN); mpfr_set_ui_2exp (u, 1, emax - 1, GMP_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, GMP_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 special (void) { mpfr_t x, y; unsigned xprec, yprec; mpfr_init (x); mpfr_init (y); mpfr_set_prec (x, 32); mpfr_set_prec (y, 32); mpfr_set_ui (x, 1, GMP_RNDN); mpfr_div_ui (y, x, 3, GMP_RNDN); mpfr_set_prec (x, 100); mpfr_set_prec (y, 100); mpfr_random (x); mpfr_div_ui (y, x, 123456, GMP_RNDN); mpfr_set_ui (x, 0, GMP_RNDN); mpfr_div_ui (y, x, 123456789, GMP_RNDN); if (mpfr_cmp_ui (y, 0)) { printf ("mpfr_div_ui gives non-zero for 0/ui\n"); exit (1); } /* bug found by Norbert Mueller, 21 Aug 2001 */ mpfr_set_prec (x, 110); mpfr_set_prec (y, 60); mpfr_set_str_binary (x, "0.110101110011111110011111001110011001110111000000111110001000111011000011E-44"); mpfr_div_ui (y, x, 17, GMP_RNDN); mpfr_set_str_binary (x, "0.11001010100101100011101110000001100001010110101001010011011E-48"); if (mpfr_cmp (x, y)) { printf ("Error in x/17 for x=1/16!\n"); printf ("Expected "); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf ("\nGot "); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\n"); exit (1); } /* corner case */ mpfr_set_prec (x, 2 * mp_bits_per_limb); mpfr_set_prec (y, 2); mpfr_set_ui (x, 4, GMP_RNDN); mpfr_nextabove (x); mpfr_div_ui (y, x, 2, GMP_RNDN); /* exactly in the middle */ MPFR_ASSERTN(mpfr_cmp_ui (y, 2) == 0); mpfr_set_prec (x, 3 * mp_bits_per_limb); mpfr_set_prec (y, 2); mpfr_set_ui (x, 2, GMP_RNDN); mpfr_nextabove (x); mpfr_div_ui (y, x, 2, GMP_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); mpfr_set_prec (x, 3 * mp_bits_per_limb); mpfr_set_prec (y, 2); mpfr_set_si (x, -4, GMP_RNDN); mpfr_nextbelow (x); mpfr_div_ui (y, x, 2, GMP_RNDD); MPFR_ASSERTN(mpfr_cmp_si (y, -3) == 0); for (xprec = 53; xprec <= 128; xprec++) { mpfr_set_prec (x, xprec); mpfr_set_str_binary (x, "0.1100100100001111110011111000000011011100001100110111E2"); for (yprec = 53; yprec <= 128; yprec++) { mpfr_set_prec (y, yprec); mpfr_div_ui (y, x, 1, GMP_RNDN); if (mpfr_cmp(x,y)) { printf ("division by 1.0 fails for xprec=%u, yprec=%u\n", xprec, yprec); printf ("expected "); mpfr_print_binary (x); puts (""); printf ("got "); mpfr_print_binary (y); puts (""); exit (1); } } } mpfr_clear (x); mpfr_clear (y); }
int main (int argc, char *argv[]) { mpfr_t x, y, z, ax; long int iy; mpfr_init (x); mpfr_init (ax); mpfr_init2 (y,sizeof(unsigned long int)*CHAR_BIT); mpfr_init (z); MPFR_SET_NAN(x); mpfr_random(y); mpfr_pow (z, x,y, GMP_RNDN); if(!MPFR_IS_NAN(z)) { printf ("evaluation of function in x=NAN does not return NAN"); exit (1); } MPFR_SET_NAN(y); mpfr_random(x); mpfr_pow (z, x,y, GMP_RNDN); if(!MPFR_IS_NAN(z)) { printf ("evaluation of function in y=NAN does not return NAN"); exit (1); } MPFR_CLEAR_FLAGS(z); MPFR_CLEAR_FLAGS(y); MPFR_CLEAR_FLAGS(x); MPFR_SET_ZERO(y); mpfr_random(x); mpfr_pow (z, x,y, GMP_RNDN); if(mpfr_cmp_ui(z,1)!=0 && !(MPFR_IS_NAN(x))) { printf ("evaluation of function in y=0 does not return 1\n"); printf ("x ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); exit (1); } MPFR_CLEAR_FLAGS(z); MPFR_CLEAR_FLAGS(y); MPFR_CLEAR_FLAGS(x); MPFR_SET_INF(y); if (MPFR_SIGN(y) < 0) MPFR_CHANGE_SIGN(y); mpfr_random(x); mpfr_set_prec (ax, MPFR_PREC(x)); mpfr_abs(ax,x,GMP_RNDN); mpfr_pow (z, x,y, GMP_RNDN); if( !MPFR_IS_INF(z) && (mpfr_cmp_ui(ax,1) > 0) ) { printf ("evaluation of function in y=INF (|x|>1) does not return INF"); exit (1); } if( !MPFR_IS_ZERO(z) && (mpfr_cmp_ui(ax,1) < 0) ) { printf ("\nevaluation of function in y=INF (|x|<1) does not return 0"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); exit (1); } MPFR_CLEAR_FLAGS(z); MPFR_CLEAR_FLAGS(y); MPFR_CLEAR_FLAGS(x); MPFR_SET_INF(y); if (MPFR_SIGN(y) > 0) MPFR_CHANGE_SIGN(y); mpfr_random(x); mpfr_set_prec (ax, MPFR_PREC(x)); mpfr_abs(ax,x,GMP_RNDN); mpfr_pow (z, x,y, GMP_RNDN); mpfr_pow (z, x,y, GMP_RNDN); if( !MPFR_IS_INF(z) && (mpfr_cmp_ui(ax,1) < 0) ) { printf ("evaluation of function in y=INF (for |x| <0) does not return INF"); exit (1); } if( !MPFR_IS_ZERO(z) && (mpfr_cmp_ui(ax,1) > 0) ) { printf ("evaluation of function in y=INF (for |x| >0) does not return 0"); exit (1); } MPFR_CLEAR_FLAGS(z); MPFR_CLEAR_FLAGS(y); MPFR_CLEAR_FLAGS(x); MPFR_SET_INF(x); if (MPFR_SIGN(x) < 0) MPFR_CHANGE_SIGN(x); mpfr_random(y); mpfr_pow (z, x,y, GMP_RNDN); if(!MPFR_IS_INF(z) && (MPFR_SIGN(y) > 0)) { printf ("evaluation of function in INF does not return INF"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); exit (1); } if(!MPFR_IS_ZERO(z) && (MPFR_SIGN(y) < 0)) { printf ("evaluation of function in INF does not return INF"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); exit (1); } MPFR_CLEAR_FLAGS(z); MPFR_CLEAR_FLAGS(y); MPFR_CLEAR_FLAGS(x); MPFR_SET_INF(x); if (MPFR_SIGN(x) > 0) MPFR_CHANGE_SIGN(x); mpfr_random(y); if (random() % 2) mpfr_neg (y, y, GMP_RNDN); mpfr_pow (z, x,y, GMP_RNDN); if(!MPFR_IS_INF(z) && (MPFR_SIGN(y) > 0) && (mpfr_isinteger(y))) { printf ("evaluation of function in x=-INF does not return INF"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); if(mpfr_isinteger(y)) printf("y is an integer\n"); else printf("y is not an integer\n"); exit (1); } if(!MPFR_IS_ZERO(z) && (MPFR_SIGN(y) < 0) && (mpfr_isinteger(y))) { printf ("evaluation of function in x=-INF does not return 0"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); if(mpfr_isinteger(y)) printf("y is an integer\n"); else printf("y is not an integer\n"); exit (1); } MPFR_CLEAR_FLAGS(z); MPFR_CLEAR_FLAGS(y); MPFR_CLEAR_FLAGS(x); MPFR_SET_INF(x); if (MPFR_SIGN(x) > 0) MPFR_CHANGE_SIGN(x); iy=random(); mpfr_random(y); if (random() % 2) iy=-iy; mpfr_set_d(y,iy,GMP_RNDN); mpfr_pow (z, x,y, GMP_RNDN); if(!MPFR_IS_INF(z) && (MPFR_SIGN(y) > 0) && (mpfr_isinteger(y))) { printf ("evaluation of function in x=-INF does not return INF"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); if(mpfr_isinteger(y)) printf("y is an integer\n"); else printf("y is not an integer\n"); exit (1); } if(!MPFR_IS_ZERO(z) && (MPFR_SIGN(y) < 0) && (mpfr_isinteger(y))) { printf ("evaluation of function in x=-INF does not return 0"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); if(mpfr_isinteger(y)) printf("y is an integer\n"); else printf("y is not an integer\n"); exit (1); } MPFR_CLEAR_FLAGS(z); MPFR_CLEAR_FLAGS(y); MPFR_CLEAR_FLAGS(x); mpfr_set_ui(x,1,GMP_RNDN); MPFR_SET_INF(y); mpfr_pow (z, x,y, GMP_RNDN); if(!MPFR_IS_NAN(z)) { printf ("evaluation of function in x=1, y=INF does not return NAN"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); exit (1); } MPFR_CLEAR_FLAGS(z); MPFR_CLEAR_FLAGS(y); MPFR_CLEAR_FLAGS(x); MPFR_SET_ZERO(x); mpfr_random(y); if (random() % 2) mpfr_neg (y, y, GMP_RNDN); mpfr_pow (z, x,y, GMP_RNDN); if(!MPFR_IS_ZERO(z) && (MPFR_SIGN(y) < 0) && !(mpfr_isinteger(y))) { printf ("evaluation of function in y<0 does not return 0"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); exit (1); } if(!MPFR_IS_INF(z) && (MPFR_SIGN(y) < 0) && (mpfr_isinteger(y))) { printf ("evaluation of function in y<0 (y integer) does not return INF"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); exit (1); } if(!MPFR_IS_ZERO(z) && (MPFR_SIGN(y) > 0) && (mpfr_isinteger(y))) { printf ("evaluation of function in y<0 (y integer) does not return 0"); printf ("\nx ="); mpfr_out_str (stdout, 10, MPFR_PREC(x), x, GMP_RNDN); printf ("\n y ="); mpfr_out_str (stdout, 10, MPFR_PREC(y), y, GMP_RNDN); printf ("\n result ="); mpfr_out_str (stdout, 10, MPFR_PREC(z), z, GMP_RNDN); putchar('\n'); exit (1); } { mp_prec_t prec, yprec; mpfr_t t, s; mp_rnd_t rnd; int inexact, compare, compare2; unsigned int n, err; int p0=2; int p1=100; int N=100; mpfr_init (s); mpfr_init (t); /* generic test */ for (prec = p0; prec <= p1; prec++) { mpfr_set_prec (x, prec); mpfr_set_prec (s, sizeof(unsigned long int)*CHAR_BIT); mpfr_set_prec (z, prec); mpfr_set_prec (t, prec); yprec = prec + 10; for (n=0; n<N; n++) { mpfr_random (x); mpfr_random (s); if (random() % 2) mpfr_neg (s, s, GMP_RNDN); rnd = random () % 4; mpfr_set_prec (y, yprec); compare = mpfr_pow (y, x, s, rnd); err = (rnd == GMP_RNDN) ? yprec + 1 : yprec; if (mpfr_can_round (y, err, rnd, rnd, prec)) { mpfr_set (t, y, rnd); inexact = mpfr_pow (z,x, s, rnd); if (mpfr_cmp (t, z)) { printf ("results differ for x="); mpfr_out_str (stdout, 2, prec, x, GMP_RNDN); printf (" values of the exponential="); mpfr_out_str (stdout, 2, prec, s, GMP_RNDN); printf (" prec=%u rnd_mode=%s\n", (unsigned) prec, mpfr_print_rnd_mode (rnd)); printf ("got "); mpfr_out_str (stdout, 2, prec, z, GMP_RNDN); putchar ('\n'); printf ("expected "); mpfr_out_str (stdout, 2, prec, t, GMP_RNDN); putchar ('\n'); printf ("approx "); mpfr_print_binary (y); putchar ('\n'); exit (1); } compare2 = mpfr_cmp (t, y); /* if rounding to nearest, cannot know the sign of t - f(x) because of composed rounding: y = o(f(x)) and t = o(y) */ if ((rnd != GMP_RNDN) && (compare * compare2 >= 0)) compare = compare + compare2; else compare = inexact; /* cannot determine sign(t-f(x)) */ if (((inexact == 0) && (compare != 0)) || ((inexact > 0) && (compare <= 0)) || ((inexact < 0) && (compare >= 0))) { fprintf (stderr, "Wrong inexact flag for rnd=%s: expected %d, got %d\n", mpfr_print_rnd_mode (rnd), compare, inexact); printf ("x="); mpfr_print_binary (x); putchar ('\n'); printf ("y="); mpfr_print_binary (y); putchar ('\n'); printf ("t="); mpfr_print_binary (t); putchar ('\n'); exit (1); } } } } mpfr_clear (s); mpfr_clear (t); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (ax); return 0; }
void check_inexact (void) { mpfr_t x, y, z, u; mp_prec_t px, py, pu, pz; int inexact, cmp; mp_rnd_t rnd; mpfr_init (x); mpfr_init (y); mpfr_init (z); mpfr_init (u); mpfr_set_prec (x, 2); mpfr_set_str_raw (x, "0.1E-4"); mpfr_set_prec (u, 33); mpfr_set_str_raw (u, "0.101110100101101100000000111100000E-1"); mpfr_set_prec (y, 31); if ((inexact = mpfr_add (y, x, u, GMP_RNDN))) { fprintf (stderr, "Wrong inexact flag (2): expected 0, got %d\n", inexact); exit (1); } mpfr_set_prec (x, 2); mpfr_set_str_raw (x, "0.1E-4"); mpfr_set_prec (u, 33); mpfr_set_str_raw (u, "0.101110100101101100000000111100000E-1"); mpfr_set_prec (y, 28); if ((inexact = mpfr_add (y, x, u, GMP_RNDN))) { fprintf (stderr, "Wrong inexact flag (1): expected 0, got %d\n", inexact); exit (1); } for (px=2; px<MAX_PREC; px++) { mpfr_set_prec (x, px); mpfr_random (x); for (pu=2; pu<MAX_PREC; pu++) { mpfr_set_prec (u, pu); mpfr_random (u); for (py=2; py<MAX_PREC; py++) { mpfr_set_prec (y, py); pz = (mpfr_cmp_abs (x, u) >= 0) ? MPFR_EXP(x)-MPFR_EXP(u) : MPFR_EXP(u)-MPFR_EXP(x); /* x + u is exactly representable with precision abs(EXP(x)-EXP(u)) + max(prec(x), prec(u)) + 1 */ pz = pz + MAX(MPFR_PREC(x), MPFR_PREC(u)) + 1; mpfr_set_prec (z, pz); rnd = LONG_RAND () % 4; if (mpfr_add (z, x, u, rnd)) { fprintf (stderr, "z <- x + u should be exact\n"); printf ("x="); mpfr_print_binary (x); putchar ('\n'); printf ("u="); mpfr_print_binary (u); putchar ('\n'); printf ("z="); mpfr_print_binary (z); putchar ('\n'); exit (1); } for (rnd=0; rnd<4; rnd++) { inexact = mpfr_add (y, x, u, rnd); cmp = mpfr_cmp (y, z); if (((inexact == 0) && (cmp != 0)) || ((inexact > 0) && (cmp <= 0)) || ((inexact < 0) && (cmp >= 0))) { fprintf (stderr, "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); putchar ('\n'); printf ("u="); mpfr_print_binary (u); putchar ('\n'); printf ("y= "); mpfr_print_binary (y); putchar ('\n'); printf ("x+u="); mpfr_print_binary (z); putchar ('\n'); exit (1); } } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (u); }
static void check_cmp(int argc, char *argv[]) { mpfr_t x, y; int n, k, rnd; mpfr_inits2 (53, x, y, (void *) 0); mpfr_set_ui(x, 1, GMP_RNDN); (mpfr_abs) (x, x, GMP_RNDN); if (mpfr_cmp_ui (x, 1)) { printf ("Error in mpfr_abs(1.0)\n"); exit (1); } mpfr_set_si(x, -1, GMP_RNDN); mpfr_abs(x, x, GMP_RNDN); if (mpfr_cmp_ui (x, 1)) { printf ("Error in mpfr_abs(1.0)\n"); exit (1); } mpfr_set_si(x, -1, GMP_RNDN); mpfr_abs(x, x, GMP_RNDN); if (mpfr_cmp_ui (x, 1)) { printf ("Error in mpfr_abs(-1.0)\n"); exit (1); } mpfr_set_inf (x, 1); mpfr_abs (x, x, GMP_RNDN); if (!mpfr_inf_p(x) || (mpfr_sgn(x) <= 0)) { printf ("Error in mpfr_abs(Inf).\n"); exit (1); } mpfr_set_inf (x, -1); mpfr_abs (x, x, GMP_RNDN); if (!mpfr_inf_p(x) || (mpfr_sgn(x) <= 0)) { printf ("Error in mpfr_abs(-Inf).\n"); exit (1); } MPFR_SET_NAN(x); mpfr_abs (x, x, GMP_RNDN); if (!MPFR_IS_NAN(x)) { printf ("Error in mpfr_abs(NAN).\n"); exit (1); } n = (argc==1) ? 25000 : atoi(argv[1]); for (k = 1; k <= n; k++) { int sign = SIGN_RAND(); mpfr_random(x); MPFR_SET_SIGN(x, sign); rnd = RND_RAND(); mpfr_abs(y, x, (mp_rnd_t) rnd); MPFR_SET_POS(x); if (mpfr_cmp(x,y)) { printf ("Mismatch for sign=%d and x=", sign); mpfr_print_binary(x); printf ("\nResults="); mpfr_print_binary(y); putchar ('\n'); exit (1); } } mpfr_clears (x, y, (void *) 0); }
/* checks that the inexact return value is correct */ static void check_exact (void) { mpfr_t a, b, c, d; mp_prec_t prec; int i, inexact; mp_rnd_t rnd; mpfr_init (a); mpfr_init (b); mpfr_init (c); mpfr_init (d); mpfr_set_prec (a, 17); mpfr_set_prec (b, 17); mpfr_set_prec (c, 32); mpfr_set_str_binary (a, "1.1000111011000100e-1"); mpfr_set_str_binary (b, "1.0010001111100111e-1"); if (mpfr_mul (c, a, b, GMP_RNDZ)) { printf ("wrong return value (1)\n"); exit (1); } for (prec = 2; prec < 100; prec++) { mpfr_set_prec (a, prec); mpfr_set_prec (b, prec); mpfr_set_prec (c, 2 * prec - 2); mpfr_set_prec (d, 2 * prec); for (i = 0; i < 1000; i++) { mpfr_random (a); mpfr_random (b); rnd = (mp_rnd_t) RND_RAND (); inexact = mpfr_mul (c, a, b, rnd); if (mpfr_mul (d, a, b, rnd)) /* should be always exact */ { printf ("unexpected inexact return value\n"); exit (1); } if ((inexact == 0) && mpfr_cmp (c, d)) { printf ("inexact=0 but results differ\n"); exit (1); } else if (inexact && (mpfr_cmp (c, d) == 0)) { printf ("inexact!=0 but results agree\n"); printf ("prec=%u rnd=%s a=", (unsigned int) prec, mpfr_print_rnd_mode (rnd)); mpfr_out_str (stdout, 2, 0, a, rnd); printf ("\nb="); mpfr_out_str (stdout, 2, 0, b, rnd); printf ("\nc="); mpfr_out_str (stdout, 2, 0, c, rnd); printf ("\nd="); mpfr_out_str (stdout, 2, 0, d, rnd); printf ("\n"); exit (1); } } } mpfr_clear (a); mpfr_clear (b); mpfr_clear (c); mpfr_clear (d); }
/* check that for x = 1.u 1 v 0^k low(x) y = 1.u 0 v 1^k low(y) mpfr_cmp2 (x, y) returns 1 + |u| + |v| + k for low(x) >= low(y), and 1 + |u| + |v| + k + 1 otherwise */ static void worst_cases (void) { mpfr_t x, y; unsigned int i, j, k, b, expected; mp_prec_t l; mpfr_init2 (x, 200); mpfr_init2 (y, 200); mpfr_set_ui (y, 1, GMP_RNDN); for (i=1; i<MPFR_PREC(x); i++) { mpfr_set_ui (x, 1, GMP_RNDN); mpfr_div_2exp (y, y, 1, GMP_RNDN); /* y = 1/2^i */ l = 0; if (mpfr_cmp2 (x, y, &l) <= 0 || l != 1) { printf ("Error in mpfr_cmp2:\nx="); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf ("\ny="); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\ngot %lu instead of %u\n", l, 1); exit (1); } mpfr_add (x, x, y, GMP_RNDN); /* x = 1 + 1/2^i */ l = 0; if (mpfr_cmp2 (x, y, &l) <= 0 || l != 0) { printf ("Error in mpfr_cmp2:\nx="); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf ("\ny="); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\ngot %lu instead of %u\n", l, 0); exit (1); } } for (i=0; i<64; i++) /* |u| = i */ { mpfr_random (x); mpfr_set (y, x, GMP_RNDN); set_bit (x, i + 1, 1); set_bit (y, i + 1, 0); for (j=0; j<64; j++) /* |v| = j */ { b = randlimb () % 2; set_bit (x, i + j + 2, b); set_bit (y, i + j + 2, b); for (k=0; k<64; k++) { if (k) set_bit (x, i + j + k + 1, 0); if (k) set_bit (y, i + j + k + 1, 1); set_bit (x, i + j + k + 2, 1); set_bit (y, i + j + k + 2, 0); l = 0; mpfr_cmp2 (x, y, &l); expected = i + j + k + 1; if (l != expected) { printf ("Error in mpfr_cmp2:\nx="); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf ("\ny="); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\ngot %lu instead of %u\n", l, expected); exit (1); } set_bit (x, i + j + k + 2, 0); set_bit (x, i + j + k + 3, 0); set_bit (y, i + j + k + 3, 1); l = 0; mpfr_cmp2 (x, y, &l); expected = i + j + k + 2; if (l != expected) { printf ("Error in mpfr_cmp2:\nx="); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf ("\ny="); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf ("\ngot %lu instead of %u\n", l, expected); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); }
static void check_inexact (mp_prec_t p) { mpfr_t x, y, z, t; unsigned long u; mp_prec_t q; int inexact, cmp; int rnd; mpfr_init2 (x, p); mpfr_init (y); mpfr_init (z); mpfr_init (t); mpfr_random (x); u = randlimb () % 2; for (q = 2; q <= p; q++) for (rnd = 0; rnd < GMP_RND_MAX; rnd++) { mpfr_set_prec (y, q); mpfr_set_prec (z, q + 10); mpfr_set_prec (t, q); inexact = mpfr_pow_ui (y, x, u, (mp_rnd_t) rnd); cmp = mpfr_pow_ui (z, x, u, (mp_rnd_t) rnd); if (mpfr_can_round (z, q + 10, (mp_rnd_t) rnd, (mp_rnd_t) rnd, q)) { cmp = mpfr_set (t, z, (mp_rnd_t) rnd) || cmp; if (mpfr_cmp (y, t)) { printf ("results differ for u=%lu rnd=%s\n", u, mpfr_print_rnd_mode ((mp_rnd_t) rnd)); printf ("x="); mpfr_print_binary (x); puts (""); printf ("y="); mpfr_print_binary (y); puts (""); printf ("t="); mpfr_print_binary (t); puts (""); printf ("z="); mpfr_print_binary (z); puts (""); exit (1); } if (((inexact == 0) && (cmp != 0)) || ((inexact != 0) && (cmp == 0))) { printf ("Wrong inexact flag for p=%u, q=%u, rnd=%s\n", (unsigned int) p, (unsigned int) q, mpfr_print_rnd_mode ((mp_rnd_t) rnd)); printf ("expected %d, got %d\n", cmp, inexact); printf ("u=%lu x=", u); mpfr_print_binary (x); puts (""); printf ("y="); mpfr_print_binary (y); puts (""); exit (1); } } } /* check exact power */ mpfr_set_prec (x, p); mpfr_set_prec (y, p); mpfr_set_prec (z, p); mpfr_set_ui (x, 4, GMP_RNDN); mpfr_set_str (y, "0.5", 10, GMP_RNDN); test_pow (z, x, y, GMP_RNDZ); mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (t); }