Question

I have this portion of a class that takes a whole number and a fraction and add them together.

def __add__(self, g):
       whole_add=self.whole_number + g.whole_number
       numerator = (self.fraction.numerator * g.fraction.denominator ) + (g.fraction.numerator * self.fraction.denominator)
       denominator = self.fraction.denominator * g.fraction.denominator
       f=Fraction(numerator,denominator)
       return '{} and {}'.format(whole_add,f)

fraction_1 = Fraction(3, 4)
fraction_2 = Fraction(2, 3)
mixed_num_1 = MixedNumber(2, fraction_1)
mixed_num_2 = MixedNumber(1, fraction_2)
print(mixed_num_1 + mixed_num_2)

The outcome of this is 3 and 17/12, when it should be 4 and 5/12, I am not sure how to do this, I assume with an if the fraction is >= 1. Any help would be much appreciated

Was it helpful?

Solution

If you are using the fractions library, ou can just sum everything, then take the int() portion of the fraction:

def __add__(self, g):
    summed = sum((self.whole_number, g.whole_number, self.fraction, g.fraction))
    whole = int(summed)
    remainder = summed - whole
    return '{} and {}'.format(whole, remainder)

The Fraction() class implements __add__ for you, you can just sum up integers and Fraction() objects and it all works as it should.

Demo using constants:

>>> from fractions import Fraction
>>> summed = sum((2, 1, Fraction(3, 4), Fraction(2, 3)))
>>> whole = int(summed)
>>> remainder = summed - whole
>>> '{} and {}'.format(whole, remainder)
'4 and 5/12'

One little-known but handy factoid is that Python the int() type has both .numerator and .denominator attributes, that the fractions.Fraction() class makes use of. If you are not using the fractions library, you can make use of that yourself:

def __add__(self, g):
    summed = 0
    for v in (self.whole_number, g.whole_number, self.fraction, g.fraction):
        summed = Fraction(summed.numerator * v.denominator +
                          v.numerator * summed.denominator,
                          summed.denominator * v.denominator)
    whole = summed._numerator // summed._denominator
    remainder = Fraction(summed.numerator * whole.denominator -
                         whole.numerator * summed.denominator,
                         summed.denominator * whole.denominator)
    return '{} and {}'.format(whole, remainder)

OTHER TIPS

One way to "fix" your version would be to deal with the improper fraction directly:

   whole_add=self.whole_number + g.whole_number
   numerator = (self.fraction.numerator * g.fraction.denominator ) + (g.fraction.numerator * self.fraction.denominator)
   denominator = self.fraction.denominator * g.fraction.denominator
   whole_add += numerator // denominator
   numerator -= numerator % denominator
   f=Fraction(numerator,denominator)
   return '{} and {}'.format(whole_add,f)

Although there are more efficient ways of doing this addition more directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top