Debian Codegraph @

texmf-dist/tex/generic/pgf/math/pgfmathfunctions.base.code.tex

raw
1 % Copyright 2019 by Mark Wibrow
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Public License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9 %
10 % This file provides basic macros for converting between bases.
11 %
12 % Version 1.4142135 8/11/2008
13
14 % Base conversion operations for the parser.
15 %
16 \pgfmathdeclarefunction{bin}{1}{%
17 \begingroup%
18 \afterassignment\pgfmath@gobbletilpgfmath@%
19 \c@pgfmath@counta=#1\relax\pgfmath@%
20 \expandafter\pgfmathbasetobase\expandafter\pgfmathresult\expandafter{\the\c@pgfmath@counta}{10}{2}%
21 \pgfmath@smuggleone\pgfmathresult%
22 \endgroup%
23 }
24 \pgfmathdeclarefunction{hex}{1}{%
25 \begingroup%
26 \afterassignment\pgfmath@gobbletilpgfmath@%
27 \c@pgfmath@counta=#1\relax\pgfmath@%
28 \expandafter\pgfmathbasetobase\expandafter\pgfmathresult\expandafter{\the\c@pgfmath@counta}{10}{16}%
29 \pgfmath@smuggleone\pgfmathresult%
30 \endgroup%
31 }
32
33 \pgfmathdeclarefunction{Hex}{1}{%
34 \begingroup%
35 \afterassignment\pgfmath@gobbletilpgfmath@%
36 \c@pgfmath@counta=#1\relax\pgfmath@%
37 \expandafter\pgfmathbasetoBase\expandafter\pgfmathresult\expandafter{\the\c@pgfmath@counta}{10}{16}%
38 \pgfmath@smuggleone\pgfmathresult%
39 \endgroup%
40 }
41
42 \pgfmathdeclarefunction{oct}{1}{%
43 \begingroup%
44 \afterassignment\pgfmath@gobbletilpgfmath@%
45 \c@pgfmath@counta=#1\relax\pgfmath@%
46 \expandafter\pgfmathbasetobase\expandafter\pgfmathresult\expandafter{\the\c@pgfmath@counta}{10}{8}%
47 \pgfmath@smuggleone\pgfmathresult%
48 \endgroup%
49 }
50
51 % Additional macros:
52
53 % \pgfmathbasetodec
54 %
55 % Convert a representation of an integer from
56 % the specified base to base 10.
57 %
58 % #1 - a macro to store the result.
59 % #2 - the representation of a number (i.e. not a register)
60 % #3 - the current base.
61 %
62 % e.g.
63 %
64 % \pgfmathbasetodec\mynumber{10111}{2}
65 %
66 % \mynumber <- 23
67 %
68 \def\pgfmathbasetodec#1#2#3{%
69 \pgfmath@checknumber{#2}%
70 \pgfmath@checkbase{#3}%
71 \def\pgfmath@base{#3}%
72 \def\pgfmath@base@macro{#1}%
73 \c@pgfmath@counta=1\relax%
74 \let\pgfmath@reversed=\pgfmath@empty%
75 \expandafter\pgfmathbasetodec@#2\pgfmath@base@stop}
76
77 \def\pgfmathbasetodec@#1{%
78 \ifx#1\pgfmath@base@stop%
79 \c@pgfmath@countb=0%
80 \divide\c@pgfmath@counta by\pgfmath@base\relax%
81 \expandafter\pgfmathbasetodec@@%
82 \else%
83 \edef\pgfmath@reversed{\pgfmath@reversed#1}%
84 \expandafter\multiply\expandafter\c@pgfmath@counta\pgfmath@base\relax%
85 \expandafter\pgfmathbasetodec@%
86 \fi}
87
88 \def\pgfmathbasetodec@@{\expandafter\pgfmathbasetodec@@@\pgfmath@reversed\pgfmathbasetodec}
89
90 \def\pgfmathbasetodec@@@#1{%
91 \ifx\pgfmathbasetodec#1\relax%
92 \expandafter\edef\pgfmath@base@macro{\the\c@pgfmath@countb}%
93 \let\pgfmath@next=\pgfmathbasetodec@@@@%
94 \else%
95 \chardef\pgfmath@charnum`#1\relax%
96 \c@pgfmath@countc=\pgfmath@charnum\relax%
97 \ifnum\c@pgfmath@countc>96\relax%
98 \advance\c@pgfmath@countc by-87\relax%
99 \else
100 \ifnum\c@pgfmath@countc>64\relax%
101 \advance\c@pgfmath@countc by-55\relax%
102 \else
103 \advance\c@pgfmath@countc by-48\relax%
104 \fi%
105 \fi%
106 \ifnum\c@pgfmath@countc<\pgfmath@base\relax%
107 \multiply\c@pgfmath@countc by\c@pgfmath@counta%
108 \advance\c@pgfmath@countb by\c@pgfmath@countc%
109 \divide\c@pgfmath@counta by\pgfmath@base\relax%
110 \let\pgfmath@next\pgfmathbasetodec@@@%
111 \else
112 \pgfmath@error{Digit `#1' invalid for base \pgfmath@base}{}%
113 \let\pgfmath@next=\relax%
114 \fi%
115 \fi%
116 \pgfmath@next}
117 \def\pgfmathbasetodec@@@@{%
118 \expandafter\pgfmath@ensurenumberlength\expandafter{\pgfmath@base@macro}%
119 }
120
121 % \pgfmathdectobase
122 %
123 % Convert a representation of an integer from
124 % base 10 to the specified base. Letters for bases
125 % greater than 10 are in lowercase.
126 %
127 % #1 - a macro to store the result.
128 % #2 - a number in base 10 (in a macro, not a register)
129 % #3 - the required base.
130 %
131 % e.g.
132 %
133 % \pgfmathdectobase\mynumber{127}{16}
134 %
135 % \mynumber <- 7f
136 %
137 \def\pgfmathdectobase#1#2#3{%
138 \pgfmath@checknumber{#2}%
139 \pgfmath@checkbase{#3}%
140 \c@pgfmath@counta=#2\relax%
141 \let#1=\pgfmath@empty%
142 \pgfmathloop
143 \ifnum\c@pgfmath@counta>0\relax%
144 \c@pgfmath@countb=\c@pgfmath@counta%
145 \divide\c@pgfmath@countb by#3\relax%
146 \multiply\c@pgfmath@countb by-#3\relax%
147 \advance\c@pgfmath@countb by\c@pgfmath@counta%
148 \edef#1{\csname pgfmath@lowercase digit@\the\c@pgfmath@countb\endcsname#1}%
149 \divide\c@pgfmath@counta by#3\relax%
150 \repeatpgfmathloop%
151 \pgfmath@ensurenumberlength{#1}}
152
153 % \pgfmathdectoBase
154 %
155 % Convert a representation of an integer from
156 % base 10 to the specified base. Letters for bases
157 % greater than 10 are in uppercase.
158 %
159 % #1 - a macro to store the result.
160 % #2 - a number in base 10 (in a macro, not a register)
161 % #3 - the required base.
162 %
163 % e.g.
164 %
165 % \pgfmathdectoBase\mynumber{127}{16}
166 %
167 % \mynumber <- 7F
168 %
169 \def\pgfmathdectoBase#1#2#3{%
170 \pgfmath@checkbase{#3}%
171 \pgfmath@checknumber{#2}%
172 \c@pgfmath@counta=#2\relax%
173 \let#1=\pgfmath@empty%
174 \pgfmathloop
175 \ifnum\c@pgfmath@counta>0\relax%
176 \c@pgfmath@countb=\c@pgfmath@counta%
177 \divide\c@pgfmath@countb by#3\relax%
178 \multiply\c@pgfmath@countb by-#3\relax%
179 \advance\c@pgfmath@countb by\c@pgfmath@counta%
180 \edef#1{\csname pgfmath@uppercase digit@\the\c@pgfmath@countb\endcsname#1}%
181 \divide\c@pgfmath@counta by#3\relax%
182 \repeatpgfmathloop%
183 \pgfmath@ensurenumberlength{#1}}
184
185 \def\pgfmath@base@digits@create{%
186 \def\pgfmath@base@digit@style{lowercase digit}%
187 \c@pgfmath@counta=0\relax%
188 \pgfmath@base@digits@create@0123456789abcdefghijklmnopqrstuvwxyz\pgfmath@base@digits@create%
189 \def\pgfmath@base@digit@style{uppercase digit}%
190 \c@pgfmath@counta=0\relax%
191 \pgfmath@base@digits@create@0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\pgfmath@base@digits@create}
192
193 \def\pgfmath@base@digits@create@#1{%
194 \ifx\pgfmath@base@digits@create#1\relax%
195 \else%
196 \expandafter\pgfmath@def\expandafter{\pgfmath@base@digit@style}{\the\c@pgfmath@counta}{#1}%
197 \advance\c@pgfmath@counta by1\relax%
198 \expandafter\pgfmath@base@digits@create@%
199 \fi}
200 \pgfmath@base@digits@create
201
202 % \pgfmathbasetobase
203 %
204 % Convert a representation of an integer from
205 % between the specified bases. Letters for target
206 % bases greater than 10 are in uppercase.
207 %
208 % #1 - a macro to store the result.
209 % #2 - a number (in a macro, not a register)
210 % #3 - the source base.
211 % #4 - the target base.
212 %
213 % e.g.
214 %
215 % \pgfmathbasetobase\mynumber{4321}{5}{9}
216 %
217 % \mynumber <- 721
218 %
219 \def\pgfmathbasetobase#1#2#3#4{%
220 \pgfmathbasetodec{\pgfmath@base@temp}{#2}{#3}%
221 \pgfmathdectobase{#1}{\pgfmath@base@temp}{#4}%
222 }
223
224 % \pgfmathbasetobase
225 %
226 % Convert a representation of an integer from
227 % between the specified bases. Letters for target
228 % bases greater than 10 are in uppercase.
229 %
230 % #1 - a macro to store the result.
231 % #2 - a number (in a macro, not a register)
232 % #3 - the source base.
233 % #4 - the target base.
234 %
235 % e.g.
236 %
237 % \pgfmathbasetobase\mynumber{1234}{5}{12}
238 %
239 % \mynumber <- 142
240 %
241 \def\pgfmathbasetoBase#1#2#3#4{%
242 \pgfmathbasetodec{\pgfmath@base@temp}{#2}{#3}%
243 \pgfmathdectoBase{#1}{\pgfmath@base@temp}{#4}%
244 }
245
246 \def\pgfmath@checkbase#1{%
247 \ifnum#1<2\relax%
248 \pgfmath@error{Cannot process numbers in base `#1'.}{}%
249 \else%
250 \ifnum#1>36\relax%
251 \pgfmath@error{Cannot process numbers in base `#1'.}{}%
252 \fi%
253 \fi}
254
255 \def\pgfmath@checknumber#1{%
256 \expandafter\pgfmath@checknumber@#1\pgfmath@}
257 \def\pgfmath@checknumber@#1#2\pgfmath@{%
258 \ifx#1-%
259 \pgfmath@error{Cannot process negative numbers.}{}%
260 \fi}
261
262 % \pgfmath@ensurenumberlength
263 %
264 % Internal macro for making a representation of a number have
265 % a specific length, byte prefixing zeros to the number.
266 %
267 % #1 - a macro containing a representation of an integer.
268 % #2 - the number of digits to ensure.
269 %
270 % e.g.
271 %
272 % \foo <- 7FF
273 %
274 % \pgfmathsetnumberlength{8}%
275 % \pgfmath@ensurenumberlength\foo
276 %
277 % \foo <- 000007FF
278 %
279 \def\pgfmath@ensurenumberlength#1{%
280 \def\pgfmath@base@tempa{#1}%
281 \expandafter\c@pgfmath@counta\pgfmath@basenumberlength\relax%
282 \expandafter\pgfmath@ensurenumberlength@#1\pgfmath@ensurenumberlength}
283 \def\pgfmath@ensurenumberlength@#1{%
284 \ifx\pgfmath@ensurenumberlength#1\relax%
285 \expandafter\pgfmath@ensurenumberlength@@%
286 \else%
287 \advance\c@pgfmath@counta by-1\relax%
288 \expandafter\pgfmath@ensurenumberlength@
289 \fi}
290
291 \def\pgfmath@ensurenumberlength@@{%
292 \edef\pgfmath@base@tempb{\pgfmath@base@tempa}%
293 \pgfmath@ensurenumberlength@@@}
294 \def\pgfmath@ensurenumberlength@@@{%
295 \ifnum\c@pgfmath@counta>0\relax%
296 \advance\c@pgfmath@counta by-1\relax%
297 \edef\pgfmath@base@tempb{0\pgfmath@base@tempb}%
298 \expandafter\pgfmath@ensurenumberlength@@@%
299 \fi%
300 \expandafter\edef\pgfmath@base@tempa{\pgfmath@base@tempb}}
301
302 \def\pgfmathsetbasenumberlength#1{\def\pgfmath@basenumberlength{#1}}
303 \pgfmathsetbasenumberlength{1}%
304
305 % \pgfmathtodigitlist\marg{macro}\marg{number}}
306 %
307 % This command converts \meta{number} into a comma-separated
308 % list of digits and stores the result in \meta{macro}.
309 % The \marg{number} is \emph{not} parsed before processing.
310 %
311 \def\pgfmathtodigitlist#1#2{%
312 \def\pgfmath@temp{#1}%
313 \let\pgfmath@digitlist=\pgfutil@empty%
314 \edef\pgfmath@@tmp{#2}%
315 \expandafter\pgfmath@todigitlist\pgfmath@@tmp @%
316 }
317 \def\pgfmath@base@atchar{@}
318 \def\pgfmath@todigitlist#1{%
319 \def\pgfmath@digit{#1}%
320 \ifx\pgfmath@digit\pgfmath@base@atchar%
321 \expandafter\let\pgfmath@temp=\pgfmath@digitlist%
322 \else%
323 \ifx\pgfmath@digitlist\pgfutil@empty%
324 \edef\pgfmath@digitlist{#1}%
325 \else%
326 \edef\pgfmath@digitlist{\pgfmath@digitlist,#1}%
327 \fi%
328 \expandafter\pgfmath@todigitlist%
329 \fi%
330 }