Debian Codegraph @

texmf-dist/source/generic/modulus/modulus.dtx

raw
1 % \iffalse
2 % +AMDG This document was begun on 1 May 1202, the
3 % feast of St. Joseph the Worker, and it
4 % is humbly dedicated to him and to the Immaculate Heart of
5 % Mary for their prayers, and to the Sacred Heart of Jesus
6 % for His mercy.
7 %
8 % This document is copyright 2018 by Donald P. Goodman, and is
9 % released publicly under the LaTeX Project Public License. The
10 % distribution and modification of this work is constrained by the
11 % conditions of that license. See
12 % http://www.latex-project.org/lppl.txt
13 % for the text of the license. This document is released
14 % under version 1.3c of that license, and this work may be distributed
15 % or modified under the terms of that license or, at your option, any
16 % later version.
17 %
18 % This work has the LPPL maintenance status 'maintained'.
19 %
20 % The Current Maintainer of this work is Donald P. Goodman
21 % ([email protected]).
22 %
23 % This work consists of modulus.dtx, modulus.ins, and
24 % derived files modulus.sty and modulus.pdf.
25 % \fi
26
27 % \iffalse
28 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
29 %<package>\ProvidesPackage{modulus}[2018/05/07 v1.0 Modulus/modulo operator in plain TeX]
30 %<*driver>
31 \documentclass{ltxdoc}
32
33 \usepackage{doc}
34 \usepackage{modulus}
35 \usepackage{booktabs}
36
37 \begin{document}
38 \DocInput{modulus.dtx}
39 \end{document}
40 %</driver> \fi
41 %
42 % \title{The |modulus| Package, v1.0}
43 % \author{Donald P.\ Goodman III}
44 % \date{\today}
45 %
46 % \maketitle
47 %
48 % \begin{abstract}
49 % \noindent
50 % \TeX\ (traditional \TeX, not even $\epsilon$-\TeX) has the four
51 % basic functions with |\advance|, |\multiply|, and
52 % |\divide|, though these are all integer operations. This
53 % tiny package makes a modulus operator usable with \TeX\
54 % (or \LaTeX, and probably most flavors). It also provides a
55 % non-destructive way of
56 % getting at the integer quotient of two numbers.
57 % \end{abstract}
58 %
59 % \tableofcontents
60 %
61 % \section{The Operators}
62 %
63 % One question immediately occurs to me: why bother with
64 % such a package, when $\epsilon$-\TeX\ already has
65 % |\numexpr|, and there are so many advanced packages
66 % providing real math, and even floating-point operations
67 % and bignums? The answer is twofold: (1) an interesting
68 % project, and (2) \TeX\ is the archival format, as Knuth
69 % has always emphasized, and so doing things in ordinary
70 % \TeX, where possible, should be preferred, to keep it
71 % such.
72 %
73 % The core of the package is \DescribeMacro{\modulo}|\modulo|:
74 %
75 % \begin{center}
76 % \cmd{\modulo} \marg{dividend} \marg{divisor}
77 % \end{center}
78 %
79 % Running this will assign the remainder of the integer
80 % division of \textit{dividend} and \textit{divisor} to the
81 % counter \DescribeMacro{\remainder}|\remainder|. The
82 % values of \textit{dividend} and \textit{divisor} will not
83 % be changed, if they are changeable (e.g., if they are
84 % counters).
85 %
86 % Note that the package works with plain \TeX\ counters,
87 % \emph{not} \LaTeX-style counters created and set with
88 % |\newcounter{counter}| and friends. If you try to
89 % use it with |\thecounter| or |\value{counter}| in the
90 % \LaTeX\ style, it will not work. Fortunately, plain-\TeX\
91 % counters are perfectly usable within \LaTeX.
92 %
93 % We can use the macro as such:
94 %
95 % \begin{quote}
96 % |\modulo{\the\dividend}{\the\divisor}\the\remainder|
97 % \end{quote}
98 %
99 % This will execute the macro and print the resulting
100 % remainder. We can also use simple number tokens in the
101 % same way:
102 %
103 % \begin{quote}
104 % |\modulo{8}{3}\the\remainder|: \modulo{8}{3}\the\remainder
105 % \end{quote}
106 %
107 % In the table above, the divisor is always the
108 % value of a counter |\testdivisor|, which is 4; the
109 % dividend is the value of a counter |\testdividend|, which
110 % is reset as indicated in each line. We then do the
111 % modulus operation and print the value of |\remainder| in
112 % the ``Modulus'' column. We then reprint the values of
113 % |\testdividend| and |\testdivisor|, to show that we have
114 % not altered them by taking the remainder.
115 %
116 % \newcount\testdivisor\testdivisor=4
117 % \newcount\testdividend
118 %
119 % \begin{table}[t]
120 % \centering
121 % \begin{tabular}{ccccc}
122 % \toprule
123 % Dividend & Divisor & Modulus & Dividend & Divisor \\
124 % \midrule
125 % \global\testdividend=1\leavevmode\the\testdividend & \the\testdivisor &
126 % \modulo{\the\testdividend}{\the\testdivisor}\the\remainder &
127 % \the\testdividend & \the\testdivisor \\
128 % \global\testdividend=2\leavevmode\the\testdividend & \the\testdivisor &
129 % \modulo{\the\testdividend}{\the\testdivisor}\the\remainder &
130 % \the\testdividend & \the\testdivisor \\
131 % \global\testdividend=3\leavevmode\the\testdividend & \the\testdivisor &
132 % \modulo{\the\testdividend}{\the\testdivisor}\the\remainder &
133 % \the\testdividend & \the\testdivisor \\
134 % \global\testdividend=4\leavevmode\the\testdividend & \the\testdivisor &
135 % \modulo{\the\testdividend}{\the\testdivisor}\the\remainder &
136 % \the\testdividend & \the\testdivisor \\
137 % \global\testdividend=5\leavevmode\the\testdividend & \the\testdivisor &
138 % \modulo{\the\testdividend}{\the\testdivisor}\the\remainder &
139 % \the\testdividend & \the\testdivisor \\
140 % \global\testdividend=6\leavevmode\the\testdividend & \the\testdivisor &
141 % \modulo{\the\testdividend}{\the\testdivisor}\the\remainder &
142 % \the\testdividend & \the\testdivisor \\
143 % \global\testdividend=7\leavevmode\the\testdividend & \the\testdivisor &
144 % \modulo{\the\testdividend}{\the\testdivisor}\the\remainder &
145 % \the\testdividend & \the\testdivisor \\
146 % \global\testdividend=8\leavevmode\the\testdividend & \the\testdivisor &
147 % \modulo{\the\testdividend}{\the\testdivisor}\the\remainder &
148 % \the\testdividend & \the\testdivisor \\
149 % \global\testdividend=9\leavevmode\the\testdividend & \the\testdivisor &
150 % \modulo{\the\testdividend}{\the\testdivisor}\the\remainder &
151 % \the\testdividend & \the\testdivisor \\
152 % \bottomrule
153 % \end{tabular}
154 % \caption{A few example modulos, dividend and divisor both
155 % before and after the operation.}
156 % \label{tab:ex}
157 % \end{table}
158 %
159 % Plain \TeX\ can also very simply take the integer quotient
160 % of a counter, with the |\divide| operator, like so:
161 %
162 % \begin{quote}
163 % |\divide\thiscounter by4|
164 % \end{quote}
165 %
166 % However, this destroys the original value of
167 % |\thiscounter| (or whatever else one is dividing). The
168 % |modulus| package therefore also provides another macro,
169 % \DescribeMacro{\quotient}|\quotient|, which
170 % non-destructively takes the integer quotient of two
171 % numbers and stores it in the counter
172 % \DescribeMacro{\intquotient}|\intquotient|:
173 %
174 % \begin{quote}
175 % |\quotient{365}{4}\leavevmode\the\intquotient|: \quotient{365}{4}\leavevmode\the\intquotient\\
176 % |\modulo{365}{4}\leavevmode\the\remainder|: \modulo{365}{4}\leavevmode\the\remainder
177 % \end{quote}
178 %
179 % Note that if |\the\thecounter| doesn't print as expected,
180 % it may be because you're still in vertical mode (the
181 % |\the| operator only works in horizontal mode), so try
182 % |\leavevmode| before printing the counter.
183 %
184 % \section{Implementation}
185 %
186 % Not much to this one. We define our counter to hold the
187 % eventual modulus:
188 % \begin{macrocode}
189 \newcount\remainder%
190 % \end{macrocode}
191 % Then we define the macro which will assign the modulus to
192 % that counter:
193 % \begin{macrocode}
194 \def\modulo#1#2{%
195 \remainder=#1%
196 \divide\remainder by#2%
197 \multiply\remainder by#2%
198 \multiply\remainder by-1%
199 \advance\remainder by#1\relax%
200 }%
201 % \end{macrocode}
202 % Then, for good measure, we also have a macro which will
203 % non-destructively take the integer quotient of a division, and
204 % store it in the counter |\intquotient|:
205 % \begin{macrocode}
206 \newcount\intquotient%
207 \def\quotient#1#2{%
208 \intquotient=#1%
209 \divide\intquotient by#2%
210 }%
211 % \end{macrocode}
212 % Happy \TeX{}ing!