Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Todo: change * to +.
- # Todo: implement stern-brocot approximations
- class Turn:
- def __init__(self, c, n):
- assert c != 0
- self.c = c
- self.n = n
- def __mul__(x, y):
- return Turn(x.c*y.c, x.n+y.n+oflow(x.c, y.c))
- def __repr__(self):
- return '{}.{}'.format(self.c, self.n)
- def __pow__(self, n):
- if n < 0:
- return Turn(1/self.c, -self.n-1+(self.c==1))**-n
- y = Turn.zero
- for _ in range(n):
- y *= self
- return y
- def __le__(x, y):
- z = x**-1 * y
- return z.n >= 0
- Turn.zero = Turn(1, 0)
- def idiv(a):
- if a.real > 0 and a.imag >= 0:
- return a, 0
- if a.real <= 0 and a.imag > 0:
- return -a*1j, 1
- if a.real < 0 and a.imag <= 0:
- return -a, 2
- if a.real >= 0 and a.imag < 0:
- return a*1j, 3
- def oflow(a, b):
- ar, aq = idiv(a)
- br, bq = idiv(b)
- if aq+bq <= 2:
- return False
- if aq+bq >= 4:
- return True
- return (ar*br).real <= 0
- a = Turn(1+4j, 0)
Advertisement