#!/usr/bin/env python3
"""N13 — arithmetic from D1287 supplied sources.
No network, external packages, fitting operations or shared runtime files.
Printed source cells and unrounded arithmetic are distinct outputs.
"""

import math
from decimal import Decimal, ROUND_HALF_UP, getcontext
from fractions import Fraction

getcontext().prec = 40
PHI = (1.0 + math.sqrt(5.0)) / 2.0


def shown(value, places=9, signed=False):
    """Decimal half-up display; never use display-rounded inputs implicitly."""
    value = Decimal(str(value))
    rounded = value.quantize(Decimal(1).scaleb(-places), rounding=ROUND_HALF_UP)
    return format(rounded, ("+" if signed else "") + "." + str(places) + "f")

def main():
    print('=== row 13 ===')
    print('Scorecard (verbatim): sin²θ W φ⁻³ = √5 − 2 theory 0.236068 experiment 0.23122 ± 0.00006 (MS-bar, M Z ; PDG 2026) deviation +2.10% · d = no d — fenced Structural (~2% target) —')
    readout = 3.0/(4.0+(6.0*PHI-1.0))
    value = PHI**(-3)
    assert abs(readout-value) < 1e-14
    assert abs(value-(math.sqrt(5.0)-2.0)) < 1e-14
    print("readout = " + shown(readout,12))
    print("radical = " + shown(math.sqrt(5.0)-2.0,12))
    print("theory_at_cell_precision = " + shown(value,6))
    for label, theory in (("structural_full_formula", Decimal(str(value))),
                          ("structural_printed_cell", Decimal("0.236068"))):
        offset_percent = 100*(theory-Decimal("0.23122"))/Decimal("0.23122")
        print(label+".theory = "+shown(theory,12))
        print(label+".signed_percent = "+shown(offset_percent,9,True)+"%"
              +"; rounded = "+shown(offset_percent,2,True)+"%")
    print("comparator_uncertainty_as_printed = 0.00006")
    print("d_policy = NOT COMPUTED: explicit suite fence; missing scheme/scale matching")
    print()


if __name__ == "__main__":
    main()
