summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/profcollege/latex/PfCDecDeci.tex
blob: 3e9cc7d1c8efb59390639ac7d3dc513d2eada7ad (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
%%%
% Décomposition décimale
%%
\setKVdefault[DecompDeci]{Parentheses,Colore=false,CouleurU=Blue,CouleurD=Red,CouleurC=Green,Fleches=false,Details=false}

\newcounter{PfCNbDecDeci}
\setcounter{PfCNbDecDeci}{0}

\NewDocumentCommand\DecompositionDecimale{o m}{%
  \stepcounter{PfCNbDecDeci}%
  \useKVdefault[DecompDeci]%
  \setKV[DecompDeci]{#1}%
  % On cherche si le nombre est décimal ou entier
  \StrCount{#2}{.}[\PfCNombrePoint]%
  \xintifboolexpr{\PfCNombrePoint==0}{%
    % on compte la longueur.
    \StrLen{#2}[\PfCNombrePuisMax]%
    \xdef\PfCRetiensPuis{0}%
    % On cherche le nombre de 0 terminaux.
    \xintFor* ##1 in{\xintSeq{1}{\PfCNombrePuisMax}}\do{%
      \xintifboolexpr{\fpeval{#2/(10^##1)}==\fpeval{round(#2/(10^##1))}}{%
        \xdef\PfCRetiensPuis{\fpeval{\PfCRetiensPuis+1}}%
      }{}%
    }%
    % On cherche un affichage du style \num de \siunitx
    \modulo{\fpeval{\PfCNombrePuisMax}}{3}%
    \xintifboolexpr{\remainder==0}{%
      \xdef\PfCDecDeciEcart{0}
    }{\xintifboolexpr{\remainder==1}{\xdef\PfCDecDeciEcart{1}}{\xdef\PfCDecDeciEcart{2}}}
    \ifboolKV[DecompDeci]{Fleches}{%
      \colorlet{oldcolor}{black}%
      % On affiche
      \begin{center}
        \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax}}}\do{%
          \StrChar{#2}{##1}[\PfCNombreChiffre]%
          \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##1}}{3}%
          \xintifboolexpr{\remainder==0}{%
            \color{\useKV[DecompDeci]{CouleurU}}\tikzmarknode[anchor=south,inner ysep=2pt]{DDA-\thePfCNbDecDeci-##1}{\PfCNombreChiffre}{}\xintifForLast{}{\,\color{oldcolor}}}{%
            \xintifboolexpr{\remainder==1}{%
              \color{\useKV[DecompDeci]{CouleurC}}\tikzmarknode[anchor=south,inner ysep=2pt]{DDA-\thePfCNbDecDeci-##1}{\PfCNombreChiffre}{}}{%
              \color{\useKV[DecompDeci]{CouleurD}}\tikzmarknode[anchor=south,inner ysep=2pt]{DDA-\thePfCNbDecDeci-##1}{\PfCNombreChiffre}{}%
            }%
          }%
        }\color{oldcolor}%
        
        \vspace*{3em}
        
        \ifboolKV[DecompDeci]{Details}{%
          \ensuremath{%
            \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
              \StrChar{#2}{##1}[\PfCNombreChiffre]
              \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##1}}{3}%
              \colorlet{oldcolor}{.}
              \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}}{%
                \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurC}}}{%
                  \color{\useKV[DecompDeci]{CouleurD}}%
                }%
              }%
              \xintifboolexpr{\PfCNombreChiffre==0}{}{%
                \tikzmarknode[anchor=north,inner ysep=2pt]{DDC-\thePfCNbDecDeci-##1}{\boxed{\num{\fpeval{\PfCNombreChiffre*10^(\PfCNombrePuisMax-##1)}}}}\color{oldcolor}%
                \xintifForLast{}{+}%
              }%
            }%
          }%

          \vspace*{3em}
          
        }{}%
        
        \ensuremath{%
          \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
            \StrChar{#2}{##1}[\PfCNombreChiffre]
            \xintifboolexpr{\PfCNombreChiffre==0}{}{%
              \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##1}}{3}%
              \colorlet{oldcolor}{.}
              \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}}{%
                \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurC}}}{%
                  \color{\useKV[DecompDeci]{CouleurD}}%
                }%
              }%
              \tikzmarknode[anchor=north,inner ysep=2pt]{DDB-\thePfCNbDecDeci-##1}{\boxed{\PfCNombreChiffre\times\num{\fpeval{10^(\PfCNombrePuisMax-##1)}}}}{}\color{oldcolor}%
              \xintifForLast{}{+}%
            }%
          }%
        }%
      \end{center}
      \begin{tikzpicture}[remember picture,overlay]
        \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
          \StrChar{#2}{##1}[\PfCNombreChiffre]%
          \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##1}}{3}%
          \xintifboolexpr{\remainder==0}{%
            \colorlet{PfCDecDeciColor}{\useKV[DecompDeci]{CouleurU}}}{%
            \xintifboolexpr{\remainder==1}{%
              \colorlet{PfCDecDeciColor}{\useKV[DecompDeci]{CouleurC}}}{%
              \colorlet{PfCDecDeciColor}{\useKV[DecompDeci]{CouleurD}}%
            }%
          }%
          \ifboolKV[DecompDeci]{Details}{%
            \xintifboolexpr{\PfCNombreChiffre>0}{%
              \draw[-stealth,PfCDecDeciColor] (pic cs:DDA-\thePfCNbDecDeci-##1) to (pic cs:DDC-\thePfCNbDecDeci-##1);
              \node (DDD-\thePfCNbDecDeci-##1) at ($(pic cs:DDC-\thePfCNbDecDeci-##1)!0.35!(pic cs:DDB-\thePfCNbDecDeci-##1)$) {};
              \draw[-stealth,PfCDecDeciColor] (DDD-\thePfCNbDecDeci-##1) to (pic cs:DDB-\thePfCNbDecDeci-##1);
            }{}%
          }{%
            \xintifboolexpr{\PfCNombreChiffre>0}{%
              \draw[-stealth,PfCDecDeciColor] (pic cs:DDA-\thePfCNbDecDeci-##1) to (pic cs:DDB-\thePfCNbDecDeci-##1);
            }{}%
          }%
        }%
      \end{tikzpicture}
    }{%
      \ifboolKV[DecompDeci]{Colore}{%
        \colorlet{oldcolor}{black}%
        \ensuremath{%
          \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax}}}\do{%
            \StrChar{#2}{##1}[\PfCNombreChiffre]%
            \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##1}}{3}%
            \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}\PfCNombreChiffre\xintifForLast{}{\,\color{oldcolor}}}{%
              \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurC}}\PfCNombreChiffre}{%
                \color{\useKV[DecompDeci]{CouleurD}}\PfCNombreChiffre%
              }%
            }%
          }\color{oldcolor}%
          =        
          \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
            \StrChar{#2}{##1}[\PfCNombreChiffre]
            \xintifboolexpr{\PfCNombreChiffre==0}{}{%
              \modulo{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis-##1}}{3}
              \colorlet{oldcolor}{.}
              \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}}{%
                \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurD}}}{%
                  \color{\useKV[DecompDeci]{CouleurC}}%
                }%
              }%
              \boxed{\PfCNombreChiffre\times\num{\fpeval{10^(\PfCNombrePuisMax-##1)}}}\color{oldcolor}%
              \xintifForLast{}{+}%
            }%
          }%
        }
      }{%
        \ensuremath{%
          \num{#2}=
          \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
            \StrChar{#2}{##1}[\PfCNombreChiffre]
            \xintifboolexpr{\PfCNombreChiffre==0}{}{%
              \ifboolKV[DecompDeci]{Parentheses}{\left(}{}\PfCNombreChiffre\times\num{\fpeval{10^(\PfCNombrePuisMax-##1)}}\ifboolKV[DecompDeci]{Parentheses}{\right)}{}%
              \xintifForLast{}{+}%
            }%
          }%
        }%
      }%
    }%
  }{%partie avec les nombres décimaux
    \StrDel{#2}{.}[\PfCRetiensNombreEntier]%
    \StrLen{#2}[\PfCNombrePuisMax]%
    \xdef\PfCNombrePuisMax{\fpeval{\PfCNombrePuisMax-1}}%
    \StrPosition{#2}{.}[\PfCPositionPoint]%
    \xdef\PfCPuisDeci{\fpeval{\PfCNombrePuisMax-\PfCPositionPoint}}%
    \xdef\PfCRetiensPuis{0}%
    % On cherche le nombre de 0 terminaux.
    \xintFor* ##1 in{\xintSeq{1}{\PfCNombrePuisMax}}\do{%
      \xintifboolexpr{\fpeval{(#2*10^(\PfCPuisDeci+1))/(10^##1)}==\fpeval{round((#2*10^(\PfCPuisDeci+1))/(10^##1))}}{%
        \xdef\PfCRetiensPuis{\fpeval{\PfCRetiensPuis+1}}%
      }{}%
    }%
    \ensuremath{%
      \num{#2}=
      \xintFor* ##1 in {\xintSeq{1}{\PfCNombrePuisMax-\PfCRetiensPuis}}\do{%
        \StrChar{\PfCRetiensNombreEntier}{##1}[\PfCNombreChiffre]
        \xintifboolexpr{\PfCNombreChiffre==0}{}{%
          \ifboolKV[DecompDeci]{Parentheses}{\left(}{}\PfCNombreChiffre\times\num{\fpeval{10^(\PfCNombrePuisMax-##1-\PfCPuisDeci-1)}}\ifboolKV[DecompDeci]{Parentheses}{\right)}{}%
          \xintifForLast{}{+}%
        }%
      }%
    }%
  }%
}%