void check_various (void) { mpf_t got, u, v; mpf_init (got); mpf_init (u); mpf_init (v); /* 100/4 == 25 */ mpf_set_prec (got, 20L); mpf_set_ui (u, 100L); mpf_set_ui (v, 4L); mpf_div (got, u, v); MPF_CHECK_FORMAT (got); ASSERT_ALWAYS (mpf_cmp_ui (got, 25L) == 0); /* 1/(2^n+1), a case where truncating the divisor would be wrong */ mpf_set_prec (got, 500L); mpf_set_prec (v, 900L); mpf_set_ui (v, 1L); mpf_mul_2exp (v, v, 800L); mpf_add_ui (v, v, 1L); mpf_div (got, u, v); check_one ("1/2^n+1, separate", got, u, v); mpf_clear (got); mpf_clear (u); mpf_clear (v); }
// The Brent-Salamin algorithm int main(int argc, char* argv[]) { if (argc < 2) return -1; int n = (int)strtol(argv[1], NULL, 10); mpf_set_default_prec(1000); mpf_t a, b, t, c, sum; // a=1 mpf_init_set_ui(a, 1); mpf_init_set_ui(sum, 0); mpf_init(b); mpf_init(t); mpf_init(c); mpf_init(sum); // b=1/sqrt(2) mpf_sqrt_ui(b, 2); mpf_ui_div(b, 1, b); // n次迭代的误差小于\frac{2^{n+9}}{20^{2n+1}} for (int i = 1; i <= n; ++i) { // t=(a+b)/2 mpf_add(t, a, b); mpf_div_ui(t, t, 2); // b=sqrt(a*b); mpf_mul(b, a, b); mpf_sqrt(b, b); // a=t mpf_swap(t, a); mpf_mul(t, a, a); mpf_mul(c, b, b); mpf_sub(c, t, c); mpf_mul_2exp(c, c, i + 1); mpf_add(sum, sum, c); } mpf_mul(t, a, a); mpf_mul_ui(t, t, 4); mpf_ui_sub(sum, 1, sum); mpf_div(t, t, sum); mpf_out_str(stdout, 10, 0, t); printf("\n"); mpf_clear(a); mpf_clear(b); mpf_clear(t); mpf_clear(c); mpf_clear(sum); return 0; }
void my_divexact(mpz_t r, mpz_t y, mpz_t x) { unsigned long prec = mpz_sizeinbase(y, 2); unsigned long prec2 = mpz_sizeinbase(x, 2); if (prec >= div_threshold) { mpf_set_prec_raw(d1, prec); mpf_set_prec_raw(d2, prec); mpf_set_z(d1, y); mpf_set_z(d2, x); mpf_div_2exp(d1, d1, prec); mpf_div_2exp(d2, d2, prec2); my_div(d2, d1, d2); mpf_mul_2exp(d2, d2, prec-prec2); mpf_set_d(d1, 0.5); mpf_add(d2, d2, d1); mpz_set_f(y, d2); } else { mpz_divexact(y, y, x); //mpz_tdiv_q(y, y, x); } }
void check_infinity (void) { mpf_t x; double y = tests_infinity_d (); if (y == 0.0) return; mpf_init (x); /* 0 cmp inf */ mpf_set_ui (x, 0L); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); /* 123 cmp inf */ mpf_set_ui (x, 123L); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); /* -123 cmp inf */ mpf_set_si (x, -123L); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); /* 2^5000 cmp inf */ mpf_set_ui (x, 1L); mpf_mul_2exp (x, x, 5000L); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); /* -2^5000 cmp inf */ mpf_neg (x, x); check_one ("check_infinity", x, y, -1); check_one ("check_infinity", x, -y, 1); mpf_clear (x); }
int main (void) { mpf_t f; tests_start (); mpf_init2 (f, 200L); mpf_set_ui (f, 0L); one (f, 1); mpf_set_ui (f, 1L); all (f, 1); mpf_set_ui (f, 1L); mpf_div_2exp (f, f, 1L); all (f, 0); mpf_set_ui (f, 1L); mpf_div_2exp (f, f, 5000L); all (f, 0); mpf_set_ui (f, 1L); mpf_mul_2exp (f, f, 5000L); all (f, 1); mpf_set_str (f, "0.5", 10); all (f, 0); mpf_set_ui (f, 1L); mpf_div_ui (f, f, 3L); all (f, 0); mpf_clear (f); tests_end (); exit (0); }
int main (void) { mpf_t x; mpfr_t y, z; unsigned long i; mpfr_exp_t e; int inex; tests_start_mpfr (); mpfr_init (y); mpfr_init (z); mpf_init (x); i = 1; while (i) { mpfr_set_ui (y, i, MPFR_RNDN); if (mpfr_get_f (x, y, MPFR_RNDN) != 0 || mpf_cmp_ui (x, i)) { printf ("Error: mpfr_get_f(%lu) fails\n", i); exit (1); } if (i <= - (unsigned long) LONG_MIN) { long j = i < - (unsigned long) LONG_MIN ? - (long) i : LONG_MIN; mpfr_set_si (y, j, MPFR_RNDN); if (mpfr_get_f (x, y, MPFR_RNDN) != 0 || mpf_cmp_si (x, j)) { printf ("Error: mpfr_get_f(-%lu) fails\n", i); exit (1); } } i *= 2; } /* same tests, but with a larger precision for y, which requires to round it */ mpfr_set_prec (y, 100); i = 1; while (i) { mpfr_set_ui (y, i, MPFR_RNDN); inex = mpfr_get_f (x, y, MPFR_RNDN); if (! SAME_SIGN (inex, - mpfr_cmp_f (y, x)) || mpf_cmp_ui (x, i)) { printf ("Error: mpfr_get_f(%lu) fails\n", i); exit (1); } mpfr_set_si (y, (signed long) -i, MPFR_RNDN); inex = mpfr_get_f (x, y, MPFR_RNDN); if (! SAME_SIGN (inex, - mpfr_cmp_f (y, x)) || mpf_cmp_si (x, (signed long) -i)) { printf ("Error: mpfr_get_f(-%lu) fails\n", i); exit (1); } i *= 2; } /* bug reported by Jim White */ for (e = 0; e <= 2 * GMP_NUMB_BITS; e++) { /* test with 2^(-e) */ mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_div_2exp (y, y, e, MPFR_RNDN); inex = mpfr_get_f (x, y, MPFR_RNDN); mpf_mul_2exp (x, x, e); if (inex != 0 || mpf_cmp_ui (x, 1) != 0) { printf ("Error: mpfr_get_f(x,y,MPFR_RNDN) fails\n"); printf ("y="); mpfr_dump (y); printf ("x="); mpf_div_2exp (x, x, e); mpf_out_str (stdout, 2, 0, x); exit (1); } /* test with 2^(e) */ mpfr_set_ui (y, 1, MPFR_RNDN); mpfr_mul_2exp (y, y, e, MPFR_RNDN); inex = mpfr_get_f (x, y, MPFR_RNDN); mpf_div_2exp (x, x, e); if (inex != 0 || mpf_cmp_ui (x, 1) != 0) { printf ("Error: mpfr_get_f(x,y,MPFR_RNDN) fails\n"); printf ("y="); mpfr_dump (y); printf ("x="); mpf_mul_2exp (x, x, e); mpf_out_str (stdout, 2, 0, x); exit (1); } } /* Bug reported by Yury Lukach on 2006-04-05 */ mpfr_set_prec (y, 32); mpfr_set_prec (z, 32); mpf_set_prec (x, 32); mpfr_set_ui_2exp (y, 0xc1234567, -30, MPFR_RNDN); mpfr_get_f (x, y, MPFR_RNDN); inex = mpfr_set_f (z, x, MPFR_RNDN); if (inex != 0 || ! mpfr_equal_p (y, z)) { printf ("Error in mpfr_get_f:\n inex = %d, y = ", inex); mpfr_dump (z); printf ("Expected:\n inex = 0, y = "); mpfr_dump (y); exit (1); } mpfr_clear (y); mpfr_clear (z); mpf_clear (x); special_test (); prec_test (); ternary_test (); tests_end_mpfr (); return 0; }
int main (void) { mpf_t f, f0p5; int got; const char *expr; int error = 0; tests_start (); mpf_init2 (f, 200L); mpf_init2 (f0p5, 200L); /* 0.5 */ mpf_set_ui (f0p5, 1L); mpf_div_2exp (f0p5, f0p5, 1L); mpf_set_ui (f, 0L); expr = "0"; EXPECT (mpf_fits_ulong_p, 1); EXPECT (mpf_fits_uint_p, 1); EXPECT (mpf_fits_ushort_p, 1); EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_ui (f, 1L); expr = "1"; EXPECT (mpf_fits_ulong_p, 1); EXPECT (mpf_fits_uint_p, 1); EXPECT (mpf_fits_ushort_p, 1); EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_si (f, -1L); expr = "-1"; EXPECT (mpf_fits_ulong_p, 0); EXPECT (mpf_fits_uint_p, 0); EXPECT (mpf_fits_ushort_p, 0); EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_ui (f, (unsigned long) USHRT_MAX); expr = "USHRT_MAX"; EXPECT (mpf_fits_ulong_p, 1); EXPECT (mpf_fits_uint_p, 1); EXPECT (mpf_fits_ushort_p, 1); mpf_set_ui (f, (unsigned long) USHRT_MAX); mpf_add (f, f, f0p5); expr = "USHRT_MAX + 0.5"; EXPECT (mpf_fits_ulong_p, 1); EXPECT (mpf_fits_uint_p, 1); EXPECT (mpf_fits_ushort_p, 1); mpf_set_ui (f, (unsigned long) USHRT_MAX); mpf_add_ui (f, f, 1L); expr = "USHRT_MAX + 1"; EXPECT (mpf_fits_ushort_p, 0); mpf_set_ui (f, (unsigned long) UINT_MAX); expr = "UINT_MAX"; EXPECT (mpf_fits_ulong_p, 1); EXPECT (mpf_fits_uint_p, 1); mpf_set_ui (f, (unsigned long) UINT_MAX); mpf_add (f, f, f0p5); expr = "UINT_MAX + 0.5"; EXPECT (mpf_fits_ulong_p, 1); EXPECT (mpf_fits_uint_p, 1); mpf_set_ui (f, (unsigned long) UINT_MAX); mpf_add_ui (f, f, 1L); expr = "UINT_MAX + 1"; EXPECT (mpf_fits_uint_p, 0); mpf_set_ui (f, ULONG_MAX); expr = "ULONG_MAX"; EXPECT (mpf_fits_ulong_p, 1); mpf_set_ui (f, ULONG_MAX); mpf_add (f, f, f0p5); expr = "ULONG_MAX + 0.5"; EXPECT (mpf_fits_ulong_p, 1); mpf_set_ui (f, ULONG_MAX); mpf_add_ui (f, f, 1L); expr = "ULONG_MAX + 1"; EXPECT (mpf_fits_ulong_p, 0); mpf_set_si (f, (long) SHRT_MAX); expr = "SHRT_MAX"; EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_si (f, (long) SHRT_MAX); expr = "SHRT_MAX + 0.5"; mpf_add (f, f, f0p5); EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_si (f, (long) SHRT_MAX); mpf_add_ui (f, f, 1L); expr = "SHRT_MAX + 1"; EXPECT (mpf_fits_sshort_p, 0); mpf_set_si (f, (long) INT_MAX); expr = "INT_MAX"; EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); mpf_set_si (f, (long) INT_MAX); mpf_add (f, f, f0p5); expr = "INT_MAX + 0.5"; EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); mpf_set_si (f, (long) INT_MAX); mpf_add_ui (f, f, 1L); expr = "INT_MAX + 1"; EXPECT (mpf_fits_sint_p, 0); mpf_set_si (f, LONG_MAX); expr = "LONG_MAX"; EXPECT (mpf_fits_slong_p, 1); mpf_set_si (f, LONG_MAX); mpf_add (f, f, f0p5); expr = "LONG_MAX + 0.5"; EXPECT (mpf_fits_slong_p, 1); mpf_set_si (f, LONG_MAX); mpf_add_ui (f, f, 1L); expr = "LONG_MAX + 1"; EXPECT (mpf_fits_slong_p, 0); mpf_set_si (f, (long) SHRT_MIN); expr = "SHRT_MIN"; EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_si (f, (long) SHRT_MIN); mpf_sub (f, f, f0p5); expr = "SHRT_MIN - 0.5"; EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_si (f, (long) SHRT_MIN); mpf_sub_ui (f, f, 1L); expr = "SHRT_MIN + 1"; EXPECT (mpf_fits_sshort_p, 0); mpf_set_si (f, (long) INT_MIN); expr = "INT_MIN"; EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); mpf_set_si (f, (long) INT_MIN); mpf_sub (f, f, f0p5); expr = "INT_MIN - 0.5"; EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); mpf_set_si (f, (long) INT_MIN); mpf_sub_ui (f, f, 1L); expr = "INT_MIN + 1"; EXPECT (mpf_fits_sint_p, 0); mpf_set_si (f, LONG_MIN); expr = "LONG_MIN"; EXPECT (mpf_fits_slong_p, 1); mpf_set_si (f, LONG_MIN); mpf_sub (f, f, f0p5); expr = "LONG_MIN - 0.5"; EXPECT (mpf_fits_slong_p, 1); mpf_set_si (f, LONG_MIN); mpf_sub_ui (f, f, 1L); expr = "LONG_MIN + 1"; EXPECT (mpf_fits_slong_p, 0); mpf_set_str_or_abort (f, "0.5", 10); expr = "0.5"; EXPECT (mpf_fits_ulong_p, 1); EXPECT (mpf_fits_uint_p, 1); EXPECT (mpf_fits_ushort_p, 1); EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_str_or_abort (f, "-0.5", 10); expr = "-0.5"; EXPECT (mpf_fits_ulong_p, 0); EXPECT (mpf_fits_uint_p, 0); EXPECT (mpf_fits_ushort_p, 0); EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_str_or_abort (f, "1.000000000000000000000000000000000001", 16); expr = "1.000000000000000000000000000000000001 base 16"; EXPECT (mpf_fits_ulong_p, 1); EXPECT (mpf_fits_uint_p, 1); EXPECT (mpf_fits_ushort_p, 1); EXPECT (mpf_fits_slong_p, 1); EXPECT (mpf_fits_sint_p, 1); EXPECT (mpf_fits_sshort_p, 1); mpf_set_str_or_abort (f, "1@1000", 16); expr = "1@1000 base 16"; EXPECT (mpf_fits_ulong_p, 0); EXPECT (mpf_fits_uint_p, 0); EXPECT (mpf_fits_ushort_p, 0); EXPECT (mpf_fits_slong_p, 0); EXPECT (mpf_fits_sint_p, 0); EXPECT (mpf_fits_sshort_p, 0); mpf_set_ui (f, 1L); mpf_mul_2exp (f, f, BITS_PER_ULONG + 1); mpf_sub_ui (f, f, 1L); expr = "2^(BITS_PER_ULONG+1) - 1"; EXPECT (mpf_fits_ulong_p, 0); EXPECT (mpf_fits_uint_p, 0); EXPECT (mpf_fits_ushort_p, 0); EXPECT (mpf_fits_slong_p, 0); EXPECT (mpf_fits_sint_p, 0); EXPECT (mpf_fits_sshort_p, 0); mpf_set_ui (f, 1L); mpf_mul_2exp (f, f, BITS_PER_ULONG + 1); mpf_sub_ui (f, f, 1L); mpf_neg (f, f); expr = "- (2^(BITS_PER_ULONG+1) - 1)"; EXPECT (mpf_fits_ulong_p, 0); EXPECT (mpf_fits_uint_p, 0); EXPECT (mpf_fits_ushort_p, 0); EXPECT (mpf_fits_slong_p, 0); EXPECT (mpf_fits_sint_p, 0); EXPECT (mpf_fits_sshort_p, 0); mpf_set_ui (f, 1L); mpf_mul_2exp (f, f, BITS_PER_ULONG + 5); mpf_sub_ui (f, f, 1L); expr = "2^(BITS_PER_ULONG+5) - 1"; EXPECT (mpf_fits_ulong_p, 0); EXPECT (mpf_fits_uint_p, 0); EXPECT (mpf_fits_ushort_p, 0); EXPECT (mpf_fits_slong_p, 0); EXPECT (mpf_fits_sint_p, 0); EXPECT (mpf_fits_sshort_p, 0); if (error) abort (); mpf_clear (f); mpf_clear (f0p5); tests_end (); exit (0); }
void check_one (mpz_srcptr z) { static const int shift[] = { 0, 1, BITS_PER_MP_LIMB, 2*BITS_PER_MP_LIMB, 5*BITS_PER_MP_LIMB }; int sh, shneg, neg; mpf_t f; mpz_t got, want; mpf_init2 (f, mpz_sizeinbase(z,2)); mpz_init (got); mpz_init (want); for (sh = 0; sh < numberof(shift); sh++) { for (shneg = 0; shneg <= 1; shneg++) { for (neg = 0; neg <= 1; neg++) { mpf_set_z (f, z); mpz_set (want, z); if (neg) { mpf_neg (f, f); mpz_neg (want, want); } if (shneg) { mpz_tdiv_q_2exp (want, want, shift[sh]); mpf_div_2exp (f, f, shift[sh]); } else { mpz_mul_2exp (want, want, shift[sh]); mpf_mul_2exp (f, f, shift[sh]); } mpz_set_f (got, f); MPZ_CHECK_FORMAT (got); if (mpz_cmp (got, want) != 0) { printf ("wrong result\n"); printf (" shift %d\n", shneg ? -shift[sh] : shift[sh]); printf (" neg %d\n", neg); mpf_trace (" f", f); mpz_trace (" got", got); mpz_trace (" want", want); abort (); } } } } mpf_clear (f); mpz_clear (got); mpz_clear (want); }
int main (void) { mpfr_t x, u; mpf_t y, z; mpfr_exp_t emax; unsigned long k, pr; int r, inexact; tests_start_mpfr (); mpf_init (y); mpf_init (z); mpf_set_d (y, 0.0); /* check prototype of mpfr_init_set_f */ mpfr_init_set_f (x, y, MPFR_RNDN); mpfr_set_prec (x, 100); mpfr_set_f (x, y, MPFR_RNDN); mpf_urandomb (y, RANDS, 10 * GMP_NUMB_BITS); mpfr_set_f (x, y, RND_RAND ()); /* bug found by Jean-Pierre Merlet */ mpfr_set_prec (x, 256); mpf_set_prec (y, 256); mpfr_init2 (u, 256); mpfr_set_str (u, "7.f10872b020c49ba5e353f7ced916872b020c49ba5e353f7ced916872b020c498@2", 16, MPFR_RNDN); mpf_set_str (y, "2033033E-3", 10); /* avoid 2033.033 which is locale-sensitive */ mpfr_set_f (x, y, MPFR_RNDN); if (mpfr_cmp (x, u)) { printf ("mpfr_set_f failed for y=2033033E-3\n"); exit (1); } mpf_set_str (y, "-2033033E-3", 10); /* avoid -2033.033 which is locale-sensitive */ mpfr_set_f (x, y, MPFR_RNDN); mpfr_neg (u, u, MPFR_RNDN); if (mpfr_cmp (x, u)) { printf ("mpfr_set_f failed for y=-2033033E-3\n"); exit (1); } mpf_set_prec (y, 300); mpf_set_str (y, "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", -2); mpf_mul_2exp (y, y, 600); mpfr_set_prec (x, 300); mpfr_set_f (x, y, MPFR_RNDN); if (mpfr_check (x) == 0) { printf ("Error in mpfr_set_f: corrupted result\n"); mpfr_dump (x); exit (1); } MPFR_ASSERTN(mpfr_cmp_ui_2exp (x, 1, 901) == 0); /* random values */ for (k = 1; k <= 1000; k++) { pr = 2 + (randlimb () & 255); mpf_set_prec (z, pr); mpf_urandomb (z, RANDS, z->_mp_prec); mpfr_set_prec (u, ((pr / GMP_NUMB_BITS + 1) * GMP_NUMB_BITS)); mpfr_set_f (u, z, MPFR_RNDN); if (mpfr_cmp_f (u , z) != 0) { printf ("Error in mpfr_set_f:\n"); printf ("mpf (precision=%lu)=", pr); mpf_out_str (stdout, 16, 0, z); printf ("\nmpfr(precision=%lu)=", ((pr / GMP_NUMB_BITS + 1) * GMP_NUMB_BITS)); mpfr_out_str (stdout, 16, 0, u, MPFR_RNDN); putchar ('\n'); exit (1); } mpfr_set_prec (x, pr); mpfr_set_f (x, z, MPFR_RNDN); mpfr_sub (u, u, x, MPFR_RNDN); mpfr_abs (u, u, MPFR_RNDN); if (mpfr_cmp_ui_2exp (u, 1, -pr - 1) > 0) { printf ("Error in mpfr_set_f: precision=%lu\n", pr); printf ("mpf ="); mpf_out_str (stdout, 16, 0, z); printf ("\nmpfr="); mpfr_out_str (stdout, 16, 0, x, MPFR_RNDN); putchar ('\n'); exit (1); } } /* Check for +0 */ mpfr_set_prec (x, 53); mpf_set_prec (y, 53); mpf_set_ui (y, 0); for (r = 0 ; r < MPFR_RND_MAX ; r++) { int i; for (i = -1; i <= 1; i++) { if (i) mpfr_set_si (x, i, MPFR_RNDN); inexact = mpfr_set_f (x, y, (mpfr_rnd_t) r); if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact) { printf ("mpfr_set_f(x,0) failed for %s, i = %d\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r), i); exit (1); } } } /* coverage test */ mpf_set_prec (y, 2); mpfr_set_prec (x, 3 * mp_bits_per_limb); mpf_set_ui (y, 1); for (r = 0; r < mp_bits_per_limb; r++) { mpfr_urandomb (x, RANDS); /* to fill low limbs with random data */ inexact = mpfr_set_f (x, y, MPFR_RNDN); MPFR_ASSERTN(inexact == 0 && mpfr_cmp_ui_2exp (x, 1, r) == 0); mpf_mul_2exp (y, y, 1); } mpf_set_ui (y, 1); mpf_mul_2exp (y, y, ULONG_MAX); mpfr_set_f (x, y, MPFR_RNDN); mpfr_set_ui (u, 1, MPFR_RNDN); mpfr_mul_2ui (u, u, ULONG_MAX, MPFR_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^ULONG_MAX\n"); exit (1); } emax = mpfr_get_emax (); /* For mpf_mul_2exp, emax must fit in an unsigned long! */ if (emax >= 0 && emax <= ULONG_MAX) { mpf_set_ui (y, 1); mpf_mul_2exp (y, y, emax); mpfr_set_f (x, y, MPFR_RNDN); mpfr_set_ui_2exp (u, 1, emax, MPFR_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^emax\n"); exit (1); } } /* For mpf_mul_2exp, emax - 1 must fit in an unsigned long! */ if (emax >= 1 && emax - 1 <= ULONG_MAX) { mpf_set_ui (y, 1); mpf_mul_2exp (y, y, emax - 1); mpfr_set_f (x, y, MPFR_RNDN); mpfr_set_ui_2exp (u, 1, emax - 1, MPFR_RNDN); if (!mpfr_equal_p (x, u)) { printf ("Error: mpfr_set_f (x, y, MPFR_RNDN) for y = 2^(emax-1)\n"); exit (1); } } mpfr_clear (x); mpfr_clear (u); mpf_clear (y); mpf_clear (z); tests_end_mpfr (); return 0; }
int main (int argc, char **argv) { int reps = 100000; int i; mpf_t u, v, w1, w2, w3; mp_size_t bprec = 100; mpf_t rerr, limit_rerr; mp_size_t un; mp_exp_t ue; tests_start (); if (argc > 1) { reps = strtol (argv[1], 0, 0); if (argc > 2) bprec = strtol (argv[2], 0, 0); } mpf_set_default_prec (bprec); mpf_init (rerr); mpf_init (limit_rerr); mpf_init (u); mpf_init (v); mpf_init (w1); mpf_init (w2); mpf_init (w3); for (i = 0; i < reps; i++) { unsigned long int res_prec; unsigned long int pow2; res_prec = urandom () % (bprec + 100); mpf_set_prec (w1, res_prec); mpf_set_prec (w2, res_prec); mpf_set_prec (w3, res_prec); mpf_set_ui (limit_rerr, 1); mpf_div_2exp (limit_rerr, limit_rerr, res_prec); pow2 = urandom () % 0x10000; mpf_set_ui (v, 1); mpf_mul_2exp (v, v, pow2); un = urandom () % (2 * SIZE) - SIZE; ue = urandom () % SIZE; mpf_random2 (u, un, ue); mpf_div_2exp (w1, u, pow2); mpf_div (w2, u, v); mpf_reldiff (rerr, w1, w2); if (mpf_cmp (rerr, limit_rerr) > 0) { printf ("ERROR in mpf_div or mpf_div_2exp after %d tests\n", i); printf (" u = "); mpf_dump (u); printf (" v = "); mpf_dump (v); printf (" w1 = "); mpf_dump (w1); printf (" w2 = "); mpf_dump (w2); abort (); } mpf_mul_2exp (w3, w1, pow2); mpf_reldiff (rerr, u, w3); if (mpf_cmp (rerr, limit_rerr) > 0) { printf ("ERROR in mpf_mul_2exp after %d tests\n", i); printf (" u = "); mpf_dump (u); printf (" v = "); mpf_dump (v); printf (" w1 = "); mpf_dump (w1); printf (" w3 = "); mpf_dump (w3); abort (); } } mpf_clear (rerr); mpf_clear (limit_rerr); mpf_clear (u); mpf_clear (v); mpf_clear (w1); mpf_clear (w2); mpf_clear (w3); tests_end (); exit (0); }
void check_various (void) { mpf_t src, trunc, ceil, floor; int n, i; mpf_init2 (src, 512L); mpf_init2 (trunc, 256L); mpf_init2 (ceil, 256L); mpf_init2 (floor, 256L); /* 0 */ mpf_set_ui (src, 0L); mpf_set_ui (trunc, 0L); mpf_set_ui (ceil, 0L); mpf_set_ui (floor, 0L); check_all (src, trunc, ceil, floor); /* 1 */ mpf_set_ui (src, 1L); mpf_set_ui (trunc, 1L); mpf_set_ui (ceil, 1L); mpf_set_ui (floor, 1L); check_all (src, trunc, ceil, floor); /* 2^1024 */ mpf_set_ui (src, 1L); mpf_mul_2exp (src, src, 1024L); mpf_set (trunc, src); mpf_set (ceil, src); mpf_set (floor, src); check_all (src, trunc, ceil, floor); /* 1/2^1024, fraction only */ mpf_set_ui (src, 1L); mpf_div_2exp (src, src, 1024L); mpf_set_si (trunc, 0L); mpf_set_si (ceil, 1L); mpf_set_si (floor, 0L); check_all (src, trunc, ceil, floor); /* 1/2 */ mpf_set_ui (src, 1L); mpf_div_2exp (src, src, 1L); mpf_set_si (trunc, 0L); mpf_set_si (ceil, 1L); mpf_set_si (floor, 0L); check_all (src, trunc, ceil, floor); /* 123+1/2^64 */ mpf_set_ui (src, 1L); mpf_div_2exp (src, src, 64L); mpf_add_ui (src, src, 123L); mpf_set_si (trunc, 123L); mpf_set_si (ceil, 124L); mpf_set_si (floor, 123L); check_all (src, trunc, ceil, floor); /* integer of full prec+1 limbs, unchanged */ n = PREC(trunc)+1; ASSERT_ALWAYS (n <= PREC(src)+1); EXP(src) = n; SIZ(src) = n; for (i = 0; i < SIZ(src); i++) PTR(src)[i] = i+100; mpf_set (trunc, src); mpf_set (ceil, src); mpf_set (floor, src); check_all (src, trunc, ceil, floor); /* full prec+1 limbs, 1 trimmed for integer */ n = PREC(trunc)+1; ASSERT_ALWAYS (n <= PREC(src)+1); EXP(src) = n-1; SIZ(src) = n; for (i = 0; i < SIZ(src); i++) PTR(src)[i] = i+200; EXP(trunc) = n-1; SIZ(trunc) = n-1; for (i = 0; i < SIZ(trunc); i++) PTR(trunc)[i] = i+201; mpf_set (floor, trunc); mpf_add_ui (ceil, trunc, 1L); check_all (src, trunc, ceil, floor); /* prec+3 limbs, 2 trimmed for size */ n = PREC(trunc)+3; ASSERT_ALWAYS (n <= PREC(src)+1); EXP(src) = n; SIZ(src) = n; for (i = 0; i < SIZ(src); i++) PTR(src)[i] = i+300; EXP(trunc) = n; SIZ(trunc) = n-2; for (i = 0; i < SIZ(trunc); i++) PTR(trunc)[i] = i+302; mpf_set (floor, trunc); mpf_set (ceil, trunc); PTR(ceil)[0]++; check_all (src, trunc, ceil, floor); /* prec+4 limbs, 2 trimmed for size, 1 trimmed for integer */ n = PREC(trunc)+4; ASSERT_ALWAYS (n <= PREC(src)+1); EXP(src) = n-1; SIZ(src) = n; for (i = 0; i < SIZ(src); i++) PTR(src)[i] = i+400; EXP(trunc) = n-1; SIZ(trunc) = n-3; for (i = 0; i < SIZ(trunc); i++) PTR(trunc)[i] = i+403; mpf_set (floor, trunc); mpf_set (ceil, trunc); PTR(ceil)[0]++; check_all (src, trunc, ceil, floor); /* F.F, carry out of ceil */ EXP(src) = 1; SIZ(src) = 2; PTR(src)[0] = GMP_NUMB_MAX; PTR(src)[1] = GMP_NUMB_MAX; EXP(trunc) = 1; SIZ(trunc) = 1; PTR(trunc)[0] = GMP_NUMB_MAX; mpf_set (floor, trunc); EXP(ceil) = 2; SIZ(ceil) = 1; PTR(ceil)[0] = 1; check_all (src, trunc, ceil, floor); /* FF.F, carry out of ceil */ EXP(src) = 2; SIZ(src) = 3; PTR(src)[0] = GMP_NUMB_MAX; PTR(src)[1] = GMP_NUMB_MAX; PTR(src)[2] = GMP_NUMB_MAX; EXP(trunc) = 2; SIZ(trunc) = 2; PTR(trunc)[0] = GMP_NUMB_MAX; PTR(trunc)[1] = GMP_NUMB_MAX; mpf_set (floor, trunc); EXP(ceil) = 3; SIZ(ceil) = 1; PTR(ceil)[0] = 1; check_all (src, trunc, ceil, floor); mpf_clear (src); mpf_clear (trunc); mpf_clear (ceil); mpf_clear (floor); }
void check_random (long reps) { unsigned long test; gmp_randstate_ptr rands = RANDS; mpf_t a, b, x; mpz_t ds; int hibits, lshift1, lshift2; int xtra; #define HIBITS 10 #define LSHIFT1 10 #define LSHIFT2 10 mpf_set_default_prec ((1 << HIBITS) + (1 << LSHIFT1) + (1 << LSHIFT2)); mpz_init (ds); mpf_inits (a, b, x, NULL); for (test = 0; test < reps; test++) { mpz_urandomb (ds, rands, HIBITS); hibits = mpz_get_ui (ds) + 1; mpz_urandomb (ds, rands, hibits); mpz_setbit (ds, hibits - 1); /* make sure msb is set */ mpf_set_z (a, ds); mpf_set_z (b, ds); mpz_urandomb (ds, rands, LSHIFT1); lshift1 = mpz_get_ui (ds); mpf_mul_2exp (a, a, lshift1 + 1); mpf_mul_2exp (b, b, lshift1 + 1); mpf_add_ui (a, a, 1); /* make a one-bit difference */ mpz_urandomb (ds, rands, LSHIFT2); lshift2 = mpz_get_ui (ds); mpf_mul_2exp (a, a, lshift2); mpf_mul_2exp (b, b, lshift2); mpz_urandomb (ds, rands, lshift2); mpf_set_z (x, ds); mpf_add (a, a, x); mpf_add (b, b, x); insert_random_low_zero_limbs (a, rands); insert_random_low_zero_limbs (b, rands); if (mpf_eq (a, b, lshift1 + hibits) == 0 || mpf_eq (b, a, lshift1 + hibits) == 0) { dump_abort (a, b, lshift1 + hibits, lshift1, lshift2, hibits, 1, test); } for (xtra = 1; xtra < 100; xtra++) if (mpf_eq (a, b, lshift1 + hibits + xtra) != 0 || mpf_eq (b, a, lshift1 + hibits + xtra) != 0) { dump_abort (a, b, lshift1 + hibits + xtra, lshift1, lshift2, hibits, 0, test); } } mpf_clears (a, b, x, NULL); mpz_clear (ds); }