| 1 |
\errorcontextlines999\relax |
| 2 |
The various ``XX'' and ``xX'' pairs test whether there are errant spaces |
| 3 |
in the macros or the macro files. |
| 4 |
|
| 5 |
X\input mfpextra\relax X |
| 6 |
X\input mfpextra\relax X |
| 7 |
\def\empty{} |
| 8 |
|
| 9 |
\def\frac#1#2{{#1\over#2}} |
| 10 |
\def\cs#1{{\tt \char`\\#1}} |
| 11 |
\def\mybreak{\vskip 0pt plus 100pt\penalty 0 \vskip 0pt plus -100pt\relax} |
| 12 |
\def\\{\hfil\break\ignorespaces} |
| 13 |
\def\y{Rpop\Z\Z\\} |
| 14 |
\def\Y{Rpop\Z\Z} |
| 15 |
\everymath{\displaystyle} |
| 16 |
|
| 17 |
{\bf Stack-only operations:}\\ |
| 18 |
Example of a program. Computes the solution of $ax^2 + bx + c = 0$ using |
| 19 |
the quadratic formula. If the result is complex, it detects this. |
| 20 |
Coefficients stored in \cs{A}, \cs{B} and \cs{C}. First case: $4x^2 + |
| 21 |
5y^2 - 1= 0$. Solutions are $x = -\frac{5}{8} \pm \frac{1}{8}\sqrt{41}$ |
| 22 |
|
| 23 |
\def\A{4}\def\B{5}\def\C{-1} |
| 24 |
|
| 25 |
X\startMFPprogram % stack |
| 26 |
\Rpush\B\Rdup\Rsq % B(B^2) |
| 27 |
\Rpush\A\Rpush\C\Rmul\Rdbl\Rdbl % B(B^2)(4AC) |
| 28 |
\Rsub % B(B^2-4AC) |
| 29 |
\IFneg{\def\I{i}\Rchs}{\def\I{}}% % B(|B^2-4AC|) |
| 30 |
\Rsqrt\Rpush\A\Rdbl\Rdiv % B(sqrt(|B^2-4AC|)/2A) |
| 31 |
\Rpop\Ypart % B |
| 32 |
\Rpush\A\Rdbl\Rdiv\Rchs % (-B/2A) |
| 33 |
\Rpop\Xpart % |
| 34 |
%\expandafter\show\csname MFP@Rstack\endcsname |
| 35 |
\Rpush\Xpart\Rpush\Ypart\Radd\Rpop\Broot |
| 36 |
\Rpush\Xpart\Rpush\Ypart\Rsub\Rpop\Sroot |
| 37 |
\Export\Xpart |
| 38 |
\Export\Ypart |
| 39 |
\Export\Broot |
| 40 |
\Export\Sroot |
| 41 |
\Export\I |
| 42 |
\stopMFPprogram X |
| 43 |
|
| 44 |
\indent Solution: $x = \Xpart \pm \I\Ypart = \Broot$ and $\Sroot$. |
| 45 |
|
| 46 |
Second case $2x^2 - 2x + 3 = 0$. Solutions are $x = \frac{1}{2} \pm |
| 47 |
\frac{i}{2}\sqrt{5}$. |
| 48 |
|
| 49 |
\def\A{2}\def\B{-2}\def\C{3} |
| 50 |
|
| 51 |
X\startMFPprogram % stack |
| 52 |
\Rpush\B\Rdup\Rsq % B(B^2) |
| 53 |
\Rpush\A\Rpush\C\Rmul\Rdbl\Rdbl % B(B^2)(4AC) |
| 54 |
\Rsub % B(B^2-4AC) |
| 55 |
\IFneg{\def\I{i}\Rchs}{\def\I{}}% % B(|B^2-4AC|) |
| 56 |
\Rsqrt\Rpush\A\Rdbl\Rdiv % B(sqrt(|B^2-4AC|)/2A) |
| 57 |
\Rpop\Ypart % B |
| 58 |
\Rpush\A\Rdbl\Rdiv\Rchs % (-B/2A) |
| 59 |
\Rpop\Xpart % |
| 60 |
\Export\Xpart |
| 61 |
\Export\Ypart |
| 62 |
\Export\I |
| 63 |
\stopMFPprogram X |
| 64 |
|
| 65 |
%\expandafter\show\csname MFP@Rstack\endcsname |
| 66 |
\indent Solution: $x = \Xpart \pm \I\Ypart$. |
| 67 |
|
| 68 |
Now try square roots (should be exactly 1234.5678 and 1524): |
| 69 |
X\startMFPprogram % stack |
| 70 |
\Rpush{1524157.65279684}\Rsqrt\Rpop\X |
| 71 |
\Rpush{1524}\Rsq\Rsqrt\Rpop\Y |
| 72 |
\Export\X |
| 73 |
\Export\Y |
| 74 |
\stopMFPprogram X \X { and }\Y. |
| 75 |
|
| 76 |
|
| 77 |
Below we test for speed and to check for any space characters |
| 78 |
accidentally produced. You should see only a few xX pairs with hopefully |
| 79 |
no spaces in between them. As curently set up, these tests perform about |
| 80 |
34000 numerical operations. It all takes about 31 seconds on a |
| 81 |
moderately old (2 years?) Windows 7 running plain tex from |
| 82 |
TeX~Live~2012. |
| 83 |
|
| 84 |
This operation count does not distinguish between basic operations like |
| 85 |
addition and multiplication, and those operations from mfpextra that are |
| 86 |
probably each equivalent to a dozen or more multiplications. Counting |
| 87 |
each such operation with the an estimated multiplicity, the tests |
| 88 |
probably perform 400 thousand or more basic operations. |
| 89 |
|
| 90 |
Of the basic operations, multiplication is (by measurement) 2--4 times |
| 91 |
as lengthy as addition, and division is 2--3 times as lengthy as |
| 92 |
multiplication. Actual times depend on the machine, but the ratios |
| 93 |
remain pretty much the same. Here is a summary of timings on my fastest |
| 94 |
machine; each operation is run 500 times in a loop. (A loop in which |
| 95 |
an input number is processed and a value returned, but no calculations are |
| 96 |
performed ({\tt\string\MFPzero}), times at $0.0\,$sec.) Timing obtained |
| 97 |
with {\tt\string\pdfelapsedtime}. |
| 98 |
|
| 99 |
\medskip |
| 100 |
|
| 101 |
\indent\vtop{\halign{\hfil$#$&\quad$#\,$sec\cr |
| 102 |
\noalign{\hrule\smallskip} |
| 103 |
2.54321+22432.87654321 &0.016\cr |
| 104 |
2.54321\times22432.87654321 &0.046\cr |
| 105 |
22432.87654321/2.54321 &0.11\cr |
| 106 |
\sqrt{23456789.54321} &0.172\cr |
| 107 |
\mathop{\fam0 rand}(23456789.54321) &0.105\cr |
| 108 |
1.00001234^{8000} &0.72\cr |
| 109 |
\exp(2.54321) &0.42\cr |
| 110 |
\sin(2.54321) &0.41\cr |
| 111 |
\log(2.54321) &0.73\cr |
| 112 |
\mathop{\fam0 angle}(254.321,100) &1.14\cr |
| 113 |
\noalign{\smallskip\hrule} |
| 114 |
}} |
| 115 |
|
| 116 |
\medskip |
| 117 |
|
| 118 |
Originally, all the tests below combined took 21 seconds on a 4-year-old |
| 119 |
Windows XP under TeX Live 2011. But since then I have changed angle and |
| 120 |
power computations so that they are considerably more accurate, but with |
| 121 |
a possible reduction in speed. I cannot test the speed reduction, since |
| 122 |
I no longer have that machine. |
| 123 |
|
| 124 |
For my current machines: On a Windows 7 machine, 64-bit, laptop, it |
| 125 |
takes 32 seconds to process this file. On another Windows 7 machine, |
| 126 |
32-bit, desktop, it takes about 10 seconds. (This difference could be |
| 127 |
explained partly by the fact that the last machine is newer and partly |
| 128 |
by the fact that TeX is a 32-bit program and therefore a better match to |
| 129 |
the 32-bit hardware.) |
| 130 |
|
| 131 |
\def\testi{% stack forms |
| 132 |
\startMFPprogram |
| 133 |
\Rpush{0.000 001}\Rpop\X |
| 134 |
\Rpush{1.2}\Rpush{-2.3}\Rexch\Rdup |
| 135 |
\Rpop\X\Rpop\X |
| 136 |
\Rpop\X\Rpush{21.34}\Rchs |
| 137 |
\Rpop\X\Rpush{21.34}\Rabs |
| 138 |
\Rpop\X\Rpush{21.34}\Rchs |
| 139 |
\Rpop\X\Rpush{21.34}\Rint |
| 140 |
\Rpop\X\Rpush{21.34}\Rfrac |
| 141 |
\Rpop\X\Rpush{21.34}\Rdbl |
| 142 |
\Rpop\X\Rpush{21.34}\Rhalve |
| 143 |
\Rpop\X\Rpush{21.34}\Rsgn |
| 144 |
\Rpop\X\Rpush{21.34}\Rsin |
| 145 |
\Rpop\X\Rpush{21.34}\Rcos |
| 146 |
\Rpop\X\Rpush{21.34}\Rdeg |
| 147 |
\Rpop\X\Rpush{21.34}\Rrad |
| 148 |
\Rpop\X\Rpush{21.34}\Rlog |
| 149 |
\Rpop\X\Rpush{21.34}\Rln |
| 150 |
\Rpop\X\Rpush{-1.34}\Rexp |
| 151 |
\Rpop\X\Rpush{3.3}\Rexp |
| 152 |
\Rpop\X\Rpush{21.34}\Rsq |
| 153 |
\Rpop\X\Rpush{21.34}\Rinv |
| 154 |
\Rpop\X\Rpush{21.34}\Rfloor |
| 155 |
\Rpop\X\Rpush{21.34}\Rceil |
| 156 |
\Rpop\X\Rpush{21.34}\Rsqrt |
| 157 |
\Rpop\X\Rpush{21.34}\Rrand |
| 158 |
\Rpop\X\Rpush{21.34}\Rpush{12.34}\Rcmp |
| 159 |
\IFlt{}{}\IFgt{}{}\IFeq{}{}\Rsub |
| 160 |
\IFneg{}{}\IFpos{}{}\IFzero{}{}\Rpop\X |
| 161 |
\Rpush{1.2}\Rpush{-2.3}\Radd |
| 162 |
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rsub |
| 163 |
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rmul |
| 164 |
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rdiv |
| 165 |
\Rpop\X\Rpush{2.3}\Rpush{17}\Rpow |
| 166 |
\Rpop\X\Rpush{2.3}\Rpush{-17}\Rpow |
| 167 |
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rmax |
| 168 |
\Rpop\X\Rpush{1.2}\Rpush{-2.3}\Rmin |
| 169 |
\stopMFPprogram} |
| 170 |
|
| 171 |
\def\testii{% unary operand forms, including |
| 172 |
\MFPchs\X\Z % extra tests of sin, log, exp and pow |
| 173 |
\MFPchs\Y\Z |
| 174 |
\MFPabs\X\Z |
| 175 |
\MFPabs\Y\Z |
| 176 |
\MFPdbl\X\Z |
| 177 |
\MFPdbl\Y\Z |
| 178 |
\MFPhalve\X\Z |
| 179 |
\MFPhalve\Y\Z |
| 180 |
\MFPint\X\Z |
| 181 |
\MFPint\Y\Z |
| 182 |
\MFPsgn\X\Z |
| 183 |
\MFPsgn\Y\Z |
| 184 |
\MFPsq\X\Z |
| 185 |
\MFPsq\Y\Z |
| 186 |
\MFPinv\X\Z |
| 187 |
\MFPinv\Y\Z |
| 188 |
\MFPfrac\X\Z |
| 189 |
\MFPfrac\Y\Z |
| 190 |
\MFPfloor\X\Z |
| 191 |
\MFPfloor\Y\Z |
| 192 |
\MFPceil\X\Z |
| 193 |
\MFPceil\Y\Z |
| 194 |
\MFPsin{30}\Z |
| 195 |
\MFPsin{420}\Z |
| 196 |
\MFPcos{60}\Z |
| 197 |
\MFPcos{390}\Z |
| 198 |
\MFPlog\X\Z |
| 199 |
\MFPln\X\Z |
| 200 |
\MFPexp\X\Z |
| 201 |
\MFPexp\Y\Z |
| 202 |
\MFPsin{1}\Z |
| 203 |
\MFPsin{2}\Z |
| 204 |
\MFPsin{3}\Z |
| 205 |
\MFPsin{4}\Z |
| 206 |
\MFPsin{5}\Z |
| 207 |
\MFPsin{6}\Z |
| 208 |
\MFPsin{7}\Z |
| 209 |
\MFPsin{8}\Z |
| 210 |
\MFPsin{9}\Z |
| 211 |
\MFPsin{10}\Z |
| 212 |
\MFPsin{20}\Z |
| 213 |
\MFPsin{30}\Z |
| 214 |
\MFPsin{40}\Z |
| 215 |
\MFPsin{50}\Z |
| 216 |
\MFPsin{60}\Z |
| 217 |
\MFPsin{70}\Z |
| 218 |
\MFPsin{80}\Z |
| 219 |
\MFPsin{90}\Z |
| 220 |
\MFPlog{.1}\Z |
| 221 |
\MFPlog{.2}\Z |
| 222 |
\MFPlog{.3}\Z |
| 223 |
\MFPlog{.4}\Z |
| 224 |
\MFPlog{.5}\Z |
| 225 |
\MFPlog{.6}\Z |
| 226 |
\MFPlog{.7}\Z |
| 227 |
\MFPlog{.8}\Z |
| 228 |
\MFPlog{.9}\Z |
| 229 |
\MFPlog{1}\Z |
| 230 |
\MFPlog{1.01}\Z |
| 231 |
\MFPlog{1.02}\Z |
| 232 |
\MFPlog{1.03}\Z |
| 233 |
\MFPlog{1.04}\Z |
| 234 |
\MFPlog{1.05}\Z |
| 235 |
\MFPlog{1.06}\Z |
| 236 |
\MFPlog{1.07}\Z |
| 237 |
\MFPlog{1.08}\Z |
| 238 |
\MFPlog{1.09}\Z |
| 239 |
\MFPexp{.000001}\Z |
| 240 |
\MFPexp{.00001}\Z |
| 241 |
\MFPexp{.0001}\Z |
| 242 |
\MFPexp{.001}\Z |
| 243 |
\MFPexp{.01}\Z |
| 244 |
\MFPexp{.1}\Z |
| 245 |
\MFPexp{1}\Z |
| 246 |
\MFPexp{2}\Z |
| 247 |
\MFPexp{3}\Z |
| 248 |
\MFPexp{4}\Z |
| 249 |
\MFPexp{5}\Z |
| 250 |
\MFPexp{6}\Z |
| 251 |
\MFPexp{7}\Z |
| 252 |
\MFPexp{8}\Z |
| 253 |
\MFPexp{9}\Z |
| 254 |
\MFPexp{10}\Z |
| 255 |
\MFPsqrt{10}\Z |
| 256 |
\MFPrand{10}\Z |
| 257 |
\MFPexp{-8.3254}\Z |
| 258 |
\MFPpow\MFPe{-10}\Z |
| 259 |
\MFPpow\MFPe{-9}\Z |
| 260 |
\MFPpow\MFPe{-8}\Z |
| 261 |
\MFPpow\MFPe{-7}\Z |
| 262 |
\MFPpow\MFPe{-6}\Z |
| 263 |
\MFPpow\MFPe{-5}\Z |
| 264 |
\MFPpow\MFPe{-4}\Z |
| 265 |
\MFPpow\MFPe{-3}\Z |
| 266 |
\MFPpow\MFPe{-2}\Z |
| 267 |
\MFPpow\MFPe{-1}\Z |
| 268 |
\MFPpow\MFPe{0}\Z |
| 269 |
\MFPpow\MFPe{1}\Z |
| 270 |
\MFPpow\MFPe{2}\Z |
| 271 |
\MFPpow\MFPe{3}\Z |
| 272 |
\MFPpow\MFPe{4}\Z |
| 273 |
\MFPpow\MFPe{5}\Z |
| 274 |
\MFPpow\MFPe{6}\Z |
| 275 |
\MFPpow\MFPe{7}\Z |
| 276 |
\MFPpow\MFPe{8}\Z |
| 277 |
\MFPpow\MFPe{9}\Z |
| 278 |
\MFPpow\MFPe{10}\Z} |
| 279 |
|
| 280 |
\def\testiii{%% binary operand forms and print formating, plus |
| 281 |
\MFPsqrt{0}\Z % additional tests of sqrt |
| 282 |
\MFPsqrt{1}\Z |
| 283 |
\MFPsqrt{2}\Z |
| 284 |
\MFPsqrt{3}\Z |
| 285 |
\MFPsqrt{4}\Z |
| 286 |
\MFPsqrt{5}\Z |
| 287 |
\MFPsqrt{6}\Z |
| 288 |
\MFPsqrt{7}\Z |
| 289 |
\MFPsqrt{8}\Z |
| 290 |
\MFPsqrt{9}\Z |
| 291 |
\MFPsqrt{10}\Z |
| 292 |
\MFPsqrt{1524157.65279684}\Z |
| 293 |
\MFPadd\X\Y\Z |
| 294 |
\MFPsub\X\Y\Z |
| 295 |
\MFPsub\Y\X\Z |
| 296 |
\MFPsub\X\X\Z |
| 297 |
\MFPsub\Y\Y\Z |
| 298 |
\MFPmul\X\Y\Z |
| 299 |
\MFPdiv\X\Y\Z |
| 300 |
\MFPdiv\Y\X\Z |
| 301 |
\MFPmax\X\Y\Z |
| 302 |
\MFPmin\X\Y\Z |
| 303 |
\MFPpow\X{5}\Z |
| 304 |
\MFPpow\X{-5}\Z |
| 305 |
\MFPpow\Y{5}\Z |
| 306 |
\MFPpow\Y{-5}\Z |
| 307 |
\MFPcmp\X\Y |
| 308 |
\IFlt{}{}\IFgt{}{}\IFeq{}{}% |
| 309 |
\MFPsub\X\Y\Z |
| 310 |
\IFneg{}{}\IFpos{}{}\IFzero{}{}% |
| 311 |
\def\T{333.00000000}% |
| 312 |
\def\S{1357.12345678}% |
| 313 |
\MFPtruncate{4}\T\Z |
| 314 |
\MFPtruncate{0}\T\Z |
| 315 |
\MFPtruncate{-2}\T\Z |
| 316 |
\MFPstrip\T\Z |
| 317 |
\MFPstrip*\T\Z |
| 318 |
\MFPround{3}\S\Z |
| 319 |
\MFPround{5}\S\Z |
| 320 |
\MFPround{0}\S\Z |
| 321 |
\MFPround{-2}\S\Z |
| 322 |
\def\T{-333.00000000}% |
| 323 |
\def\S{-1357.12345678}% |
| 324 |
\MFPtruncate{4}\T\Z |
| 325 |
\MFPtruncate{0}\T\Z |
| 326 |
\MFPtruncate{-2}\T\Z |
| 327 |
\MFPstrip\T\Z |
| 328 |
\MFPstrip*\T\Z |
| 329 |
\MFPround{3}\S\Z |
| 330 |
\MFPround{5}\S\Z |
| 331 |
\MFPround{0}\S\Z |
| 332 |
\MFPround{-2}\S\Z} |
| 333 |
|
| 334 |
Three test loops follow. The first repeats 500 times a stack program |
| 335 |
that performs each available command followed by popping the result and |
| 336 |
repushing the original value(s). |
| 337 |
|
| 338 |
\newcount\n |
| 339 |
\def\testloopi{% |
| 340 |
\ifnum \n>0 |
| 341 |
\advance\n -1 |
| 342 |
\testi |
| 343 |
\expandafter |
| 344 |
\testloopi |
| 345 |
\fi |
| 346 |
} |
| 347 |
\n=500 |
| 348 |
x\testloopi X |
| 349 |
|
| 350 |
|
| 351 |
The second repeats 100 times a sequence in which all the unary operand |
| 352 |
commands are performed twice, plus extra of sine, log and exp. |
| 353 |
|
| 354 |
\def\testloopii{% |
| 355 |
\ifnum \n>0 |
| 356 |
\advance\n -1 |
| 357 |
\testii |
| 358 |
\expandafter |
| 359 |
\testloopii |
| 360 |
\fi |
| 361 |
} |
| 362 |
\def\X{1.2} |
| 363 |
\def\Y{-2.3} |
| 364 |
\n=100 |
| 365 |
x\testloopii X |
| 366 |
|
| 367 |
|
| 368 |
The third repeats 100 times a sequence in which all the binary operand |
| 369 |
commands are performed, plus some extra tests of sqrt and then all the |
| 370 |
print-preparation commands. |
| 371 |
|
| 372 |
\def\testloopiii{% |
| 373 |
\ifnum \n>0 |
| 374 |
\advance\n -1 |
| 375 |
\testiii |
| 376 |
\expandafter |
| 377 |
\testloopiii |
| 378 |
\fi |
| 379 |
} |
| 380 |
|
| 381 |
\n=100 |
| 382 |
x\testloopiii X |
| 383 |
\end |
| 384 |
|
| 385 |
\end{document} |