fraction.py

Tue, 02 Feb 2016 20:47:47 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 02 Feb 2016 20:47:47 +0200
changeset 167
032c90f1727b
parent 164
e18f73e4c2e2
permissions
-rw-r--r--

Stuff

def gcd(a, b):
	"""
	Calculate the Greatest Common Divisor of a and b.

	Unless b==0, the result will have the same sign as b (so that when
	b is divided by it, the result comes out positive).
	"""
	while b:
		a, b = b, a % b
	return a

class Fraction (object):
	def __init__ (self, numerator, denominator = 1):
		if denominator == 0:
			raise ValueError ('Denominator may not be 0')

		self.numerator = int (numerator)
		self.denominator = int (denominator)
		self.simplify()

	@property
	def real (self):
		if self.numerator.real:
			return Fraction(self.numerator.real, self.denominator.real)
		else:
			return Fraction(0,1)

	@property
	def imag (self):
		if self.numerator.imag:
			return Fraction(self.numerator.imag, self.denominator.imag)
		else:
			return Fraction(0,1)

	def simplify (self):
		divisor = gcd (self.numerator, self.denominator)
		self.numerator //= divisor
		self.denominator //= divisor

	def __repr__ (self):
		return '%s(%d, %d)' % (self.__class__.__name__, self.numerator, self.denominator)

	def __str__ (self):
		if self.numerator > self.denominator:
			if self.numerator % self.denominator == 0:
				return str (self.numerator // self.denominator)
			else:
				return '%d + (%d / %d)' % (self.numerator // self.denominator,
					self.numerator % self.denominator, self.denominator)
		else:
			return str (self.numerator) + '/' + str (self.denominator)

	def __add__ (self, other):
		if isinstance (other, Fraction):
			n1, n2, d1, d2 = (self.numerator, other.numerator, self.denominator, other.denominator)
			return Fraction ((n1 * d2) + (n2 * d1), d1 * d2)
		else:
			return Fraction (self.numerator + (other * self.denominator), self.denominator)

	def __radd__ (self, other):
		return self + other

	def __sub__ (self, other):
		if isinstance (other, Fraction):
			return self.__add__ (Fraction (-other.numerator, other.denominator))
		else:
			return self.__add__ (-other)

	def __rsub__ (self, other):
		return -self + other

	def __mul__ (self, other):
		if isinstance (other, Fraction):
			return Fraction (self.numerator * other.numerator, self.denominator * other.denominator)
		elif isinstance (other, float):
			return (self.numerator / self.denominator) * other
		elif isinstance (other, int):
			return Fraction (self.numerator * int (other), self.denominator)

	def __rmul__ (self, other):
		return self * other

	def __truediv__ (self, other):
		if isinstance (other, Fraction):
			return Fraction (self.numerator * other.denominator, self.denominator * other.numerator)
		else:
			return Fraction (self.numerator, self.denominator * int (other))

	def __rtruediv__ (self, other):
		return other * Fraction (self.denominator, self.numerator)

	def __neg__ (self):
		return Fraction (-self.numerator, self.denominator)

	def __abs__ (self):
		return Fraction (abs (self.numerator), self.denominator)

mercurial