summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/profcollege/PfCCalculsFractions.tex
blob: 94e3a8d120174a4ab55b8d1899433a26cf2debf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
\setKVdefault[CalculsFractions]{Couleur=Crimson}

\NewDocumentCommand\tofrac{m}{%
  \IfDecimal{#1}{#1}{%
    \StrBefore{#1}{/}[\PfCnum]%
    \StrBehind{#1}{/}[\PfCden]%
    \frac{\PfCnum}{\PfCden}%
  }%
}%

\NewDocumentCommand\AddFraction{soommo}{%
  \useKVdefault[CalculsFractions]%
  \setKV[CalculsFractions]{#2}%
  \IfBooleanTF{#1}{%
    \xdef\PfCFractionOp{-}%
  }{%
    \xdef\PfCFractionOp{+}%
  }%
  \IfDecimal{#4}{%
    \xdef\PfCNumA{#4}%
    \xdef\PfCNumB{1}%
  }{%
    \StrCut{#4}{/}\PfCNumA\PfCNumB%
  }%
  \IfDecimal{#5}{%
    \xdef\PfCNumC{#5}%
    \xdef\PfCNumD{1}%
  }{%
    \StrCut{#5}{/}\PfCNumC\PfCNumD%
  }
  \xdef\PfCAddFoo{\PfCNumA/\PfCNumB,\PfCNumC/\PfCNumD}%
  \setsepchar[*]{,*/}\ignoreemptyitems%
  \readlist*\ListeFractions{\PfCAddFoo}%
  \reademptyitems
  \setsepchar{,}%
  % Partie calculs
  \PGCD{\ListeFractions[1,2]}{\ListeFractions[2,2]}%
  \xdef\PfCPremierMul{\fpeval{\ListeFractions[2,2]/\the\pgcd}}%
  \xdef\PfCDeuxiemeMul{\fpeval{\ListeFractions[1,2]/\the\pgcd}}%
  \xdef\PfCAddNumFin{\fpeval{(\ListeFractions[1,1])*(\PfCPremierMul)\PfCFractionOp(\ListeFractions[2,1])*(\PfCDeuxiemeMul)}}%
  \xdef\PfCAddDenoFin{\fpeval{(\ListeFractions[1,2])*(\PfCPremierMul)}}%
  \PGCD{\fpeval{abs(\PfCAddNumFin)}}{\fpeval{abs(\PfCAddDenoFin)}}\xdef\PfCAddPgcd{\the\pgcd}%
  \xdef\PfCAddNumSimp{\fpeval{\PfCAddNumFin/\PfCAddPgcd}}%
  \xdef\PfCAddDenoSimp{\fpeval{\PfCAddDenoFin/\PfCAddPgcd}}%
  \xdef\PfCAddSumSimp{\PfCAddNumSimp/\PfCAddDenoSimp}%
  \IfValueT{#6}{\xdef#6{\PfCAddSumSimp}}%
  % Partie affichage
  \ensuremath{%
    \IfValueTF{#3}{%
      \ifnum#3=4\relax%
      \tofrac{\PfCAddSumSimp}%
      \else%
      \ifnum#3=3\relax%
      \SSimpliTest{\fpeval{abs(\PfCAddNumFin)}}{\fpeval{abs(\PfCAddDenoFin)}}\ifthenelse{\boolean{Simplification}}{\tofrac{\PfCAddNumFin/\PfCAddDenoFin}}{\tofrac{\PfCAddSumSimp}}%
      \else%
      \ifnum#3=1\relax%
      \ifnum\PfCPremierMul>1\relax%
      \frac{\ListeFractions[1,1]_{\times\num{\PfCPremierMul}}}{\ListeFractions[1,2]_{\times\num{\PfCPremierMul}}}
      \else%
      \tofrac{#4}%
      \fi%
      \PfCFractionOp%
      \ifnum\PfCDeuxiemeMul>1\relax%
      \frac{\ListeFractions[2,1]_{\times\num{\PfCDeuxiemeMul}}}{\ListeFractions[2,2]_{\times\num{\PfCDeuxiemeMul}}}%
      \else%
      \tofrac{#5}%
      \fi%
      \else%
      \ifnum#3=2\relax%
      \frac{\num{\fpeval{\ListeFractions[1,1]*\PfCPremierMul}}}{\num{\fpeval{\ListeFractions[1,2]*\PfCPremierMul}}}\PfCFractionOp\frac{\num{\fpeval{\ListeFractions[2,1]*\PfCDeuxiemeMul}}}{\num{\fpeval{\ListeFractions[2,2]*\PfCDeuxiemeMul}}}%
      \fi%
      \fi%
      \fi%
      \fi%
    }{\tofrac{#4}\PfCFractionOp\tofrac{#5}
    }%
  }%
}%

\NewDocumentCommand\SousFraction{oomm}{%
  \AddFraction*[#1][#2]{#3}{#4}%
}%

\NewDocumentCommand\MulFraction{oommo}{%
  \useKVdefault[CalculsFractions]%
  \setKV[CalculsFractions]{#1}%
  \IfDecimal{#3}{%
    \xdef\PfCNumA{#3}%
    \xdef\PfCNumB{1}%
  }{%
    \StrCut{#3}{/}{\PfCNumA}{\PfCNumB}%
  }%
  \IfDecimal{#4}{%
    \xdef\PfCNumC{#4}%
    \xdef\PfCNumD{1}%
  }{%
    \StrCut{#4}{/}{\PfCNumC}{\PfCNumD}%
  }%
  \xdef\PfCMulFoo{\PfCNumA/\PfCNumB,\PfCNumC/\PfCNumD}%
  \setsepchar[*]{,*/}\ignoreemptyitems%
  \readlist*\ListeFractions{\PfCMulFoo}%
  \reademptyitems
  \setsepchar{,}%
  % Calculs
  \xdef\PfCMulNumFin{\fpeval{(\ListeFractions[1,1])*(\ListeFractions[2,1])}}
  \xdef\PfCMulDenoFin{\fpeval{\ListeFractions[1,2]*\ListeFractions[2,2]}}
  \PGCD{\fpeval{abs(\PfCMulNumFin)}}{\fpeval{abs(\PfCMulDenoFin)}}\xdef\PfCMulPgcd{\pgcd}%
  \xdef\PfCMulNumSimp{\fpeval{\PfCMulNumFin/\PfCMulPgcd}}%
  \xdef\PfCMulDenoSimp{\fpeval{\PfCMulDenoFin/\PfCMulPgcd}}%
  \xdef\PfCMulPdtSimp{\PfCMulNumSimp/\PfCMulDenoSimp}%
  \IfValueT{#5}{\xdef#5{\PfCMulPdtSimp}}%
  \ensuremath{%
    \IfValueTF{#2}{%
      \ifnum#2=3\relax
      \tofrac{\PfCMulPdtSimp}%
      \else
      \ifnum#2=2\relax%
      \SSimpliTest{\PfCMulNumFin}{\PfCMulDenoFin}\ifthenelse{\boolean{Simplification}}{\frac{\ifnum\PfCMulNumFin<0\relax-\fi\Decomposition[Longue]{\fpeval{abs(\PfCMulNumFin)}}}{\ifnum\PfCMulDenoFin<0\relax-\fi\Decomposition[Longue]{\fpeval{abs(\PfCMulDenoFin)}}}}{\tofrac{\PfCMulPdtSimp}}%
      \else%
      \ifnum#2=1\relax%
       \frac{\ListeFractions[1,1]\times\ifnum\ListeFractions[2,1]<0\relax(\fi\ListeFractions[2,1]\ifnum\ListeFractions[2,1]<0\relax)\fi}{\ListeFractions[1,2]\times\ifnum\ListeFractions[2,2]<0\relax(\fi\ListeFractions[2,2]\ifnum\ListeFractions[2,2]<0\relax)\fi}
      \fi
      \fi
      \fi
    }{\tofrac{#3}\times\tofrac{#4}
    }
  }%
}%

\NewDocumentCommand\DivFraction{oommo}{%
  \useKVdefault[CalculsFractions]%
  \setKV[CalculsFractions]{#1}%
  \IfDecimal{#3}{%
    \xdef\PfCNumA{#3}%
    \xdef\PfCNumB{1}%
  }{%
    \StrCut{#3}{/}{\PfCNumA}{\PfCNumB}%
  }%
  \IfDecimal{#4}{%
    \xdef\PfCNumC{#4}%
    \xdef\PfCNumD{1}%
  }{%
    \StrCut{#4}{/}{\PfCNumC}{\PfCNumD}%
  }%
  \xdef\PfCDivFoo{\PfCNumA/\PfCNumB,\PfCNumC/\PfCNumD}%
  \setsepchar[*]{,*/}\ignoreemptyitems%
  \readlist*\ListeFractions{\PfCDivFoo}%
  \reademptyitems%
  \setsepchar{,}%
  % Calculs
  \xdef\PfCDivNumFin{\fpeval{(\ListeFractions[1,1])*(\ListeFractions[2,2])}}%
  \xdef\PfCDivDenoFin{\fpeval{\ListeFractions[1,2]*\ListeFractions[2,1]}}%
  \PGCD{\fpeval{abs(\PfCDivNumFin)}}{\fpeval{abs(\PfCDivDenoFin)}}\xdef\PfCDivPgcd{\pgcd}%
  \xdef\PfCDivNumSimp{\fpeval{\PfCDivNumFin/\PfCDivPgcd}}%
  \xdef\PfCDivDenoSimp{\fpeval{\PfCDivDenoFin/\PfCDivPgcd}}%
  \xdef\PfCDivPdtSimp{\PfCDivNumSimp/\PfCDivDenoSimp}%
  \IfValueT{#5}{\xdef#5{\PfCDivPdtSimp}}%
  \ensuremath{%
    \IfValueTF{#2}{%
      \ifnum#2=4\relax
      \tofrac{\PfCDivPdtSimp}%
      \else\ifnum#2=3\relax
      \MulFraction[][\fpeval{#2-1}]{\ListeFractions[1,1]/\ListeFractions[1,2]}{\ListeFractions[2,2]/\ListeFractions[2,1]}%
      \else
      \ifnum#2=2\relax%
      \MulFraction[][\fpeval{#2-1}]{\ListeFractions[1,1]/\ListeFractions[1,2]}{\ListeFractions[2,2]/\ListeFractions[2,1]}%
      \else%
      \ifnum#2=1\relax%
      \tofrac{\ListeFractions[1,1]/\ListeFractions[1,2]}\times\tofrac{\ListeFractions[2,2]/\ListeFractions[2,1]}
      \fi
      \fi
      \fi
      \fi
    }{\tofrac{#3}\div\tofrac{#4}
    }
  }%
}%