check_math.py

back to table · edit · history · where entries came from · files · download

4437 bytes, as of the version from 2026-12-02 03:19 (current). Recorded here, not run.

"""Independent rational-series enclosure of exp(pi), without Arb transcendental calls.

Run under Sage: sage -python check_math.py [100-digit bare stored decimal]
Uses exact QQ alternating arctangent bounds, Machin's identity, positive
exponential Taylor bounds, and outward rational rounding after squaring.
This is builder evidence; a separate critic must make the review decision.
"""
import json
import sys
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ

SCALE = 10 ** 180


def floor_q(x):
    return QQ((x * SCALE).floor()) / SCALE


def ceil_q(x):
    return QQ((x * SCALE).ceil()) / SCALE


def atan_bounds(q, n):
    x = QQ(1) / q
    total = sum(((-1) ** k * x ** (2*k+1) / (2*k+1) for k in range(n)), QQ(0))
    next_term = (-1) ** n * x ** (2*n+1) / (2*n+1)
    return min(total, total + next_term), max(total, total + next_term)


def pi_bounds():
    # tan(4 atan(1/5)) = 120/119; tan(4 atan(1/5)-atan(1/239)) = 1.
    a = QQ(1) / 5
    tan2 = 2*a / (1-a*a)
    tan4 = 2*tan2 / (1-tan2*tan2)
    b = QQ(1) / 239
    assert tan4 == QQ(120)/119
    assert (tan4-b)/(1+tan4*b) == 1
    # 0 < 4 atan(1/5)-atan(1/239) < 4/5 < pi/2, so the angle is pi/4.
    a0,a1 = atan_bounds(5, 180)
    b0,b1 = atan_bounds(239, 60)
    lo,hi = 16*a0-4*b1, 16*a1-4*b0
    assert 3 < lo < hi < 4
    return floor_q(lo), ceil_q(hi)


def exp_point_bounds(x, terms=120):
    if x < 0:
        lo,hi = exp_point_bounds(-x, terms)
        return floor_q(1/hi), ceil_q(1/lo)
    if x == 0:
        return QQ(1), QQ(1)
    y = x/16
    term = QQ(1)
    total = QQ(1)
    for k in range(1, terms+1):
        term *= y/k
        total += term
    # Terms after y^(M+1)/(M+1)! have ratio at most y/(M+2).
    ratio = y/(terms+2)
    assert 0 <= ratio < 1
    tail = term*y/(terms+1)/(1-ratio)
    lo,hi = floor_q(total), ceil_q(total+tail)
    for _ in range(4):
        lo,hi = floor_q(lo*lo), ceil_q(hi*hi)
    assert 0 < lo < hi and hi-lo < QQ(1)/10**172
    return lo,hi


def constant_bounds(multiplier=1):
    lo,hi = pi_bounds()
    if multiplier < 0:
        lo,hi = multiplier*hi, multiplier*lo
    else:
        lo,hi = multiplier*lo, multiplier*hi
    lower = exp_point_bounds(lo)[0]
    upper = exp_point_bounds(hi)[1]
    return lower,upper


def decimal_interval(text):
    # A bare NumberDB decimal carries one unit in the last place as radius.
    whole,frac = text.split('.')
    assert whole.isdigit() and frac.isdigit()
    unit = QQ(1)/10**len(frac)
    center = QQ(int(whole+frac))/10**len(frac)
    return center-unit, center+unit, unit


def report(stored=None):
    lo,hi = constant_bounds()
    one_lo,one_hi = exp_point_bounds(QQ(1))
    # Exact, nonzero known-answer interval for e from the factorial series.
    term = QQ(1)
    total = QQ(1)
    for k in range(1, 251):
        term /= k
        total += term
    e_upper = total + term/251/(1-QQ(1)/252)
    zero_control = exp_point_bounds(QQ(0)) == (QQ(1),QQ(1))
    positive_control = one_lo <= total < e_upper <= one_hi and one_lo > 2 and one_hi < 3
    wrong_reciprocal = constant_bounds(-1)
    wrong_fourier = constant_bounds(2)
    negative_control = wrong_reciprocal[1] < lo and wrong_fourier[0] > hi
    assert zero_control and positive_control and negative_control
    result = {'method': 'exact QQ Machin arctangent bounds and exponential Taylor bounds with x/16 range reduction and four outward-rounded squarings',
              'lower': str(lo), 'upper': str(hi), 'width': str(hi-lo),
              'width_less_than_1e-172': bool(hi-lo < QQ(1)/10**172),
              'known_answer_control': bool(zero_control and positive_control),
              'negative_control_rejected': bool(negative_control),
              'controls': {'exp_zero_exact': bool(zero_control), 'exp_one_against_direct_factorial_series': bool(positive_control), 'exp_minus_pi_rejected': bool(wrong_reciprocal[1] < lo), 'exp_two_pi_rejected': bool(wrong_fourier[0] > hi)},
              'checked_entries': 1, 'total_entries': 1, 'failures': []}
    if stored is not None:
        s0,s1,unit = decimal_interval(stored)
        assert s0 <= lo <= hi <= s1
        result.update(stored=stored, stored_contains_independent_enclosure=True,
                      stored_radius=str(unit), independent_width_in_stored_ulps=str((hi-lo)/unit))
    return result


if __name__ == '__main__':
    print(json.dumps(report(sys.argv[1] if len(sys.argv)>1 else None), indent=2))