/* pos is 512 times the proportion of negative numbers. If pos=256, half of the numbers are negative. If pos=0, all generated numbers are positive. */ void tests_default_random (mpfr_ptr x, int pos, mpfr_exp_t emin, mpfr_exp_t emax) { MPFR_ASSERTN (emin <= emax); MPFR_ASSERTN (emin >= MPFR_EMIN_MIN); MPFR_ASSERTN (emax <= MPFR_EMAX_MAX); /* but it isn't required that emin and emax are in the current exponent range (see below), so that underflow/overflow checks can be done on 64-bit machines. */ mpfr_urandomb (x, RANDS); if (MPFR_IS_PURE_FP (x) && (emin >= 1 || (randlimb () & 1))) { mpfr_exp_t e; e = MPFR_GET_EXP (x) + (emin + (long) (randlimb () % (emax - emin + 1))); /* Note: There should be no overflow here because both terms are between MPFR_EMIN_MIN and MPFR_EMAX_MAX, but the sum e isn't necessarily between MPFR_EMIN_MIN and MPFR_EMAX_MAX. */ if (mpfr_set_exp (x, e)) { /* The random number doesn't fit in the current exponent range. In this case, test the function in the extended exponent range, which should be restored by the caller. */ mpfr_set_emin (MPFR_EMIN_MIN); mpfr_set_emax (MPFR_EMAX_MAX); mpfr_set_exp (x, e); } } if (randlimb () % 512 < pos) mpfr_neg (x, x, MPFR_RNDN); }
/* pos is 512 times the proportion of negative numbers. If pos=256, half of the numbers are negative. If pos=0, all generated numbers are positive. */ void tests_default_random (mpfr_ptr x, int pos, mpfr_exp_t emin, mpfr_exp_t emax, int always_scale) { MPFR_ASSERTN (emin <= emax); MPFR_ASSERTN (emin >= MPFR_EMIN_MIN); MPFR_ASSERTN (emax <= MPFR_EMAX_MAX); /* but it isn't required that emin and emax are in the current exponent range (see below), so that underflow/overflow checks can be done on 64-bit machines without a manual change of the exponent range (well, this is a bit ugly...). */ mpfr_urandomb (x, RANDS); if (MPFR_IS_PURE_FP (x) && (emin >= 1 || always_scale || (randlimb () & 1))) { mpfr_exp_t e; e = emin + (mpfr_exp_t) (randlimb () % (emax - emin + 1)); /* Note: There should be no overflow here because both terms are between MPFR_EMIN_MIN and MPFR_EMAX_MAX. */ MPFR_ASSERTD (e >= emin && e <= emax); if (mpfr_set_exp (x, e)) { /* The random number doesn't fit in the current exponent range. In this case, test the function in the extended exponent range, which should be restored by the caller. */ mpfr_set_emin (MPFR_EMIN_MIN); mpfr_set_emax (MPFR_EMAX_MAX); mpfr_set_exp (x, e); } } if (randlimb () % 512 < pos) mpfr_neg (x, x, MPFR_RNDN); }
/* bug found by Kevin P. Rauch on 22 Oct 2007 */ static void check2 (void) { mpfr_t x, y, z; int tern; mpfr_exp_t emin; emin = mpfr_get_emin (); mpfr_init2 (x, 32); mpfr_init2 (y, 32); mpfr_init2 (z, 32); mpfr_set_ui (x, 0xC0000000U, MPFR_RNDN); mpfr_neg (x, x, MPFR_RNDN); mpfr_set_ui (y, 0xFFFFFFFEU, MPFR_RNDN); mpfr_set_exp (x, 0); mpfr_set_exp (y, 0); mpfr_set_emin (-29); tern = mpfr_mul (z, x, y, MPFR_RNDN); /* z = -0.BFFFFFFE, tern > 0 */ tern = mpfr_subnormalize (z, tern, MPFR_RNDN); /* z should be -0.75 */ MPFR_ASSERTN (tern < 0 && mpfr_cmp_si_2exp (z, -3, -2) == 0); mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); MPFR_ASSERTN (mpfr_get_emin () == -29); set_emin (emin); }
/* from Christopher Creutzig, 18 Jul 2007 */ static void smallvals_atan2 (void) { mpfr_t a, x, y; mpfr_exp_t old_emin; mpfr_inits (a, x, y, (mpfr_ptr) 0); mpfr_set_ui (y, 0, MPFR_RNDN); mpfr_nextbelow (y); mpfr_set_ui (x, 1, MPFR_RNDN); /* y=-2^(-emin-1), x=1 */ mpfr_atan2 (a, y, x, MPFR_RNDD); MPFR_ASSERTN (mpfr_equal_p (a, y)); mpfr_atan2 (a, y, x, MPFR_RNDU); MPFR_ASSERTN (mpfr_zero_p (a) && MPFR_IS_NEG(a)); mpfr_set_prec (x, 8); mpfr_set_prec (y, 8); mpfr_set_prec (a, 8); old_emin = mpfr_get_emin (); mpfr_set_emin (MPFR_EMIN_MIN); mpfr_set_si (y, 3, MPFR_RNDN); mpfr_set_exp (y, mpfr_get_emin ()); mpfr_set_str_binary (x, "1.1"); mpfr_atan2 (a, y, x, MPFR_RNDU); mpfr_set_si (y, 1, MPFR_RNDN); mpfr_set_exp (y, mpfr_get_emin ()); MPFR_ASSERTN (mpfr_equal_p (a, y)); /* From a bug reported by Christopher Creutzig on 2007-08-28. Added test in each rounding mode. Segmentation fault or assertion failure due to an infinite Ziv loop. */ mpfr_set_si (y, 1, MPFR_RNDN); mpfr_set_exp (y, mpfr_get_emin ()); mpfr_set_str_binary (x, "1.01"); mpfr_atan2 (a, y, x, MPFR_RNDZ); MPFR_ASSERTN (mpfr_zero_p (a)); mpfr_atan2 (a, y, x, MPFR_RNDD); MPFR_ASSERTN (mpfr_zero_p (a)); mpfr_atan2 (a, y, x, MPFR_RNDU); MPFR_ASSERTN (mpfr_equal_p (a, y)); mpfr_atan2 (a, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_equal_p (a, y)); /* trigger underflow with rounding to nearest */ mpfr_set_ui (x, 4, MPFR_RNDN); mpfr_atan2 (a, y, x, MPFR_RNDN); MPFR_ASSERTN (mpfr_zero_p (a)); mpfr_set_emin (old_emin); mpfr_clears (a, x, y, (mpfr_ptr) 0); }
/* bug found by Kevin P. Rauch on 22 Oct 2007 */ static void check3 (void) { mpfr_t x, y, z; int tern; mpfr_exp_t emin; emin = mpfr_get_emin (); mpfr_init2 (x, 32); mpfr_init2 (y, 32); mpfr_init2 (z, 32); mpfr_set_ui (x, 0xBFFFFFFFU, MPFR_RNDN); /* 3221225471/2^32 */ mpfr_set_ui (y, 0x80000001U, MPFR_RNDN); /* 2147483649/2^32 */ mpfr_set_exp (x, 0); mpfr_set_exp (y, 0); mpfr_set_emin (-1); /* the exact product is 6917529028714823679/2^64, which is rounded to 3/8 = 0.375, which is smaller, thus tern < 0 */ tern = mpfr_mul (z, x, y, MPFR_RNDN); MPFR_ASSERTN (tern < 0 && mpfr_cmp_ui_2exp (z, 3, -3) == 0); tern = mpfr_subnormalize (z, tern, MPFR_RNDN); /* since emin = -1, and EXP(z)=-1, z should be rounded to precision EXP(z)-emin+1 = 1, i.e., z should be a multiple of the smallest possible positive representable value with emin=-1, which is 1/4. The two possible values are 1/4 and 2/4, which are at equal distance of z. But since tern < 0, we should choose the largest value, i.e., 2/4. */ MPFR_ASSERTN (tern > 0 && mpfr_cmp_ui_2exp (z, 1, -1) == 0); /* here is another test for the alternate case, where z was rounded up first, thus we have to round down */ mpfr_set_str_binary (x, "0.11111111111010110101011011011011"); mpfr_set_str_binary (y, "0.01100000000001111100000000001110"); tern = mpfr_mul (z, x, y, MPFR_RNDN); MPFR_ASSERTN (tern > 0 && mpfr_cmp_ui_2exp (z, 3, -3) == 0); tern = mpfr_subnormalize (z, tern, MPFR_RNDN); MPFR_ASSERTN (tern < 0 && mpfr_cmp_ui_2exp (z, 1, -2) == 0); /* finally the case where z was exact, which we simulate here */ mpfr_set_ui_2exp (z, 3, -3, MPFR_RNDN); tern = mpfr_subnormalize (z, 0, MPFR_RNDN); MPFR_ASSERTN (tern > 0 && mpfr_cmp_ui_2exp (z, 1, -1) == 0); mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); MPFR_ASSERTN (mpfr_get_emin () == -1); set_emin (emin); }
inline mpfr_class ldexp(const mpfr_class& v, int e) { //int e = mpfr_get_exp(*v.__get_mp()); mpfr_class result(v); mpfr_set_exp(result.__get_mp(), e); return result; }
int mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate) { mp_ptr rp; mp_prec_t nbits; mp_size_t nlimbs; mp_size_t k; /* number of high zero limbs */ mp_exp_t exp; int cnt; MPFR_CLEAR_FLAGS (rop); rp = MPFR_MANT (rop); nbits = MPFR_PREC (rop); nlimbs = MPFR_LIMB_SIZE (rop); MPFR_SET_POS (rop); /* Uniform non-normalized significand */ _gmp_rand (rp, rstate, nlimbs * BITS_PER_MP_LIMB); /* If nbits isn't a multiple of BITS_PER_MP_LIMB, mask the low bits */ cnt = nlimbs * BITS_PER_MP_LIMB - nbits; if (MPFR_LIKELY (cnt != 0)) rp[0] &= ~MPFR_LIMB_MASK (cnt); /* Count the null significant limbs and remaining limbs */ exp = 0; k = 0; while (nlimbs != 0 && rp[nlimbs - 1] == 0) { k ++; nlimbs --; exp -= BITS_PER_MP_LIMB; } if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */ { count_leading_zeros (cnt, rp[nlimbs - 1]); /* Normalization */ if (mpfr_set_exp (rop, exp - cnt)) { /* If the exponent is not in the current exponent range, we choose to return a NaN as this is probably a user error. Indeed this can happen only if the exponent range has been reduced to a very small interval and/or the precision is huge (very unlikely). */ MPFR_SET_NAN (rop); __gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */ return 1; } if (cnt != 0) mpn_lshift (rp + k, rp, nlimbs, cnt); if (k != 0) MPN_ZERO (rp, k); } else MPFR_SET_ZERO (rop); return 0; }
inline mpfr_class frexp(const mpfr_class& v, int* expon) { int e = mpfr_get_exp(v.__get_mp()); mpfr_class result(v); mpfr_set_exp(result.__get_mp(), 0); *expon = e; return result; }
int mpfr_urandomb (mpfr_ptr rop, gmp_randstate_t rstate) { mpfr_limb_ptr rp; mpfr_prec_t nbits; mp_size_t nlimbs; mp_size_t k; /* number of high zero limbs */ mpfr_exp_t exp; int cnt; rp = MPFR_MANT (rop); nbits = MPFR_PREC (rop); nlimbs = MPFR_LIMB_SIZE (rop); MPFR_SET_POS (rop); cnt = nlimbs * GMP_NUMB_BITS - nbits; /* Uniform non-normalized significand */ /* generate exactly nbits so that the random generator stays in the same state, independent of the machine word size GMP_NUMB_BITS */ mpfr_rand_raw (rp, rstate, nbits); if (MPFR_LIKELY (cnt != 0)) /* this will put the low bits to zero */ mpn_lshift (rp, rp, nlimbs, cnt); /* Count the null significant limbs and remaining limbs */ exp = 0; k = 0; while (nlimbs != 0 && rp[nlimbs - 1] == 0) { k ++; nlimbs --; exp -= GMP_NUMB_BITS; } if (MPFR_LIKELY (nlimbs != 0)) /* otherwise value is zero */ { count_leading_zeros (cnt, rp[nlimbs - 1]); /* Normalization */ if (mpfr_set_exp (rop, exp - cnt)) { /* If the exponent is not in the current exponent range, we choose to return a NaN as this is probably a user error. Indeed this can happen only if the exponent range has been reduced to a very small interval and/or the precision is huge (very unlikely). */ MPFR_SET_NAN (rop); __gmpfr_flags |= MPFR_FLAGS_NAN; /* Can't use MPFR_RET_NAN */ return 1; } if (cnt != 0) mpn_lshift (rp + k, rp, nlimbs, cnt); if (k != 0) MPN_ZERO (rp, k); } else MPFR_SET_ZERO (rop); return 0; }
static void test_sum (mpfr_prec_t f, unsigned long n) { mpfr_t sum, real_sum, real_non_rounded; mpfr_t *tab; unsigned long i; int rnd_mode; /* Init */ tab = (mpfr_t *) (*__gmp_allocate_func) (n * sizeof(mpfr_t)); for (i = 0; i < n; i++) mpfr_init2 (tab[i], f); mpfr_inits2 (f, sum, real_sum, real_non_rounded, (mpfr_ptr) 0); /* First Uniform */ for (i = 0; i < n; i++) mpfr_urandomb (tab[i], RANDS); algo_exact (real_non_rounded, tab, n, f); for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX; rnd_mode++) { sum_tab (sum, tab, n, (mpfr_rnd_t) rnd_mode); mpfr_set (real_sum, real_non_rounded, (mpfr_rnd_t) rnd_mode); if (mpfr_cmp (real_sum, sum) != 0) { printf ("mpfr_sum incorrect.\n"); mpfr_dump (real_sum); mpfr_dump (sum); exit (1); } } /* Then non uniform */ for (i = 0; i < n; i++) { mpfr_urandomb (tab[i], RANDS); if (! mpfr_zero_p (tab[i])) mpfr_set_exp (tab[i], randlimb () % 1000); } algo_exact (real_non_rounded, tab, n, f); for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX; rnd_mode++) { sum_tab (sum, tab, n, (mpfr_rnd_t) rnd_mode); mpfr_set (real_sum, real_non_rounded, (mpfr_rnd_t) rnd_mode); if (mpfr_cmp (real_sum, sum) != 0) { printf ("mpfr_sum incorrect.\n"); mpfr_dump (real_sum); mpfr_dump (sum); exit (1); } } /* Clear stuff */ for (i = 0; i < n; i++) mpfr_clear (tab[i]); mpfr_clears (sum, real_sum, real_non_rounded, (mpfr_ptr) 0); (*__gmp_free_func) (tab, n * sizeof(mpfr_t)); }
static void check_overflow (void) { mpfr_t a, b, c; mpfr_prec_t prec_a; int r; mpfr_init2 (a, 256); mpfr_init2 (b, 256); mpfr_init2 (c, 256); mpfr_set_ui (b, 1, MPFR_RNDN); mpfr_setmax (b, mpfr_get_emax ()); mpfr_set_ui (c, 1, MPFR_RNDN); mpfr_set_exp (c, mpfr_get_emax () - 192); RND_LOOP(r) for (prec_a = 128; prec_a < 512; prec_a += 64) { mpfr_set_prec (a, prec_a); mpfr_clear_overflow (); test_add (a, b, c, (mpfr_rnd_t) r); if (!mpfr_overflow_p ()) { printf ("No overflow in check_overflow\n"); exit (1); } } mpfr_set_exp (c, mpfr_get_emax () - 512); mpfr_set_prec (a, 256); mpfr_clear_overflow (); test_add (a, b, c, MPFR_RNDU); if (!mpfr_overflow_p ()) { printf ("No overflow in check_overflow\n"); exit (1); } mpfr_clear (a); mpfr_clear (b); mpfr_clear (c); }
inline mpfr_class min_value<mpfr_class>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(mpfr_class)) { static bool has_init = false; static mpfr_class val; if(!has_init) { val = 0.5; mpfr_set_exp(val.__get_mp(), mpfr_get_emin()); has_init = true; } return val; }
int main (int argc, char *argv[]) { mpfr_t x; int ret; mpfr_exp_t emin, emax; tests_start_mpfr (); emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_init (x); mpfr_set_ui (x, 1, MPFR_RNDN); ret = mpfr_set_exp (x, 2); MPFR_ASSERTN(ret == 0 && mpfr_cmp_ui (x, 2) == 0); set_emin (-1); ret = mpfr_set_exp (x, -1); MPFR_ASSERTN(ret == 0 && mpfr_cmp_ui_2exp (x, 1, -2) == 0); set_emax (1); ret = mpfr_set_exp (x, 1); MPFR_ASSERTN(ret == 0 && mpfr_cmp_ui (x, 1) == 0); ret = mpfr_set_exp (x, -2); MPFR_ASSERTN(ret != 0 && mpfr_cmp_ui (x, 1) == 0); ret = mpfr_set_exp (x, 2); MPFR_ASSERTN(ret != 0 && mpfr_cmp_ui (x, 1) == 0); mpfr_clear (x); set_emin (emin); set_emax (emax); tests_end_mpfr (); return 0; }
/* Test provided by Christopher Creutzig, 2007-05-21. */ static void check_tiny (void) { mpfr_t x, y; mpfr_init2 (x, 53); mpfr_init2 (y, 53); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); mpfr_sin (y, x, MPFR_RNDD); if (mpfr_cmp (x, y) < 0) { printf ("Error in check_tiny: got sin(x) > x for x = 2^(emin-1)\n"); exit (1); } mpfr_sin (y, x, MPFR_RNDU); mpfr_mul_2ui (y, y, 1, MPFR_RNDU); if (mpfr_cmp (x, y) > 0) { printf ("Error in check_tiny: got sin(x) < x/2 for x = 2^(emin-1)\n"); exit (1); } mpfr_neg (x, x, MPFR_RNDN); mpfr_sin (y, x, MPFR_RNDU); if (mpfr_cmp (x, y) > 0) { printf ("Error in check_tiny: got sin(x) < x for x = -2^(emin-1)\n"); exit (1); } mpfr_sin (y, x, MPFR_RNDD); mpfr_mul_2ui (y, y, 1, MPFR_RNDD); if (mpfr_cmp (x, y) < 0) { printf ("Error in check_tiny: got sin(x) > x/2 for x = -2^(emin-1)\n"); exit (1); } mpfr_clear (y); mpfr_clear (x); }
static void tiny (void) { mpfr_t x, s, c; int i, inex; mpfr_inits2 (64, x, s, c, (mpfr_ptr) 0); for (i = -1; i <= 1; i += 2) { mpfr_set_si (x, i, MPFR_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); inex = mpfr_sin_cos (s, c, x, MPFR_RNDN); MPFR_ASSERTN (inex != 0); MPFR_ASSERTN (mpfr_equal_p (s, x)); MPFR_ASSERTN (!mpfr_nan_p (c) && mpfr_cmp_ui (c, 1) == 0); } mpfr_clears (x, s, c, (mpfr_ptr) 0); }
static int mpfr_fsss (mpfr_ptr z, mpfr_srcptr a, mpfr_srcptr c, mpfr_rnd_t rnd) { /* Computes z = a^2 - c^2. Assumes that a and c are finite and non-zero; so a squaring yielding an infinity is an overflow, and a squaring yielding 0 is an underflow. Assumes further that z is distinct from a and c. */ int inex; mpfr_t u, v; /* u=a^2, v=c^2 exactly */ mpfr_init2 (u, 2*mpfr_get_prec (a)); mpfr_init2 (v, 2*mpfr_get_prec (c)); mpfr_sqr (u, a, MPFR_RNDN); mpfr_sqr (v, c, MPFR_RNDN); /* tentatively compute z as u-v; here we need z to be distinct from a and c to not lose the latter */ inex = mpfr_sub (z, u, v, rnd); if (mpfr_inf_p (z)) { /* replace by "correctly rounded overflow" */ mpfr_set_si (z, (mpfr_signbit (z) ? -1 : 1), MPFR_RNDN); inex = mpfr_mul_2ui (z, z, mpfr_get_emax (), rnd); } else if (mpfr_zero_p (u) && !mpfr_zero_p (v)) { /* exactly u underflowed, determine inexact flag */ inex = (mpfr_signbit (u) ? 1 : -1); } else if (mpfr_zero_p (v) && !mpfr_zero_p (u)) { /* exactly v underflowed, determine inexact flag */ inex = (mpfr_signbit (v) ? -1 : 1); } else if (mpfr_nan_p (z) || (mpfr_zero_p (u) && mpfr_zero_p (v))) { /* In the first case, u and v are +inf. In the second case, u and v are zeroes; their difference may be 0 or the least representable number, with a sign to be determined. Redo the computations with mpz_t exponents */ mpfr_exp_t ea, ec; mpz_t eu, ev; /* cheat to work around the const qualifiers */ /* Normalise the input by shifting and keep track of the shifts in the exponents of u and v */ ea = mpfr_get_exp (a); ec = mpfr_get_exp (c); mpfr_set_exp ((mpfr_ptr) a, (mpfr_prec_t) 0); mpfr_set_exp ((mpfr_ptr) c, (mpfr_prec_t) 0); mpz_init (eu); mpz_init (ev); mpz_set_si (eu, (long int) ea); mpz_mul_2exp (eu, eu, 1); mpz_set_si (ev, (long int) ec); mpz_mul_2exp (ev, ev, 1); /* recompute u and v and move exponents to eu and ev */ mpfr_sqr (u, a, MPFR_RNDN); /* exponent of u is non-positive */ mpz_sub_ui (eu, eu, (unsigned long int) (-mpfr_get_exp (u))); mpfr_set_exp (u, (mpfr_prec_t) 0); mpfr_sqr (v, c, MPFR_RNDN); mpz_sub_ui (ev, ev, (unsigned long int) (-mpfr_get_exp (v))); mpfr_set_exp (v, (mpfr_prec_t) 0); if (mpfr_nan_p (z)) { mpfr_exp_t emax = mpfr_get_emax (); int overflow; /* We have a = ma * 2^ea with 1/2 <= |ma| < 1 and ea <= emax. So eu <= 2*emax, and eu > emax since we have an overflow. The same holds for ev. Shift u and v by as much as possible so that one of them has exponent emax and the remaining exponents in eu and ev are the same. Then carry out the addition. Shifting u and v prevents an underflow. */ if (mpz_cmp (eu, ev) >= 0) { mpfr_set_exp (u, emax); mpz_sub_ui (eu, eu, (long int) emax); mpz_sub (ev, ev, eu); mpfr_set_exp (v, (mpfr_exp_t) mpz_get_ui (ev)); /* remaining common exponent is now in eu */ } else { mpfr_set_exp (v, emax); mpz_sub_ui (ev, ev, (long int) emax); mpz_sub (eu, eu, ev); mpfr_set_exp (u, (mpfr_exp_t) mpz_get_ui (eu)); mpz_set (eu, ev); /* remaining common exponent is now also in eu */ } inex = mpfr_sub (z, u, v, rnd); /* Result is finite since u and v have the same sign. */ overflow = mpfr_mul_2ui (z, z, mpz_get_ui (eu), rnd); if (overflow) inex = overflow; } else { int underflow; /* Subtraction of two zeroes. We have a = ma * 2^ea with 1/2 <= |ma| < 1 and ea >= emin and similarly for b. So 2*emin < 2*emin+1 <= eu < emin < 0, and analogously for v. */ mpfr_exp_t emin = mpfr_get_emin (); if (mpz_cmp (eu, ev) <= 0) { mpfr_set_exp (u, emin); mpz_add_ui (eu, eu, (unsigned long int) (-emin)); mpz_sub (ev, ev, eu); mpfr_set_exp (v, (mpfr_exp_t) mpz_get_si (ev)); } else { mpfr_set_exp (v, emin); mpz_add_ui (ev, ev, (unsigned long int) (-emin)); mpz_sub (eu, eu, ev); mpfr_set_exp (u, (mpfr_exp_t) mpz_get_si (eu)); mpz_set (eu, ev); } inex = mpfr_sub (z, u, v, rnd); mpz_neg (eu, eu); underflow = mpfr_div_2ui (z, z, mpz_get_ui (eu), rnd); if (underflow) inex = underflow; } mpz_clear (eu); mpz_clear (ev); mpfr_set_exp ((mpfr_ptr) a, ea); mpfr_set_exp ((mpfr_ptr) c, ec); /* works also when a == c */ } mpfr_clear (u); mpfr_clear (v); return inex; }
static void check4 (const char *as, const char *bs, mpfr_rnd_t rnd_mode, const char *res, int inex) { mpfr_t ta, tb, tc, tres; mpfr_exp_t emin, emax; int i; emin = mpfr_get_emin (); emax = mpfr_get_emax (); mpfr_inits2 (53, ta, tb, tc, tres, (mpfr_ptr) 0); for (i = 0; i <= 2; i++) { unsigned int expflags, newflags; int inex2; mpfr_set_str1 (ta, as); mpfr_set_str1 (tb, bs); mpfr_set_str1 (tc, res); if (i > 0) { mpfr_exp_t ea, eb, ec, e0; set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); ea = mpfr_get_exp (ta); eb = mpfr_get_exp (tb); ec = mpfr_get_exp (tc); e0 = i == 1 ? __gmpfr_emin : __gmpfr_emax; if ((i == 1 && ea < eb) || (i == 2 && ea > eb)) { mpfr_set_exp (ta, e0); mpfr_set_exp (tb, e0 + (eb - ea)); mpfr_set_exp (tc, e0 + (ec - ea)); } else { mpfr_set_exp (ta, e0 + (ea - eb)); mpfr_set_exp (tb, e0); mpfr_set_exp (tc, e0 + (ec - eb)); } } __gmpfr_flags = expflags = (randlimb () & 1) ? MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE : 0; inex2 = mpfr_agm (tres, ta, tb, rnd_mode); newflags = __gmpfr_flags; expflags |= MPFR_FLAGS_INEXACT; if (SIGN (inex2) != inex || newflags != expflags || ! mpfr_equal_p (tres, tc)) { printf ("mpfr_agm failed in rnd_mode=%s for\n", mpfr_print_rnd_mode (rnd_mode)); printf (" a = "); mpfr_out_str (stdout, 10, 0, ta, MPFR_RNDN); printf ("\n"); printf (" b = "); mpfr_out_str (stdout, 10, 0, tb, MPFR_RNDN); printf ("\n"); printf ("expected inex = %d, flags = %u,\n" " ", inex, expflags); mpfr_dump (tc); printf ("got inex = %d, flags = %u,\n" " ", inex2, newflags); mpfr_dump (tres); exit (1); } set_emin (emin); set_emax (emax); } mpfr_clears (ta, tb, tc, tres, (mpfr_ptr) 0); }
static void check_underflow (void) { mpfr_t sum1, sum2, t[NUNFL]; mpfr_ptr p[NUNFL]; mpfr_prec_t precmax = 444; mpfr_exp_t emin, emax; unsigned int ex_flags, flags; int c, i; emin = mpfr_get_emin (); emax = mpfr_get_emax (); set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); ex_flags = MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT; mpfr_init2 (sum1, MPFR_PREC_MIN); mpfr_init2 (sum2, precmax); for (i = 0; i < NUNFL; i++) { mpfr_init2 (t[i], precmax); p[i] = t[i]; } for (c = 0; c < 8; c++) { mpfr_prec_t fprec; int n, neg, r; fprec = MPFR_PREC_MIN + (randlimb () % (precmax - MPFR_PREC_MIN + 1)); n = 3 + (randlimb () % (NUNFL - 2)); MPFR_ASSERTN (n <= NUNFL); mpfr_set_prec (sum2, (randlimb () & 1) ? MPFR_PREC_MIN : precmax); mpfr_set_prec (t[0], fprec + 64); mpfr_set_zero (t[0], 1); for (i = 1; i < n; i++) { int inex; mpfr_set_prec (t[i], MPFR_PREC_MIN + (randlimb () % (fprec - MPFR_PREC_MIN + 1))); do mpfr_urandomb (t[i], RANDS); while (MPFR_IS_ZERO (t[i])); mpfr_set_exp (t[i], MPFR_EMIN_MIN); inex = mpfr_sub (t[0], t[0], t[i], MPFR_RNDN); MPFR_ASSERTN (inex == 0); } neg = randlimb () & 1; if (neg) mpfr_nextbelow (t[0]); else mpfr_nextabove (t[0]); RND_LOOP(r) { int inex1, inex2; mpfr_set_zero (sum1, 1); if (neg) mpfr_nextbelow (sum1); else mpfr_nextabove (sum1); inex1 = mpfr_div_2ui (sum1, sum1, 2, (mpfr_rnd_t) r); mpfr_clear_flags (); inex2 = mpfr_sum (sum2, p, n, (mpfr_rnd_t) r); flags = __gmpfr_flags; MPFR_ASSERTN (mpfr_check (sum1)); MPFR_ASSERTN (mpfr_check (sum2)); if (flags != ex_flags) { printf ("Bad flags in check_underflow on %s, c = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), c); printf ("Expected flags:"); flags_out (ex_flags); printf ("Got flags: "); flags_out (flags); printf ("sum = "); mpfr_dump (sum2); exit (1); } if (!(mpfr_equal_p (sum1, sum2) && SAME_SIGN (inex1, inex2))) { printf ("Error in check_underflow on %s, c = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), c); printf ("Expected "); mpfr_dump (sum1); printf ("with inex = %d\n", inex1); printf ("Got "); mpfr_dump (sum2); printf ("with inex = %d\n", inex2); exit (1); } } } for (i = 0; i < NUNFL; i++) mpfr_clear (t[i]); mpfr_clears (sum1, sum2, (mpfr_ptr) 0); set_emin (emin); set_emax (emax); }
static void generic_tests (void) { mpfr_t exact_sum, sum1, sum2; mpfr_t *t; mpfr_ptr *p; mpfr_prec_t precmax = 444; int i, m, nmax = 500; int rnd_mode; t = (mpfr_t *) tests_allocate (nmax * sizeof(mpfr_t)); p = (mpfr_ptr *) tests_allocate (nmax * sizeof(mpfr_ptr)); for (i = 0; i < nmax; i++) { mpfr_init2 (t[i], precmax); p[i] = t[i]; } mpfr_inits2 (precmax, exact_sum, sum1, sum2, (mpfr_ptr) 0); for (m = 0; m < 4000; m++) { int non_uniform, n; mpfr_prec_t prec; non_uniform = randlimb () % 10; n = (randlimb () % nmax) + 1; prec = MPFR_PREC_MIN + (randlimb () % (precmax - MPFR_PREC_MIN + 1)); mpfr_set_prec (sum1, prec); mpfr_set_prec (sum2, prec); for (i = 0; i < n; i++) { mpfr_set_prec (t[i], MPFR_PREC_MIN + (randlimb () % (precmax - MPFR_PREC_MIN + 1))); mpfr_urandomb (t[i], RANDS); if (m % 8 != 0 && (m % 8 == 1 || (randlimb () & 1))) mpfr_neg (t[i], t[i], MPFR_RNDN); if (non_uniform && MPFR_NOTZERO (t[i])) mpfr_set_exp (t[i], randlimb () % 1000); /* putchar ("-0+"[SIGN (mpfr_sgn (t[i])) + 1]); */ } /* putchar ('\n'); */ get_exact_sum (exact_sum, t, n); RND_LOOP (rnd_mode) { int inex1, inex2; inex1 = mpfr_set (sum1, exact_sum, (mpfr_rnd_t) rnd_mode); inex2 = mpfr_sum (sum2, p, n, (mpfr_rnd_t) rnd_mode); if (!(mpfr_equal_p (sum1, sum2) && SAME_SIGN (inex1, inex2))) { printf ("generic_tests failed on m = %d, %s\n", m, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd_mode)); printf ("Expected "); mpfr_dump (sum1); printf ("with inex = %d\n", inex1); printf ("Got "); mpfr_dump (sum2); printf ("with inex = %d\n", inex2); exit (1); } } } for (i = 0; i < nmax; i++) mpfr_clear (t[i]); mpfr_clears (exact_sum, sum1, sum2, (mpfr_ptr) 0); tests_free (t, nmax * sizeof(mpfr_t)); tests_free (p, nmax * sizeof(mpfr_ptr)); }
int main (void) { mpfr_t *tab; mpfr_ptr *tabtmp; unsigned long i, n; mp_prec_t f; int rnd_mode; mpfr_srcptr *perm; mpfr_t sum, real_sum, real_non_rounded; tests_start_mpfr (); n = 1026; f = 1764; tab = (mpfr_t *) malloc (n * sizeof(mpfr_t)); for (i = 0; i < n; i++) { mpfr_init2 (tab[i], f); mpfr_urandomb (tab[i], RANDS); } mpfr_init2 (sum, f); mpfr_init2 (real_sum, f); algo_exact (real_non_rounded, tab, n, f); for (rnd_mode = 0; rnd_mode < GMP_RND_MAX; rnd_mode++) { mpfr_list_sum (sum, tab, n, (mp_rnd_t) rnd_mode); mpfr_set (real_sum, real_non_rounded, (mp_rnd_t) rnd_mode); if (mpfr_cmp (real_sum, sum) != 0) { printf ("mpfr_list_sum incorrect.\n"); mpfr_print_binary (real_sum); putchar ('\n'); mpfr_print_binary (sum); putchar ('\n'); return 1; } } for (i = 0; i < n; i++) { mpfr_urandomb (tab[i], RANDS); } mpfr_set_exp (tab[0], 1000); mpfr_clear (real_non_rounded); algo_exact (real_non_rounded, tab, n, f); for (rnd_mode = 0; rnd_mode < GMP_RND_MAX; rnd_mode++) { mpfr_list_sum (sum, tab, n, (mp_rnd_t) rnd_mode); mpfr_set (real_sum, real_non_rounded, (mp_rnd_t) rnd_mode); if (mpfr_cmp (real_sum, sum) != 0) { printf ("mpfr_list_sum incorrect.\n"); mpfr_print_binary (real_sum); putchar ('\n'); mpfr_print_binary (sum); putchar ('\n'); return 1; } } /* list_sum tested, now test the sorting function */ for (i = 0; i < n; i++) mpfr_urandomb (tab[i], RANDS); tabtmp = (mpfr_ptr *) malloc (n * sizeof(mpfr_ptr)); perm = (mpfr_srcptr *) malloc (n * sizeof(mpfr_srcptr)); for (i = 0; i < n; i++) tabtmp[i] = tab[i]; mpfr_count_sort (tabtmp, n, perm); if (is_sorted (n, perm) == 0) { printf ("mpfr_count_sort incorrect.\n"); for (i = 0; i < n; i++) { mpfr_print_binary (perm[i]); putchar ('\n'); } return 1; } for (i = 0; i < n; i++) mpfr_clear (tab[i]); mpfr_clear (sum); mpfr_clear (real_sum); mpfr_clear (real_non_rounded); free (tab); free (perm); tests_end_mpfr (); return 0; }
static void special (void) { mpfr_t x, y, z; int inexact; mpfr_prec_t p; mpfr_init (x); mpfr_init (y); mpfr_init (z); mpfr_set_prec (x, 64); mpfr_set_str_binary (x, "1010000010100011011001010101010010001100001101011101110001011001E-1"); mpfr_set_prec (y, 32); test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 2405743844UL)) { printf ("Error for n^2+n+1/2 with n=2405743843\n"); exit (1); } mpfr_set_prec (x, 65); mpfr_set_str_binary (x, "10100000101000110110010101010100100011000011010111011100010110001E-2"); mpfr_set_prec (y, 32); test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 2405743844UL)) { printf ("Error for n^2+n+1/4 with n=2405743843\n"); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 66); mpfr_set_str_binary (x, "101000001010001101100101010101001000110000110101110111000101100011E-3"); mpfr_set_prec (y, 32); test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 2405743844UL)) { printf ("Error for n^2+n+1/4+1/8 with n=2405743843\n"); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 66); mpfr_set_str_binary (x, "101000001010001101100101010101001000110000110101110111000101100001E-3"); mpfr_set_prec (y, 32); test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 2405743843UL)) { printf ("Error for n^2+n+1/8 with n=2405743843\n"); mpfr_dump (y); exit (1); } mpfr_set_prec (x, 27); mpfr_set_str_binary (x, "0.110100111010101000010001011"); if ((inexact = test_sqrt (x, x, MPFR_RNDZ)) >= 0) { printf ("Wrong inexact flag: expected -1, got %d\n", inexact); exit (1); } mpfr_set_prec (x, 2); for (p=2; p<1000; p++) { mpfr_set_prec (z, p); mpfr_set_ui (z, 1, MPFR_RNDN); mpfr_nexttoinf (z); test_sqrt (x, z, MPFR_RNDU); if (mpfr_cmp_ui_2exp(x, 3, -1)) { printf ("Error: sqrt(1+ulp(1), up) should give 1.5 (prec=%u)\n", (unsigned int) p); printf ("got "); mpfr_print_binary (x); puts (""); exit (1); } } /* check inexact flag */ mpfr_set_prec (x, 5); mpfr_set_str_binary (x, "1.1001E-2"); if ((inexact = test_sqrt (x, x, MPFR_RNDN))) { printf ("Wrong inexact flag: expected 0, got %d\n", inexact); exit (1); } mpfr_set_prec (x, 2); mpfr_set_prec (z, 2); /* checks the sign is correctly set */ mpfr_set_si (x, 1, MPFR_RNDN); mpfr_set_si (z, -1, MPFR_RNDN); test_sqrt (z, x, MPFR_RNDN); if (mpfr_cmp_ui (z, 0) < 0) { printf ("Error: square root of 1 gives "); mpfr_print_binary(z); putchar('\n'); exit (1); } mpfr_set_prec (x, 192); mpfr_set_prec (z, 160); mpfr_set_str_binary (z, "0.1011010100000100100100100110011001011100100100000011000111011001011101101101110000110100001000100001100001011000E1"); mpfr_set_prec (x, 160); test_sqrt(x, z, MPFR_RNDN); test_sqrt(z, x, MPFR_RNDN); mpfr_set_prec (x, 53); mpfr_set_str (x, "8093416094703476.0", 10, MPFR_RNDN); mpfr_div_2exp (x, x, 1075, MPFR_RNDN); test_sqrt (x, x, MPFR_RNDN); mpfr_set_str (z, "1e55596835b5ef@-141", 16, MPFR_RNDN); if (mpfr_cmp (x, z)) { printf ("Error: square root of 8093416094703476*2^(-1075)\n"); printf ("expected "); mpfr_dump (z); printf ("got "); mpfr_dump (x); exit (1); } mpfr_set_prec (x, 33); mpfr_set_str_binary (x, "0.111011011011110001100111111001000e-10"); mpfr_set_prec (z, 157); inexact = test_sqrt (z, x, MPFR_RNDN); mpfr_set_prec (x, 157); mpfr_set_str_binary (x, "0.11110110101100101111001011100011100011100001101010111011010000100111011000111110100001001011110011111100101110010110010110011001011011010110010000011001101E-5"); if (mpfr_cmp (x, z)) { printf ("Error: square root (1)\n"); exit (1); } if (inexact <= 0) { printf ("Error: wrong inexact flag (1)\n"); exit (1); } /* case prec(result) << prec(input) */ mpfr_set_prec (z, 2); for (p = 2; p < 1000; p++) { mpfr_set_prec (x, p); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_nextabove (x); /* 1.0 < x <= 1.5 thus 1 < sqrt(x) <= 1.23 */ inexact = test_sqrt (z, x, MPFR_RNDN); MPFR_ASSERTN(inexact < 0 && mpfr_cmp_ui (z, 1) == 0); inexact = test_sqrt (z, x, MPFR_RNDZ); MPFR_ASSERTN(inexact < 0 && mpfr_cmp_ui (z, 1) == 0); inexact = test_sqrt (z, x, MPFR_RNDU); MPFR_ASSERTN(inexact > 0 && mpfr_cmp_ui_2exp (z, 3, -1) == 0); inexact = test_sqrt (z, x, MPFR_RNDD); MPFR_ASSERTN(inexact < 0 && mpfr_cmp_ui (z, 1) == 0); inexact = test_sqrt (z, x, MPFR_RNDA); MPFR_ASSERTN(inexact > 0 && mpfr_cmp_ui_2exp (z, 3, -1) == 0); } /* corner case rw = 0 in rounding to nearest */ mpfr_set_prec (z, GMP_NUMB_BITS - 1); mpfr_set_prec (y, GMP_NUMB_BITS - 1); mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_mul_2exp (y, y, GMP_NUMB_BITS - 1, MPFR_RNDN); mpfr_nextabove (y); for (p = 2 * GMP_NUMB_BITS - 1; p <= 1000; p++) { mpfr_set_prec (x, p); mpfr_set_ui (x, 1, MPFR_RNDN); mpfr_set_exp (x, GMP_NUMB_BITS); mpfr_add_ui (x, x, 1, MPFR_RNDN); /* now x = 2^(GMP_NUMB_BITS - 1) + 1 (GMP_NUMB_BITS bits) */ MPFR_ASSERTN (mpfr_mul (x, x, x, MPFR_RNDN) == 0); /* exact */ inexact = test_sqrt (z, x, MPFR_RNDN); /* even rule: z should be 2^(GMP_NUMB_BITS - 1) */ MPFR_ASSERTN (inexact < 0); MPFR_ASSERTN (mpfr_cmp_ui_2exp (z, 1, GMP_NUMB_BITS - 1) == 0); mpfr_nextbelow (x); /* now x is just below [2^(GMP_NUMB_BITS - 1) + 1]^2 */ inexact = test_sqrt (z, x, MPFR_RNDN); MPFR_ASSERTN(inexact < 0 && mpfr_cmp_ui_2exp (z, 1, GMP_NUMB_BITS - 1) == 0); mpfr_nextabove (x); mpfr_nextabove (x); /* now x is just above [2^(GMP_NUMB_BITS - 1) + 1]^2 */ inexact = test_sqrt (z, x, MPFR_RNDN); if (mpfr_cmp (z, y)) { printf ("Error for sqrt(x) in rounding to nearest\n"); printf ("x="); mpfr_dump (x); printf ("Expected "); mpfr_dump (y); printf ("Got "); mpfr_dump (z); exit (1); } if (inexact <= 0) { printf ("Wrong inexact flag in corner case for p = %lu\n", (unsigned long) p); exit (1); } } mpfr_set_prec (x, 1000); mpfr_set_ui (x, 9, MPFR_RNDN); mpfr_set_prec (y, 10); inexact = test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 3) || inexact != 0) { printf ("Error in sqrt(9:1000) for prec=10\n"); exit (1); } mpfr_set_prec (y, GMP_NUMB_BITS); mpfr_nextabove (x); inexact = test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 3) || inexact >= 0) { printf ("Error in sqrt(9:1000) for prec=%d\n", (int) GMP_NUMB_BITS); exit (1); } mpfr_set_prec (x, 2 * GMP_NUMB_BITS); mpfr_set_prec (y, GMP_NUMB_BITS); mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_nextabove (y); mpfr_set (x, y, MPFR_RNDN); inexact = test_sqrt (y, x, MPFR_RNDN); if (mpfr_cmp_ui (y, 1) || inexact >= 0) { printf ("Error in sqrt(1) for prec=%d\n", (int) GMP_NUMB_BITS); mpfr_dump (y); exit (1); } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); }
static void test_generic (mp_prec_t p0, mp_prec_t p1, unsigned int N) { mp_prec_t prec, xprec, yprec; mpfr_t x, y, z, t; #ifdef TWO_ARGS mpfr_t u; #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_t u; double d; #endif mp_rnd_t rnd; int inexact, compare, compare2; unsigned int n; unsigned long ctrt = 0, ctrn = 0; mp_exp_t old_emin, old_emax; old_emin = mpfr_get_emin (); old_emax = mpfr_get_emax (); mpfr_init (x); mpfr_init (y); mpfr_init (z); mpfr_init (t); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_init (u); #endif /* generic test */ for (prec = p0; prec <= p1; prec++) { mpfr_set_prec (z, prec); mpfr_set_prec (t, prec); yprec = prec + 10; mpfr_set_prec (y, yprec); /* Note: in precision p1, we test 4 special cases. */ for (n = 0; n < (prec == p1 ? N + 4 : N); n++) { xprec = prec; if (randlimb () & 1) { xprec *= (double) randlimb () / MP_LIMB_T_MAX; if (xprec < MPFR_PREC_MIN) xprec = MPFR_PREC_MIN; } mpfr_set_prec (x, xprec); #ifdef TWO_ARGS mpfr_set_prec (u, xprec); #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_prec (u, IEEE_DBL_MANT_DIG); #endif if (n > 3 || prec < p1) { #if defined(RAND_FUNCTION) RAND_FUNCTION (x); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) RAND_FUNCTION (u); #endif #else tests_default_random (x, TEST_RANDOM_POS, TEST_RANDOM_EMIN, TEST_RANDOM_EMAX); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) tests_default_random (u, TEST_RANDOM_POS2, TEST_RANDOM_EMIN, TEST_RANDOM_EMAX); #endif #endif } else { /* Special cases tested in precision p1 if n <= 3. They are useful really in the extended exponent range. */ set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); if (n <= 1) { mpfr_set_si (x, n == 0 ? 1 : -1, GMP_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, GMP_RNDN); mpfr_set_exp (u, mpfr_get_emin ()); #endif } else /* 2 <= n <= 3 */ { if (getenv ("MPFR_CHECK_MAX") == NULL) goto next_n; mpfr_set_si (x, n == 0 ? 1 : -1, GMP_RNDN); mpfr_setmax (x, REDUCE_EMAX); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, GMP_RNDN); mpfr_setmax (u, mpfr_get_emax ()); #endif } } rnd = RND_RAND (); mpfr_clear_flags (); #ifdef DEBUG_TGENERIC TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (y)); #endif #if defined(TWO_ARGS) compare = TEST_FUNCTION (y, x, u, rnd); #elif defined(DOUBLE_ARG1) d = mpfr_get_d (u, rnd); compare = TEST_FUNCTION (y, d, x, rnd); #elif defined(DOUBLE_ARG2) d = mpfr_get_d (u, rnd); compare = TEST_FUNCTION (y, x, d, rnd); #else compare = TEST_FUNCTION (y, x, rnd); #endif TGENERIC_CHECK ("Bad inexact flag", (compare != 0) ^ (mpfr_inexflag_p () == 0)); ctrt++; if (MPFR_IS_SINGULAR (y)) { if (MPFR_IS_NAN (y) || mpfr_nanflag_p ()) TGENERIC_CHECK ("Bad NaN flag", MPFR_IS_NAN (y) && mpfr_nanflag_p ()); else if (MPFR_IS_INF (y)) TGENERIC_CHECK ("Bad overflow flag", (compare != 0) ^ (mpfr_overflow_p () == 0)); else if (MPFR_IS_ZERO (y)) TGENERIC_CHECK ("Bad underflow flag", (compare != 0) ^ (mpfr_underflow_p () == 0)); } else if (mpfr_overflow_p ()) { TGENERIC_CHECK ("Bad compare value (overflow)", compare != 0); mpfr_nexttoinf (y); TGENERIC_CHECK ("Should have been max MPFR number", MPFR_IS_INF (y)); } else if (mpfr_underflow_p ()) { TGENERIC_CHECK ("Bad compare value (underflow)", compare != 0); mpfr_nexttozero (y); TGENERIC_CHECK ("Should have been min MPFR number", MPFR_IS_ZERO (y)); } else if (mpfr_can_round (y, yprec, rnd, rnd, prec)) { ctrn++; mpfr_set (t, y, rnd); /* Risk of failures are known when some flags are already set before the function call. Do not set the erange flag, as it will remain set after the function call and no checks are performed in such a case (see the mpfr_erangeflag_p test below). */ if (randlimb () & 1) __gmpfr_flags = MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE; #ifdef DEBUG_TGENERIC TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (z)); #endif /* Let's increase the precision of the inputs in a random way. In most cases, this doesn't make any difference, but this triggers the mpfr_fmod bug fixed in r6235. */ mpfr_prec_round (x, mpfr_get_prec (x) + (randlimb () & 15), GMP_RNDN); #if defined(TWO_ARGS) mpfr_prec_round (u, mpfr_get_prec (u) + (randlimb () & 15), GMP_RNDN); inexact = TEST_FUNCTION (z, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (z, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (z, x, d, rnd); #else inexact = TEST_FUNCTION (z, x, rnd); #endif if (mpfr_erangeflag_p ()) goto next_n; if (mpfr_nan_p (z) || mpfr_cmp (t, z) != 0) { printf ("results differ for x="); mpfr_out_str (stdout, 2, xprec, x, GMP_RNDN); #ifdef TWO_ARGS printf ("\nu="); mpfr_out_str (stdout, 2, xprec, u, GMP_RNDN); #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) printf ("\nu="); mpfr_out_str (stdout, 2, IEEE_DBL_MANT_DIG, u, GMP_RNDN); #endif 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); puts (""); printf ("expected "); mpfr_out_str (stdout, 2, prec, t, GMP_RNDN); puts (""); printf ("approx "); mpfr_print_binary (y); puts (""); 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 (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))) { printf ("Wrong inexact flag for rnd=%s: expected %d, got %d" "\n", mpfr_print_rnd_mode (rnd), compare, inexact); printf ("x="); mpfr_print_binary (x); puts (""); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) printf ("u="); mpfr_print_binary (u); puts (""); #endif printf ("y="); mpfr_print_binary (y); puts (""); printf ("t="); mpfr_print_binary (t); puts (""); exit (1); } } else if (getenv ("MPFR_SUSPICIOUS_OVERFLOW") != NULL) { /* For developers only! */ MPFR_ASSERTN (MPFR_IS_PURE_FP (y)); mpfr_nexttoinf (y); if (MPFR_IS_INF (y) && MPFR_IS_LIKE_RNDZ (rnd, MPFR_IS_NEG (y)) && !mpfr_overflow_p ()) { printf ("Possible bug! |y| is the maximum finite number " "and has been obtained when\nrounding toward zero" " (%s). Thus there is a very probable overflow,\n" "but the overflow flag is not set!\n", mpfr_print_rnd_mode (rnd)); printf ("x="); mpfr_print_binary (x); puts (""); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) printf ("u="); mpfr_print_binary (u); puts (""); #endif exit (1); } } next_n: /* In case the exponent range has been changed by tests_default_random() or for special values... */ mpfr_set_emin (old_emin); mpfr_set_emax (old_emax); } } #ifndef TGENERIC_NOWARNING if (3 * ctrn < 2 * ctrt) printf ("Warning! Too few normal cases in generic tests (%lu / %lu)\n", ctrn, ctrt); #endif mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (t); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_clear (u); #endif }
static void test_generic (mpfr_prec_t p0, mpfr_prec_t p1, unsigned int nmax) { mpfr_prec_t prec, xprec, yprec; mpfr_t x, y, z, t, w; #if defined(TWO_ARGS_ALL) mpfr_t u; #endif #if defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) double d; #endif #if defined(ULONG_ARG1) || defined(ULONG_ARG2) unsigned long i; #endif mpfr_rnd_t rnd; int inexact, compare, compare2; unsigned int n; unsigned long ctrt = 0, ctrn = 0; int test_of = 1, test_uf = 1; mpfr_exp_t old_emin, old_emax; old_emin = mpfr_get_emin (); old_emax = mpfr_get_emax (); mpfr_inits2 (MPFR_PREC_MIN, x, y, z, t, w, (mpfr_ptr) 0); #if defined(TWO_ARGS_ALL) mpfr_init2 (u, MPFR_PREC_MIN); #endif /* generic test */ for (prec = p0; prec <= p1; prec++) { mpfr_set_prec (z, prec); mpfr_set_prec (t, prec); yprec = prec + 10; mpfr_set_prec (y, yprec); mpfr_set_prec (w, yprec); /* Note: in precision p1, we test 4 special cases. */ for (n = 0; n < (prec == p1 ? nmax + 4 : nmax); n++) { int infinite_input = 0; unsigned int flags; mpfr_exp_t oemin, oemax; xprec = prec; if (randlimb () & 1) { xprec *= (double) randlimb () / MP_LIMB_T_MAX; if (xprec < MPFR_PREC_MIN) xprec = MPFR_PREC_MIN; } mpfr_set_prec (x, xprec); #if defined(TWO_ARGS) mpfr_set_prec (u, xprec); #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_prec (u, IEEE_DBL_MANT_DIG); #elif defined(ULONG_ARG1) || defined(ULONG_ARG2) mpfr_set_prec (u, sizeof (unsigned long) * CHAR_BIT); #endif if (n > 3 || prec < p1) { #if defined(RAND_FUNCTION) RAND_FUNCTION (x); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) RAND_FUNCTION (u); #endif #else /* ! defined(RAND_FUNCTION) */ tests_default_random (x, TEST_RANDOM_POS, TEST_RANDOM_EMIN, TEST_RANDOM_EMAX, TEST_RANDOM_ALWAYS_SCALE); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) tests_default_random (u, TEST_RANDOM_POS2, TEST_RANDOM_EMIN, TEST_RANDOM_EMAX, TEST_RANDOM_ALWAYS_SCALE); #endif #endif /* ! defined(RAND_FUNCTION) */ } else { /* Special cases tested in precision p1 if n <= 3. They are useful really in the extended exponent range. */ #if (defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2)) && defined(MPFR_ERRDIVZERO) goto next_n; #endif set_emin (MPFR_EMIN_MIN); set_emax (MPFR_EMAX_MAX); if (n <= 1) { mpfr_set_si (x, n == 0 ? 1 : -1, MPFR_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, MPFR_RNDN); mpfr_set_exp (u, mpfr_get_emin ()); #endif } else /* 2 <= n <= 3 */ { if (getenv ("MPFR_CHECK_MAX") == NULL) goto next_n; mpfr_set_si (x, n == 0 ? 1 : -1, MPFR_RNDN); mpfr_setmax (x, REDUCE_EMAX); #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, MPFR_RNDN); mpfr_setmax (u, mpfr_get_emax ()); #endif } } #if defined(ULONG_ARG1) || defined(ULONG_ARG2) i = randlimb (); inexact = mpfr_set_ui (u, i, MPFR_RNDN); MPFR_ASSERTN (inexact == 0); #endif /* Exponent range for the test. */ oemin = mpfr_get_emin (); oemax = mpfr_get_emax (); rnd = RND_RAND (); mpfr_clear_flags (); #ifdef DEBUG_TGENERIC TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (y)); #endif #if defined(TWO_ARGS) compare = TEST_FUNCTION (y, x, u, rnd); #elif defined(DOUBLE_ARG1) d = mpfr_get_d (u, rnd); compare = TEST_FUNCTION (y, d, x, rnd); /* d can be infinite due to overflow in mpfr_get_d */ infinite_input |= DOUBLE_ISINF (d); #elif defined(DOUBLE_ARG2) d = mpfr_get_d (u, rnd); compare = TEST_FUNCTION (y, x, d, rnd); /* d can be infinite due to overflow in mpfr_get_d */ infinite_input |= DOUBLE_ISINF (d); #elif defined(ULONG_ARG1) compare = TEST_FUNCTION (y, i, x, rnd); #elif defined(ULONG_ARG2) compare = TEST_FUNCTION (y, x, i, rnd); #else compare = TEST_FUNCTION (y, x, rnd); #endif flags = __gmpfr_flags; if (mpfr_get_emin () != oemin || mpfr_get_emax () != oemax) { printf ("tgeneric: the exponent range has been modified" " by the tested function!\n"); exit (1); } TGENERIC_CHECK ("bad inexact flag", (compare != 0) ^ (mpfr_inexflag_p () == 0)); ctrt++; /* Tests in a reduced exponent range. */ { unsigned int oldflags = flags; mpfr_exp_t e, emin, emax; /* Determine the smallest exponent range containing the exponents of the mpfr_t inputs (x, and u if TWO_ARGS) and output (y). */ emin = MPFR_EMAX_MAX; emax = MPFR_EMIN_MIN; if (MPFR_IS_PURE_FP (x)) { e = MPFR_GET_EXP (x); if (e < emin) emin = e; if (e > emax) emax = e; } #if defined(TWO_ARGS) if (MPFR_IS_PURE_FP (u)) { e = MPFR_GET_EXP (u); if (e < emin) emin = e; if (e > emax) emax = e; } #endif if (MPFR_IS_PURE_FP (y)) { e = MPFR_GET_EXP (y); if (test_of && e - 1 >= emax) { unsigned int ex_flags; mpfr_set_emax (e - 1); mpfr_clear_flags (); #if defined(TWO_ARGS) inexact = TEST_FUNCTION (w, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (w, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (w, x, d, rnd); #elif defined(ULONG_ARG1) inexact = TEST_FUNCTION (w, i, x, rnd); #elif defined(ULONG_ARG2) inexact = TEST_FUNCTION (w, x, i, rnd); #else inexact = TEST_FUNCTION (w, x, rnd); #endif flags = __gmpfr_flags; mpfr_set_emax (oemax); ex_flags = MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT; if (flags != ex_flags) { printf ("tgeneric: error for " MAKE_STR(TEST_FUNCTION) ", reduced exponent range [%" MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d] (overflow test) on:\n", (mpfr_eexp_t) oemin, (mpfr_eexp_t) e - 1); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif printf ("yprec = %u, rnd_mode = %s\n", (unsigned int) yprec, mpfr_print_rnd_mode (rnd)); printf ("Expected flags ="); flags_out (ex_flags); printf (" got flags ="); flags_out (flags); printf ("inex = %d, w = ", inexact); mpfr_dump (w); exit (1); } test_of = 0; /* Overflow is tested only once. */ } if (test_uf && e + 1 <= emin) { unsigned int ex_flags; mpfr_set_emin (e + 1); mpfr_clear_flags (); #if defined(TWO_ARGS) inexact = TEST_FUNCTION (w, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (w, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (w, x, d, rnd); #elif defined(ULONG_ARG1) inexact = TEST_FUNCTION (w, i, x, rnd); #elif defined(ULONG_ARG2) inexact = TEST_FUNCTION (w, x, i, rnd); #else inexact = TEST_FUNCTION (w, x, rnd); #endif flags = __gmpfr_flags; mpfr_set_emin (oemin); ex_flags = MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT; if (flags != ex_flags) { printf ("tgeneric: error for " MAKE_STR(TEST_FUNCTION) ", reduced exponent range [%" MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d] (underflow test) on:\n", (mpfr_eexp_t) e + 1, (mpfr_eexp_t) oemax); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif printf ("yprec = %u, rnd_mode = %s\n", (unsigned int) yprec, mpfr_print_rnd_mode (rnd)); printf ("Expected flags ="); flags_out (ex_flags); printf (" got flags ="); flags_out (flags); printf ("inex = %d, w = ", inexact); mpfr_dump (w); exit (1); } test_uf = 0; /* Underflow is tested only once. */ } if (e < emin) emin = e; if (e > emax) emax = e; } if (emin > emax) emin = emax; /* case where all values are singular */ /* Consistency test in a reduced exponent range. Doing it for the first 10 samples and for prec == p1 (which has some special cases) should be sufficient. */ if (ctrt <= 10 || prec == p1) { mpfr_set_emin (emin); mpfr_set_emax (emax); #ifdef DEBUG_TGENERIC /* Useful information in case of assertion failure. */ printf ("tgeneric: reduced exponent range [%" MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d]\n", (mpfr_eexp_t) emin, (mpfr_eexp_t) emax); #endif mpfr_clear_flags (); #if defined(TWO_ARGS) inexact = TEST_FUNCTION (w, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (w, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (w, x, d, rnd); #elif defined(ULONG_ARG1) inexact = TEST_FUNCTION (w, i, x, rnd); #elif defined(ULONG_ARG2) inexact = TEST_FUNCTION (w, x, i, rnd); #else inexact = TEST_FUNCTION (w, x, rnd); #endif flags = __gmpfr_flags; mpfr_set_emin (oemin); mpfr_set_emax (oemax); if (! (SAME_VAL (w, y) && SAME_SIGN (inexact, compare) && flags == oldflags)) { printf ("tgeneric: error for " MAKE_STR(TEST_FUNCTION) ", reduced exponent range [%" MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d] on:\n", (mpfr_eexp_t) emin, (mpfr_eexp_t) emax); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif printf ("yprec = %u, rnd_mode = %s\n", (unsigned int) yprec, mpfr_print_rnd_mode (rnd)); printf ("Expected:\n y = "); mpfr_dump (y); printf (" inex = %d, flags =", compare); flags_out (oldflags); printf ("Got:\n w = "); mpfr_dump (w); printf (" inex = %d, flags =", inexact); flags_out (flags); exit (1); } } __gmpfr_flags = oldflags; /* restore the flags */ } if (MPFR_IS_SINGULAR (y)) { if (MPFR_IS_NAN (y) || mpfr_nanflag_p ()) TGENERIC_CHECK ("bad NaN flag", MPFR_IS_NAN (y) && mpfr_nanflag_p ()); else if (MPFR_IS_INF (y)) { TGENERIC_CHECK ("bad overflow flag", (compare != 0) ^ (mpfr_overflow_p () == 0)); TGENERIC_CHECK ("bad divide-by-zero flag", (compare == 0 && !infinite_input) ^ (mpfr_divby0_p () == 0)); } else if (MPFR_IS_ZERO (y)) TGENERIC_CHECK ("bad underflow flag", (compare != 0) ^ (mpfr_underflow_p () == 0)); } else if (mpfr_divby0_p ()) { TGENERIC_CHECK ("both overflow and divide-by-zero", ! mpfr_overflow_p ()); TGENERIC_CHECK ("both underflow and divide-by-zero", ! mpfr_underflow_p ()); TGENERIC_CHECK ("bad compare value (divide-by-zero)", compare == 0); } else if (mpfr_overflow_p ()) { TGENERIC_CHECK ("both underflow and overflow", ! mpfr_underflow_p ()); TGENERIC_CHECK ("bad compare value (overflow)", compare != 0); mpfr_nexttoinf (y); TGENERIC_CHECK ("should have been max MPFR number (overflow)", MPFR_IS_INF (y)); } else if (mpfr_underflow_p ()) { TGENERIC_CHECK ("bad compare value (underflow)", compare != 0); mpfr_nexttozero (y); TGENERIC_CHECK ("should have been min MPFR number (underflow)", MPFR_IS_ZERO (y)); } else if (mpfr_can_round (y, yprec, rnd, rnd, prec)) { ctrn++; mpfr_set (t, y, rnd); /* Risk of failures are known when some flags are already set before the function call. Do not set the erange flag, as it will remain set after the function call and no checks are performed in such a case (see the mpfr_erangeflag_p test below). */ if (randlimb () & 1) __gmpfr_flags = MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE; #ifdef DEBUG_TGENERIC TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (z)); #endif /* Let's increase the precision of the inputs in a random way. In most cases, this doesn't make any difference, but for the mpfr_fmod bug fixed in r6230, this triggers the bug. */ mpfr_prec_round (x, mpfr_get_prec (x) + (randlimb () & 15), MPFR_RNDN); #if defined(TWO_ARGS) mpfr_prec_round (u, mpfr_get_prec (u) + (randlimb () & 15), MPFR_RNDN); inexact = TEST_FUNCTION (z, x, u, rnd); #elif defined(DOUBLE_ARG1) inexact = TEST_FUNCTION (z, d, x, rnd); #elif defined(DOUBLE_ARG2) inexact = TEST_FUNCTION (z, x, d, rnd); #elif defined(ULONG_ARG1) inexact = TEST_FUNCTION (z, i, x, rnd); #elif defined(ULONG_ARG2) inexact = TEST_FUNCTION (z, x, i, rnd); #else inexact = TEST_FUNCTION (z, x, rnd); #endif if (mpfr_erangeflag_p ()) goto next_n; if (! mpfr_equal_p (t, z)) { printf ("tgeneric: results differ for " MAKE_STR(TEST_FUNCTION) " on\n x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf (" u = "); mpfr_dump (u); #endif printf (" prec = %u, rnd_mode = %s\n", (unsigned int) prec, mpfr_print_rnd_mode (rnd)); printf ("Got "); mpfr_dump (z); printf ("Expected "); mpfr_dump (t); printf ("Approx "); mpfr_dump (y); 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 (compare * compare2 >= 0) compare = compare + compare2; else compare = inexact; /* cannot determine sign(t-f(x)) */ if (! SAME_SIGN (inexact, compare)) { printf ("Wrong inexact flag for rnd=%s: expected %d, got %d" "\n", mpfr_print_rnd_mode (rnd), compare, inexact); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif printf ("y = "); mpfr_dump (y); printf ("t = "); mpfr_dump (t); exit (1); } } else if (getenv ("MPFR_SUSPICIOUS_OVERFLOW") != NULL) { /* For developers only! */ MPFR_ASSERTN (MPFR_IS_PURE_FP (y)); mpfr_nexttoinf (y); if (MPFR_IS_INF (y) && MPFR_IS_LIKE_RNDZ (rnd, MPFR_IS_NEG (y)) && !mpfr_overflow_p () && TGENERIC_SO_TEST) { printf ("Possible bug! |y| is the maximum finite number " "and has been obtained when\nrounding toward zero" " (%s). Thus there is a very probable overflow,\n" "but the overflow flag is not set!\n", mpfr_print_rnd_mode (rnd)); printf ("x = "); mpfr_dump (x); #if defined(TWO_ARGS_ALL) printf ("u = "); mpfr_dump (u); #endif exit (1); } } next_n: /* In case the exponent range has been changed by tests_default_random() or for special values... */ mpfr_set_emin (old_emin); mpfr_set_emax (old_emax); } } #ifndef TGENERIC_NOWARNING if (3 * ctrn < 2 * ctrt) printf ("Warning! Too few normal cases in generic tests (%lu / %lu)\n", ctrn, ctrt); #endif mpfr_clears (x, y, z, t, w, (mpfr_ptr) 0); #if defined(TWO_ARGS_ALL) mpfr_clear (u); #endif }
int main (int argc, char *argv[]) { int b; mpfr_t x; mpfr_rnd_t r; char s[MAX_DIGITS + 2]; mpfr_exp_t e, f; size_t m; mpfr_prec_t p; int i; tests_start_mpfr (); check_small (); check_special (2, 2); for (i = 0; i < ITER; i++) { p = 2 + (randlimb () % (MAX_DIGITS - 1)); b = 2 + (randlimb () % 35); check_special (b, p); } mpfr_init2 (x, MAX_DIGITS); for (i = 0; i < ITER; i++) { m = 2 + (randlimb () % (MAX_DIGITS - 1)); mpfr_urandomb (x, RANDS); e = (mpfr_exp_t) (randlimb () % 21) - 10; if (!MPFR_IS_ZERO(x)) mpfr_set_exp (x, (e == -10) ? mpfr_get_emin () : ((e == 10) ? mpfr_get_emax () : e)); b = 2 + (randlimb () % 35); r = RND_RAND (); mpfr_get_str (s, &f, b, m, x, r); } mpfr_clear (x); check_large (); check3 ("4.059650008e-83", MPFR_RNDN, "40597"); check3 ("-6.606499965302424244461355e233", MPFR_RNDN, "-66065"); check3 ("-7.4", MPFR_RNDN, "-74000"); check3 ("0.997", MPFR_RNDN, "99700"); check3 ("-4.53063926135729747564e-308", MPFR_RNDN, "-45306"); check3 ("2.14478198760196000000e+16", MPFR_RNDN, "21448"); check3 ("7.02293374921793516813e-84", MPFR_RNDN, "70229"); check3 ("-6.7274500420134077e-87", MPFR_RNDN, "-67275"); check3 ("-6.7274500420134077e-87", MPFR_RNDZ, "-67274"); check3 ("-6.7274500420134077e-87", MPFR_RNDU, "-67274"); check3 ("-6.7274500420134077e-87", MPFR_RNDD, "-67275"); check3 ("-6.7274500420134077e-87", MPFR_RNDA, "-67275"); check3 ("6.7274500420134077e-87", MPFR_RNDN, "67275"); check3 ("6.7274500420134077e-87", MPFR_RNDZ, "67274"); check3 ("6.7274500420134077e-87", MPFR_RNDU, "67275"); check3 ("6.7274500420134077e-87", MPFR_RNDD, "67274"); check3 ("6.7274500420134077e-87", MPFR_RNDA, "67275"); check_bug_base2k (); check_reduced_exprange (); tests_end_mpfr (); return 0; }
static void underflows (void) { mpfr_t x, y, z; int err = 0; int inexact; int i; mp_exp_t emin; mpfr_init2 (x, 64); mpfr_init2 (y, 64); mpfr_set_ui (x, 1, GMP_RNDN); mpfr_set_exp (x, mpfr_get_emin()); for (i = 3; i < 10; i++) { mpfr_set_ui (y, i, GMP_RNDN); mpfr_div_2ui (y, y, 1, GMP_RNDN); test_pow (y, x, y, GMP_RNDN); if (!MPFR_IS_FP(y) || mpfr_cmp_ui (y, 0)) { printf ("Error in mpfr_pow for "); mpfr_out_str (stdout, 2, 0, x, GMP_RNDN); printf (" ^ (%d/2)\nGot ", i); mpfr_out_str (stdout, 2, 0, y, GMP_RNDN); printf (" instead of 0.\n"); exit (1); } } mpfr_init2 (z, 55); mpfr_set_str (x, "0.110011010011101001110001110100010000110111101E0", 2, GMP_RNDN); mpfr_set_str (y, "0.101110010011111001011010100011011100111110011E40", 2, GMP_RNDN); mpfr_clear_flags (); inexact = mpfr_pow (z, x, y, GMP_RNDU); if (!mpfr_underflow_p ()) { printf ("Underflow flag is not set for special underflow test.\n"); err = 1; } if (inexact <= 0) { printf ("Ternary value is wrong for special underflow test.\n"); err = 1; } mpfr_set_ui (x, 0, GMP_RNDN); mpfr_nextabove (x); if (mpfr_cmp (x, z) != 0) { printf ("Wrong value for special underflow test.\nGot "); mpfr_out_str (stdout, 2, 0, z, GMP_RNDN); printf ("\ninstead of "); mpfr_out_str (stdout, 2, 2, x, GMP_RNDN); printf ("\n"); err = 1; } if (err) exit (1); /* MPFR currently (2006-08-19) segfaults on the following code (and possibly makes other programs crash due to the lack of memory), because y is converted into an mpz_t, and the required precision is too high. */ mpfr_set_prec (x, 2); mpfr_set_prec (y, 2); mpfr_set_prec (z, 12); mpfr_set_ui_2exp (x, 3, -2, GMP_RNDN); mpfr_set_ui_2exp (y, 1, mpfr_get_emax () - 1, GMP_RNDN); mpfr_clear_flags (); mpfr_pow (z, x, y, GMP_RNDN); if (!mpfr_underflow_p () || MPFR_NOTZERO (z)) { printf ("Underflow test with large y fails.\n"); exit (1); } emin = mpfr_get_emin (); mpfr_set_emin (-256); mpfr_set_prec (x, 2); mpfr_set_prec (y, 2); mpfr_set_prec (z, 12); mpfr_set_ui_2exp (x, 3, -2, GMP_RNDN); mpfr_set_ui_2exp (y, 1, 38, GMP_RNDN); mpfr_clear_flags (); inexact = mpfr_pow (z, x, y, GMP_RNDN); if (!mpfr_underflow_p () || MPFR_NOTZERO (z) || inexact >= 0) { printf ("Bad underflow detection for 0.75^(2^38). Obtained:\n" "Underflow flag... %-3s (should be 'yes')\n" "Zero result...... %-3s (should be 'yes')\n" "Inexact value.... %-3d (should be negative)\n", mpfr_underflow_p () ? "yes" : "no", MPFR_IS_ZERO (z) ? "yes" : "no", inexact); exit (1); } mpfr_set_emin (emin); emin = mpfr_get_emin (); mpfr_set_emin (-256); mpfr_set_prec (x, 2); mpfr_set_prec (y, 40); mpfr_set_prec (z, 12); mpfr_set_ui_2exp (x, 3, -1, GMP_RNDN); mpfr_set_si_2exp (y, -1, 38, GMP_RNDN); for (i = 0; i < 4; i++) { if (i == 2) mpfr_neg (x, x, GMP_RNDN); mpfr_clear_flags (); inexact = mpfr_pow (z, x, y, GMP_RNDN); if (!mpfr_underflow_p () || MPFR_NOTZERO (z) || (i == 3 ? (inexact <= 0) : (inexact >= 0))) { printf ("Bad underflow detection for ("); mpfr_out_str (stdout, 10, 0, x, GMP_RNDN); printf (")^(-2^38-%d). Obtained:\n" "Overflow flag.... %-3s (should be 'no')\n" "Underflow flag... %-3s (should be 'yes')\n" "Zero result...... %-3s (should be 'yes')\n" "Inexact value.... %-3d (should be %s)\n", i, mpfr_overflow_p () ? "yes" : "no", mpfr_underflow_p () ? "yes" : "no", MPFR_IS_ZERO (z) ? "yes" : "no", inexact, i == 3 ? "positive" : "negative"); exit (1); } inexact = mpfr_sub_ui (y, y, 1, GMP_RNDN); MPFR_ASSERTN (inexact == 0); } mpfr_set_emin (emin); mpfr_clears (x, y, z, (void *) 0); }
static void consistency (void) { mpfr_t x, s1, s2, c1, c2; mpfr_exp_t emin, emax; mpfr_rnd_t rnd; unsigned int flags_sin, flags_cos, flags, flags_before, flags_ref; int inex_sin, is, inex_cos, ic, inex, inex_ref; int i; emin = mpfr_get_emin (); emax = mpfr_get_emax (); for (i = 0; i <= 10000; i++) { mpfr_init2 (x, MPFR_PREC_MIN + (randlimb () % 8)); mpfr_inits2 (MPFR_PREC_MIN + (randlimb () % 8), s1, s2, c1, c2, (mpfr_ptr) 0); if (i < 8 * MPFR_RND_MAX) { int j = i / MPFR_RND_MAX; if (j & 1) mpfr_set_emin (MPFR_EMIN_MIN); mpfr_set_si (x, (j & 2) ? 1 : -1, MPFR_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); rnd = (mpfr_rnd_t) (i % MPFR_RND_MAX); flags_before = 0; if (j & 4) mpfr_set_emax (-17); } else { tests_default_random (x, 256, -5, 50, 0); rnd = RND_RAND (); flags_before = (randlimb () & 1) ? (unsigned int) (MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE) : (unsigned int) 0; } __gmpfr_flags = flags_before; inex_sin = mpfr_sin (s1, x, rnd); is = inex_sin < 0 ? 2 : inex_sin > 0 ? 1 : 0; flags_sin = __gmpfr_flags; __gmpfr_flags = flags_before; inex_cos = mpfr_cos (c1, x, rnd); ic = inex_cos < 0 ? 2 : inex_cos > 0 ? 1 : 0; flags_cos = __gmpfr_flags; __gmpfr_flags = flags_before; inex = mpfr_sin_cos (s2, c2, x, rnd); flags = __gmpfr_flags; inex_ref = is + 4 * ic; flags_ref = flags_sin | flags_cos; if (!(mpfr_equal_p (s1, s2) && mpfr_equal_p (c1, c2)) || inex != inex_ref || flags != flags_ref) { printf ("mpfr_sin_cos and mpfr_sin/mpfr_cos disagree on %s," " i = %d\nx = ", mpfr_print_rnd_mode (rnd), i); mpfr_dump (x); printf ("s1 = "); mpfr_dump (s1); printf ("s2 = "); mpfr_dump (s2); printf ("c1 = "); mpfr_dump (c1); printf ("c2 = "); mpfr_dump (c2); printf ("inex_sin = %d (s = %d), inex_cos = %d (c = %d), " "inex = %d (expected %d)\n", inex_sin, is, inex_cos, ic, inex, inex_ref); printf ("flags_sin = 0x%x, flags_cos = 0x%x, " "flags = 0x%x (expected 0x%x)\n", flags_sin, flags_cos, flags, flags_ref); exit (1); } mpfr_clears (x, s1, s2, c1, c2, (mpfr_ptr) 0); mpfr_set_emin (emin); mpfr_set_emax (emax); } }
int main (int argc, char *argv[]) { char *filenameCompressed = FILE_NAME_RW; char *data = FILE_NAME_R; int status; FILE *fh; mpfr_t x[9]; mpfr_t y; unsigned char badData[6][2] = { { 7, 0 }, { 16, 0 }, { 23, 118 }, { 23, 95 }, { 23, 127 }, { 23, 47 } }; int badDataSize[6] = { 1, 1, 2, 2, 2, 2 }; int i; if (argc != 1) { printf ("Usage: %s\n", argv[0]); exit (1); } tests_start_mpfr (); mpfr_init2 (x[0], 130); mpfr_init2 (x[8], 130); mpfr_inits2 (2048, x[1], x[2], x[3], x[4], x[5], x[6], x[7], (mpfr_ptr) 0); mpfr_set_str1 (x[0], "45.2564215000000018562786863185465335845947265625"); mpfr_set_str1 (x[1], "45.2564215000000018562786863185465335845947265625"); mpfr_set_str1 (x[2], "45.2564215000000018562786863185465335845947265625"); mpfr_set_exp (x[2], -48000); mpfr_set_inf (x[3], -1); mpfr_set_zero (x[4], 0); mpfr_set_nan (x[5]); mpfr_set_ui (x[6], 104348, MPFR_RNDN); mpfr_set_ui (x[7], 33215, MPFR_RNDN); mpfr_div (x[8], x[6], x[7], MPFR_RNDN); mpfr_div (x[6], x[6], x[7], MPFR_RNDN); /* we first write to file FILE_NAME_RW the numbers x[i] */ fh = fopen (filenameCompressed, "w"); if (fh == NULL) { printf ("Failed to open for writing %s, exiting...\n", filenameCompressed); exit (1); } for (i = 0; i < 9; i++) { status = mpfr_fpif_export (fh, x[i]); if (status != 0) { fclose (fh); printf ("Failed to export number %d, exiting...\n", i); exit (1); } } fclose (fh); /* we then read back FILE_NAME_RW and check we get the same numbers x[i] */ fh = fopen (filenameCompressed, "r"); if (fh == NULL) { printf ("Failed to open for reading %s, exiting...\n", filenameCompressed); exit (1); } for (i = 0; i < 9; i++) { mpfr_init2 (y, 2); mpfr_fpif_import (y, fh); if (mpfr_cmp(x[i], y) != 0) { printf ("mpfr_cmp failed on written number %d, exiting...\n", i); printf ("expected "); mpfr_dump (x[i]); printf ("got "); mpfr_dump (y); exit (1); } mpfr_clear (y); } fclose (fh); /* we do the same for the fixed file FILE_NAME_R, this ensures we get same results with different word size or endianness */ fh = src_fopen (data, "r"); if (fh == NULL) { printf ("Failed to open for reading %s in srcdir, exiting...\n", data); exit (1); } for (i = 0; i < 9; i++) { mpfr_init2 (y, 2); mpfr_fpif_import (y, fh); if (mpfr_cmp (x[i], y) != 0) { printf ("mpfr_cmp failed on data number %d, exiting...\n", i); printf ("expected "); mpfr_dump (x[i]); printf ("got "); mpfr_dump (y); exit (1); } mpfr_clear (y); } fclose (fh); for (i = 0; i < 9; i++) mpfr_clear (x[i]); remove (filenameCompressed); mpfr_init2 (y, 2); status = mpfr_fpif_export (NULL, y); if (status == 0) { printf ("mpfr_fpif_export did not fail with a NULL file\n"); exit(1); } status = mpfr_fpif_import (y, NULL); if (status == 0) { printf ("mpfr_fpif_import did not fail with a NULL file\n"); exit(1); } fh = fopen (filenameCompressed, "w+"); if (fh == NULL) { printf ("Failed to open for reading/writing %s, exiting...\n", filenameCompressed); fclose (fh); remove (filenameCompressed); exit (1); } status = mpfr_fpif_import (y, fh); if (status == 0) { printf ("mpfr_fpif_import did not fail on a empty file\n"); fclose (fh); remove (filenameCompressed); exit(1); } for (i = 0; i < 6; i++) { rewind (fh); status = fwrite (&badData[i][0], badDataSize[i], 1, fh); if (status != 1) { printf ("Write error on the test file\n"); fclose (fh); remove (filenameCompressed); exit(1); } rewind (fh); status = mpfr_fpif_import (y, fh); if (status == 0) { printf ("mpfr_fpif_import did not fail on a bad imported data\n"); switch (i) { case 0: printf (" not enough precision data\n"); break; case 1: printf (" no exponent data\n"); break; case 2: printf (" too big exponent\n"); break; case 3: printf (" not enough exponent data\n"); break; case 4: printf (" exponent data wrong\n"); break; case 5: printf (" no limb data\n"); break; default: printf ("Test fatal error, unknown case\n"); break; } fclose (fh); remove (filenameCompressed); exit(1); } } fclose (fh); mpfr_clear (y); fh = fopen (filenameCompressed, "r"); if (fh == NULL) { printf ("Failed to open for reading %s, exiting...\n", filenameCompressed); exit (1); } mpfr_init2 (y, 2); status = mpfr_fpif_export (fh, y); if (status == 0) { printf ("mpfr_fpif_export did not fail on a read only stream\n"); exit(1); } fclose (fh); remove (filenameCompressed); mpfr_clear (y); tests_end_mpfr (); return 0; }
static void test_generic_ui (mpfr_prec_t p0, mpfr_prec_t p1, unsigned int N) { mpfr_prec_t prec, yprec; mpfr_t x, y, z, t; INTEGER_TYPE u; mpfr_rnd_t rnd; int inexact, compare, compare2; unsigned int n; mpfr_init (x); mpfr_init (y); mpfr_init (z); mpfr_init (t); /* generic test */ for (prec = p0; prec <= p1; prec++) { mpfr_set_prec (x, prec); mpfr_set_prec (z, prec); mpfr_set_prec (t, prec); yprec = prec + 10; for (n = 0; n <= N; n++) { if (n > 1 || prec < p1) RAND_FUNCTION (x); else { /* Special cases tested in precision p1 if n <= 1. */ mpfr_set_si (x, n == 0 ? 1 : -1, MPFR_RNDN); mpfr_set_exp (x, mpfr_get_emin ()); } u = INT_RAND_FUNCTION (); rnd = RND_RAND (); mpfr_set_prec (y, yprec); compare = TEST_FUNCTION (y, x, u, rnd); if (mpfr_can_round (y, yprec, rnd, rnd, prec)) { mpfr_set (t, y, rnd); inexact = TEST_FUNCTION (z, x, u, rnd); if (mpfr_cmp (t, z)) { printf ("results differ for x="); mpfr_out_str (stdout, 2, prec, x, MPFR_RNDN); printf ("\nu=%lu", (unsigned long) u); printf (" prec=%lu rnd_mode=%s\n", (unsigned long ) prec, mpfr_print_rnd_mode (rnd)); #ifdef TEST_FUNCTION_NAME printf ("Function: %s\n", TEST_FUNCTION_NAME); #endif printf ("got "); mpfr_out_str (stdout, 2, prec, z, MPFR_RNDN); puts (""); printf ("expected "); mpfr_out_str (stdout, 2, prec, t, MPFR_RNDN); puts (""); printf ("approx "); mpfr_print_binary (y); puts (""); 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 (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))) { printf ("Wrong inexact flag for rnd=%s: expected %d, got %d" "\n", mpfr_print_rnd_mode (rnd), compare, inexact); printf ("x="); mpfr_print_binary (x); puts (""); printf ("u=%lu", (unsigned long) u); printf ("y="); mpfr_print_binary (y); puts (""); printf ("t="); mpfr_print_binary (t); puts (""); exit (1); } } } } mpfr_clear (x); mpfr_clear (y); mpfr_clear (z); mpfr_clear (t); }