From 54ee272dfb207d6d20cd3217f7f100751496772d Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sun, 20 Oct 2019 02:29:36 +0100 Subject: [PATCH] catacomb/__init__.py: Implement `rich' comparisons on rationals. These are the only kind in Python 3, and they work everywhere. --- catacomb/__init__.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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 -- 2.11.0