#!/usr/bin/env python3 """Evaluate Comellas--Yebra (2002), page 7, using only the standard library.""" import argparse from decimal import Decimal, localcontext from fractions import Fraction def configuration(digits): with localcontext() as ctx: ctx.prec = digits + 40 a = Decimal(27) + 3 * Decimal(57).sqrt() u = Decimal(4) for _ in range(100): next_u = (2 * u + a / (u * u)) / 3 if abs(next_u - u) < Decimal(10) ** (-(digits + 30)): u = next_u break u = next_u else: raise ArithmeticError("cube root did not converge") x = 1 - (u * u + 6) / (6 * u) y = 2 * x * x - 3 * x + Decimal('0.5') quantum = Decimal(1).scaleb(-digits) # Round only these two parameters. Exact rational complements preserve # the construction's symmetries in the submitted finite decimals. x, y = (Fraction(v.quantize(quantum)) for v in (x, y)) half = Fraction(1, 2) return [(x, 0), (1-x, 0), (0, x), (1, x), (half, y), (y, half), (1-y, half), (half, 1-y), (0, 1-x), (1, 1-x), (x, 1), (1-x, 1)] def fixed(value, digits): scaled = Fraction(value) * 10 ** digits if scaled.denominator != 1: raise ArithmeticError("coordinate is not representable at this precision") whole, tail = divmod(scaled.numerator, 10 ** digits) return f'{whole}.{tail:0{digits}d}' def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--digits', type=int, default=80) args = parser.parse_args() if not 6 <= args.digits <= 200: parser.error('--digits must be between 6 and 200') for x, y in configuration(args.digits): print(f'{fixed(x, args.digits)} {fixed(y, args.digits)}') if __name__ == '__main__': main()