wprec = max(prec + 30, 140) pairs = max(52, (wprec // 2) + 10) s = 0 scale_int = 1 with _clean_dec_ctx(wprec, ROUND_CEILING): while z_hi / Decimal(scale_int) > Decimal("0.05"): scale_int <<= 1 s += 1 u_hi = z_hi / Decimal(scale_int) with _clean_dec_ctx(wprec, ROUND_FLOOR): u_lo = z_lo / Decimal(scale_int) # Lower bound on exp(-u_hi): stop at odd term 2*pairs - 1 with _clean_dec_ctx(wprec, ROUND_FLOOR): pos_lo = Decimal(1) term = Decimal(1) u2_lo = u_hi * u_hi for k in range(1, pairs): term = (term * u2_lo) / Decimal((2 * k - 1) * (2 * k)) pos_lo = pos_lo + term with _clean_dec_ctx(wprec, ROUND_CEILING): neg_hi = +u_hi term = +u_hi u2_hi = u_hi * u_hi for k in range(1, pairs): term = (term * u2_hi) / Decimal((2 * k) * (2 * k + 1)) neg_hi = neg_hi + term with _clean_dec_ctx(wprec, ROUND_FLOOR): lo = pos_lo - neg_hi if lo < Decimal(0): lo = Decimal(0) # Upper bound on exp(-u_lo): stop at even term 2*pairs with _clean_dec_ctx(wprec, ROUND_CEILING): pos_hi = Decimal(1) term = Decimal(1) u2_hi = u_lo * u_lo for k in range(1, pairs + 1): term = (term * u2_hi) / Decimal((2 * k - 1) * (2 * k)) pos_hi = pos_hi + term with _clean_dec_ctx(wprec, ROUND_FLOOR): neg_lo = +u_lo term = +u_lo u2_lo = u_lo * u_lo for k in range(1, pairs): term = (term * u2_lo) / Decimal((2 * k) * (2 * k + 1)) neg_lo = neg_lo + term with _clean_dec_ctx(wprec, ROUND_CEILING): hi = pos_hi - neg_lo if hi > Decimal(1): hi = Decimal(1) for _ in range(s): with _clean_dec_ctx(wprec, ROUND_FLOOR): lo = lo * lo with _clean_dec_ctx(wprec, ROUND_CEILING): hi = hi * hi with _clean_dec_ctx(prec, ROUND_FLOOR): lo_out = +lo with _clean_dec_ctx(prec, ROUND_CEILING): hi_out = +hi return lo_out, hi_out def _exact_one_minus_exp_neg_interval( z_lo: Decimal, z_hi: Decimal, prec: int = 120 ) -> Tuple[Decimal, Decimal]: """ Provably outward-directed interval [p_lo, p_hi] enclosing 1 - exp(-z) for z in [z_lo, z_hi] > 0. For z_hi < 0.05, evaluates paired alternating Taylor series directly (zero cancellation); for larger z, delegates to _exact_exp_neg_interval. """ if z_lo <= Decimal(0) or z_hi < z_lo: raise ValueError("Require 0 < z_lo <= z_hi") wprec = max(prec + 30, 140) if z_hi < Decimal("0.05"): max_pairs = max(68, (wprec // 2) + 14) with _clean_dec_ctx(wprec, ROUND_FLOOR) as ctx_t: thresh_lo = z_lo * Decimal(1).scaleb(-(prec + 14)) fact = 1 pow_lo = +z_lo pow_hi = +z_lo s_lo = Decimal(0) for pair_idx in range(1, max_pairs + 1): ctx_t.rounding = ROUND_FLOOR s_lo = s_lo + (pow_lo / Decimal(fact)) k_even = 2 * pair_idx fact *= k_even ctx_t.rounding = ROUND_FLOOR pow_lo = pow_lo * z_lo ctx_t.rounding = ROUND_CEILING pow_hi = pow_hi * z_lo term_even_hi = pow_hi / Decimal(fact) ctx_t.rounding = ROUND_FLOOR s_lo = s_lo - term_even_hi if term_even_hi <= thresh_lo: break fact *= k_even + 1 ctx_t.rounding = ROUND_FLOOR pow_lo = pow_lo * z_lo ctx_t.rounding = ROUND_CEILING pow_hi = pow_hi * z_lo if s_lo < Decimal(0): s_lo = Decimal(0) thresh_hi = z_hi * Decimal(1).scaleb(-(prec + 14)) fact = 1 ctx_t.rounding = ROUND_FLOOR pow_lo = +z_hi ctx_t.rounding = ROUND_CEILING pow_hi = +z_hi s_hi = +z_hi for pair_idx in range(1, max_pairs + 1): k_even = 2 * pair_idx fact *= k_even ctx_t.rounding = ROUND_FLOOR pow_lo = pow_lo * z_hi ctx_t.rounding = ROUND_CEILING pow_hi = pow_hi * z_hi ctx_t.rounding = ROUND_FLOOR term_even_lo = pow_lo / Decimal(fact) ctx_t.rounding = ROUND_CEILING s_hi = s_hi - term_even_lo k_odd = k_even + 1 fact *= k_odd ctx_t.rounding = ROUND_FLOOR pow_lo = pow_lo * z_hi ctx_t.rounding = ROUND_CEILING pow_hi = pow_hi * z_hi ctx_t.rounding = ROUND_CEILING term_odd_hi = pow_hi / Decimal(fact) s_hi = s_hi + term_odd_hi if term_odd_hi <= thresh_hi: break if s_hi > Decimal(1): s_hi = Decimal(1) with _clean_dec_ctx(prec, ROUND_FLOOR): p_lo_out = +s_lo with _clean_dec_ctx(prec, ROUND_CEILING): p_hi_out = +s_hi return p_lo_out, p_hi_out e_lo, e_hi = _exact_exp_neg_interval(z_lo, z_hi, prec=wprec) with _clean_dec_ctx(prec, ROUND_FLOOR): p_lo_out = max(Decimal(0), Decimal(1) - e_hi) with _clean_dec_ctx(prec, ROUND_CEILING): p_hi_out = min(Decimal(1), Decimal(1) - e_lo) return p_lo_out, p_hi_out def _alpha_root_interval(alpha: Decimal, n: int, prec: int = 120) -> Tuple[Decimal, Decimal]: """Provably outward-directed interval [lo, hi] enclosing alpha^(1/n) using pure +, -, *, /.""" wprec = max(prec + 25, 140) ln_lo, ln_hi = _exact_ln_interval(alpha, prec=wprec) with _clean_dec_ctx(wprec, ROUND_FLOOR): z_lo = ln_hi.copy_negate() / Decimal(n) with _clean_dec_ctx(wprec, ROUND_CEILING): z_hi = ln_lo.copy_negate() / Decimal(n) return _exact_exp_neg_interval(z_lo, z_hi, prec=prec) def _pow_directed(x: Decimal, exp_n: int, prec: int, rounding: str) -> Decimal: """Binary exponentiation x^exp_n for x >= 0 under explicit directed rounding.""" if exp_n == 0: return Decimal(1) with _clean_dec_ctx(prec, rounding): res = Decimal(1) base = +x e = exp_n while e > 0: if e & 1: res = res * base e >>= 1 if e > 0: base = base * base return res def _binomial_lower_tail_monotone_interval( k: int, n: int, p_lo: Decimal, p_hi: Decimal, q_lo: Decimal, q_hi: Decimal, prec: int ) -> Tuple[Decimal, Decimal]: """ Fast provably outward-directed interval for P(Bin(n, p) <= k) when k <= floor((n+1)*p), where terms f(j) are monotonically non-decreasing for j = 0..k. Steps downward from j = k using exact recurrence with rigorous tail bound <= j * f_hi(j). Handles arbitrary n (including n = 100,000, k = 50,000) in milliseconds and is 100% immune to narrow ambient exponent ranges (Emin/Emax) and Inexact traps! """ wprec = max(prec + 30, 125) with _clean_dec_ctx(wprec, ROUND_HALF_EVEN): c_int = math.comb(n, k) with _clean_dec_ctx(wprec, ROUND_FLOOR): c_lo = +Decimal(c_int) p_k_lo = _pow_directed(p_lo, k, wprec, ROUND_FLOOR) q_nk_lo = _pow_directed(q_lo, n - k, wprec, ROUND_FLOOR) f_lo = c_lo * p_k_lo * q_nk_lo with _clean_dec_ctx(wprec, ROUND_CEILING): c_hi = +Decimal(c_int) p_k_hi = _pow_directed(p_hi, k, wprec, ROUND_CEILING) q_nk_hi = _pow_directed(q_hi, n - k, wprec, ROUND_CEILING) f_hi = c_hi * p_k_hi * q_nk_hi tot_lo = f_lo tot_hi = f_hi with _clean_dec_ctx(wprec, ROUND_FLOOR): stop_thresh = Decimal(1).scaleb(-(prec + 10)) cur_lo = f_lo cur_hi = f_hi for j in range(k, 0, -1): # f(j-1) = f(j) * (j * q) / ((n - j + 1) * p) j_dec = Decimal(j) nj_dec = Decimal(n - j + 1) with _clean_dec_ctx(wprec, ROUND_CEILING): den_hi = nj_dec * p_hi num_hi = cur_hi * j_dec * q_hi with _clean_dec_ctx(wprec, ROUND_FLOOR): den_lo = nj_dec * p_lo num_lo = cur_lo * j_dec * q_lo next_lo = num_lo / den_hi if den_hi > Decimal(0) else Decimal(0) tot_lo = tot_lo + next_lo with _clean_dec_ctx(wprec, ROUND_CEILING): next_hi = num_hi / den_lo if den_lo > Decimal(0) else Decimal(1) tot_hi = tot_hi + next_hi rem_bound = Decimal(j - 1) * next_hi cur_lo = next_lo cur_hi = next_hi if j > 1 and rem_bound <= stop_thresh: with _clean_dec_ctx(wprec, ROUND_CEILING): tot_hi = tot_hi + rem_bound break with _clean_dec_ctx(prec, ROUND_FLOOR): out_lo = max(Decimal(0), min(Decimal(1), +tot_lo)) with _clean_dec_ctx(prec, ROUND_CEILING): out_hi = max(Decimal(0), min(Decimal(1), +tot_hi)) return out_lo, out_hi def _binomial_tail_lower_eq_interval(k: int, n: int, p: Decimal, prec: int = 80) -> Tuple[Decimal, Decimal]: """ Provably outward-directed interval [tail_lo, tail_hi] enclosing P(Bin(n, p) <= k) for ANY 0 <= k <= n and p in [0, 1] using unimodal symmetry + directed rounding (strict ROUND_FLOOR for lower endpoint, strict ROUND_CEILING for upper endpoint on ALL branches, and 100% isolated from ambient Decimal exponent range and Inexact traps). """