#!/usr/bin/env python3 """Independent deterministic binary64/Fraction cancellation oracles (no Vile). All expectations come from Python stdlib Fraction.from_float, never C/FMA or Vile. The original12368 sum inputs reproduce the independent compensation probe seed. """ from fractions import Fraction as F from pathlib import Path import hashlib,json,math,random,struct HERE=Path(__file__).resolve().parent rng=random.Random(0xF4A53);cases=[] def raw(x):return f'{struct.unpack(">Q",struct.pack(">d",x))[0]:016x}' def frac(x):return F.from_float(x) def risk(a,b,c,d): p,q=a*b,c*d return abs(p+q)<.01*max(abs(p),abs(q)) and max(abs(p),abs(q))<=math.ldexp(1.,-916) def put(t,shift=0,label='random'): if not all(math.isfinite(x) for x in t) or any(x==0 for x in t):return p,q=t[0]*t[1],t[2]*t[3] if not math.isfinite(p) or not math.isfinite(q):return exact=frac(t[0])*frac(t[1])+frac(t[2])*frac(t[3]) try:expected=float(exact*F(2)**shift) except OverflowError:return if not math.isfinite(expected):return cases.append((t,shift,label)) for k in range(1,53): e=2.**-k for exponent in [0,-800,-916,-917,-940,-969,-1000]: scale=math.ldexp(1.,exponent) put((1.,scale,-(1+e),math.ldexp(1-e,exponent)),500 if exponent<-500 else 0,'epsilon-square') for _ in range(12000): exponent=rng.choice([0,-100,-800,-915,-916,-917,-940,-969,-1000]) a=rng.uniform(.5,1);b=math.ldexp(rng.uniform(.5,1),exponent) c=math.nextafter(a,rng.choice([math.inf,0.]));d=-(a*b)/c put((a,b,c,d),500 if exponent<-500 else 0) for exponent in [-800,-916,-940,-1000]: t=math.ldexp(1.,exponent);put((.5,t,-t,.5),500,'genuine-zero') assert len(cases)==12368 # After each operand is normalized by frexp, these cover the exact inclusive # 2^-916 boundary and its adjacent powers. The top rounded product is exact. for exponent in [-913,-914,-915]: for k in [1,26,40,51,52]: e=2.**-k;scale=math.ldexp(1.,exponent) put((1.,scale,-(1+e),math.ldexp(1-e,exponent)),500,'normalized-guard-boundary') for t in [(1.0000000000000002,-.9999999999999998,1.,1.),(1.0000000000000002,.9999999999999998,1.,-1.)]:put(t,0,'parent-regression') rows=['#id\tlabel\ta_bits\tb_bits\tc_bits\td_bits\tshift\texact_zero\tdirect_risk\tsum_bits\tproduct_re_bits\tproduct_im_bits\tdivide_re_bits\tdivide_im_bits\tdivide_re_risk\tdivide_im_risk\texisting_tiny_product_guard'] counts={'zero':0,'existing_tiny_product_guard':0,'near_residual_risk':0,'risk_without_existing_guard':0,'inclusive_guard':0} for index,(t,shift,label) in enumerate(cases): a,b,c,d=t;fa,fb,fc,fd=map(frac,t);scale=F(2)**shift summ=fa*fb+fc*fd # Imaginary product component reuses the independently summed expression. pr=(fa*fd-fc*fb)*scale;pi=summ*scale # Commute the second product so both complex operands normalize near1. cs,dl=(d,c) if abs(c)>abs(d) else (c,d) ar,ai=math.ldexp(a,shift),math.ldexp(cs,shift);br,bi=b,dl den=frac(br)**2+frac(bi)**2 dr=(frac(ar)*frac(br)+frac(ai)*frac(bi))/den di=(frac(ai)*frac(br)-frac(ar)*frac(bi))/den _,ae=math.frexp(max(abs(ar),abs(ai)));_,be=math.frexp(max(abs(br),abs(bi))) nar,nai,nbr,nbi=[math.ldexp(x,-e) for x,e in [(ar,ae),(ai,ae),(br,be),(bi,be)]] rr,ir=risk(nar,nbr,nai,nbi),risk(nai,nbr,-nar,nbi) products=[nar*nbr,nai*nbi,nai*nbr,nar*nbi] tiny=any(x!=0 and y!=0 and abs(p)math.ldexp(1.,-513) counts['inclusive_guard']+=max(abs(nar*nbr),abs(nai*nbi))==math.ldexp(1.,-916) rows.append('\t'.join([str(index),label,*map(raw,t),str(shift),str(int(summ==0)),str(int(risk(*t))),raw(float(summ)),raw(float(pr)),raw(float(pi)),raw(float(dr)),raw(float(di)),str(int(rr)),str(int(ir)),str(int(tiny))])) outputs={'complex_compensation.tsv':'\n'.join(rows)+'\n'} for name,text in outputs.items():(HERE/name).write_text(text) manifest={'oracle':'Python stdlib Fraction.from_float, exact rational arithmetic then float rounding; no Vile/C output expectations','seed':'0xF4A53','sum_cases':len(cases),'original_probe_cases':12368,'counts':counts,'files':{name:{'bytes':len(text.encode()),'sha256':hashlib.sha256(text.encode()).hexdigest()} for name,text in outputs.items()},'tolerance':'component absolute error <=4e-13*abs(expected)+8*minimum_subnormal; not a correct-rounding promise'} (HERE/'complex_compensation_provenance.json').write_text(json.dumps(manifest,indent=2)+'\n') print(json.dumps(manifest,indent=2))