"""Check the rounded inequalities in smoothed-prime-race.tex using mpmath 1.3."""

from mpmath import iv

iv.dps = 50
num = iv.mpf
pi = iv.pi
log = iv.log
exp = iv.exp
sqrt = iv.sqrt


def below(label, value, bound):
    if not (value < num(bound)):
        raise ArithmeticError(f"{label}: enclosure {value} fails bound {bound}")
    print(f"{label} < {bound}")


scale = sqrt(108) / (2 * pi)
odd_prime_sum = num(25) / 24 * (log(5) / 25 + (1 + log(5)) / 10)
budget = log(scale) + 1 - iv.euler + 2 * odd_prime_sum
below("Hadamard weight sum", budget, "1.62")

first = num("2.8")
line_ratio = (num("2.25") + first**2) / num("1.5") * sqrt(2*pi) * exp(-pi*first/2)
below("critical-line coefficient", num("1.62") * line_ratio, "0.34")
height = num(94)
angle = num("1.55")
below("reciprocal squared cosine", 1 / iv.cos(angle)**2, 2500)
tail_ratio = 2500 * (height + 4 / height) * exp(-angle * height)
below("off-line coefficient", num("1.62") * tail_ratio, "3e-58")

integral_error = 2 * sqrt(2*pi)/pi * (40/(pi/2) + 8/(pi/2)**2)
below("shifted contour constant", integral_error, 100)

start = num("8e9")
below("progression error margin", 3/(160*log(start)) + 2*log(2)/start, "0.002")
below("small-endpoint error", start * (log(start) + num("0.498")) + 2*log(2), "3e11")
below("square coefficient lower bound check", num("0.44"), num("0.498")*sqrt(pi)/2)

s = num("1.2")
tail_integral = 3**(1-s) * (log(3)/(s-1) + 1/(s-1)**2)
all_terms = log(2)/2**s + log(3)/3**s + tail_integral
below("higher-power coefficient", 2 * all_terms / (1 - num(2)**num("-0.4")), 250)

xmin = num("1e50")
remainder = (num("3e11") + log(xmin) + 5 + 100 / sqrt(xmin)) / sqrt(xmin)
remainder += 250 * xmin**num("-0.1")
for beta, exponent in [(num(1), 110), (num(7)/8, 147)]:
    off_line = num("3e-58") * num(10)**(exponent*(beta-num("0.5")))
    below(f"normalized race up to 10^{exponent}", num("-0.10") + off_line + remainder, "-0.09")
