static void check_one (mpz_ptr z) { int inex; 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*GMP_NUMB_BITS ; sh < 2*GMP_NUMB_BITS ; sh++) { for (neg = 0; neg <= 1; neg++) { mpz_neg (z, z); mpfr_set_z (f, z, MPFR_RNDN); if (sh < 0) { mpz_tdiv_q_2exp (z, z, -sh); mpfr_div_2exp (f, f, -sh, MPFR_RNDN); } else { mpz_mul_2exp (z, z, sh); mpfr_mul_2exp (f, f, sh, MPFR_RNDN); } inex = mpfr_get_z (got, f, MPFR_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); } if (! SAME_SIGN (inex, - mpfr_cmp_z (f, z))) { printf ("Wrong inexact value for shift=%d\n", sh); printf (" f "); mpfr_dump (f); printf (" got %+d\n", inex); printf (" want %+d\n", -mpfr_cmp_z (f, z)); exit (1); } } } mpfr_clear (f); mpz_clear (got); }
static void test_cmp_z (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpz_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, MPFR_PREC_MIN); mpz_init (y); /* check the erange flag when x is NaN */ mpfr_set_nan (x); mpz_set_ui (y, 17); mpfr_clear_erangeflag (); res1 = mpfr_cmp_z (x, y); if (res1 != 0 || mpfr_erangeflag_p () == 0) { printf ("Error for mpfr_cmp_z (NaN, 17)\n"); printf ("Return value: expected 0, got %d\n", res1); printf ("Erange flag: expected set, got %d\n", mpfr_erangeflag_p ()); exit (1); } for(p=pmin ; p < pmax ; p++) { mpfr_set_prec (x, p); for ( n = 0; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpz_urandomb (y, RANDS, 1024); if (!MPFR_IS_SINGULAR (x)) { mpfr_sub_z (z, x, y, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_z (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_z: res=%d sub_z gives %d\n", res2, res1); exit (1); } } } } mpz_clear (y); mpfr_clear (x); mpfr_clear (z); }
static void test_cmp_z (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpz_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, MPFR_PREC_MIN); mpz_init (y); for(p=pmin ; p < pmax ; p++) { mpfr_set_prec (x, p); for ( n = 0; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpz_urandomb (y, RANDS, 1024); if (!MPFR_IS_SINGULAR (x)) { mpfr_sub_z (z, x, y, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_z (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_z: res=%d sub_z gives %d\n", res2, res1); exit (1); } } } } mpz_clear (y); mpfr_clear (x); mpfr_clear (z); }
static void reduced_expo_range (void) { mpfr_t x; mpz_t z; mpq_t q; mpfr_exp_t emin; int inex; emin = mpfr_get_emin (); set_emin (4); mpfr_init2 (x, 32); mpz_init (z); mpfr_clear_flags (); inex = mpfr_set_ui (x, 17, MPFR_RNDN); MPFR_ASSERTN (inex == 0); mpz_set_ui (z, 3); inex = mpfr_mul_z (x, x, z, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 51) != 0) { printf ("Error 1 in reduce_expo_range: expected 51 with inex = 0," " got\n"); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf ("with inex = %d\n", inex); exit (1); } inex = mpfr_div_z (x, x, z, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 17) != 0) { printf ("Error 2 in reduce_expo_range: expected 17 with inex = 0," " got\n"); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf ("with inex = %d\n", inex); exit (1); } inex = mpfr_add_z (x, x, z, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 20) != 0) { printf ("Error 3 in reduce_expo_range: expected 20 with inex = 0," " got\n"); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf ("with inex = %d\n", inex); exit (1); } inex = mpfr_sub_z (x, x, z, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 17) != 0) { printf ("Error 4 in reduce_expo_range: expected 17 with inex = 0," " got\n"); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN); printf ("with inex = %d\n", inex); exit (1); } MPFR_ASSERTN (__gmpfr_flags == 0); if (mpfr_cmp_z (x, z) <= 0) { printf ("Error 5 in reduce_expo_range: expected a positive value.\n"); exit (1); } mpz_clear (z); mpq_init (q); mpq_set_ui (q, 1, 1); mpfr_set_ui (x, 16, MPFR_RNDN); inex = mpfr_add_q (x, x, q, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 17) != 0) { printf ("Error in reduce_expo_range for 16 + 1/1," " got inex = %d and\nx = ", inex); mpfr_dump (x); exit (1); } inex = mpfr_sub_q (x, x, q, MPFR_RNDN); if (inex != 0 || MPFR_IS_NAN (x) || mpfr_cmp_ui (x, 16) != 0) { printf ("Error in reduce_expo_range for 17 - 1/1," " got inex = %d and\nx = ", inex); mpfr_dump (x); exit (1); } mpq_clear (q); mpfr_clear (x); set_emin (emin); }
static void check_one (mpz_ptr z) { int inex, ex_inex, same; int sh, neg; mpfr_t f; mpz_t got, ex; mpfr_init2 (f, MAX (mpz_sizeinbase (z, 2), MPFR_PREC_MIN)); mpz_init (got); mpz_init (ex); for (sh = -2*GMP_NUMB_BITS ; sh < 2*GMP_NUMB_BITS ; sh++) { inex = mpfr_set_z (f, z, MPFR_RNDN); /* exact */ MPFR_ASSERTN (inex == 0); if (sh < 0) { mpz_tdiv_q_2exp (ex, z, -sh); inex = mpfr_div_2exp (f, f, -sh, MPFR_RNDN); } else { mpz_mul_2exp (ex, z, sh); inex = mpfr_mul_2exp (f, f, sh, MPFR_RNDN); } MPFR_ASSERTN (inex == 0); for (neg = 0; neg <= 1; neg++) { /* Test (-1)^neg * z * 2^sh */ int fi; mpfr_flags_t flags[3] = { 0, MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE, MPFR_FLAGS_ALL }, ex_flags, gt_flags; for (fi = 0; fi < numberof (flags); fi++) { ex_inex = - mpfr_cmp_z (f, ex); ex_flags = __gmpfr_flags = flags[fi]; if (ex_inex != 0) ex_flags |= MPFR_FLAGS_INEXACT; inex = mpfr_get_z (got, f, MPFR_RNDZ); gt_flags = __gmpfr_flags; same = SAME_SIGN (inex, ex_inex); if (mpz_cmp (got, ex) != 0 || !same || gt_flags != ex_flags) { printf ("Error in check_one for sh=%d, fi=%d\n", sh, fi); printf (" f = "); mpfr_dump (f); printf ("expected "); mpz_dump (ex); printf (" got "); mpz_dump (got); printf ("Expected inex ~ %d, got %d (%s)\n", inex, ex_inex, same ? "OK" : "wrong"); printf ("Flags:\n"); printf (" in"); flags_out (gt_flags); printf ("expected"); flags_out (ex_flags); printf (" got"); flags_out (gt_flags); exit (1); } } mpz_neg (ex, ex); mpfr_neg (f, f, MPFR_RNDN); } } mpfr_clear (f); mpz_clear (got); mpz_clear (ex); }