Add case when numerator is 0

This commit is contained in:
Lafrite 2014-02-21 11:05:25 +01:00
parent 6e1ca2eff8
commit 1abd903c9b

View File

@ -23,6 +23,11 @@ class Fraction(object):
""" """
steps = [] steps = []
if self._num == 0:
steps.append(0)
return steps
if self._denom < 0: if self._denom < 0:
n_frac = Fraction(-self._num, -self._denom) n_frac = Fraction(-self._num, -self._denom)
steps.append(n_frac) steps.append(n_frac)
@ -70,16 +75,12 @@ class Fraction(object):
coef1 = number._denom // gcd_denom coef1 = number._denom // gcd_denom
coef2 = self._denom // gcd_denom coef2 = self._denom // gcd_denom
#steps.append("( {num1} * {coef1} ) / ( {den1} * {coef1} ) + ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2))
steps.append([self._num, coef1, "*", self._denom, coef1, "*", '/', number._num, coef2, "*", number._denom, coef2, "*", "/",'+']) steps.append([self._num, coef1, "*", self._denom, coef1, "*", '/', number._num, coef2, "*", number._denom, coef2, "*", "/",'+'])
com_denom = self._denom * coef1 com_denom = self._denom * coef1
num1 = self._num * coef1 num1 = self._num * coef1
num2 = number._num * coef2 num2 = number._num * coef2
#steps.append("( {num1} + {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom))
steps.append([num1, num2, '+', com_denom, '/']) steps.append([num1, num2, '+', com_denom, '/'])
num = num1 + num2 num = num1 + num2
@ -109,14 +110,12 @@ class Fraction(object):
coef1 = number._denom // gcd_denom coef1 = number._denom // gcd_denom
coef2 = self._denom // gcd_denom coef2 = self._denom // gcd_denom
#steps.append("( {num1} * {coef1} ) / ( {den1} * {coef1} ) - ( {num2} * {coef2} ) / ( {den2} * {coef2} )".format(num1 = self._num, den1 = self._denom, coef1 = coef1, num2 = number._num, den2 = number._denom, coef2 = coef2))
steps.append([self._num, coef1, "*", self._denom, coef1, "*", '/', number._num, coef2, "*", number._denom, coef2, "*", "/",'-']) steps.append([self._num, coef1, "*", self._denom, coef1, "*", '/', number._num, coef2, "*", number._denom, coef2, "*", "/",'-'])
com_denom = self._denom * coef1 com_denom = self._denom * coef1
num1 = self._num * coef1 num1 = self._num * coef1
num2 = number._num * coef2 num2 = number._num * coef2
#steps.append("( {num1} - {num2} ) / {denom}".format(num1 = num1, num2 = num2, denom = com_denom))
steps.append([num1, num2, '-', com_denom, '/']) steps.append([num1, num2, '-', com_denom, '/'])
num = num1 - num2 num = num1 - num2
@ -190,7 +189,7 @@ if __name__ == '__main__':
for i in (f + 1): for i in (f + 1):
print(i) print(i)
print("---------") print("---------")
for i in (f + g): for i in (f - g):
print(i) print(i)
#print("---------") #print("---------")
#for i in (f - g): #for i in (f - g):