void _fmprb_get_rand_fmpq(fmpz_t num, fmpz_t den, flint_rand_t state, const fmpz_t den_mult, const fmprb_t x) { fmpz_t a, b, exp; fmpz_init(a); fmpz_init(b); fmpz_init(exp); fmprb_get_interval_fmpz_2exp(a, b, exp, x); if (COEFF_IS_MPZ(*exp)) { printf("exception: fmprb_get_rand_fmpq: too large exponent\n"); abort(); } if (*exp >= 0) { fmpz_mul_2exp(a, a, *exp); fmpz_mul_2exp(b, b, *exp); } /* generate random integer in [a*den, b*den] */ fmpz_mul(a, a, den_mult); fmpz_mul(b, b, den_mult); fmpz_add_ui(b, b, 1UL); fmpz_sub(b, b, a); /* return one endpoint with high probability (used for stress testing rounding) */ if (n_randint(state, 6) == 0) { if (n_randint(state, 2)) fmpz_zero(num); else fmpz_sub_ui(num, b, 1UL); } else { fmpz_randtest_mod(num, state, b); } fmpz_add(num, num, a); fmpz_set(den, den_mult); if (*exp < 0) fmpz_mul_2exp(den, den, -(*exp)); fmpz_clear(a); fmpz_clear(b); fmpz_clear(exp); }
void fmpz_randtest_mod_signed(fmpz_t f, flint_rand_t state, const fmpz_t m) { /* Randomly generate m/2 when included in the range */ if ((n_randlimb(state) % 32 == 1) && (fmpz_fdiv_ui(m, 2) == 0)) { fmpz_fdiv_q_ui(f, m, UWORD(2)); } else { fmpz_t t; fmpz_init(t); fmpz_tdiv_q_ui(t, m, UWORD(2)); fmpz_randtest_mod(t, state, t); if (n_randlimb(state) & UWORD(1)) { fmpz_neg(t, t); } fmpz_set(f, t); fmpz_clear(t); } }
int main() { long i, j; int sign; fmpz_t input; fmpz_t result; fmpz_t r1; fmpz_t m1; fmpz_t mprod; ulong r2, m2; flint_rand_t state; printf("CRT_ui...."); fflush(stdout); fmpz_init(input); fmpz_init(result); fmpz_init(r1); fmpz_init(m1); fmpz_init(mprod); flint_randinit(state); for (i = 0; i < 10000; i++) { long nprimes; m2 = n_randtest_prime(state, 0); nprimes = 1 + n_randint(state, 4); fmpz_set_ui(m1, 1UL); for (j = 0; j < nprimes; ) { ulong t = n_randtest_prime(state, 0); if (t != m2) { fmpz_mul_ui(m1, m1, t); j++; } } fmpz_mul_ui(mprod, m1, m2); sign = n_randint(state, 2); if (sign) fmpz_randtest_mod_signed(input, state, mprod); else fmpz_randtest_mod(input, state, mprod); fmpz_mod(r1, input, m1); r2 = fmpz_fdiv_ui(input, m2); if (sign) fmpz_CRT_ui(result, r1, m1, r2, m2); else fmpz_CRT_ui_unsigned(result, r1, m1, r2, m2); if (!fmpz_equal(result, input)) { printf("FAIL:\n"); printf("m1: "); fmpz_print(m1); printf("\n"); printf("m2: %lu\n", m2); printf("m1*m2: "); fmpz_print(mprod); printf("\n"); printf("input: "); fmpz_print(input); printf("\n"); printf("r1: "); fmpz_print(r1); printf("\n"); printf("r2: %lu\n", r2); printf("result: "); fmpz_print(result); printf("\n"); printf("%ld Equalness: %d\n", i, fmpz_equal(result, input)); printf("\n"); abort(); } } fmpz_clear(input); fmpz_clear(result); fmpz_clear(r1); fmpz_clear(m1); fmpz_clear(mprod); flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main() { slong i, j; int sign; fmpz_t input; fmpz_t result; fmpz_t r1; fmpz_t m1; fmpz_t mprod; ulong r2, m2; FLINT_TEST_INIT(state); flint_printf("CRT_ui...."); fflush(stdout); fmpz_init(input); fmpz_init(result); fmpz_init(r1); fmpz_init(m1); fmpz_init(mprod); for (i = 0; i < 1000 * flint_test_multiplier(); i++) { slong nprimes; m2 = n_randtest_prime(state, 0); nprimes = 1 + n_randint(state, 4); fmpz_set_ui(m1, UWORD(1)); for (j = 0; j < nprimes; ) { ulong t = n_randtest_prime(state, 0); if (t != m2) { fmpz_mul_ui(m1, m1, t); j++; } } fmpz_mul_ui(mprod, m1, m2); sign = n_randint(state, 2); if (sign) fmpz_randtest_mod_signed(input, state, mprod); else fmpz_randtest_mod(input, state, mprod); fmpz_mod(r1, input, m1); r2 = fmpz_fdiv_ui(input, m2); fmpz_CRT_ui(result, r1, m1, r2, m2, sign); if (!fmpz_equal(result, input)) { flint_printf("FAIL:\n"); flint_printf("m1: "); fmpz_print(m1); flint_printf("\n"); flint_printf("m2: %wu\n", m2); flint_printf("m1*m2: "); fmpz_print(mprod); flint_printf("\n"); flint_printf("input: "); fmpz_print(input); flint_printf("\n"); flint_printf("r1: "); fmpz_print(r1); flint_printf("\n"); flint_printf("r2: %wu\n", r2); flint_printf("result: "); fmpz_print(result); flint_printf("\n"); flint_printf("%wd Equalness: %d\n", i, fmpz_equal(result, input)); flint_printf("\n"); abort(); } } fmpz_clear(input); fmpz_clear(result); fmpz_clear(r1); fmpz_clear(m1); fmpz_clear(mprod); FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }