Rat.py 7.17 KB
Newer Older
1 2
'''\
This module implements rational numbers.
Guido van Rossum's avatar
Guido van Rossum committed
3

4 5 6 7 8 9 10 11
The entry point of this module is the function
	rat(numerator, denominator)
If either numerator or denominator is of an integral or rational type,
the result is a rational number, else, the result is the simplest of
the types float and complex which can hold numerator/denominator.
If denominator is omitted, it defaults to 1.
Rational numbers can be used in calculations with any other numeric
type.  The result of the calculation will be rational if possible.
Guido van Rossum's avatar
Guido van Rossum committed
12

13 14 15 16 17
There is also a test function with calling sequence
	test()
The documentation string of the test function contains the expected
output.
'''
Guido van Rossum's avatar
Guido van Rossum committed
18

19 20 21
# Contributed by Sjoerd Mullender

from types import *
Guido van Rossum's avatar
Guido van Rossum committed
22 23

def gcd(a, b):
24
	'''Calculate the Greatest Common Divisor.'''
Guido van Rossum's avatar
Guido van Rossum committed
25 26 27 28
	while b:
		a, b = b, a%b
	return a

29 30 31 32 33 34 35 36 37 38
def rat(num, den = 1):
	# must check complex before float
	if type(num) is ComplexType or type(den) is ComplexType:
		# numerator or denominator is complex: return a complex
		return complex(num) / complex(den)
	if type(num) is FloatType or type(den) is FloatType:
		# numerator or denominator is float: return a float
		return float(num) / float(den)
	# otherwise return a rational
	return Rat(num, den)
Guido van Rossum's avatar
Guido van Rossum committed
39 40

class Rat:
41
	'''This class implements rational numbers.'''
Guido van Rossum's avatar
Guido van Rossum committed
42

43
	def __init__(self, num, den = 1):
Guido van Rossum's avatar
Guido van Rossum committed
44 45
		if den == 0:
			raise ZeroDivisionError, 'rat(x, 0)'
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

		# normalize

		# must check complex before float
		if type(num) is ComplexType or type(den) is ComplexType:
			# numerator or denominator is complex:
			# normalized form has denominator == 1+0j
			self.__num = complex(num) / complex(den)
			self.__den = complex(1)
			return
		if type(num) is FloatType or type(den) is FloatType:
			# numerator or denominator is float:
			# normalized form has denominator == 1.0
			self.__num = float(num) / float(den)
			self.__den = 1.0
			return
		if (type(num) is InstanceType and
		    num.__class__ is self.__class__) or \
		   (type(den) is InstanceType and
		    den.__class__ is self.__class__):
			# numerator or denominator is rational
			new = num / den
			if type(new) is not InstanceType or \
			   new.__class__ is not self.__class__:
				self.__num = new
				if type(new) is ComplexType:
					self.__den = complex(1)
				else:
					self.__den = 1.0
			else:
				self.__num = new.__num
				self.__den = new.__den
78
		else:
79 80 81
			# make sure numerator and denominator don't
			# have common factors
			# this also makes sure that denominator > 0
82
			g = gcd(num, den)
83 84 85 86 87 88 89 90 91 92 93 94
			self.__num = num / g
			self.__den = den / g
		# try making numerator and denominator of IntType if they fit
		try:
			numi = int(self.__num)
			deni = int(self.__den)
		except (OverflowError, TypeError):
			pass
		else:
			if self.__num == numi and self.__den == deni:
				self.__num = numi
				self.__den = deni
Guido van Rossum's avatar
Guido van Rossum committed
95 96

	def __repr__(self):
97
		return 'Rat(%s,%s)' % (self.__num, self.__den)
98 99

	def __str__(self):
100 101
		if self.__den == 1:
			return str(self.__num)
102
		else:
103
			return '(%s/%s)' % (str(self.__num), str(self.__den))
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

	# a + b
	def __add__(a, b):
		try:
			return rat(a.__num * b.__den + b.__num * a.__den,
				   a.__den * b.__den)
		except OverflowError:
			return rat(long(a.__num) * long(b.__den) +
				   long(b.__num) * long(a.__den),
				   long(a.__den) * long(b.__den))

	def __radd__(b, a):
		return Rat(a) + b

	# a - b
	def __sub__(a, b):
		try:
			return rat(a.__num * b.__den - b.__num * a.__den,
				   a.__den * b.__den)
		except OverflowError:
			return rat(long(a.__num) * long(b.__den) -
				   long(b.__num) * long(a.__den),
				   long(a.__den) * long(b.__den))

	def __rsub__(b, a):
		return Rat(a) - b

	# a * b
	def __mul__(a, b):
		try:
			return rat(a.__num * b.__num, a.__den * b.__den)
		except OverflowError:
			return rat(long(a.__num) * long(b.__num),
				   long(a.__den) * long(b.__den))

	def __rmul__(b, a):
		return Rat(a) * b

	# a / b
	def __div__(a, b):
		try:
			return rat(a.__num * b.__den, a.__den * b.__num)
		except OverflowError:
			return rat(long(a.__num) * long(b.__den),
				   long(a.__den) * long(b.__num))

	def __rdiv__(b, a):
		return Rat(a) / b
Guido van Rossum's avatar
Guido van Rossum committed
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	# a % b
	def __mod__(a, b):
		div = a / b
		try:
			div = int(div)
		except OverflowError:
			div = long(div)
		return a - b * div

	def __rmod__(b, a):
		return Rat(a) % b

	# a ** b
	def __pow__(a, b):
		if b.__den != 1:
			if type(a.__num) is ComplexType:
				a = complex(a)
			else:
				a = float(a)
			if type(b.__num) is ComplexType:
				b = complex(b)
			else:
				b = float(b)
			return a ** b
		try:
			return rat(a.__num ** b.__num, a.__den ** b.__num)
		except OverflowError:
			return rat(long(a.__num) ** b.__num,
				   long(a.__den) ** b.__num)

	def __rpow__(b, a):
		return Rat(a) ** b

	# -a
	def __neg__(a):
		try:
			return rat(-a.__num, a.__den)
		except OverflowError:
			# a.__num == sys.maxint
			return rat(-long(a.__num), a.__den)

	# abs(a)
	def __abs__(a):
		return rat(abs(a.__num), a.__den)

	# int(a)
	def __int__(a):
		return int(a.__num / a.__den)

	# long(a)
	def __long__(a):
		return long(a.__num) / long(a.__den)

	# float(a)
	def __float__(a):
		return float(a.__num) / float(a.__den)

	# complex(a)
	def __complex__(a):
		return complex(a.__num) / complex(a.__den)

	# cmp(a,b)
Guido van Rossum's avatar
Guido van Rossum committed
215
	def __cmp__(a, b):
216
		diff = Rat(a - b)
217
		if diff.__num < 0:
Guido van Rossum's avatar
Guido van Rossum committed
218
			return -1
219
		elif diff.__num > 0:
Guido van Rossum's avatar
Guido van Rossum committed
220
			return 1
221 222
		else:
			return 0
Guido van Rossum's avatar
Guido van Rossum committed
223

224 225
	def __rcmp__(b, a):
		   return cmp(Rat(a), b)
Guido van Rossum's avatar
Guido van Rossum committed
226

227 228 229
	# a != 0
	def __nonzero__(a):
		return a.__num != 0
Guido van Rossum's avatar
Guido van Rossum committed
230

231
	# coercion
Guido van Rossum's avatar
Guido van Rossum committed
232
	def __coerce__(a, b):
233
		return a, Rat(b)
234

235 236 237
def test():
	'''\
	Test function for rat module.
Guido van Rossum's avatar
Guido van Rossum committed
238

239 240 241 242 243 244 245 246
	The expected output is (module some differences in floating
	precission):
	-1
	-1
	0 0L 0.1 (0.1+0j)
	[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
247 248
	(11/10)
	(11/10)
249 250
	1.1
	OK
251
	2 1.5 (3/2) (1.5+1.5j) (15707963/5000000)
252
	2 2 2.0 (2+0j)
Guido van Rossum's avatar
Guido van Rossum committed
253

254 255
	4 0 4 1 4 0
	3.5 0.5 3.0 1.33333333333 2.82842712475 1
256
	(7/2) (1/2) 3 (4/3) 2.82842712475 1
257 258
	(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)
Guido van Rossum's avatar
Guido van Rossum committed
259

260 261 262 263
	3.5 -0.5 3.0 0.75 2.25 -1
	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
264
	(3/2) 1 1.5 (1.5+0j)
Guido van Rossum's avatar
Guido van Rossum committed
265

266
	(7/2) (-1/2) 3 (3/4) (9/4) -1
267
	3.0 0.0 2.25 1.0 1.83711730709 -1
268
	3 0 (9/4) 1 1.83711730709 0
269 270
	(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)
Guido van Rossum's avatar
Guido van Rossum committed
271

272 273 274 275 276 277 278 279 280 281
	(3.5+1.5j) (-0.5+1.5j) (3+3j) (0.75+0.75j) 4.5j -1
	(3+1.5j) 1.5j (2.25+2.25j) (1+1j) (1.18235814075+2.85446505899j) 1
	(3+1.5j) 1.5j (2.25+2.25j) (1+1j) (1.18235814075+2.85446505899j) 1
	(3+3j) 0j 4.5j (1+0j) (-0.638110484918+0.705394566962j) 0
	'''
	print rat(-1L, 1)
	print rat(1, -1)
	a = rat(1, 10)
	print int(a), long(a), float(a), complex(a)
	b = rat(2, 5)
Guido van Rossum's avatar
Guido van Rossum committed
282 283 284 285
	l = [a+b, a-b, a*b, a/b]
	print l
	l.sort()
	print l
286
	print rat(0, 1)
Guido van Rossum's avatar
Guido van Rossum committed
287 288 289
	print a+1
	print a+1L
	print a+1.0
Guido van Rossum's avatar
Guido van Rossum committed
290
	try:
291
		print rat(1, 0)
Guido van Rossum's avatar
Guido van Rossum committed
292 293 294
		raise SystemError, 'should have been ZeroDivisionError'
	except ZeroDivisionError:
		print 'OK'
295 296 297 298 299 300 301 302 303 304
	print rat(2), rat(1.5), rat(3, 2), rat(1.5+1.5j), rat(31415926,10000000)
	list = [2, 1.5, rat(3,2), 1.5+1.5j]
	for i in list:
		print i,
		if type(i) is not ComplexType:
			print int(i), float(i),
		print complex(i)
		print
		for j in list:
			print i + j, i - j, i * j, i / j, i ** j, cmp(i, j)
Guido van Rossum's avatar
Guido van Rossum committed
305

306 307
if __name__ == '__main__':
    test()