Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ca9a1a

Browse files
committed
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.)
1 parent 76d1f96 commit 7ca9a1a

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

Demo/classes/Rat.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __str__(self):
100100
if self.__den == 1:
101101
return str(self.__num)
102102
else:
103-
return '%s/%s' % (str(self.__num), str(self.__den))
103+
return '(%s/%s)' % (str(self.__num), str(self.__den))
104104

105105
# a + b
106106
def __add__(a, b):
@@ -213,7 +213,7 @@ def __complex__(a):
213213

214214
# cmp(a,b)
215215
def __cmp__(a, b):
216-
diff = a - b
216+
diff = Rat(a - b)
217217
if diff.__num < 0:
218218
return -1
219219
elif diff.__num > 0:
@@ -244,28 +244,28 @@ def test():
244244
[Rat(1,2), Rat(-3,10), Rat(1,25), Rat(1,4)]
245245
[Rat(-3,10), Rat(1,25), Rat(1,4), Rat(1,2)]
246246
0
247-
11/10
248-
11/10
247+
(11/10)
248+
(11/10)
249249
1.1
250250
OK
251-
2 1.5 3/2 (1.5+1.5j) 15707963/5000000
251+
2 1.5 (3/2) (1.5+1.5j) (15707963/5000000)
252252
2 2 2.0 (2+0j)
253253
254254
4 0 4 1 4 0
255255
3.5 0.5 3.0 1.33333333333 2.82842712475 1
256-
7/2 1/2 3 4/3 2.82842712475 1
256+
(7/2) (1/2) 3 (4/3) 2.82842712475 1
257257
(3.5+1.5j) (0.5-1.5j) (3+3j) (0.666666666667-0.666666666667j) (1.43248815986+2.43884761145j) 1
258258
1.5 1 1.5 (1.5+0j)
259259
260260
3.5 -0.5 3.0 0.75 2.25 -1
261261
3.0 0.0 2.25 1.0 1.83711730709 0
262262
3.0 0.0 2.25 1.0 1.83711730709 1
263263
(3+1.5j) -1.5j (2.25+2.25j) (0.5-0.5j) (1.50768393746+1.04970907623j) -1
264-
3/2 1 1.5 (1.5+0j)
264+
(3/2) 1 1.5 (1.5+0j)
265265
266-
7/2 -1/2 3 3/4 9/4 -1
266+
(7/2) (-1/2) 3 (3/4) (9/4) -1
267267
3.0 0.0 2.25 1.0 1.83711730709 -1
268-
3 0 9/4 1 1.83711730709 0
268+
3 0 (9/4) 1 1.83711730709 0
269269
(3+1.5j) -1.5j (2.25+2.25j) (0.5-0.5j) (1.50768393746+1.04970907623j) -1
270270
(1.5+1.5j) (1.5+1.5j)
271271

0 commit comments

Comments
 (0)