| 1 | #!/usr/bin/env python3 |
| 2 | """Independent deterministic binary64/Fraction cancellation oracles (no Vile). |
| 3 | All expectations come from Python stdlib Fraction.from_float, never C/FMA or Vile. |
| 4 | The original12368 sum inputs reproduce the independent compensation probe seed. |
| 5 | """ |
| 6 | from fractions import Fraction as F |
| 7 | from pathlib import Path |
| 8 | import hashlib,json,math,random,struct |
| 9 | HERE=Path(__file__).resolve().parent |
| 10 | rng=random.Random(0xF4A53);cases=[] |
| 11 | def raw(x):return f'{struct.unpack(">Q",struct.pack(">d",x))[0]:016x}' |
| 12 | def frac(x):return F.from_float(x) |
| 13 | def risk(a,b,c,d): |
| 14 | p,q=a*b,c*d |
| 15 | return abs(p+q)<.01*max(abs(p),abs(q)) and max(abs(p),abs(q))<=math.ldexp(1.,-916) |
| 16 | def put(t,shift=0,label='random'): |
| 17 | if not all(math.isfinite(x) for x in t) or any(x==0 for x in t):return |
| 18 | p,q=t[0]*t[1],t[2]*t[3] |
| 19 | if not math.isfinite(p) or not math.isfinite(q):return |
| 20 | exact=frac(t[0])*frac(t[1])+frac(t[2])*frac(t[3]) |
| 21 | try:expected=float(exact*F(2)**shift) |
| 22 | except OverflowError:return |
| 23 | if not math.isfinite(expected):return |
| 24 | cases.append((t,shift,label)) |
| 25 | for k in range(1,53): |
| 26 | e=2.**-k |
| 27 | for exponent in [0,-800,-916,-917,-940,-969,-1000]: |
| 28 | scale=math.ldexp(1.,exponent) |
| 29 | put((1.,scale,-(1+e),math.ldexp(1-e,exponent)),500 if exponent<-500 else 0,'epsilon-square') |
| 30 | for _ in range(12000): |
| 31 | exponent=rng.choice([0,-100,-800,-915,-916,-917,-940,-969,-1000]) |
| 32 | a=rng.uniform(.5,1);b=math.ldexp(rng.uniform(.5,1),exponent) |
| 33 | c=math.nextafter(a,rng.choice([math.inf,0.]));d=-(a*b)/c |
| 34 | put((a,b,c,d),500 if exponent<-500 else 0) |
| 35 | for exponent in [-800,-916,-940,-1000]: |
| 36 | t=math.ldexp(1.,exponent);put((.5,t,-t,.5),500,'genuine-zero') |
| 37 | assert len(cases)==12368 |
| 38 | # After each operand is normalized by frexp, these cover the exact inclusive |
| 39 | # 2^-916 boundary and its adjacent powers. The top rounded product is exact. |
| 40 | for exponent in [-913,-914,-915]: |
| 41 | for k in [1,26,40,51,52]: |
| 42 | e=2.**-k;scale=math.ldexp(1.,exponent) |
| 43 | put((1.,scale,-(1+e),math.ldexp(1-e,exponent)),500,'normalized-guard-boundary') |
| 44 | for t in [(1.0000000000000002,-.9999999999999998,1.,1.),(1.0000000000000002,.9999999999999998,1.,-1.)]:put(t,0,'parent-regression') |
| 45 | 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'] |
| 46 | counts={'zero':0,'existing_tiny_product_guard':0,'near_residual_risk':0,'risk_without_existing_guard':0,'inclusive_guard':0} |
| 47 | for index,(t,shift,label) in enumerate(cases): |
| 48 | a,b,c,d=t;fa,fb,fc,fd=map(frac,t);scale=F(2)**shift |
| 49 | summ=fa*fb+fc*fd |
| 50 | # Imaginary product component reuses the independently summed expression. |
| 51 | pr=(fa*fd-fc*fb)*scale;pi=summ*scale |
| 52 | # Commute the second product so both complex operands normalize near1. |
| 53 | cs,dl=(d,c) if abs(c)>abs(d) else (c,d) |
| 54 | ar,ai=math.ldexp(a,shift),math.ldexp(cs,shift);br,bi=b,dl |
| 55 | den=frac(br)**2+frac(bi)**2 |
| 56 | dr=(frac(ar)*frac(br)+frac(ai)*frac(bi))/den |
| 57 | di=(frac(ai)*frac(br)-frac(ar)*frac(bi))/den |
| 58 | _,ae=math.frexp(max(abs(ar),abs(ai)));_,be=math.frexp(max(abs(br),abs(bi))) |
| 59 | nar,nai,nbr,nbi=[math.ldexp(x,-e) for x,e in [(ar,ae),(ai,ae),(br,be),(bi,be)]] |
| 60 | rr,ir=risk(nar,nbr,nai,nbi),risk(nai,nbr,-nar,nbi) |
| 61 | products=[nar*nbr,nai*nbi,nai*nbr,nar*nbi] |
| 62 | tiny=any(x!=0 and y!=0 and abs(p)<math.ldexp(1.,-1022) for x,y,p in [(nar,nbr,products[0]),(nai,nbi,products[1]),(nai,nbr,products[2]),(nar,nbi,products[3])]) |
| 63 | counts['zero']+=summ==0;counts['existing_tiny_product_guard']+=tiny;counts['near_residual_risk']+=rr or ir;counts['risk_without_existing_guard']+=(rr or ir) and not tiny |
| 64 | assert not (rr or ir) or tiny |
| 65 | if not tiny: |
| 66 | for p,q in [(products[0],products[1]),(products[2],-products[3])]: |
| 67 | size=max(abs(p),abs(q)) |
| 68 | if abs(p+q)<.01*size:assert size>math.ldexp(1.,-513) |
| 69 | counts['inclusive_guard']+=max(abs(nar*nbr),abs(nai*nbi))==math.ldexp(1.,-916) |
| 70 | 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))])) |
| 71 | outputs={'complex_compensation.tsv':'\n'.join(rows)+'\n'} |
| 72 | for name,text in outputs.items():(HERE/name).write_text(text) |
| 73 | 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'} |
| 74 | (HERE/'complex_compensation_provenance.json').write_text(json.dumps(manifest,indent=2)+'\n') |
| 75 | print(json.dumps(manifest,indent=2)) |
| 76 | |