JoelSjogren

main.py

Apr 2nd, 2017
303
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. # Todo: change * to +.
  2. # Todo: implement stern-brocot approximations
  3.  
  4. class Turn:
  5.     def __init__(self, c, n):
  6.         assert c != 0
  7.         self.c = c
  8.         self.n = n
  9.    
  10.     def __mul__(x, y):
  11.         return Turn(x.c*y.c, x.n+y.n+oflow(x.c, y.c))
  12.    
  13.     def __repr__(self):
  14.         return '{}.{}'.format(self.c, self.n)
  15.    
  16.     def __pow__(self, n):
  17.         if n < 0:
  18.             return Turn(1/self.c, -self.n-1+(self.c==1))**-n
  19.         y = Turn.zero
  20.         for _ in range(n):
  21.             y *= self
  22.         return y
  23.    
  24.     def __le__(x, y):
  25.         z = x**-1 * y
  26.         return z.n >= 0
  27.    
  28. Turn.zero = Turn(1, 0)
  29.  
  30. def idiv(a):
  31.     if a.real > 0 and a.imag >= 0:
  32.         return a, 0
  33.     if a.real <= 0 and a.imag > 0:
  34.         return -a*1j, 1
  35.     if a.real < 0 and a.imag <= 0:
  36.         return -a, 2
  37.     if a.real >= 0 and a.imag < 0:
  38.         return a*1j, 3
  39.  
  40. def oflow(a, b):
  41.     ar, aq = idiv(a)
  42.     br, bq = idiv(b)
  43.     if aq+bq <= 2:
  44.         return False
  45.     if aq+bq >= 4:
  46.         return True
  47.     return (ar*br).real <= 0
  48.    
  49. a = Turn(1+4j, 0)
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment