diff options
author | Guido van Rossum <guido@python.org> | 1998-09-09 14:07:06 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-09-09 14:07:06 (GMT) |
commit | 7ca9a1a466992e556b3192a7a0ba8baa652e342a (patch) | |
tree | 2c69ff8adee11493c7e176a7e19df60fe20a6fab /Demo | |
parent | 76d1f96fe2bf905288ac8c4ef991302f5aeb695a (diff) | |
download | cpython-7ca9a1a466992e556b3192a7a0ba8baa652e342a.zip cpython-7ca9a1a466992e556b3192a7a0ba8baa652e342a.tar.gz cpython-7ca9a1a466992e556b3192a7a0ba8baa652e342a.tar.bz2 |
Fix a bug where comparison of a rational with a float failed because
the difference got converted to float.
Put brackets around the string representation of (non-integer)
rationals.
(Sjoerd Mullender.)
Diffstat (limited to 'Demo')
-rwxr-xr-x | Demo/classes/Rat.py | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/Demo/classes/Rat.py b/Demo/classes/Rat.py index 5273e32..4fc4a17 100755 --- a/Demo/classes/Rat.py +++ b/Demo/classes/Rat.py @@ -100,7 +100,7 @@ class Rat: if self.__den == 1: return str(self.__num) else: - return '%s/%s' % (str(self.__num), str(self.__den)) + return '(%s/%s)' % (str(self.__num), str(self.__den)) # a + b def __add__(a, b): @@ -213,7 +213,7 @@ class Rat: # cmp(a,b) def __cmp__(a, b): - diff = a - b + diff = Rat(a - b) if diff.__num < 0: return -1 elif diff.__num > 0: @@ -244,16 +244,16 @@ def test(): [Rat(1,2), Rat(-3,10), Rat(1,25), Rat(1,4)] [Rat(-3,10), Rat(1,25), Rat(1,4), Rat(1,2)] 0 - 11/10 - 11/10 + (11/10) + (11/10) 1.1 OK - 2 1.5 3/2 (1.5+1.5j) 15707963/5000000 + 2 1.5 (3/2) (1.5+1.5j) (15707963/5000000) 2 2 2.0 (2+0j) 4 0 4 1 4 0 3.5 0.5 3.0 1.33333333333 2.82842712475 1 - 7/2 1/2 3 4/3 2.82842712475 1 + (7/2) (1/2) 3 (4/3) 2.82842712475 1 (3.5+1.5j) (0.5-1.5j) (3+3j) (0.666666666667-0.666666666667j) (1.43248815986+2.43884761145j) 1 1.5 1 1.5 (1.5+0j) @@ -261,11 +261,11 @@ def test(): 3.0 0.0 2.25 1.0 1.83711730709 0 3.0 0.0 2.25 1.0 1.83711730709 1 (3+1.5j) -1.5j (2.25+2.25j) (0.5-0.5j) (1.50768393746+1.04970907623j) -1 - 3/2 1 1.5 (1.5+0j) + (3/2) 1 1.5 (1.5+0j) - 7/2 -1/2 3 3/4 9/4 -1 + (7/2) (-1/2) 3 (3/4) (9/4) -1 3.0 0.0 2.25 1.0 1.83711730709 -1 - 3 0 9/4 1 1.83711730709 0 + 3 0 (9/4) 1 1.83711730709 0 (3+1.5j) -1.5j (2.25+2.25j) (0.5-0.5j) (1.50768393746+1.04970907623j) -1 (1.5+1.5j) (1.5+1.5j) |