def EffectueSommeFractions(fr1, fr2, s, pre, post): if s == "+": fr = fr1 + fr2 else: fr = fr1 - fr2 cor = [] if fr1.n and fr2.n: ppcm = Arithmetique.ppcm(fr2.d, fr1.d) if abs(fr1.d) - abs(fr2.d): cor.append("%s%s%s%s%s" % (pre, Fractions.TeX(fr1, True, coef=ppcm // abs(fr1.d)), s, Fractions.TeX(fr2, True, coef=ppcm // abs(fr2.d)), post)) if pre.rstrip().endswith('(') and post.lstrip().startswith(')'): pre = pre.rstrip()[:len(pre.rstrip()) - 1] post = post.lstrip()[1:] cor.append("%s%s%s" % (pre, Fractions.TeX(fr, True), post)) if fr.n: frs = Fractions.simplifie(fr) if abs(frs.n) != abs(fr.n): cor.append("%s%s%s" % (pre, Fractions.TeX(frs, True, coef=fr.d // frs.d), post)) cor.append("%s%s%s" % (pre, Fractions.TeX(frs, True), post)) else: cor.append("%s%s%s" % (pre, Fractions.TeX(fr, True), post)) return cor
def __add__(self, fraction): if (isinstance(fraction,int) or isinstance(fraction,float)): fraction=Fractions(fraction) if isinstance(fraction,Fractions): ppcm = Arithmetique.ppcm(self.d, fraction.d) return Fractions((self.n * ppcm) // self.d + (fraction.n * ppcm) // fraction.d, ppcm) else: return fraction+self
def TeXProduit(self, fraction): if self.d < 0: (self.n, self.d) = (-self.n, -self.d) if fraction.d < 0: (fraction.n, fraction.d) = (-fraction.n, -fraction.d) c1 = abs(Arithmetique.pgcd(self.n, fraction.d)) c2 = abs(Arithmetique.pgcd(self.d, fraction.n)) simplifiable = 0 # permet de savoir si on a simplifiée le produit if c1 > 1: n1 = "%s \\times \\cancel{%s}" % (self.n // c1, c1) d2 = "%s \\times \\cancel{%s}" % (fraction.d // c1, c1) else: n1 = self.n d2 = fraction.d if c2 > 1: d1 = "%s \\times \\bcancel{%s}" % (self.d // c2, c2) n2 = "%s \\times \\bcancel{%s}" % (fraction.n // c2, c2) else: d1 = self.d n2 = fraction.n return "%s \\times %s" % (Fractions.TeX(Fractions(n1, d1), signe= None), Fractions.TeX(Fractions(n2, d2), signe=None))
def simplifie(self): # retourne la fraction rendue irréductible pgcd = Arithmetique.pgcd(self.n, self.d) return Fractions(self.n // pgcd, self.d // pgcd)