]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix a bug where comparison of a rational with a float failed because
authorGuido van Rossum <guido@python.org>
Wed, 9 Sep 1998 14:07:06 +0000 (14:07 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 9 Sep 1998 14:07:06 +0000 (14:07 +0000)
the difference got converted to float.
Put brackets around the string representation of (non-integer)
rationals.
(Sjoerd Mullender.)

Demo/classes/Rat.py

index 5273e3230b9b59bd917eca9f64e9445d909f0249..4fc4a1786f1f447c272d884598d2b0c65c21a1b2 100755 (executable)
@@ -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)