5.1.2. Fractions
— Nombres rationnels¶
Le module Fractions
définit la classe Fractions
qui permet de
faire des calculs détaillés avec des nombres en écriture fractionnaire.
Un objet Fractions
est construit à partir de son numérateur et son
dénominateur.
-
class
pyromaths.classes.Fractions.
Fraction
(n, d=1, code='')[source]¶ Cette classe crée la notion de fractions. code permet de préciser si une décomposition a pour objectif une mise au même dénominateur “r” ou une simplification “s”
>>> from pyromaths.classes.Fractions import Fraction >>> repr(Fraction(5,6)) Fraction(5, 6) >>> repr(Fraction('x',6)) Fraction("x", 6) >>> repr(Fraction(3.4)) Fraction(34, 10) >>> repr(Fraction(3.0, 4.0)) Fraction(3, 4) >>> repr(Fraction(Fraction(1,3))) Fraction(1, 3)
-
__add__
(*others)[source]¶ object.__add__(other)
p.__add__(q)
est équivalent àp + q
calcule la somme de deux fractions.other peut être une chaîne représentant une fraction, un entier ou un réel.
>>> from pyromaths.classes.Fractions import Fraction >>> Fraction(2,5) + Fraction(2,10) Fraction("2*2", "5*2", "r")+Fraction(2, 10) >>> Fraction(2,20) + Fraction(2,10) Fraction(2, 20)+Fraction("2*2", "10*2", "r") >>> repr(Fraction(5,10) + Fraction(2,10)) Fraction(7, 10) >>> Fraction(5,7) + Fraction(2,10) Fraction("5*10", "7*10", "r")+Fraction("2*7", "10*7", "r")
Param: other Type: Fraction ou string Type renvoyé: Fraction ou string TODO : Attention, 1+3/4 donne 1*4/1*4 + 3/4 à la place de 4/4+3/4. À corriger
-
__invert__
()[source]¶ __invert__(self)
__invert__(p)
est équivalent à~p
calcule l’inverse de la fraction p.>>> from pyromaths.classes.Fractions import Fraction >>> repr(~Fraction(8,27)) Fraction(27, 8)
-
__mul__
(*others)[source]¶ object.__mul__(other)
p.__mul__(q)
est équivalent àp * q
calcule le produit deux fractions.other peut être une chaîne représentant une fraction, un entier ou un réel.
>>> from pyromaths.classes.Fractions import Fraction >>> repr(Fraction(2,5) * Fraction(2,10)) Fraction("2*2", "5*2*5", "s") >>> repr(Fraction(2,5) * 4) Fraction(8, 5) >>> repr(Fraction(63,20) * Fraction(8,27)) Fraction("9*7*4*2", "4*5*9*3", "s") >>> repr(Fraction(24,12) * 12) Fraction("24*12", "12*1", "s") >>> repr(12*Fraction(24,12)) Fraction("12*24", "1*12", "s")
Param: other Type: Fraction ou string Type renvoyé: Fraction ou string
-
__neg__
()[source]¶ __neg__(self)
__neg__(p)
est équivalent à-p
calcule l’opposé de la fraction p.>>> from pyromaths.classes.Fractions import Fraction >>> repr(-Fraction(8,27)) Fraction(-8, 27)
-
__pos__
()[source]¶ __pos__(self)
__pos__(p)
est équivalent à+p
Renvoie la fraction p.>>> from pyromaths.classes.Fractions import Fraction >>> repr(+Fraction(8,27)) Fraction(8, 27)
-
__pow__
(n)[source]¶ __pow__(self, n)
p__pow__(q)
est équivalent àp**q
calcule p à la puissance q.>>> from pyromaths.classes.Fractions import Fraction >>> repr(Fraction(2,3)**4) Fraction(16, 81)
Param: n Type: Integer Type renvoyé: Fraction
-
__radd__
(other)[source]¶ object.__radd__(other)
p.__radd__(q)
est équivalent àp + q
calcule la somme de l’objet p avec la fraction qother peut être une chaîne représentant un entier ou un réel.
>>> from pyromaths.classes.Fractions import Fraction >>> 3 + Fraction(2,5) Fraction("3*5", "1*5", "r")+Fraction(2, 5)
Param: other Type: real ou integer Type renvoyé: string
-
__repr__
()[source]¶ repr(object)
Renvoie une chaîne de caractère représentant une
Fraction
évaluable pour créer unFraction
.>>> from pyromaths.classes.Fractions import Fraction >>> repr(Fraction(5,6)) Fraction(5, 6) >>> repr(Fraction('-72*2', '11*2', 'r')) Fraction("-72*2", "11*2", "r") >>> repr(Fraction('-72*2', '11*2', 's')) Fraction("-72*2", "11*2", "s")
Type renvoyé: string
-
__rmul__
(other)[source]¶ object.__rmul__(other)
p.__rmul__(q)
est équivalent àp * q
calcule le produit de l’objet p par la fraction q.other peut être un entier ou un réel.
Pour plus de détails, voir
__radd__()
-
__rsub__
(other)[source]¶ object.__rsub__(other)
p.__rsub__(q)
est équivalent àp - q
calcule la différence de l’objet p par la fraction q.other peut être un entier ou un réel.
Pour plus de détails, voir
__radd__()
-
__rtruediv__
(other)[source]¶ object.__rtruediv__(other)
p.__rtruediv__(q)
est équivalent àp / q
calcule le produit de l’objet p par la fraction q.other peut être un entier ou un réel.
>>> from pyromaths.classes.Fractions import Fraction >>> repr(1/Fraction(1,3)) '1*Fraction(3, 1)'
Pour plus de détails, voir
__radd__()
-
__str__
()[source]¶ str(object)
- Renvoie une version LaTeX de la
Fraction
. >>> from pyromaths.classes.Fractions import Fraction >>> str(Fraction(8,1)) 8 >>> str(Fraction(5,6)) \dfrac{5}{6} >>> str(Fraction('-5*2', '3*2', 'r')) \dfrac{-5_{\times 2}}{3_{\times 2}} >>> str(Fraction('5*-7*2', '11*2*5', 's')) \dfrac{\cancel{5}\times \left( -7\right) \times \cancel{2}}{11\times \cancel{2}\times \cancel{5}} >>> str(Fraction('-144', '22', 's')) \dfrac{-72\times \cancel{2}}{11\times \cancel{2}} >>> from pyromaths.classes.SquareRoot import SquareRoot >>> str(Fraction(SquareRoot([[-10, None], [-1, 80]]), -2)) \dfrac{-10-\sqrt{80}}{-2} >>> str(Fraction(SquareRoot([[-10, None], [-4, 5]]), -2, 's')) \dfrac{\left( 5+2\,\sqrt{5}\right) \times \cancel{-2}}{1\times \cancel{-2}}
Type renvoyé: string - Renvoie une version LaTeX de la
-
__sub__
(*others)[source]¶ object.__sub__(other)
p.__sub__(q)
est équivalent àp - q
calcule la différence de deux fractions.other peut être une chaîne représentant une fraction, un entier ou un réel.
Pour plus de détails, voir
__add__()
-
__truediv__
(other)[source]¶ object.__div__(other)
p.__div__(q)
est équivalent àp / q
calcule le quotient de deux fractions.other peut être une chaîne représentant une fraction, un entier ou un réel.
>>> from pyromaths.classes.Fractions import Fraction >>> repr(Fraction(2,5) / Fraction(10,2)) 'Fraction(2, 5)*Fraction(2, 10)' >>> repr(Fraction(2,5) / 4) 'Fraction(2, 5)*Fraction(1, 4)'
Param: other Type: Fraction ou string Type renvoyé: Fraction ou string
-
__weakref__
¶ list of weak references to the object (if defined)
-
decompose
()[source]¶ decompose(object)
Retourne une décomposition de la fraction afin de la simplifier
>>> from pyromaths.classes.Fractions import Fraction >>> repr(Fraction.decompose(Fraction(8,20))) Fraction("2*4", "5*4", "s")
Paramètres: type – Fraction Type renvoyé: Fraction
-
reduit
()[source]¶ reduit(object)
Retourne une version réduite de la fraction, ie calcule le numérateur et le dénominateur
>>> from pyromaths.classes.Fractions import Fraction >>> repr(Fraction.reduit(Fraction(2*4,5*4))) Fraction(8, 20) >>> repr(Fraction.reduit(Fraction('2*4', '5*4'))) Fraction(8, 20)
Paramètres: type – Fraction Type renvoyé: Fraction
-
simplifie
()[source]¶ simplifie(object)
Retourne une version irréductible de la fraction
>>> from pyromaths.classes.Fractions import Fraction >>> repr(Fraction.simplifie(Fraction(2*4,5*4))) Fraction(2, 5) >>> repr(Fraction.simplifie(Fraction('2*4','5*4'))) Fraction(2, 5)
Paramètres: type – Fraction Type renvoyé: Fraction
-
traitement
(final=False)[source]¶ traitement(object,self)
Finit la mise au même dénominateur ou la simplification de la fraction. Si final est vrai, alors essaie de simplifier la fraction.
>>> from pyromaths.classes.Fractions import Fraction >>> repr(Fraction("3*4", "3*7", "r").traitement()) Fraction(12, 21) >>> repr(Fraction("3*4", "3*7", "s").traitement()) Fraction(4, 7) >>> repr(Fraction(12, 21).traitement()) Fraction(12, 21) >>> repr(Fraction(12, 21).traitement(True)) Fraction("4*3", "7*3", "s")
Param: final Type: boolean Type renvoyé: Fraction
-