catacomb/__init__.py: Implement `rich' comparisons on rationals.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 20 Oct 2019 01:29:36 +0000 (02:29 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 11:44:21 +0000 (12:44 +0100)
These are the only kind in Python 3, and they work everywhere.

catacomb/__init__.py

index 24f3a61..508f1f8 100644 (file)
@@ -387,12 +387,15 @@ class BaseRat (object):
     return type(me)(me._d*n, me._n*d)
   __div__ = __truediv__
   __rdiv__ = __rtruediv__
-  def __cmp__(me, you):
+  def _order(me, you, op):
     n, d = _split_rat(you)
-    return cmp(me._n*d, n*me._d)
-  def __rcmp__(me, you):
-    n, d = _split_rat(you)
-    return cmp(n*me._d, me._n*d)
+    return op(me._n*d, n*me._d)
+  def __eq__(me, you): return me._order(you, lambda x, y: x == y)
+  def __ne__(me, you): return me._order(you, lambda x, y: x != y)
+  def __le__(me, you): return me._order(you, lambda x, y: x <= y)
+  def __lt__(me, you): return me._order(you, lambda x, y: x <  y)
+  def __gt__(me, you): return me._order(you, lambda x, y: x >  y)
+  def __ge__(me, you): return me._order(you, lambda x, y: x >= y)
 
 class IntRat (BaseRat):
   RING = MP