From: Mark Wooding Date: Sun, 20 Oct 2019 01:29:36 +0000 (+0100) Subject: catacomb/__init__.py: Implement `rich' comparisons on rationals. X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/commitdiff_plain/54ee272dfb207d6d20cd3217f7f100751496772d catacomb/__init__.py: Implement `rich' comparisons on rationals. These are the only kind in Python 3, and they work everywhere. --- diff --git a/catacomb/__init__.py b/catacomb/__init__.py index 24f3a61..508f1f8 100644 --- a/catacomb/__init__.py +++ b/catacomb/__init__.py @@ -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