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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
\errorcontextlines999\relax
The various ``XX'' and ``xX'' pairs test whether there are errant spaces
in the macros or the macro files.
X\input mfpextra\relax X
X\input mfpextra\relax X
\def\empty{}
\def\frac#1#2{{#1\over#2}}
\def\cs#1{{\tt \char`\\#1}}
\def\mybreak{\vskip 0pt plus 100pt\penalty 0 \vskip 0pt plus -100pt\relax}
\def\\{\hfil\break\ignorespaces}
\def\y{Rpop\Z\Z\\}
\def\Y{Rpop\Z\Z}
\everymath{\displaystyle}
{\bf Stack-only operations:}\\
Example of a program. Computes the solution of $ax^2 + bx + c = 0$ using
the quadratic formula. If the result is complex, it detects this.
Coefficients stored in \cs{A}, \cs{B} and \cs{C}. First case: $4x^2 +
5y^2 - 1= 0$. Solutions are $x = -\frac{5}{8} \pm \frac{1}{8}\sqrt{41}$
\def\A{4}\def\B{5}\def\C{-1}
X\startMFPprogram % stack
\Rpush\B\Rdup\Rsq % B(B^2)
\Rpush\A\Rpush\C\Rmul\Rdbl\Rdbl % B(B^2)(4AC)
\Rsub % B(B^2-4AC)
\IFneg{\def\I{i}\Rchs}{\def\I{}}% % B(|B^2-4AC|)
\Rsqrt\Rpush\A\Rdbl\Rdiv % B(sqrt(|B^2-4AC|)/2A)
\Rpop\Ypart % B
\Rpush\A\Rdbl\Rdiv\Rchs % (-B/2A)
\Rpop\Xpart %
%\expandafter\show\csname MFP@Rstack\endcsname
\Rpush\Xpart\Rpush\Ypart\Radd\Rpop\Broot
\Rpush\Xpart\Rpush\Ypart\Rsub\Rpop\Sroot
\Export\Xpart
\Export\Ypart
\Export\Broot
\Export\Sroot
\Export\I
\stopMFPprogram X
\indent Solution: $x = \Xpart \pm \I\Ypart = \Broot$ and $\Sroot$.
Second case $2x^2 - 2x + 3 = 0$. Solutions are $x = \frac{1}{2} \pm
\frac{i}{2}\sqrt{5}$.
\def\A{2}\def\B{-2}\def\C{3}
X\startMFPprogram % stack
\Rpush\B\Rdup\Rsq % B(B^2)
\Rpush\A\Rpush\C\Rmul\Rdbl\Rdbl % B(B^2)(4AC)
\Rsub % B(B^2-4AC)
\IFneg{\def\I{i}\Rchs}{\def\I{}}% % B(|B^2-4AC|)
\Rsqrt\Rpush\A\Rdbl\Rdiv % B(sqrt(|B^2-4AC|)/2A)
\Rpop\Ypart % B
\Rpush\A\Rdbl\Rdiv\Rchs % (-B/2A)
\Rpop\Xpart %
\Export\Xpart
\Export\Ypart
\Export\I
\stopMFPprogram X
%\expandafter\show\csname MFP@Rstack\endcsname
\indent Solution: $x = \Xpart \pm \I\Ypart$.
Now try square roots (should be exactly 1234.5678 and 1524):
X\startMFPprogram % stack
\Rpush{1524157.65279684}\Rsqrt\Rpop\X
\Rpush{1524}\Rsq\Rsqrt\Rpop\Y
\Export\X
\Export\Y
\stopMFPprogram X \X { and }\Y.
Below we test for speed and to check for any space characters
accidentally produced. You should see only a few xX pairs with hopefully
no spaces in between them. As curently set up, these tests perform about
34000 numerical operations. It all takes about 31 seconds on a
moderately old (2 years?) Windows 7 running plain tex from
TeX~Live~2012.
This operation count does not distinguish between basic operations like
addition and multiplication, and those operations from mfpextra that are
probably each equivalent to a dozen or more multiplications. Counting
each such operation with the an estimated multiplicity, the tests
probably perform 400 thousand or more basic operations.
Of the basic operations, multiplication is (by measurement) 2--4 times
as lengthy as addition, and division is 2--3 times as lengthy as
multiplication. Actual times depend on the machine, but the ratios
remain pretty much the same. Here is a summary of timings on my fastest
machine; each operation is run 500 times in a loop. (A loop in which
an input number is processed and a value returned, but no calculations are
performed ({\tt\string\MFPzero}), times at $0.0\,$sec.) Timing obtained
with {\tt\string\pdfelapsedtime}.
\medskip
\indent\vtop{\halign{\hfil$#$&\quad$#\,$sec\cr
\noalign{\hrule\smallskip}
2.54321+22432.87654321 &0.016\cr
2.54321\times22432.87654321 &0.046\cr
22432.87654321/2.54321 &0.11\cr
\sqrt{23456789.54321} &0.172\cr
\mathop{\fam0 rand}(23456789.54321) &0.105\cr
1.00001234^{8000} &0.72\cr
\exp(2.54321) &0.42\cr
\sin(2.54321) &0.41\cr
\log(2.54321) &0.73\cr
\mathop{\fam0 angle}(254.321,100) &1.14\cr
\noalign{\smallskip\hrule}
}}
\medskip
Originally, all the tests below combined took 21 seconds on a 4-year-old
Windows XP under TeX Live 2011. But since then I have changed angle and
power computations so that they are considerably more accurate, but with
a possible reduction in speed. I cannot test the speed reduction, since
I no longer have that machine.
For my current machines: On a Windows 7 machine, 64-bit, laptop, it
takes 32 seconds to process this file. On another Windows 7 machine,
32-bit, desktop, it takes about 10 seconds. (This difference could be
explained partly by the fact that the last machine is newer and partly
by the fact that TeX is a 32-bit program and therefore a better match to
the 32-bit hardware.)
\def\testi{% stack forms
\startMFPprogram
\Rpush{0.000 001}\Rpop\X
\Rpush{1.2}\Rpush{-2.3}\Rexch\Rdup
\Rpop\X\Rpop\X
\Rpop\X\Rpush{21.34}\Rchs
\Rpop\X\Rpush{21.34}\Rabs
\Rpop\X\Rpush{21.34}\Rchs
\Rpop\X\Rpush{21.34}\Rint
\Rpop\X\Rpush{21.34}\Rfrac
\Rpop\X\Rpush{21.34}\Rdbl
\Rpop\X\Rpush{21.34}\Rhalve
\Rpop\X\Rpush{21.34}\Rsgn
\Rpop\X\Rpush{21.34}\Rsin
\Rpop\X\Rpush{21.34}\Rcos
\Rpop\X\Rpush{21.34}\Rdeg
\Rpop\X\Rpush{21.34}\Rrad
\Rpop\X\Rpush{21.34}\Rlog
\Rpop\X\Rpush{21.34}\Rln
\Rpop\X\Rpush{-1.34}\Rexp
\Rpop\X\Rpush{3.3}\Rexp
\Rpop\X\Rpush{21.34}\Rsq
\Rpop\X\Rpush{21.34}\Rinv
\Rpop\X\Rpush{21.34}\Rfloor
\Rpop\X\Rpush{21.34}\Rceil
\Rpop\X\Rpush{21.34}\Rsqrt
\Rpop\X\Rpush{21.34}\Rrand
\Rpop\X\Rpush{21.34}\Rpush{12.34}\Rcmp
\IFlt{}{}\IFgt{}{}\IFeq{}{}\Rsub
\IFneg{}{}\IFpos{}{}\IFzero{}{}\Rpop\X
\Rpush{1.2}\Rpush{-2.3}\Radd
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rsub
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rmul
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rdiv
\Rpop\X\Rpush{2.3}\Rpush{17}\Rpow
\Rpop\X\Rpush{2.3}\Rpush{-17}\Rpow
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rmax
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rm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t{}{}\IFeq{}{}%
\MFPsub\X\Y\Z
\IFneg{}{}\IFpos{}{}\IFzero{}{}%
\def\T{333.00000000}%
\def\S{1357.12345678}%
\MFPtruncate{4}\T\Z
\MFPtruncate{0}\T\Z
\MFPtruncate{-2}\T\Z
\MFPstrip\T\Z
\MFPstrip*\T\Z
\MFPround{3}\S\Z
\MFPround{5}\S\Z
\MFPround{0}\S\Z
\MFPround{-2}\S\Z
\def\T{-333.00000000}%
\def\S{-1357.12345678}%
\MFPtruncate{4}\T\Z
\MFPtruncate{0}\T\Z
\MFPtruncate{-2}\T\Z
\MFPstrip\T\Z
\MFPstrip*\T\Z
\MFPround{3}\S\Z
\MFPround{5}\S\Z
\MFPround{0}\S\Z
\MFPround{-2}\S\Z}
Three test loops follow. The first repeats 500 times a stack program
that performs each available command followed by popping the result and
repushing the original value(s).
\newcount\n
\def\testloopi{%
\ifnum \n>0
\advance\n -1
\testi
\expandafter
\testloopi
\fi
}
\n=500
x\testloopi X
The second repeats 100 times a sequence in which all the unary operand
commands are performed twice, plus extra of sine, log and exp.
\def\testloopii{%
\ifnum \n>0
\advance\n -1
\testii
\expandafter
\testloopii
\fi
}
\def\X{1.2}
\def\Y{-2.3}
\n=100
x\testloopii X
The third repeats 100 times a sequence in which all the binary operand
commands are performed, plus some extra tests of sqrt and then all the
print-preparation commands.
\def\testloopiii{%
\ifnum \n>0
\advance\n -1
\testiii
\expandafter
\testloopiii
\fi
}
\n=100
x\testloopiii X
\end
\end{document}
|