/* With MPFR 2.3.0, this yields an assertion failure in mpfr_acosh. */ static void bug20070831 (void) { mpfr_t x, y, z; int inex; mpfr_init2 (x, 256); mpfr_init2 (y, 32); mpfr_init2 (z, 32); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_nextabove (x); inex = mpfr_acosh (y, x, MPFR_RNDZ); mpfr_set_ui_2exp (z, 1, -127, MPFR_RNDN); mpfr_nextbelow (z); if (!mpfr_equal_p (y, z)) { printf ("Error in bug20070831 (1):\nexpected "); mpfr_dump (z); printf ("got "); mpfr_dump (y); exit (1); } MPFR_ASSERTN (inex < 0); mpfr_nextabove (x); mpfr_set_prec (y, 29); inex = mpfr_acosh (y, x, MPFR_RNDN); mpfr_set_str_binary (z, "1.011010100000100111100110011E-127"); if (!mpfr_equal_p (y, z)) { printf ("Error in bug20070831 (2):\nexpected "); mpfr_dump (z); printf ("got "); mpfr_dump (y); exit (1); } MPFR_ASSERTN (inex < 0); mpfr_clears (x, y, z, (mpfr_ptr) 0); }
/* Bug reported by Laurent Fousse for the 2.4 branch. No problem in the trunk. https://sympa.inria.fr/sympa/arc/mpfr/2009-11/msg00044.html */ static void bug20091122 (void) { mpfr_t x, y, z, yref, zref; mpfr_prec_t p = 3; mpfr_rnd_t r = MPFR_RNDN; mpfr_init2 (x, 5); mpfr_init2 (y, p); mpfr_init2 (z, p); mpfr_init2 (yref, p); mpfr_init2 (zref, p); mpfr_set_str (x, "0.11111E49", 2, MPFR_RNDN); mpfr_sin_cos (yref, zref, x, r); mpfr_sin (y, x, r); mpfr_cos (z, x, r); if (! mpfr_equal_p (y, yref)) { printf ("mpfr_sin_cos and mpfr_sin disagree (bug20091122)\n"); printf ("yref = "); mpfr_dump (yref); printf ("y = "); mpfr_dump (y); exit (1); } if (! mpfr_equal_p (z, zref)) { printf ("mpfr_sin_cos and mpfr_cos disagree (bug20091122)\n"); printf ("zref = "); mpfr_dump (zref); printf ("z = "); mpfr_dump (z); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (yref); mpfr_clear (zref); }
static void teq (mpfr_t x) { mpfr_t y; unsigned long k, px, mx; mpfr_init2 (y, MPFR_PREC(x)); mx = (MPFR_PREC(x) - 1) / mp_bits_per_limb; px = mp_bits_per_limb - 2; for (k = 2; k < MPFR_PREC(x); k++) { mpfr_set (y, x, MPFR_RNDN); MPFR_MANT(y) [mx] ^= MPFR_LIMB_ONE << px; if (mpfr_eq(y, x, k) || !mpfr_eq(y, x, k - 1)) { printf ("Error in eq.\n"); printf ("x = "); mpfr_dump (x); printf ("y = "); mpfr_dump (y); printf ("k = %lu\n", k); printf ("mpfr_eq(y, x, k) = %d\nmpfr_eq(y, x, k - 1) = %d\n", mpfr_eq (y, x, k), mpfr_eq (y, x, k - 1)); exit (1); } if (px) { --px; } else { --mx; px = mp_bits_per_limb - 1; } } mpfr_clear (y); }
/* TODO: A test with more inputs (but can't be compared to mpfr_add). */ static void check_extreme (void) { mpfr_t u, v, w, x, y; mpfr_ptr t[2]; int i, inex1, inex2, r; t[0] = u; t[1] = v; mpfr_inits2 (32, u, v, w, x, y, (mpfr_ptr) 0); mpfr_setmin (u, mpfr_get_emax ()); mpfr_setmax (v, mpfr_get_emin ()); mpfr_setmin (w, mpfr_get_emax () - 40); RND_LOOP (r) for (i = 0; i < 2; i++) { mpfr_set_prec (x, 64); inex1 = mpfr_add (x, u, w, MPFR_RNDN); MPFR_ASSERTN (inex1 == 0); inex1 = mpfr_prec_round (x, 32, (mpfr_rnd_t) r); inex2 = mpfr_sum (y, t, 2, (mpfr_rnd_t) r); if (!(mpfr_equal_p (x, y) && SAME_SIGN (inex1, inex2))) { printf ("Error in check_extreme (%s, i = %d)\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), i); printf ("Expected "); mpfr_dump (x); printf ("with inex = %d\n", inex1); printf ("Got "); mpfr_dump (y); printf ("with inex = %d\n", inex2); exit (1); } mpfr_neg (v, v, MPFR_RNDN); mpfr_neg (w, w, MPFR_RNDN); } mpfr_clears (u, v, w, x, y, (mpfr_ptr) 0); }
static void check (mpfr_prec_t p0, mpfr_prec_t p1) { mpfr_t x, y, z; mpfr_rnd_t rnd; int dif; mpfr_init (x); mpfr_init (y); mpfr_init2 (z, p1 + 10); mpfr_const_log2 (z, MPFR_RNDN); mpfr_clear_cache (__gmpfr_cache_const_log2); for (; p0<=p1; p0++) { mpfr_set_prec (x, p0); mpfr_set_prec (y, p0); { rnd = RND_RAND (); mpfr_const_log2 (x, rnd); mpfr_set (y, z, rnd); if ((dif = mpfr_cmp (x, y)) && mpfr_can_round (z, mpfr_get_prec(z), MPFR_RNDN, rnd, p0)) { printf ("mpfr_const_log2 fails for prec=%u, rnd=%s Diff=%d\n", (unsigned int) p0, mpfr_print_rnd_mode (rnd), dif); printf ("expected "), mpfr_dump (y); printf ("got "), mpfr_dump (x); exit (1); } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
/* check sqrt(x^2) = x */ static void test_property2 (mpfr_prec_t p, mpfr_rnd_t r) { mpfr_t x, y; mpfr_init2 (x, p); mpfr_init2 (y, p); mpfr_urandomb (x, RANDS); mpfr_mul (y, x, x, r); mpfr_sqrt (y, y, r); if (mpfr_cmp (y, x)) { printf ("Error, sqrt(x^2) = x does not hold for r=%s\n", mpfr_print_rnd_mode (r)); printf ("x="); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_clear (x); mpfr_clear (y); }
static void check_bugs (void) { mpfr_t x, y; mpfr_init (x); mpfr_init (y); /* bug found by Rob (Sisyphus) on 16 Sep 2005 */ mpfr_set_ui (x, 2, MPFR_RNDN); mpfr_set_prec (y, 2); mpfr_coth (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1)) { printf ("Error for coth(2), expected 1, got "); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_str (x, "18.368400284838550", 10, MPFR_RNDN); mpfr_set_str (y, "1.0000000000000002", 10, MPFR_RNDN); mpfr_coth (x, x, MPFR_RNDN); if (mpfr_cmp (x, y) != 0) { printf ("Error for coth(18.368400284838550)\n"); exit (1); } mpfr_set_str (x, "18.714973875118520", 10, MPFR_RNDN); mpfr_coth (x, x, MPFR_RNDN); if (mpfr_cmp (x, y) != 0) { printf ("Error for coth(18.714973875118520)\n"); exit (1); } mpfr_set_str (x, "18.714973875118524", 10, MPFR_RNDN); mpfr_coth (x, x, MPFR_RNDN); if (mpfr_cmp_ui (x, 1) != 0) { printf ("Error for coth(18.714973875118524)\n"); exit (1); } mpfr_clear (x); mpfr_clear (y); }
static void huge (void) { mpfr_t x, y, z; int inex; /* TODO: extend the exponent range and use mpfr_get_emax (). */ mpfr_inits2 (32, x, y, z, (mpfr_ptr) 0); mpfr_set_ui_2exp (x, 1, 1073741822, GMP_RNDN); inex = mpfr_acosh (y, x, GMP_RNDN); mpfr_set_str_binary (z, "0.10110001011100100001011111110101E30"); if (!mpfr_equal_p (y, z)) { printf ("Error in huge:\nexpected "); mpfr_dump (z); printf ("got "); mpfr_dump (y); exit (1); } MPFR_ASSERTN (inex < 0); mpfr_clears (x, y, z, (mpfr_ptr) 0); }
/* check that -1 <= x/sqrt(x^2+s*y^2) <= 1 for rounding to nearest or up with s = 0 and s = 1 */ static void test_property1 (mpfr_prec_t p, mpfr_rnd_t r, int s) { mpfr_t x, y, z, t; mpfr_init2 (x, p); mpfr_init2 (y, p); mpfr_init2 (z, p); mpfr_init2 (t, p); mpfr_urandomb (x, RANDS); mpfr_mul (z, x, x, r); if (s) { mpfr_urandomb (y, RANDS); mpfr_mul (t, y, y, r); mpfr_add (z, z, t, r); } mpfr_sqrt (z, z, r); mpfr_div (z, x, z, r); /* Note: if both x and y are 0, z is NAN, but the test below will be false. So, everything is fine. */ if (mpfr_cmp_si (z, -1) < 0 || mpfr_cmp_ui (z, 1) > 0) { printf ("Error, -1 <= x/sqrt(x^2+y^2) <= 1 does not hold for r=%s\n", mpfr_print_rnd_mode (r)); printf ("x="); mpfr_dump (x); printf ("y="); mpfr_dump (y); printf ("got "); mpfr_dump (z); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (t); }
static void check_zero (void) { mpfr_t x, y, r; mpfr_init2 (x, 53); mpfr_init2 (y, 53); mpfr_init2 (r, 53); mpfr_set_str_binary (r, "10110101110001100011110010110001001110001010110111E-51"); mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_ai (y, x, MPFR_RNDN); if (mpfr_equal_p (y, r) == 0) { printf ("Error in mpfr_ai for x=0\n"); printf ("Expected "); mpfr_dump (r); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (r); }
/* Bug found by Christoph Lauter. */ static void bug20081028 (void) { mpfr_t x; const char *s = "0.10000000000000000000000000000001E1"; mpfr_init2 (x, 32); mpfr_set_str (x, "1.00000000000000000006", 10, MPFR_RNDU); if (! mpfr_greater_p (x, __gmpfr_one)) { printf ("Error in bug20081028:\nExpected %s\nGot ", s); mpfr_dump (x); exit (1); } mpfr_clear (x); }
/* bug reported by Joseph S. Myers on 2013-10-27 https://sympa.inria.fr/sympa/arc/mpfr/2013-10/msg00015.html */ static void bug20131027 (void) { mpfr_t sum, t[4]; mpfr_ptr p[4]; char *s[4] = { "0x1p1000", "-0x0.fffffffffffff80000000000000001p1000", "-0x1p947", "0x1p880" }; int i, r; mpfr_init2 (sum, 53); for (i = 0; i < 4; i++) { mpfr_init2 (t[i], i == 0 ? 53 : 1000); mpfr_set_str (t[i], s[i], 0, MPFR_RNDN); p[i] = t[i]; } RND_LOOP(r) { int expected_sign = (mpfr_rnd_t) r == MPFR_RNDD ? -1 : 1; int inex; inex = mpfr_sum (sum, p, 4, (mpfr_rnd_t) r); if (MPFR_NOTZERO (sum) || MPFR_SIGN (sum) != expected_sign || inex != 0) { printf ("mpfr_sum incorrect in bug20131027 for %s:\n" "expected %c0 with inex = 0, got ", mpfr_print_rnd_mode ((mpfr_rnd_t) r), expected_sign > 0 ? '+' : '-'); mpfr_dump (sum); printf ("with inex = %d\n", inex); exit (1); } } for (i = 0; i < 4; i++) mpfr_clear (t[i]); mpfr_clear (sum); }
static void check_one (mpz_ptr z) { int sh, neg; mpfr_t f; mpz_t got; mpfr_init2 (f, MAX( mpz_sizeinbase (z, 2), MPFR_PREC_MIN) ); mpz_init (got); for (sh = -2*BITS_PER_MP_LIMB ; sh < 2*BITS_PER_MP_LIMB ; sh++) { for (neg = 0; neg <= 1; neg++) { mpz_neg (z, z); mpfr_set_z (f, z, GMP_RNDN); if (sh < 0) { mpz_tdiv_q_2exp (z, z, -sh); mpfr_div_2exp (f, f, -sh, GMP_RNDN); } else { mpz_mul_2exp (z, z, sh); mpfr_mul_2exp (f, f, sh, GMP_RNDN); } mpfr_get_z (got, f, GMP_RNDZ); if (mpz_cmp (got, z) != 0) { printf ("Wrong result for shift=%d\n", sh); printf (" f "); mpfr_dump (f); printf (" got "); mpz_dump (got); printf (" want "); mpz_dump (z); exit (1); } } } mpfr_clear (f); mpz_clear (got); }
static void check (long i, mpfr_rnd_t rnd) { mpfr_t f; mpz_t z; mpfr_exp_t e; int inex; /* using CHAR_BIT * sizeof(long) bits of precision ensures that mpfr_set_z_2exp is exact below */ mpfr_init2 (f, CHAR_BIT * sizeof(long)); mpz_init (z); mpz_set_ui (z, i); /* the following loop ensures that no overflow occurs */ do e = randexp (); while (e > mpfr_get_emax () - CHAR_BIT * sizeof(long)); inex = mpfr_set_z_2exp (f, z, e, rnd); if (inex != 0) { printf ("Error in mpfr_set_z_2exp for i=%ld, e=%ld," " wrong ternary value\n", i, (long) e); printf ("expected 0, got %d\n", inex); exit (1); } mpfr_div_2si (f, f, e, rnd); if (mpfr_get_si (f, MPFR_RNDZ) != i) { printf ("Error in mpfr_set_z_2exp for i=%ld e=", i); if (e < LONG_MIN) printf ("(<LONG_MIN)"); else if (e > LONG_MAX) printf ("(>LONG_MAX)"); else printf ("%ld", (long) e); printf (" rnd_mode=%d\n", rnd); printf ("expected %ld\n", i); printf ("got "); mpfr_dump (f); exit (1); } mpfr_clear (f); mpz_clear (z); }
/* https://sympa.inria.fr/sympa/arc/mpfr/2011-05/msg00008.html * Incorrect flags (in debug mode on a 32-bit machine, assertion failure). */ static void reduced_expo_range (void) { mpfr_exp_t emin, emax; mpfr_t x, y, ex_y; int inex, ex_inex; unsigned int flags, ex_flags; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_inits2 (12, x, y, ex_y, (mpfr_ptr) 0); mpfr_set_str (x, "0.1e-5", 2, MPFR_RNDN); set_emin (-5); set_emax (-5); mpfr_clear_flags (); inex = mpfr_atan (y, x, MPFR_RNDN); flags = __gmpfr_flags; set_emin (emin); set_emax (emax); mpfr_set_str (ex_y, "0.1e-5", 2, MPFR_RNDN); ex_inex = 1; ex_flags = MPFR_FLAGS_INEXACT; if (SIGN (inex) != ex_inex || flags != ex_flags || ! mpfr_equal_p (y, ex_y)) { printf ("Error in reduced_expo_range\non x = "); mpfr_dump (x); printf ("Expected y = "); mpfr_out_str (stdout, 2, 0, ex_y, MPFR_RNDN); printf ("\n inex = %d, flags = %u\n", ex_inex, ex_flags); printf ("Got y = "); mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN); printf ("\n inex = %d, flags = %u\n", SIGN (inex), flags); exit (1); } mpfr_clears (x, y, ex_y, (mpfr_ptr) 0); }
static void test2 (void) { mpfr_t x; char *org = stack; long *a, *b, *c; a = dummy_set_si (42); b = dummy_set_si (17); c = dummy_add (a, b); c = dummy_compact (c, org); (mpfr_custom_init_set) (x, c[0], c[1], p, &c[2]); if (c != a || mpfr_cmp_ui (x, 59) != 0) { printf ("Compact (2) failed! c=%p a=%p\n", (void *) c, (void *) a); mpfr_dump (x); exit (1); } stack = org; }
static void x_near_one (void) { mpfr_t x, y; int inex; mpfr_init2 (x, 32); mpfr_init2 (y, 16); mpfr_set_ui (x, 1, GMP_RNDN); mpfr_nextbelow (x); inex = mpfr_log (y, x, GMP_RNDD); if (mpfr_cmp_str (y, "-0.1000000000000001E-31", 2, GMP_RNDN) || inex >= 0) { printf ("Failure in x_near_one, got inex = %d and\ny = ", inex); mpfr_dump (y); } mpfr_clears (x, y, (mpfr_ptr) 0); }
static void check_regression (void) { mpfr_t x, y; mpfr_prec_t p; int i; p = strlen (xs) - 2 - 3; mpfr_inits2 (p, x, y, (mpfr_ptr) 0); mpfr_set_str (x, xs, 2, MPFR_RNDN); i = mpfr_sin (y, x, MPFR_RNDN); if (i >= 0 || mpfr_cmp_str (y, "0.111001110011110011110001010110011101110E-1", 2, MPFR_RNDN)) { printf ("Regression test failed (1) i=%d\ny=", i); mpfr_dump (y); exit (1); } mpfr_clears (x, y, (mpfr_ptr) 0); }
/* Bug found while adding tests for mpfr_cot */ static void test_20070628 (void) { mpfr_exp_t old_emax; mpfr_t x, y; int inex, err = 0; old_emax = mpfr_get_emax (); if (mpfr_set_emax (256)) { printf ("Can't change exponent range\n"); exit (1); } mpfr_inits2 (53, x, y, (mpfr_ptr) 0); mpfr_set_si (x, -1, MPFR_RNDN); mpfr_set_si_2exp (y, 1, -256, MPFR_RNDN); mpfr_clear_flags (); inex = mpfr_div (x, x, y, MPFR_RNDD); if (MPFR_SIGN (x) >= 0 || ! mpfr_inf_p (x)) { printf ("Error in test_20070628: expected -Inf, got\n"); mpfr_dump (x); err++; } if (inex >= 0) { printf ("Error in test_20070628: expected inex < 0, got %d\n", inex); err++; } if (! mpfr_overflow_p ()) { printf ("Error in test_20070628: overflow flag is not set\n"); err++; } mpfr_clears (x, y, (mpfr_ptr) 0); mpfr_set_emax (old_emax); }
/* Test the sorting function */ static void test_sort (mpfr_prec_t f, unsigned long n) { mpfr_t *tab; mpfr_ptr *tabtmp; mpfr_srcptr *perm; unsigned long i; /* Init stuff */ tab = (mpfr_t *) (*__gmp_allocate_func) (n * sizeof (mpfr_t)); for (i = 0; i < n; i++) mpfr_init2 (tab[i], f); tabtmp = (mpfr_ptr *) (*__gmp_allocate_func) (n * sizeof(mpfr_ptr)); perm = (mpfr_srcptr *) (*__gmp_allocate_func) (n * sizeof(mpfr_srcptr)); for (i = 0; i < n; i++) { mpfr_urandomb (tab[i], RANDS); tabtmp[i] = tab[i]; } mpfr_sum_sort ((mpfr_srcptr *)tabtmp, n, perm); if (check_is_sorted (n, perm) == 0) { printf ("mpfr_sum_sort incorrect.\n"); for (i = 0; i < n; i++) mpfr_dump (perm[i]); exit (1); } /* Clear stuff */ for (i = 0; i < n; i++) mpfr_clear (tab[i]); (*__gmp_free_func) (tab, n * sizeof (mpfr_t)); (*__gmp_free_func) (tabtmp, n * sizeof(mpfr_ptr)); (*__gmp_free_func) (perm, n * sizeof(mpfr_srcptr)); }
static void x_near_one (void) { mpfr_t x, y, z; int inex; mpfr_init2 (x, 32); mpfr_init2 (y, 4); mpfr_init2 (z, 33); mpfr_set_ui (x, 1, GMP_RNDN); mpfr_nextbelow (x); mpfr_set_ui_2exp (y, 11, -2, GMP_RNDN); inex = mpfr_pow (z, x, y, GMP_RNDN); if (mpfr_cmp_str (z, "0.111111111111111111111111111111011E0", 2, GMP_RNDN) || inex <= 0) { printf ("Failure in x_near_one, got inex = %d and\nz = ", inex); mpfr_dump (z); } mpfr_clears (x, y, z, (void *) 0); }
static void special (void) { mpfr_t x, y; mpfr_init (x); mpfr_init (y); mpfr_set_inf (y, -1); mpfr_set_inf (x, 1); mpfr_digamma (y, x, MPFR_RNDN); if (mpfr_inf_p (y) == 0 || mpfr_sgn (y) < 0) { printf ("error for Psi(+Inf)\n"); printf ("expected +Inf\n"); printf ("got "); mpfr_dump (y); exit (1); } mpfr_clear (x); mpfr_clear (y); }
static void check_underflow (void) { mpfr_t a; mp_exp_t emin, emax; int res; mpfr_init (a); /* Check underflow */ emin = mpfr_get_emin (); set_emin (-20); res = mpfr_set_str (a, "0.00000000001", 10, GMP_RNDZ); if (!MPFR_IS_ZERO (a)) { printf("ERROR for mpfr_set_str (a, \"0.00000000001\", 10, GMP_RNDN)\n" " with emin=-20\n" "res=%d\n", res); mpfr_dump (a); exit (1); } set_emin (emin); /* check overflow */ emax = mpfr_get_emax (); set_emax (1073741823); /* 2^30-1 */ mpfr_set_str (a, "2E1000000000", 10, GMP_RNDN); if (!mpfr_inf_p (a) || mpfr_sgn (a) < 0) { printf("ERROR for mpfr_set_str (a, \"2E1000000000\", 10, GMP_RNDN);\n"); exit (1); } set_emax (emax); mpfr_clear (a); }
int main (int argc, char *argv[]) { mpfr_t x, y; long n; mpfr_prec_t prec = 53; tests_start_mpfr (); mpfr_init (x); mpfr_init (y); if (argc != 1) { if (argc != 4) { printf ("Usage: tyn n x prec\n"); exit (1); } n = atoi (argv[1]); prec = atoi (argv[3]); mpfr_set_prec (x, prec); mpfr_set_prec (y, prec); mpfr_set_str (x, argv[2], 10, MPFR_RNDN); mpfr_yn (y, n, x, MPFR_RNDN); printf ("Y(%ld,", n); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf (")="); mpfr_out_str (stdout, 10, 0, y, MPFR_RNDN); printf ("\n"); goto end; } /* special values */ mpfr_set_nan (x); mpfr_yn (y, 17, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (y)); mpfr_set_inf (x, 1); /* +Inf */ mpfr_yn (y, 17, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_cmp_ui (y, 0) == 0 && MPFR_IS_POS (y)); mpfr_set_inf (x, -1); /* -Inf */ mpfr_yn (y, 17, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_nan_p (y)); mpfr_set_ui (x, 0, MPFR_RNDN); /* +0 */ mpfr_yn (y, 0, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && MPFR_IS_NEG (y)); /* y0(+0)=-Inf */ mpfr_yn (y, 17, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && MPFR_IS_NEG (y)); /* y17(+0)=-Inf */ mpfr_yn (y, -17, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && MPFR_IS_POS (y)); /* y(-17,+0)=+Inf */ mpfr_yn (y, -42, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && MPFR_IS_NEG (y)); /* y(-42,+0)=-Inf */ mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); /* -0 */ mpfr_yn (y, 0, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && MPFR_IS_NEG (y)); /* y0(-0)=-Inf */ mpfr_yn (y, 17, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && MPFR_IS_NEG (y)); /* y17(-0)=-Inf */ mpfr_yn (y, -17, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && MPFR_IS_POS (y)); /* y(-17,-0)=+Inf */ mpfr_yn (y, -42, x, MPFR_RNDN); MPFR_ASSERTN(mpfr_inf_p (y) && MPFR_IS_NEG (y)); /* y(-42,-0)=-Inf */ mpfr_set_prec (x, 53); mpfr_set_prec (y, 53); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_yn (y, 0, x, MPFR_RNDN); mpfr_set_str_binary (x, "0.00010110100110000000001000100110111100110101100011011111"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_yn for n=0, x=1, rnd=MPFR_RNDN\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_yn (y, 1, x, MPFR_RNDN); mpfr_set_str_binary (x, "-0.110001111111110110010000001111101011001101011100101"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_yn for n=1, x=1, rnd=MPFR_RNDN\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_yn (y, -1, x, MPFR_RNDN); mpfr_set_str_binary (x, "0.110001111111110110010000001111101011001101011100101"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_yn for n=-1, x=1, rnd=MPFR_RNDN\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_yn (y, 2, x, MPFR_RNDN); mpfr_set_str_binary (x, "-1.101001101001001100100010101001000101101000010010001"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_yn for n=2, x=1, rnd=MPFR_RNDN\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_yn (y, -2, x, MPFR_RNDN); mpfr_set_str_binary (x, "-1.101001101001001100100010101001000101101000010010001"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_yn for n=-2, x=1, rnd=MPFR_RNDN\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_yn (y, 17, x, MPFR_RNDN); mpfr_set_str_binary (x, "-0.11000100111000100010101101011000110011001101100001011E60"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_yn for n=17, x=1, rnd=MPFR_RNDN\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_yn (y, -17, x, MPFR_RNDN); mpfr_set_str_binary (x, "0.11000100111000100010101101011000110011001101100001011E60"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_yn for n=-17, x=1, rnd=MPFR_RNDN\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } mpfr_set_ui (x, 17, MPFR_RNDN); mpfr_yn (y, 1, x, MPFR_RNDN); mpfr_set_str_binary (x, "0.00101010110011011111001100000001101011011001111111"); if (mpfr_cmp (x, y)) { printf ("Error in mpfr_yn for n=1, x=17, rnd=MPFR_RNDN\n"); printf ("Expected "); mpfr_dump (x); printf ("Got "); mpfr_dump (y); exit (1); } end: mpfr_clear (x); mpfr_clear (y); tests_end_mpfr (); return 0; }
int main (void) { mpfr_t x, y; float f, g, infp; int i; infp = (float) DBL_POS_INF; if (infp * 0.5 != infp) { fprintf (stderr, "Error, FLT_MAX + FLT_MAX does not yield INFP\n"); fprintf (stderr, "(this is probably a compiler bug, please report)\n"); exit (1); } tests_start_mpfr (); mpfr_init2 (x, 24); mpfr_init2 (y, 24); #if !defined(MPFR_ERRDIVZERO) mpfr_set_nan (x); f = mpfr_get_flt (x, MPFR_RNDN); if (f == f) { printf ("Error for mpfr_get_flt(NaN)\n"); exit (1); } mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_nan_p (x) == 0) { printf ("Error for mpfr_set_flt(NaN)\n"); exit (1); } mpfr_set_inf (x, 1); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_inf_p (x) == 0 || mpfr_sgn (x) < 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(+Inf)):\n"); printf ("f=%f, expected -Inf\n", f); printf ("got "); mpfr_dump (x); exit (1); } mpfr_set_inf (x, -1); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_inf_p (x) == 0 || mpfr_sgn (x) > 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(-Inf)):\n"); printf ("f=%f, expected -Inf\n", f); printf ("got "); mpfr_dump (x); exit (1); } #endif mpfr_set_ui (x, 0, MPFR_RNDN); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_zero_p (x) == 0 || MPFR_SIGN (x) < 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(+0))\n"); exit (1); } #ifdef HAVE_SIGNEDZ mpfr_set_ui (x, 0, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_zero_p (x) == 0 || MPFR_SIGN (x) > 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(-0))\n"); exit (1); } #endif /* HAVE_SIGNEDZ */ mpfr_set_ui (x, 17, MPFR_RNDN); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_cmp_ui (x, 17) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(17))\n"); printf ("expected 17\n"); printf ("got "); mpfr_dump (x); exit (1); } mpfr_set_si (x, -42, MPFR_RNDN); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (x, f, MPFR_RNDN); if (mpfr_cmp_si (x, -42) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(-42))\n"); printf ("expected -42\n"); printf ("got "); mpfr_dump (x); exit (1); } mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN); for (i = -126; i < 128; i++) { f = mpfr_get_flt (x, MPFR_RNDN); mpfr_set_flt (y, f, MPFR_RNDN); if (mpfr_cmp (x, y) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_mul_2exp (x, x, 1, MPFR_RNDN); } mpfr_set_prec (x, 53); mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN); for (i = -126; i < 128; i++) { mpfr_nextbelow (x); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_nextabove (x); mpfr_set_flt (y, f, MPFR_RNDN); if (mpfr_cmp (x, y) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_mul_2exp (x, x, 1, MPFR_RNDN); } mpfr_set_prec (x, 53); mpfr_set_si_2exp (x, 1, -126, MPFR_RNDN); for (i = -126; i < 128; i++) { mpfr_nextabove (x); f = mpfr_get_flt (x, MPFR_RNDN); mpfr_nextbelow (x); mpfr_set_flt (y, f, MPFR_RNDN); if (mpfr_cmp (x, y) != 0) { printf ("Error for mpfr_set_flt(mpfr_get_flt(x))\n"); printf ("expected "); mpfr_dump (x); printf ("got "); mpfr_dump (y); exit (1); } mpfr_mul_2exp (x, x, 1, MPFR_RNDN); } mpfr_set_si_2exp (x, 1, -150, MPFR_RNDN); g = 0.0; f = mpfr_get_flt (x, MPFR_RNDN); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } g = FLT_MIN * FLT_EPSILON; f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^(-150),RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } mpfr_set_si_2exp (x, 1, -151, MPFR_RNDN); g = 0.0; f = mpfr_get_flt (x, MPFR_RNDN); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } g = FLT_MIN * FLT_EPSILON; f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^(-151),RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } mpfr_set_si_2exp (x, 1, -149, MPFR_RNDN); g = FLT_MIN * FLT_EPSILON; f = mpfr_get_flt (x, MPFR_RNDN); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^(-149),RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } mpfr_set_si_2exp (x, 1, 128, MPFR_RNDN); g = FLT_MAX; f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } #if !defined(MPFR_ERRDIVZERO) f = mpfr_get_flt (x, MPFR_RNDN); /* 2^128 rounds to itself with extended exponent range, we should get +Inf */ g = infp; if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^128,RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } #endif /* corner case: take x with 25 bits just below 2^128 */ mpfr_set_prec (x, 25); mpfr_set_si_2exp (x, 1, 128, MPFR_RNDN); mpfr_nextbelow (x); g = FLT_MAX; f = mpfr_get_flt (x, MPFR_RNDZ); if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDZ)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDD); if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDD)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDN); /* first round to 2^128 (even rule), thus we should get +Inf */ g = infp; if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDN)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDU); if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDU)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } f = mpfr_get_flt (x, MPFR_RNDA); if (f != g) { printf ("Error for mpfr_get_flt(2^128*(1-2^(-25)),RNDA)\n"); printf ("expected %.8e, got %.8e\n", g, f); exit (1); } mpfr_clear (x); mpfr_clear (y); tests_end_mpfr (); return 0; }
static void test_urandomb (long nbtests, mpfr_prec_t prec, int verbose) { mpfr_t x; int *tab, size_tab, k, sh, xn; double d, av = 0, var = 0, chi2 = 0, th; mpfr_exp_t emin; size_tab = (nbtests >= 1000 ? nbtests / 50 : 20); tab = (int *) calloc (size_tab, sizeof(int)); if (tab == NULL) { fprintf (stderr, "trandom: can't allocate memory in test_urandomb\n"); exit (1); } mpfr_init2 (x, prec); xn = 1 + (prec - 1) / mp_bits_per_limb; sh = xn * mp_bits_per_limb - prec; for (k = 0; k < nbtests; k++) { mpfr_urandomb (x, RANDS); /* check that lower bits are zero */ if (MPFR_MANT(x)[0] & MPFR_LIMB_MASK(sh)) { printf ("Error: mpfr_urandomb() returns invalid numbers:\n"); mpfr_print_binary (x); puts (""); exit (1); } d = mpfr_get_d1 (x); av += d; var += d*d; tab[(int)(size_tab * d)]++; } /* coverage test */ emin = mpfr_get_emin (); set_emin (1); /* the generated number in [0,1[ is not in the exponent range, except if it is zero */ k = mpfr_urandomb (x, RANDS); if (MPFR_IS_ZERO(x) == 0 && (k == 0 || mpfr_nan_p (x) == 0)) { printf ("Error in mpfr_urandomb, expected NaN, got "); mpfr_dump (x); exit (1); } set_emin (emin); mpfr_clear (x); if (!verbose) { free(tab); return; } av /= nbtests; var = (var / nbtests) - av * av; th = (double)nbtests / size_tab; printf("Average = %.5f\nVariance = %.5f\n", av, var); printf("Repartition for urandomb. Each integer should be close to %d.\n", (int)th); for (k = 0; k < size_tab; k++) { chi2 += (tab[k] - th) * (tab[k] - th) / th; printf("%d ", tab[k]); if (((k+1) & 7) == 0) printf("\n"); } printf("\nChi2 statistics value (with %d degrees of freedom) : %.5f\n\n", size_tab - 1, chi2); free(tab); return; }
static void check_overflow (void) { mpfr_t x; char *s; mpfr_init (x); /* Huge overflow */ mpfr_strtofr (x, "123456789E2147483646", &s, 0, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_INF (x) || !MPFR_IS_POS (x) ) { printf ("Check overflow failed (1) with:\n s=%s\n x=", s); mpfr_dump (x); exit (1); } mpfr_strtofr (x, "123456789E9223372036854775807", &s, 0, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_INF (x) || !MPFR_IS_POS (x) ) { printf ("Check overflow failed (2) with:\n s='%s'\n x=", s); mpfr_dump (x); exit (1); } mpfr_strtofr (x, "123456789E170141183460469231731687303715884105728", &s, 0, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_INF (x) || !MPFR_IS_POS (x) ) { printf ("Check overflow failed (3) with:\n s=%s\n x=", s); mpfr_dump (x); exit (1); } /* Limit overflow */ mpfr_strtofr (x, "12E2147483646", &s, 0, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_INF (x) || !MPFR_IS_POS (x) ) { printf ("Check overflow failed (4) with:\n s=%s\n x=", s); mpfr_dump (x); exit (1); } mpfr_strtofr (x, "12E2147483645", &s, 0, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_INF (x) || !MPFR_IS_POS (x)) { printf ("Check overflow failed (5) with:\n s=%s\n x=", s); mpfr_dump (x); exit (1); } mpfr_strtofr (x, "0123456789ABCDEF@2147483640", &s, 16, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_INF (x) || !MPFR_IS_POS (x)) { printf ("Check overflow failed (6) with:\n s=%s\n x=", s); mpfr_dump (x); exit (1); } mpfr_strtofr (x, "0123456789ABCDEF@540000000", &s, 16, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_INF (x) || !MPFR_IS_POS (x)) { printf ("Check overflow failed (7) with:\n s=%s\n x=", s); mpfr_dump (x); exit (1); } /* Check underflow */ mpfr_strtofr (x, "123456789E-2147483646", &s, 0, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_ZERO (x) || !MPFR_IS_POS (x) ) { printf ("Check underflow failed (1) with:\n s=%s\n x=", s); mpfr_dump (x); exit (1); } mpfr_strtofr (x, "123456789E-9223372036854775807", &s, 0, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_ZERO (x) || !MPFR_IS_POS (x) ) { printf ("Check underflow failed (2) with:\n s='%s'\n x=", s); mpfr_dump (x); exit (1); } mpfr_strtofr (x, "-123456789E-170141183460469231731687303715884105728", &s, 0, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_ZERO (x) || !MPFR_IS_NEG (x) ) { printf ("Check underflow failed (3) with:\n s=%s\n x=", s); mpfr_dump (x); exit (1); } mpfr_strtofr (x, "0123456789ABCDEF@-540000000", &s, 16, MPFR_RNDN); if (s[0] != 0 || !MPFR_IS_ZERO (x) || !MPFR_IS_POS (x)) { printf ("Check overflow failed (7) with:\n s=%s\n x=", s); mpfr_dump (x); exit (1); } mpfr_clear (x); }
static void check_parse (void) { mpfr_t x; char *s; int res; mpfr_init (x); /* Invalid data */ mpfr_set_si (x, -1, MPFR_RNDN); res = mpfr_strtofr (x, " invalid", NULL, 10, MPFR_RNDN); if (MPFR_NOTZERO (x) || MPFR_IS_NEG (x)) { printf ("Failed parsing ' invalid' (1)\n X="); mpfr_dump (x); exit (1); } MPFR_ASSERTN (res == 0); mpfr_set_si (x, -1, MPFR_RNDN); res = mpfr_strtofr (x, " invalid", &s, 0, MPFR_RNDN); if (MPFR_NOTZERO (x) || MPFR_IS_NEG (x) || strcmp (s, " invalid")) { printf ("Failed parsing ' invalid' (2)\n S=%s\n X=", s); mpfr_dump (x); exit (1); } MPFR_ASSERTN (res == 0); /* Check if it stops correctly */ mpfr_strtofr (x, "15*x", &s, 10, MPFR_RNDN); if (mpfr_cmp_ui (x, 15) || strcmp (s, "*x")) { printf ("Failed parsing '15*x'\n S=%s\n X=", s); mpfr_dump (x); exit (1); } /* Check for leading spaces */ mpfr_strtofr (x, " 1.5E-10 *x^2", &s, 10, MPFR_RNDN); if (mpfr_cmp_str1 (x, "1.5E-10") || strcmp (s, " *x^2")) { printf ("Failed parsing '1.5E-10*x^2'\n S=%s\n X=", s); mpfr_dump (x); exit (1); } /* Check for leading sign */ mpfr_strtofr (x, " +17.5E-42E ", &s, 10, MPFR_RNDN); if (mpfr_cmp_str1 (x, "17.5E-42") || strcmp (s, "E ")) { printf ("Failed parsing '+17.5E-42E '\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-17.5E+42E\n", &s, 10, MPFR_RNDN); if (mpfr_cmp_str1 (x, "-17.5E42") || strcmp (s, "E\n")) { printf ("Failed parsing '-17.5E+42\\n'\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* P form */ mpfr_strtofr (x, "0x42P17", &s, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "8650752", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '0x42P17' (base = 16)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-0X42p17", &s, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "-8650752", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-0x42p17' (base = 16)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "42p17", &s, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "8650752", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '42p17' (base = 16)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-42P17", &s, 16, MPFR_RNDN); if (mpfr_cmp_str (x, "-8650752", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-42P17' (base = 16)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "0b1001P17", &s, 2, MPFR_RNDN); if (mpfr_cmp_str (x, "1179648", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '0b1001P17' (base = 2)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-0B1001p17", &s, 2, MPFR_RNDN); if (mpfr_cmp_str (x, "-1179648", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-0B1001p17' (base = 2)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "1001p17", &s, 2, MPFR_RNDN); if (mpfr_cmp_str (x, "1179648", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '1001p17' (base = 2)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-1001P17", &s, 2, MPFR_RNDN); if (mpfr_cmp_str (x, "-1179648", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-1001P17' (base = 2)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check for auto-detection of the base */ mpfr_strtofr (x, "+0x42P17", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "42P17", 16, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '+0x42P17'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-42E17", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "-42E17", 10, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '-42E17'\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-42P17", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "-42", 10, MPFR_RNDN) || strcmp (s, "P17")) { printf ("Failed parsing '-42P17' (base = 0)\n S='%s'\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, " 0b0101.011@42", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "0101.011@42", 2, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '0101.011@42'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, " 0b0101.011P42", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "0101.011@42", 2, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '0101.011@42'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "+0x42@17", &s, 0, MPFR_RNDN); if (mpfr_cmp_str (x, "4.2@18", 16, MPFR_RNDN) || *s != 0) { printf ("Failed parsing '+0x42P17'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check for space inside the mantissa */ mpfr_strtofr (x, "+0x4 2@17", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 4) || strcmp(s," 2@17")) { printf ("Failed parsing '+0x4 2@17'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "+0x42 P17", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 0x42) || strcmp(s," P17")) { printf ("Failed parsing '+0x42 P17'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Space between mantissa and exponent */ mpfr_strtofr (x, " -0b0101P 17", &s, 0, MPFR_RNDN); if (mpfr_cmp_si (x, -5) || strcmp(s,"P 17")) { printf ("Failed parsing '-0b0101P 17'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check for Invalid exponent. */ mpfr_strtofr (x, " -0b0101PF17", &s, 0, MPFR_RNDN); if (mpfr_cmp_si (x, -5) || strcmp(s,"PF17")) { printf ("Failed parsing '-0b0101PF17'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* At least one digit in the mantissa. */ mpfr_strtofr (x, " .E10", &s, 0, MPFR_RNDN); if (strcmp(s," .E10")) { printf ("Failed parsing ' .E10'\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check 2 '.': 2.3.4 */ mpfr_strtofr (x, "-1.2.3E4", &s, 0, MPFR_RNDN); if (mpfr_cmp_str1 (x, "-1.2") || strcmp(s,".3E4")) { printf ("Failed parsing '-1.2.3E4'\n S=%s\n X=", s); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check for 0x and 0b */ mpfr_strtofr (x, " 0xG", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) || strcmp(s,"xG")) { printf ("Failed parsing ' 0xG'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, " 0b2", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) || strcmp(s,"b2")) { printf ("Failed parsing ' 0b2'\n S=%s\n X=", s); mpfr_out_str (stdout, 2, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, "-0x.23@2Z33", &s, 0, MPFR_RNDN); if (mpfr_cmp_si (x, -0x23) || strcmp(s,"Z33")) { printf ("Failed parsing '-0x.23@2Z33'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_strtofr (x, " 0x", &s, 0, MPFR_RNDN); if (mpfr_cmp_ui (x, 0) || strcmp(s,"x")) { printf ("Failed parsing ' 0x'\n S=%s\n X=", s); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_clear (x); }
static void check_special (void) { mpfr_t x, y; int res; char *s; mpfr_init (x); mpfr_init (y); /* Check dummy case */ res = mpfr_strtofr (x, "1234567.89E1", NULL, 10, MPFR_RNDN); mpfr_set_str (y, "1234567.89E1", 10, MPFR_RNDN); if (mpfr_cmp (x, y)) { printf ("Results differ between strtofr and set_str.\n" " set_str gives: "); mpfr_dump (y); printf (" strtofr gives: "); mpfr_dump (x); exit (1); } /* Check NAN */ mpfr_set_ui (x, 0, MPFR_RNDN); /* make sure that x is modified */ res = mpfr_strtofr (x, "NaN", &s, 10, MPFR_RNDN); if (res != 0 || !mpfr_nan_p (x) || *s != 0) { printf ("Error for setting NAN (1)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); /* make sure that x is modified */ res = mpfr_strtofr (x, "+NaN", &s, 10, MPFR_RNDN); if (res != 0 || !mpfr_nan_p (x) || *s != 0) { printf ("Error for setting +NAN (1)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); /* make sure that x is modified */ res = mpfr_strtofr (x, " -NaN", &s, 10, MPFR_RNDN); if (res != 0 || !mpfr_nan_p (x) || *s != 0) { printf ("Error for setting -NAN (1)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); /* make sure that x is modified */ res = mpfr_strtofr (x, "@nAn@xx", &s, 16, MPFR_RNDN); if (res != 0 || !mpfr_nan_p (x) || strcmp(s, "xx") ) { printf ("Error for setting NAN (2)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); /* make sure that x is modified */ res = mpfr_strtofr (x, "NAN(abcdEDF__1256)Hello", &s, 10, MPFR_RNDN); if (res != 0 || !mpfr_nan_p (x) || strcmp(s, "Hello") ) { printf ("Error for setting NAN (3)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); /* make sure that x is modified */ res = mpfr_strtofr (x, "NAN(abcdEDF)__1256)Hello", &s, 10, MPFR_RNDN); if (res != 0 || !mpfr_nan_p (x) || strcmp(s, "__1256)Hello") ) { printf ("Error for setting NAN (4)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); /* make sure that x is modified */ res = mpfr_strtofr (x, "NAN(abc%dEDF)__1256)Hello", &s, 10, MPFR_RNDN); if (res != 0 || !mpfr_nan_p (x) || strcmp(s, "(abc%dEDF)__1256)Hello") ) { printf ("Error for setting NAN (5)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); /* make sure that x is modified */ res = mpfr_strtofr (x, "NAN((abc))", &s, 10, MPFR_RNDN); if (res != 0 || !mpfr_nan_p (x) || strcmp(s, "((abc))") ) { printf ("Error for setting NAN (6)\n"); exit (1); } mpfr_set_ui (x, 0, MPFR_RNDN); /* make sure that x is modified */ res = mpfr_strtofr (x, "NAN()foo", &s, 10, MPFR_RNDN); if (res != 0 || !mpfr_nan_p (x) || strcmp(s, "foo") ) { printf ("Error for setting NAN (7)\n"); exit (1); } /* Check INF */ res = mpfr_strtofr (x, "INFINITY", &s, 8, MPFR_RNDN); if (res != 0 || !mpfr_inf_p (x) || *s != 0) { printf ("Error for setting INFINITY (1)\n s=%s\n x=", s); mpfr_dump (x); exit (1); } res = mpfr_strtofr (x, "INFANITY", &s, 8, MPFR_RNDN); if (res != 0 || !mpfr_inf_p (x) || strcmp(s, "ANITY")) { printf ("Error for setting INFINITY (2)\n s=%s\n x=", s); mpfr_dump (x); exit (1); } res = mpfr_strtofr (x, "@INF@*2", &s, 11, MPFR_RNDN); if (res != 0 || !mpfr_inf_p (x) || strcmp(s, "*2")) { printf ("Error for setting INFINITY (3)\n s=%s\n x=", s); mpfr_dump (x); exit (1); } /* Check Zero */ res = mpfr_strtofr (x, " 00000", &s, 11, MPFR_RNDN); if (res != 0 || !mpfr_zero_p (x) || s[0] != 0) { printf ("Error for setting ZERO (1)\n s=%s\n x=", s); mpfr_dump (x); exit (1); } /* Check base 62 */ res = mpfr_strtofr (x, "A", NULL, 62, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 10)) { printf ("Error for setting 'A' in base 62\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } res = mpfr_strtofr (x, "a", NULL, 62, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 36)) { printf ("Error for setting 'a' in base 62\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } res = mpfr_strtofr (x, "Z", NULL, 62, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 35)) { printf ("Error for setting 'Z' in base 62\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } res = mpfr_strtofr (x, "z", NULL, 62, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 61)) { printf ("Error for setting 'z' in base 62\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } res = mpfr_strtofr (x, "ZA", NULL, 62, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 2180)) { printf ("Error for setting 'ZA' in base 62\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } res = mpfr_strtofr (x, "za", NULL, 62, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 3818)) { printf ("Error for setting 'za' in base 62\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } res = mpfr_strtofr (x, "aZ", NULL, 62, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 2267)) { printf ("Error for setting 'aZ' in base 62\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } res = mpfr_strtofr (x, "Az", NULL, 62, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 681)) { printf ("Error for setting 'Az' in base 62\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check base 60 */ res = mpfr_strtofr (x, "Aa", NULL, 60, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 636)) { printf ("Error for setting 'Aa' in base 60\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } res = mpfr_strtofr (x, "Zz", &s, 60, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 35) || strcmp(s, "z") ) { printf ("Error for setting 'Zz' in base 60\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } /* Check base 61 */ res = mpfr_strtofr (x, "z", &s, 61, MPFR_RNDN); if (res != 0 || mpfr_cmp_ui (x, 0) || strcmp(s, "z") ) { printf ("Error for setting 'z' in base 61\n x="); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_clear (x); mpfr_clear (y); }
static void test_overflow2 (void) { mpfr_t x, y, z, r; int i, inex, rnd, err = 0; mpfr_inits2 (8, x, y, z, r, (void *) 0); MPFR_SET_POS (x); mpfr_setmin (x, mpfr_get_emax ()); /* x = 0.1@emax */ mpfr_set_si (y, -2, GMP_RNDN); /* y = -2 */ /* The intermediate multiplication x * y will overflow. */ for (i = -9; i <= 9; i++) RND_LOOP (rnd) { int inf, overflow; inf = rnd == GMP_RNDN || rnd == GMP_RNDD; overflow = inf || i <= 0; inex = mpfr_set_si_2exp (z, i, mpfr_get_emin (), GMP_RNDN); MPFR_ASSERTN (inex == 0); mpfr_clear_flags (); /* One has: x * y = -1@emax exactly (but not representable). */ inex = mpfr_fma (r, x, y, z, rnd); if (overflow ^ (mpfr_overflow_p () != 0)) { printf ("Error in test_overflow2 (i = %d, %s): wrong overflow" " flag (should be %d)\n", i, mpfr_print_rnd_mode (rnd), overflow); err = 1; } if (mpfr_nanflag_p ()) { printf ("Error in test_overflow2 (i = %d, %s): NaN flag should" " not be set\n", i, mpfr_print_rnd_mode (rnd)); err = 1; } if (mpfr_nan_p (r)) { printf ("Error in test_overflow2 (i = %d, %s): got NaN\n", i, mpfr_print_rnd_mode (rnd)); err = 1; } else if (MPFR_SIGN (r) >= 0) { printf ("Error in test_overflow2 (i = %d, %s): wrong sign " "(+ instead of -)\n", i, mpfr_print_rnd_mode (rnd)); err = 1; } else if (inf && ! mpfr_inf_p (r)) { printf ("Error in test_overflow2 (i = %d, %s): expected -Inf," " got\n", i, mpfr_print_rnd_mode (rnd)); mpfr_dump (r); err = 1; } else if (!inf && (mpfr_inf_p (r) || (mpfr_nextbelow (r), ! mpfr_inf_p (r)))) { printf ("Error in test_overflow2 (i = %d, %s): expected -MAX," " got\n", i, mpfr_print_rnd_mode (rnd)); mpfr_dump (r); err = 1; } if (inf ? inex >= 0 : inex <= 0) { printf ("Error in test_overflow2 (i = %d, %s): wrong inexact" " flag (got %d)\n", i, mpfr_print_rnd_mode (rnd), inex); err = 1; } } if (err) exit (1); mpfr_clears (x, y, z, r, (void *) 0); }