static slong _fmpr_sub_special(fmpr_t z, const fmpr_t x, const fmpr_t y, slong prec, fmpr_rnd_t rnd) { if (fmpr_is_zero(x)) { return fmpr_neg_round(z, y, prec, rnd); } else if (fmpr_is_zero(y)) { return fmpr_set_round(z, x, prec, rnd); } else if (fmpr_is_nan(x) || fmpr_is_nan(y) || (fmpr_is_pos_inf(x) && fmpr_is_pos_inf(y)) || (fmpr_is_neg_inf(x) && fmpr_is_neg_inf(y))) { fmpr_nan(z); return FMPR_RESULT_EXACT; } else if (fmpr_is_special(x)) { fmpr_set(z, x); return FMPR_RESULT_EXACT; } else { fmpr_neg(z, y); return FMPR_RESULT_EXACT; } }
int _fmprb_vec_rad_has_inf_nan(fmprb_srcptr vec, long len) { long i; for (i = 0; i < len; i++) if (fmpr_is_nan(fmprb_radref(vec + i)) || fmpr_is_inf(fmprb_radref(vec + i))) return 1; return 0; }
static void _fmpr_div_special(fmpr_t z, const fmpr_t x, const fmpr_t y) { if ((fmpr_is_zero(x) && !fmpr_is_zero(y) && !fmpr_is_nan(y)) || (fmpr_is_inf(y) && !fmpr_is_special(x))) { fmpr_zero(z); } else if (fmpr_is_zero(y) || (fmpr_is_special(x) && fmpr_is_special(y)) || fmpr_is_nan(x) || fmpr_is_nan(y)) { fmpr_nan(z); } else if (fmpr_sgn(x) == fmpr_sgn(y)) fmpr_pos_inf(z); else fmpr_neg_inf(z); }
int fmpr_cmpabs(const fmpr_t x, const fmpr_t y) { int res, xsign, ysign; fmpr_t t; if (fmpr_equal(x, y)) return 0; if (fmpr_is_special(x) || fmpr_is_special(y)) { if (fmpr_is_nan(x) || fmpr_is_nan(y)) return 0; if (fmpr_is_zero(x)) return -1; if (fmpr_is_zero(y)) return 1; if (fmpr_is_inf(x)) return fmpr_is_inf(y) ? 0 : 1; if (fmpr_is_inf(y)) return -1; return -1; } /* Reduces to integer comparison if bottom exponents are the same */ if (fmpz_equal(fmpr_expref(x), fmpr_expref(y))) { res = fmpz_cmpabs(fmpr_manref(x), fmpr_manref(y)); if (res != 0) res = (res < 0) ? -1 : 1; } else { /* TODO: compare position of top exponents to avoid subtraction */ xsign = fmpr_sgn(x); ysign = fmpr_sgn(y); fmpr_init(t); if (xsign == ysign) fmpr_sub(t, x, y, 2, FMPR_RND_DOWN); else fmpr_add(t, x, y, 2, FMPR_RND_DOWN); res = fmpr_sgn(t) * xsign; fmpr_clear(t); } return res; }
int fmpr_cmp(const fmpr_t x, const fmpr_t y) { int res, xsign, ysign; fmpr_t t; if (fmpr_equal(x, y)) return 0; if (fmpr_is_special(x) || fmpr_is_special(y)) { if (fmpr_is_nan(x) || fmpr_is_nan(y)) return 0; if (fmpr_is_zero(y)) return fmpr_sgn(x); if (fmpr_is_zero(x)) return -fmpr_sgn(y); if (fmpr_is_pos_inf(x)) return 1; if (fmpr_is_neg_inf(y)) return 1; return -1; } xsign = fmpr_sgn(x); ysign = fmpr_sgn(y); if (xsign != ysign) return (xsign < 0) ? -1 : 1; /* Reduces to integer comparison if bottom exponents are the same */ if (fmpz_equal(fmpr_expref(x), fmpr_expref(y))) return fmpz_cmp(fmpr_manref(x), fmpr_manref(y)) < 0 ? -1 : 1; /* TODO: compare position of top exponents to avoid subtraction */ fmpr_init(t); fmpr_sub(t, x, y, 2, FMPR_RND_DOWN); res = fmpr_sgn(t); fmpr_clear(t); return res; }
int main() { slong iter; flint_rand_t state; flint_printf("sub...."); fflush(stdout); flint_randinit(state); /* test exact subtraction: (x - y) - z == (x - z) - y */ for (iter = 0; iter < 100000; iter++) { slong bits, res1, res2, res3, res4; fmpr_t x, y, z, t, u; bits = 2 + n_randint(state, 200); fmpr_init(x); fmpr_init(y); fmpr_init(z); fmpr_init(t); fmpr_init(u); fmpr_randtest_special(x, state, bits, 10); fmpr_randtest_special(y, state, bits, 10); fmpr_randtest_special(z, state, bits, 10); fmpr_randtest_special(t, state, bits, 10); fmpr_randtest_special(u, state, bits, 10); res1 = fmpr_sub(t, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN); res2 = fmpr_sub(t, t, z, FMPR_PREC_EXACT, FMPR_RND_DOWN); res3 = fmpr_sub(u, x, z, FMPR_PREC_EXACT, FMPR_RND_DOWN); res4 = fmpr_sub(u, u, y, FMPR_PREC_EXACT, FMPR_RND_DOWN); if (!fmpr_equal(t, u) || res1 != FMPR_RESULT_EXACT || res2 != FMPR_RESULT_EXACT || res3 != FMPR_RESULT_EXACT || res4 != FMPR_RESULT_EXACT) { flint_printf("FAIL\n\n"); flint_printf("bits = %wd\n", bits); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("t = "); fmpr_print(t); flint_printf("\n\n"); flint_printf("u = "); fmpr_print(u); flint_printf("\n\n"); abort(); } fmpr_clear(x); fmpr_clear(y); fmpr_clear(z); fmpr_clear(t); fmpr_clear(u); } /* compare rounding with mpfr */ for (iter = 0; iter < 100000; iter++) { slong bits, res; int mpfr_res; fmpr_t x, y, z, w; mpfr_t X, Y, Z; bits = 2 + n_randint(state, 200); fmpr_init(x); fmpr_init(y); fmpr_init(z); fmpr_init(w); mpfr_init2(X, bits + 100); mpfr_init2(Y, bits + 100); mpfr_init2(Z, bits); fmpr_randtest_special(x, state, bits + n_randint(state, 100), 10); fmpr_randtest_special(y, state, bits + n_randint(state, 100), 10); fmpr_randtest_special(z, state, bits, 10); fmpr_get_mpfr(X, x, MPFR_RNDN); fmpr_get_mpfr(Y, y, MPFR_RNDN); switch (n_randint(state, 4)) { case 0: mpfr_res = mpfr_sub(Z, X, Y, MPFR_RNDZ); res = fmpr_sub(z, x, y, bits, FMPR_RND_DOWN); break; case 1: mpfr_res = mpfr_sub(Z, X, Y, MPFR_RNDA); res = fmpr_sub(z, x, y, bits, FMPR_RND_UP); break; case 2: mpfr_res = mpfr_sub(Z, X, Y, MPFR_RNDD); res = fmpr_sub(z, x, y, bits, FMPR_RND_FLOOR); break; default: mpfr_res = mpfr_sub(Z, X, Y, MPFR_RNDU); res = fmpr_sub(z, x, y, bits, FMPR_RND_CEIL); break; } fmpr_set_mpfr(w, Z); if (!fmpr_equal(z, w) || (res == FMPR_RESULT_EXACT) != (mpfr_res == 0) || !fmpr_check_ulp(z, res, bits)) { flint_printf("FAIL\n\n"); flint_printf("bits = %wd\n", bits); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("w = "); fmpr_print(w); flint_printf("\n\n"); flint_printf("returned: %wd, %d\n", res, mpfr_res); abort(); } /* check error bound */ if (!fmpr_is_nan(z) && !fmpr_is_inf(z)) { fmpr_t z_exact, error_bound, true_error; fmpr_init(z_exact); fmpr_init(error_bound); fmpr_init(true_error); fmpr_set_error_result(error_bound, z, res); fmpr_sub(z_exact, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN); fmpr_sub(true_error, z, z_exact, FMPR_PREC_EXACT, FMPR_RND_DOWN); fmpr_abs(true_error, true_error); if (fmpr_is_zero(error_bound) != fmpr_is_zero(true_error) || fmpr_cmp(true_error, error_bound) > 0) { flint_printf("FAIL: error bound\n\n"); flint_printf("bits = %wd\n", bits); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("z_exact = "); fmpr_print(z_exact); flint_printf("\n\n"); flint_printf("true_error = "); fmpr_print(true_error); flint_printf("\n\n"); flint_printf("error_bound = "); fmpr_print(error_bound); flint_printf("\n\n"); abort(); } fmpr_clear(z_exact); fmpr_clear(error_bound); fmpr_clear(true_error); } fmpr_clear(x); fmpr_clear(y); fmpr_clear(z); fmpr_clear(w); mpfr_clear(X); mpfr_clear(Y); mpfr_clear(Z); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("add...."); fflush(stdout); flint_randinit(state); /* test exact addition: (x + y) + z == x + (y + z) */ for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { slong bits, res1, res2, res3, res4; fmpr_t x, y, z, t, u; bits = 2 + n_randint(state, 200); fmpr_init(x); fmpr_init(y); fmpr_init(z); fmpr_init(t); fmpr_init(u); fmpr_randtest_special(x, state, bits, 10); fmpr_randtest_special(y, state, bits, 10); fmpr_randtest_special(z, state, bits, 10); fmpr_randtest_special(t, state, bits, 10); fmpr_randtest_special(u, state, bits, 10); res1 = fmpr_add(t, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN); res2 = fmpr_add(t, t, z, FMPR_PREC_EXACT, FMPR_RND_DOWN); res3 = fmpr_add(u, y, z, FMPR_PREC_EXACT, FMPR_RND_DOWN); res4 = fmpr_add(u, x, u, FMPR_PREC_EXACT, FMPR_RND_DOWN); if (!fmpr_equal(t, u) || res1 != FMPR_RESULT_EXACT || res2 != FMPR_RESULT_EXACT || res3 != FMPR_RESULT_EXACT || res4 != FMPR_RESULT_EXACT) { flint_printf("FAIL\n\n"); flint_printf("bits = %wd\n", bits); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("t = "); fmpr_print(t); flint_printf("\n\n"); flint_printf("u = "); fmpr_print(u); flint_printf("\n\n"); abort(); } fmpr_clear(x); fmpr_clear(y); fmpr_clear(z); fmpr_clear(t); fmpr_clear(u); } /* compare with add_naive */ for (iter = 0; iter < 1000000 * arb_test_multiplier(); iter++) { slong prec, ret1, ret2; fmpr_t x, y, z, w; fmpr_rnd_t rnd; fmpr_init(x); fmpr_init(y); fmpr_init(z); fmpr_init(w); fmpr_randtest_special(x, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200)); fmpr_randtest_special(y, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200)); fmpr_randtest_special(z, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200)); prec = 2 + n_randint(state, 1000); if (n_randint(state, 10) == 0 && fmpz_bits(fmpr_expref(x)) < 10 && fmpz_bits(fmpr_expref(y)) < 10) prec = FMPR_PREC_EXACT; switch (n_randint(state, 4)) { case 0: rnd = FMPR_RND_DOWN; break; case 1: rnd = FMPR_RND_UP; break; case 2: rnd = FMPR_RND_FLOOR; break; default: rnd = FMPR_RND_CEIL; break; } ret1 = fmpr_add(z, x, y, prec, rnd); ret2 = fmpr_add_naive(w, x, y, prec, rnd); if (!fmpr_equal(z, w) || ret1 != ret2 || !fmpr_check_ulp(z, ret1, prec) || !fmpr_check_ulp(w, ret2, prec)) { flint_printf("FAIL\n\n"); flint_printf("iter %wd\n", iter); flint_printf("prec = %wd\n", prec); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("w = "); fmpr_print(w); flint_printf("\n\n"); flint_printf("ret1 = %wd, ret2 = %wd\n", ret1, ret2); abort(); } fmpr_clear(x); fmpr_clear(y); fmpr_clear(z); fmpr_clear(w); } /* compare rounding with mpfr */ for (iter = 0; iter < 1000000 * arb_test_multiplier(); iter++) { slong bits, res; int mpfr_res; fmpr_t x, y, z, w; mpfr_t X, Y, Z; bits = 2 + n_randint(state, 500); fmpr_init(x); fmpr_init(y); fmpr_init(z); fmpr_init(w); mpfr_init2(X, bits + 500); mpfr_init2(Y, bits + 500); mpfr_init2(Z, bits); fmpr_randtest_special(x, state, bits + n_randint(state, 500), 10); fmpr_randtest_special(y, state, bits + n_randint(state, 500), 10); fmpr_randtest_special(z, state, bits, 10); fmpr_get_mpfr(X, x, MPFR_RNDN); fmpr_get_mpfr(Y, y, MPFR_RNDN); switch (n_randint(state, 4)) { case 0: mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDZ); res = fmpr_add(z, x, y, bits, FMPR_RND_DOWN); break; case 1: mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDA); res = fmpr_add(z, x, y, bits, FMPR_RND_UP); break; case 2: mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDD); res = fmpr_add(z, x, y, bits, FMPR_RND_FLOOR); break; default: mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDU); res = fmpr_add(z, x, y, bits, FMPR_RND_CEIL); break; } fmpr_set_mpfr(w, Z); if (!fmpr_equal(z, w) || (res == FMPR_RESULT_EXACT) != (mpfr_res == 0) || !fmpr_check_ulp(z, res, bits)) { flint_printf("FAIL\n\n"); flint_printf("iter %wd\n", iter); flint_printf("bits = %wd\n", bits); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("w = "); fmpr_print(w); flint_printf("\n\n"); flint_printf("returned: %wd, %d\n", res, mpfr_res); abort(); } /* check error bound */ if (!fmpr_is_nan(z) && !fmpr_is_inf(z)) { fmpr_t z_exact, error_bound, true_error; fmpr_init(z_exact); fmpr_init(error_bound); fmpr_init(true_error); fmpr_set_error_result(error_bound, z, res); fmpr_add(z_exact, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN); fmpr_sub(true_error, z, z_exact, FMPR_PREC_EXACT, FMPR_RND_DOWN); fmpr_abs(true_error, true_error); if (fmpr_is_zero(error_bound) != fmpr_is_zero(true_error) || fmpr_cmp(true_error, error_bound) > 0) { flint_printf("FAIL: error bound\n\n"); flint_printf("bits = %wd\n", bits); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("z_exact = "); fmpr_print(z_exact); flint_printf("\n\n"); flint_printf("true_error = "); fmpr_print(true_error); flint_printf("\n\n"); flint_printf("error_bound = "); fmpr_print(error_bound); flint_printf("\n\n"); abort(); } fmpr_clear(z_exact); fmpr_clear(error_bound); fmpr_clear(true_error); } fmpr_clear(x); fmpr_clear(y); fmpr_clear(z); fmpr_clear(w); mpfr_clear(X); mpfr_clear(Y); mpfr_clear(Z); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
static void _fmprb_gamma(fmprb_t y, const fmprb_t x, long prec, int inverse) { int reflect; long r, n, wp; fmprb_t t, u, v; if (fmprb_is_exact(x)) { const fmpr_struct * mid = fmprb_midref(x); if (fmpr_is_special(mid)) { if (!inverse && fmpr_is_pos_inf(mid)) { fmprb_set(y, x); } else if (fmpr_is_nan(mid) || fmpr_is_neg_inf(mid) || !inverse) { fmpr_nan(fmprb_midref(y)); fmpr_pos_inf(fmprb_radref(y)); } else { fmprb_zero(y); } return; } else { const fmpz exp = *fmpr_expref(mid); const fmpz man = *fmpr_manref(mid); /* fast gamma(n), gamma(n/2) or gamma(n/4) */ if (!COEFF_IS_MPZ(exp) && (exp >= -2) && ((double) fmpz_bits(&man) + exp < prec)) { fmpq_t a; fmpq_init(a); fmpr_get_fmpq(a, mid); fmprb_gamma_fmpq(y, a, prec + 2 * inverse); if (inverse) fmprb_ui_div(y, 1, y, prec); fmpq_clear(a); return; } } } wp = prec + FLINT_BIT_COUNT(prec); gamma_stirling_choose_param_fmprb(&reflect, &r, &n, x, 1, 0, wp); fmprb_init(t); fmprb_init(u); fmprb_init(v); if (reflect) { /* gamma(x) = (rf(1-x, r) * pi) / (gamma(1-x+r) sin(pi x)) */ fmprb_sub_ui(t, x, 1, wp); fmprb_neg(t, t); gamma_rising_fmprb_ui_bsplit(u, t, r, wp); fmprb_const_pi(v, wp); fmprb_mul(u, u, v, wp); fmprb_add_ui(t, t, r, wp); gamma_stirling_eval_fmprb(v, t, n, 0, wp); fmprb_exp(v, v, wp); fmprb_sin_pi(t, x, wp); fmprb_mul(v, v, t, wp); } else { /* gamma(x) = gamma(x+r) / rf(x,r) */ fmprb_add_ui(t, x, r, wp); gamma_stirling_eval_fmprb(u, t, n, 0, wp); fmprb_exp(u, u, prec); gamma_rising_fmprb_ui_bsplit(v, x, r, wp); } if (inverse) fmprb_div(y, v, u, prec); else fmprb_div(y, u, v, prec); fmprb_clear(t); fmprb_clear(u); fmprb_clear(v); }
int main() { slong iter; flint_rand_t state; flint_printf("abs_bound_lt_2exp_si...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { fmpr_t x; fmpz_t b; slong c; fmpr_init(x); fmpz_init(b); fmpr_randtest_special(x, state, 2 + n_randint(state, 1000), 100); fmpr_abs_bound_lt_2exp_fmpz(b, x); c = fmpr_abs_bound_lt_2exp_si(x); if (c == -FMPR_PREC_EXACT) { if (!(fmpr_is_zero(x) || fmpz_cmp_si(b, -FMPR_PREC_EXACT) <= 0)) { flint_printf("FAIL (small/zero)\n\n"); abort(); } } else if (c == FMPR_PREC_EXACT) { if (!(fmpr_is_inf(x) || fmpr_is_nan(x) || fmpz_cmp_si(b, FMPR_PREC_EXACT) >= 0)) { flint_printf("FAIL (large/inf/nan)\n\n"); abort(); } } else { if (fmpz_cmp_si(b, c) != 0) { flint_printf("FAIL (normal)\n\n"); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("b = "); fmpz_print(b); flint_printf("\n\n"); flint_printf("c = %wd\n\n", c); abort(); } } fmpr_clear(x); fmpz_clear(b); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int fmprb_contains(const fmprb_t x, const fmprb_t y) { fmpr_t t; fmpr_t u; fmpr_struct tmp[4]; int left_ok, right_ok; if (fmprb_is_exact(y)) return fmprb_contains_fmpr(x, fmprb_midref(y)); if (fmpr_is_nan(fmprb_midref(y))) return fmpr_is_nan(fmprb_midref(x)); fmpr_init(t); fmpr_init(u); /* fast check */ fmpr_sub(t, fmprb_midref(x), fmprb_radref(x), 30, FMPR_RND_CEIL); fmpr_sub(u, fmprb_midref(y), fmprb_radref(y), 30, FMPR_RND_FLOOR); left_ok = fmpr_cmp(t, u) <= 0; /* exact check */ if (!left_ok) { fmpr_init(tmp + 0); fmpr_init(tmp + 1); fmpr_init(tmp + 2); fmpr_init(tmp + 3); fmpr_set(tmp + 0, fmprb_midref(x)); fmpr_neg(tmp + 1, fmprb_radref(x)); fmpr_neg(tmp + 2, fmprb_midref(y)); fmpr_set(tmp + 3, fmprb_radref(y)); fmpr_sum(t, tmp, 4, 30, FMPR_RND_DOWN); left_ok = fmpr_sgn(t) <= 0; fmpr_clear(tmp + 0); fmpr_clear(tmp + 1); fmpr_clear(tmp + 2); fmpr_clear(tmp + 3); } /* fast check */ fmpr_add(t, fmprb_midref(x), fmprb_radref(x), 30, FMPR_RND_FLOOR); fmpr_add(u, fmprb_midref(y), fmprb_radref(y), 30, FMPR_RND_CEIL); right_ok = (fmpr_cmp(t, u) >= 0); /* exact check */ if (!right_ok) { fmpr_init(tmp + 0); fmpr_init(tmp + 1); fmpr_init(tmp + 2); fmpr_init(tmp + 3); fmpr_set(tmp + 0, fmprb_midref(x)); fmpr_set(tmp + 1, fmprb_radref(x)); fmpr_neg(tmp + 2, fmprb_midref(y)); fmpr_neg(tmp + 3, fmprb_radref(y)); fmpr_sum(t, tmp, 4, 30, FMPR_RND_DOWN); right_ok = fmpr_sgn(t) >= 0; fmpr_clear(tmp + 0); fmpr_clear(tmp + 1); fmpr_clear(tmp + 2); fmpr_clear(tmp + 3); } fmpr_clear(t); fmpr_clear(u); return left_ok && right_ok; }