1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# Rational numbers
from types import *
def rat(num, den):
if type(num) == FloatType or type(den) == FloatType:
return num/den
return Rat(num, den)
def gcd(a, b):
while b:
a, b = b, a%b
return a
class Rat:
def __init__(self, num, den):
if den == 0:
raise ZeroDivisionError, 'rat(x, 0)'
if type(den) == FloatType or type(num) == FloatType:
g = float(den)
else:
g = gcd(num, den)
self.num = num/g
self.den = den/g
def __repr__(self):
return 'Rat(%s, %s)' % (self.num, self.den)
def __str__(self):
if self.den == 1:
return str(self.num)
else:
return '%s/%s' % (self.num, self.den)
def __cmp__(a, b):
c = a-b
if c.num < 0:
return -1
if c.num > 0:
return 1
return 0
def __float__(self):
return float(self.num) / float(self.den)
def __long__(self):
return long(self.num) / long(self.den)
def __int__(self):
return int(self.num / self.den)
def __coerce__(a, b):
t = type(b)
if t == IntType:
return a, Rat(b, 1)
if t == LongType:
return a, Rat(b, 1L)
if t == FloatType:
return a, Rat(b, 1.0)
if t == InstanceType and a.__class__ == b.__class__:
return a, b
raise TypeError, 'Rat.__coerce__: bad other arg'
def __add__(a, b):
return rat(a.num*b.den + b.num*a.den, a.den*b.den)
def __sub__(a, b):
return rat(a.num*b.den - b.num*a.den, a.den*b.den)
def __mul__(a, b):
return rat(a.num*b.num, a.den*b.den)
def __div__(a, b):
return rat(a.num*b.den, a.den*b.num)
def __neg__(self):
return rat(-self.num, self.den)
def test():
print Rat(-1L, 1)
print Rat(1, -1)
a = Rat(1, 10)
print int(a), long(a), float(a)
b = Rat(2, 5)
l = [a+b, a-b, a*b, a/b]
print l
l.sort()
print l
print Rat(0, 1)
print a+1
print a+1L
print a+1.0
try:
print Rat(1, 0)
raise SystemError, 'should have been ZeroDivisionError'
except ZeroDivisionError:
print 'OK'
test()
|