| 1 |
% \iffalse |
| 2 |
% ------------------------------------------------------------------- |
| 3 |
% |
| 4 |
% Copyright 2008--2013, Daniel H. Luecking |
| 5 |
% |
| 6 |
% minifp may be distributed and/or modified under the conditions of the |
| 7 |
% LaTeX Project Public License, either version 1.3b of this license or (at |
| 8 |
% your option) any later version. The latest version of this license is in |
| 9 |
% <http://www.latex-project.org/lppl.txt> |
| 10 |
% and version 1.3c or later is part of all distributions of LaTeX version |
| 11 |
% 2008/12/01 or later. |
| 12 |
% |
| 13 |
% minifp has maintenance status "author-maintained". The Current Maintainer |
| 14 |
% is Daniel H. Luecking. The Base Interpreter is TeX (plain TeX or LaTeX). |
| 15 |
%<*driver|sty> |
| 16 |
\def\MFPfiledate{2013/12/30}% |
| 17 |
\def\MFPfileversion{0.96}% |
| 18 |
%</driver|sty> |
| 19 |
% |
| 20 |
%<*driver> |
| 21 |
\ProvidesFile{minifp.dtx} |
| 22 |
[\MFPfiledate\space v\MFPfileversion. Macros for real number operations and a |
| 23 |
stack-based programing language.]% |
| 24 |
\documentclass[draft]{ltxdoc} |
| 25 |
|
| 26 |
\addtolength{\textwidth}{1pt} |
| 27 |
|
| 28 |
\usepackage[morefloats=5]{morefloats} |
| 29 |
\usepackage{amssymb} |
| 30 |
% This avoids messages about nonexistent font variants (e.g., in \section): |
| 31 |
\def\mytt{\upshape\mdseries\ttfamily} |
| 32 |
% I use it instead of \texttt: |
| 33 |
\renewcommand\marg[1]{{\mytt\{#1\}}} |
| 34 |
\renewcommand\oarg[1]{{\mytt [#1]}} |
| 35 |
\renewcommand\parg[1]{{\mytt (#1)}} |
| 36 |
\renewcommand \arg[1]{{\mytt \##1}} |
| 37 |
\renewcommand\#{\char`\#\relax} |
| 38 |
\DeclareRobustCommand\cs[1]{{\mytt\char`\\#1}} |
| 39 |
% sometimes I want a <meta> without enclosing braces: |
| 40 |
\renewcommand{\meta}[1]{\mbox{$\langle$\rmfamily\itshape#1\/$\rangle$}} |
| 41 |
% and sometimes I want the braces: |
| 42 |
\newcommand\mmarg[1]{\marg{\meta{#1}}} |
| 43 |
|
| 44 |
\def\prog#1{{\mdseries\scshape #1}} |
| 45 |
\def\mfp{\prog{minifp}} |
| 46 |
\def\Mfp{\prog{Minifp}} |
| 47 |
\def\file#1{{\mytt #1}} |
| 48 |
\let\dim\file |
| 49 |
\let\env\file |
| 50 |
\def\sgn{\mathop{\mathrm{sgn}}\nolimits} |
| 51 |
% \op is for abstract operations (e.g., \op{add}) as opposed to |
| 52 |
% the macro that performs it (e.g., \cs{Radd}). And \reg is for |
| 53 |
% a "register" (e.g., the 3 macros \MFP@x@Sgn, \MFP@x@Int and \MFP@x@Frc) |
| 54 |
% conceived of as a single entity. |
| 55 |
\let\op\textit |
| 56 |
\def\reg#1{$#1$} |
| 57 |
% The occasional bare \tt braces |
| 58 |
\renewcommand\{{\char`\{} |
| 59 |
\renewcommand\}{\char`\}} |
| 60 |
% this gives the alternative symbol in BNF productions, i.e., the bar |
| 61 |
% in: { this | that } |
| 62 |
\renewcommand\|{${}\mathrel{|}{}$} |
| 63 |
|
| 64 |
\makeatletter |
| 65 |
\newcommand\bsl{{\mytt\@backslashchar}} |
| 66 |
% better lists |
| 67 |
\def\@listi{\leftmargin\leftmargini |
| 68 |
\parsep \z@ \@plus\p@ \@minus\z@ |
| 69 |
\topsep 4\p@ \@plus\p@ \@minus2\p@ |
| 70 |
\itemsep\parsep} |
| 71 |
\let\@listI\@listi \@listi |
| 72 |
\renewcommand\labelitemi{\normalfont\bfseries \textendash} |
| 73 |
\renewcommand\labelitemii{\textasteriskcentered} |
| 74 |
\renewcommand\labelitemiii{\textperiodcentered} |
| 75 |
\leftmargini\parindent |
| 76 |
% better index |
| 77 |
\def\usage#1{\textrm{#1}} |
| 78 |
\def\index@prologue{\section*{Index}\markboth{Index}{Index}% |
| 79 |
Numbers refer to the page(s) where the corresponding entry is described.} |
| 80 |
\def\IndexParms{% |
| 81 |
\parindent \z@ \columnsep 15pt |
| 82 |
\parskip 0pt plus 1pt |
| 83 |
\rightskip 5pt plus2em \mathsurround \z@ |
| 84 |
\parfillskip-5pt \small |
| 85 |
% less hanging: |
| 86 |
\def\@idxitem{\par\hangindent 20pt}% |
| 87 |
\def\subitem{\@idxitem\hspace*{15pt}}% |
| 88 |
\def\subsubitem{\@idxitem\hspace*{25pt}}% |
| 89 |
\def\indexspace{\par\vspace{10pt plus 2pt minus 3pt}}} |
| 90 |
\makeatother |
| 91 |
|
| 92 |
\title{The \mfp{} package\thanks{This file has version number |
| 93 |
\fileversion, last revised \filedate. The code described here |
| 94 |
was developed by Dan Luecking.}} |
| 95 |
\author{Dan Luecking} |
| 96 |
\date{\filedate} |
| 97 |
\DisableCrossrefs |
| 98 |
\CodelineIndex |
| 99 |
\AlsoImplementation |
| 100 |
|
| 101 |
\begin{document} |
| 102 |
\DeleteShortVerb{\|} |
| 103 |
\DocInput{minifp.dtx} |
| 104 |
\end{document} |
| 105 |
%</driver> |
| 106 |
%\fi |
| 107 |
% \CheckSum{3541} |
| 108 |
% \CharacterTable |
| 109 |
% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z |
| 110 |
% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z |
| 111 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 112 |
% Exclamation \! Double quote \" Hash (number) \# |
| 113 |
% Dollar \$ Percent \% Ampersand \& |
| 114 |
% Acute accent \' Left paren \( Right paren \) |
| 115 |
% Asterisk \* Plus \+ Comma \, |
| 116 |
% Minus \- Point \. Solidus \/ |
| 117 |
% Colon \: Semicolon \; Less than \< |
| 118 |
% Equals \= Greater than \> Question mark \? |
| 119 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 120 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 121 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 122 |
% Right brace \} Tilde \~} |
| 123 |
% |
| 124 |
% \GetFileInfo{minifp.dtx} |
| 125 |
% \maketitle |
| 126 |
% |
| 127 |
% \begin{abstract} |
| 128 |
% This package provides minimal fixed point exact decimal arithmetic |
| 129 |
% operations. `Minimal' means numbers are limited to eight digits on |
| 130 |
% either side of the decimal point. `Exact' means that when a number |
| 131 |
% \emph{can} be represented exactly within those limits, it will be. |
| 132 |
% \end{abstract} |
| 133 |
% |
| 134 |
% \StopEventually{\PrintIndex} |
| 135 |
% \tableofcontents |
| 136 |
% |
| 137 |
% \section{Introduction} |
| 138 |
% In working on an application that needed to be able to automatically |
| 139 |
% generate numeric labels on the axes of a graph, I needed to be able |
| 140 |
% to make simple calculations with real numbers. What \TeX{} provides is |
| 141 |
% far too limited. In fact, its only native user-level support for real |
| 142 |
% numbers is as factors for dimensions. For example one can ``multiply'' |
| 143 |
% $3.1\times 0.2$ by the following: \verb$\dimen0=0.2pt \dimen0=3.1\dimen0$. |
| 144 |
% |
| 145 |
% Unfortunately \TeX{} stores dimensions as integer multiples of the |
| 146 |
% ``scaled points'' (\dim{sp}) with \dim{sp}${}=2^{-16}$\dim{pt}, and |
| 147 |
% therefore \dim{.2pt} is approximated by $\frac{13107}{65536}$, which is |
| 148 |
% not exact. Then mutiplying by $3.1$ produces $\frac{40631}{65536}$. If |
| 149 |
% we ask \TeX{} to display this, it produces $0.61998$\dim{pt} and not the |
| 150 |
% exact value $0.62$. This is sufficiently accurate for positioning |
| 151 |
% elements on a page, but not for displaying automatically computed axis |
| 152 |
% labels if five digit accuracy is needed. |
| 153 |
% |
| 154 |
% The \mfp{} package was written to provide the necessary calculations |
| 155 |
% with the necessary accuracy for this application. The implementation |
| 156 |
% would have been an order of magnitude smaller and faster if only four digit |
| 157 |
% accuracy were provided (and I may eventually do that for the application |
| 158 |
% under consideration), but I have decided to clean up what I have |
| 159 |
% produced and release it as is. The full \mfp{} package provides nearly |
| 160 |
% the same operations as a subset of the \prog{fp} package, but the latter |
| 161 |
% carries calculations to 18 decimal places, which is far more than |
| 162 |
% necessary for my purposes. I want something small and fast to embed in |
| 163 |
% the \prog{mfpic} drawing package. |
| 164 |
% |
| 165 |
% I decided on eight digits on both sides of the decimal point essentially |
| 166 |
% because I wanted at least five digits and the design I chose made multiples |
| 167 |
% of four the easiest to work with. |
| 168 |
% |
| 169 |
% \Mfp{} also provides a simple stack-based language for writing assembly |
| 170 |
% language-like programs. Originally, this was to be the native |
| 171 |
% calculation method, but it turned out to be too unwieldy for ordinary |
| 172 |
% use. I left it in because it adds only about 10\% overhead to the code. |
| 173 |
% |
| 174 |
% But why \emph{only} eight digits? \TeX{} only works with integers, and |
| 175 |
% since the maximum integer allowed is about $2\,000\,000\,000$, the |
| 176 |
% largest numbers that can be added are limited to about $999\,999\,999$. |
| 177 |
% It is very little trouble to add numbers by adding their fractional |
| 178 |
% parts and integer parts separately as 9-digit integers. So it would seem |
| 179 |
% multiples of $9$ digits would be easy to implement. |
| 180 |
% |
| 181 |
% However, something we have to do repeatedly in \emph{division} is |
| 182 |
% multiply the integer and fractional parts of a number by a one-digit |
| 183 |
% number. For that purpose, nine digits would be too much, but eight |
| 184 |
% digits is just right. For nine digits, we would have to inconveniently |
| 185 |
% break the number into more than two parts. Limiting our numbers to |
| 186 |
% eight-digit parts drastically simplifies division. |
| 187 |
% |
| 188 |
% Another simplification: multiplication has to be done by breaking the |
| 189 |
% number into parts. \TeX{} can multiply any two 4-digit integers without |
| 190 |
% overflow, but it cannot multiply most pairs of 5-digit integers. Two |
| 191 |
% 8-digit numbers conveniently break into four 4-digit parts. To get even |
| 192 |
% nine digits of accuracy would require six parts (five, if we don't |
| 193 |
% insist on a separation occuring at the decimal point). The complexity of |
| 194 |
% the multiplication process goes up as the square of the number of parts, |
| 195 |
% so six parts would more than double the complexity. |
| 196 |
% |
| 197 |
% A final simplification: \TeX{} places a limit of nine on the number of |
| 198 |
% arguments a macro can have. Quite often the last argument is needed to |
| 199 |
% clear out unused text to be discarded. Thus, a string of eight digits |
| 200 |
% can quite often be processed with one execution of one nine-argument |
| 201 |
% macro. |
| 202 |
% |
| 203 |
% Addition and subtraction can be exact, multiplication and division can |
| 204 |
% extend numbers past the 8-digit limit so they might be rounded. |
| 205 |
% However, when the exact answer fits in the 8-digit limit, our code |
| 206 |
% should produce it. Overflow (in the sense that the integer part can |
| 207 |
% exceed the allowed eight digits) is always possible, but is much more |
| 208 |
% likely with multiplication and division. |
| 209 |
% |
| 210 |
% Multiplication is carried out internally to an exact answer, with 16 |
| 211 |
% digits on each side of the decimal point. The underflow digits (places 9 |
| 212 |
% through 16 after the decimal point) are used to round to an 8-digit |
| 213 |
% result. Overflow digits (those to the left of the lowest 8 in the |
| 214 |
% integer part) are discarded, usually without warning. Division is |
| 215 |
% internally carried to nine digits after the decimal, which is then also |
| 216 |
% rounded to an 8-digit result. Overflow digits are ignored for division |
| 217 |
% also. |
| 218 |
% |
| 219 |
% We supply two kinds of operations in this package. There are stack-based |
| 220 |
% operations, in which the operands are \op{popped} from a stack and the |
| 221 |
% results \op{pushed} onto it, and argument-based, in which the operands (and a |
| 222 |
% macro to hold the result^^A |
| 223 |
% \footnote{Unlike most other packages for decimal |
| 224 |
% arithmetic, \mfp{} puts the macro to hold the result |
| 225 |
% last. This allows the calculation to be performed before the |
| 226 |
% macro is even read, and this makes it somewhat easier for the |
| 227 |
% stack- and argument-based versions to share code.}^^A |
| 228 |
% ) are arguments of a macro. Both types load the arguments into internal |
| 229 |
% macros (think of them as ``registers''), then call internal commands |
| 230 |
% (think ``microcode'') which return the results in internal macros. |
| 231 |
% These results are then \op{pushed} onto the stack (stack-based |
| 232 |
% operations) or stored in a supplied macro argument (think ``variable''). |
| 233 |
% The difference lies entirely in where the operands come from (arguments |
| 234 |
% or stack) and where they go (macro or stack). |
| 235 |
% |
| 236 |
% The stack is implemented as an internal macro which is redefined with |
| 237 |
% each command. The binary operations act on the last two \op{pushed} objects |
| 238 |
% in the order they were \op{pushed}. For example, the sequence ``\op{push} 5, |
| 239 |
% \op{push} 3, \op{subtract}'' performs $5-3$ by popping $3$ and $5$ into |
| 240 |
% registers (thereby removing them from the stack), subtracting them |
| 241 |
% and then pushing the result ($2$) onto the stack. |
| 242 |
% |
| 243 |
% Our implementation of the \op{push} operation first prepares the number |
| 244 |
% in a standard form. Thus, stack-based operations always obtain numbers |
| 245 |
% in this form. The argument based operations will prepare the arguments |
| 246 |
% in the same way. The internal commands will thus have a standard form to |
| 247 |
% operate on. All results are returned in standard form. |
| 248 |
% |
| 249 |
% The standard form referred to above is an integer part (one to eight digits |
| 250 |
% with no unnecessary leading zeros nor unnecessary sign) followed by the |
| 251 |
% decimal point (always a dot, which is ASCII \number`\.), followed by exactly |
| 252 |
% eight digits, all of this preceded by a minus sign if the number is |
| 253 |
% negative. Thus, $-{-0.25}$ would be processed and stored as |
| 254 |
% ``\texttt{0.25000000}'' and $-.333333$ as ``\texttt{-0.33333300}''. |
| 255 |
% |
| 256 |
% |
| 257 |
% \section{User macros} |
| 258 |
% |
| 259 |
% \Mfp{} provides (so far) six binary operations (that act on a pair of |
| 260 |
% numbers): addition, subtraction, multiplication, division, maximum and |
| 261 |
% minimum, as well as fourteen unary operations (that act on one number): |
| 262 |
% negation, absolute value, doubling, halving, integer part, fractional |
| 263 |
% part, floor, ceiling, signum, squaring, increment, decrement and |
| 264 |
% inversion. With the ``\texttt{extra}'' option, the unary operations |
| 265 |
% sine, cosine, logarithm, powers, square root and random number are |
| 266 |
% available, and the binary operation angle. See section~\ref{extras}. |
| 267 |
% |
| 268 |
% These extra operations are made available using the \texttt{extra} |
| 269 |
% option in \LaTeX{}: |
| 270 |
% \begin{verbatim} |
| 271 |
% \usepackage[extra]{minifp} \end{verbatim} |
| 272 |
% In plain \TeX{}, they will be loaded if you give the macro |
| 273 |
% \cs{MFPextra} a definition (any definition) before inputting |
| 274 |
% \file{minifp.sty}: |
| 275 |
% \begin{verbatim} |
| 276 |
% \def\MFPextra{} \input minifp.sty \end{verbatim} |
| 277 |
% The extras can also be loaded by means of the command |
| 278 |
% \cs{MFPloadextra}, issued after \file{minifp.sty} is loaded. |
| 279 |
% As of version 0.95 \file{mfpextra} can be directly \cs{input}. |
| 280 |
% It will detect whether \file{minifp.sty} has been loaded and input it |
| 281 |
% if not. This will work only in plain \TeX{}. |
| 282 |
% |
| 283 |
% If the extra operations are not needed, some memory and time might be |
| 284 |
% saved by using \file{minifp.sty} alone. I have not seriously tried to |
| 285 |
% keep \file{mfpextra.tex} as small or fast as possible, but I do try |
| 286 |
% to improve the accuracy when I can. |
| 287 |
% |
| 288 |
% As previously mentioned, each of these operations come in two versions: |
| 289 |
% a version that acts on operands and stores the result in a macro, and a |
| 290 |
% version that acts on the stack. The former all have names that begin |
| 291 |
% \cs{MFP} and the latter begin with \cs{R}. The former can be used |
| 292 |
% anywhere, while the latter can only be used in a ``program''. |
| 293 |
% A program is started with \cs{startMFPprogram} and terminated with |
| 294 |
% \cs{stopMFPprogram}. The \texttt{R} in the names is for `real'. This is |
| 295 |
% because it is possible that stacks of other types will be implemented in |
| 296 |
% the future. |
| 297 |
% |
| 298 |
% For example, \verb$\MFPadd{1.2}{3.4}\X$ will add $1.20000000$ to |
| 299 |
% $3.40000000$ and then define \cs{X} to be the resulting |
| 300 |
% \texttt{4.60000000}. These operand forms do not alter or even address |
| 301 |
% the stack in any way. The stack-based version of the same operation |
| 302 |
% would look like the following: |
| 303 |
% \begin{verbatim} |
| 304 |
% \Rpush{1.2} |
| 305 |
% \Rpush{3.4} |
| 306 |
% \Radd |
| 307 |
% \Rpop\X \end{verbatim} |
| 308 |
% which would \op{push} first \texttt{1.20000000} then \texttt{3.40000000} onto |
| 309 |
% the stack, then replace them with \texttt{4.60000000}, then remove that |
| 310 |
% and store it in \verb$\X$. Clearly the stack is intended for |
| 311 |
% calculations that produce a lot of intermediate values and only the |
| 312 |
% final result needs to be stored. |
| 313 |
% |
| 314 |
% \SpecialUsageIndex{\startMFPprogram} |
| 315 |
% The command \cs{startMFPprogram} starts a group. That group should be |
| 316 |
% ended by \cs{stopMFPprogram}. |
| 317 |
% \SpecialUsageIndex{\stopMFPprogram} |
| 318 |
% Changes to the stack and defined macros are local to that group. Thus |
| 319 |
% the macro \cs{X} in the example above might seem to be useful only as a |
| 320 |
% temporary storage for later calculations in the same program group. |
| 321 |
% However, there are commands provided to force such a macro to survive |
| 322 |
% the group, and even to force the contents of the stack to survive the |
| 323 |
% group (see the end of subsection~\ref{stack}). Do not try to turn a |
| 324 |
% \mfp{} program into a \LaTeX{} environment. The extra grouping added by |
| 325 |
% environments would defeat the effects of these commands. |
| 326 |
% |
| 327 |
% \subsection{Nonstack-based operations} |
| 328 |
% |
| 329 |
% In the following tables, an argument designated \meta{num} can be any |
| 330 |
% decimal real number with at most 8 digits on each side of the decimal |
| 331 |
% point, or it can be a macro that contains such a number. If the decimal |
| 332 |
% dot is absent, the fractional part will be taken to be zero, if the |
| 333 |
% integer part or the fractional part is absent, it will be taken to be |
| 334 |
% zero. (One consequence of these rules is that all the following |
| 335 |
% arguments produce the same internal representation of zero: \marg{0.0}, |
| 336 |
% \marg{0.}, \marg{.0}, \marg{0}, \marg{.}, and \marg{}\,.) Spaces may |
| 337 |
% appear anywhere in the \meta{num} arguments and are stripped out before |
| 338 |
% the number is used. For example, \marg{3 . 1415 9265} is a valid |
| 339 |
% argument. Commas are not permitted. The decimal point \emph{must} be |
| 340 |
% ASCII 46 (variously called a dot, period, or fullstop) with category 12 |
| 341 |
% (`other'). If an input encoding is used that allows more than one `dot', |
| 342 |
% the user must be sure to enter this one. If some babel language |
| 343 |
% definitions make it a shorthand, it must be inactivated before use. |
| 344 |
% |
| 345 |
% The \cs{macro} argument is any legal macro. The result of using one of |
| 346 |
% these commands is that the macro is defined (or redefined, there is no |
| 347 |
% checking done) to contain the standard form of the result. If the |
| 348 |
% \meta{num} is a macro, the braces surrounding it are optional. |
| 349 |
% |
| 350 |
% \medskip |
| 351 |
% \centerline{% |
| 352 |
% \begin{tabular}{lp{3in}} |
| 353 |
% \textit{Binary Operations}&\\[3pt] |
| 354 |
% \hline \hline |
| 355 |
% \textbf{Command}&\textbf{operation}\\ |
| 356 |
% \hline |
| 357 |
% \SpecialUsageIndex{\MFPadd}^^A |
| 358 |
% \cs{MFPadd}\mmarg{num$_1$}\mmarg{num$_2$}\cs{macro}& |
| 359 |
% Stores the result of \meta{num$_1$}${}+{}$\meta{num$_2$} in \cs{macro}\\ |
| 360 |
% \SpecialUsageIndex{\MFPsub}^^A |
| 361 |
% \cs{MFPsub}\mmarg{num$_1$}\mmarg{num$_2$}\cs{macro}& |
| 362 |
% Stores the result of \meta{num$_1$}${}-{}$\meta{num$_2$} in \cs{macro}\\ |
| 363 |
% \SpecialUsageIndex{\MFPmul}^^A |
| 364 |
% \cs{MFPmul}\mmarg{num$_1$}\mmarg{num$_2$}\cs{macro}& |
| 365 |
% Stores the result of \meta{num$_1$}${}\times{}$\meta{num$_2$}, |
| 366 |
% rounded to 8 places after the decimal point, in \cs{macro}\\ |
| 367 |
% \SpecialUsageIndex{\MFPmpy}^^A |
| 368 |
% \cs{MFPmpy}\mmarg{num$_1$}\mmarg{num$_2$}\cs{macro}& |
| 369 |
% Same as \cs{MFPmul}\\ |
| 370 |
% \SpecialUsageIndex{\MFPdiv}^^A |
| 371 |
% \cs{MFPdiv}\mmarg{num$_1$}\mmarg{num$_2$}\cs{macro}& |
| 372 |
% Stores the result of \meta{num$_1$}${}/{}$\meta{num$_2$}, |
| 373 |
% rounded to 8 places after the decimal point, in \cs{macro}\\ |
| 374 |
% \SpecialUsageIndex{\MFPmin}^^A |
| 375 |
% \cs{MFPmin}\mmarg{num$_1$}\mmarg{num$_2$}\cs{macro}& |
| 376 |
% Stores the smaller of \meta{num$_1$} and \meta{num$_2$} in \cs{macro}\\ |
| 377 |
% \SpecialUsageIndex{\MFPmax}^^A |
| 378 |
% \cs{MFPmax}\mmarg{num$_1$}\mmarg{num$_2$}\cs{macro}& |
| 379 |
% Stores the larger of \meta{num$_1$} and \meta{num$_2$} in \cs{macro} |
| 380 |
% \end{tabular}} |
| 381 |
% |
| 382 |
%\bigskip |
| 383 |
% |
| 384 |
% \centerline{% |
| 385 |
% \begin{tabular}{lp{3.4in}} |
| 386 |
% \textit{Unary Operations}&\\[3pt] |
| 387 |
% \hline\hline |
| 388 |
% \textbf{Command}&\textbf{operation}\\ |
| 389 |
% \hline |
| 390 |
% \SpecialUsageIndex{\MFPchs}^^A |
| 391 |
% \cs{MFPchs}\mmarg{num}\cs{macro}& |
| 392 |
% Stores $-{}$\meta{num} in \cs{macro}.\\ |
| 393 |
% \SpecialUsageIndex{\MFPabs}^^A |
| 394 |
% \cs{MFPabs}\mmarg{num}\cs{macro}& |
| 395 |
% Stores $|$\meta{num}$|$ in \cs{macro}.\\ |
| 396 |
% \SpecialUsageIndex{\MFPdbl}^^A |
| 397 |
% \cs{MFPdbl}\mmarg{num}\cs{macro}& |
| 398 |
% Stores $2\times{}$\meta{num} in \cs{macro}.\\ |
| 399 |
% \SpecialUsageIndex{\MFPhalve}^^A |
| 400 |
% \cs{MFPhalve}\mmarg{num}\cs{macro}& |
| 401 |
% Stores \meta{num}/2, rounded to 8 places after the decimal point, in |
| 402 |
% \cs{macro}.\\ |
| 403 |
% \SpecialUsageIndex{\MFPint}^^A |
| 404 |
% \cs{MFPint}\mmarg{num}\cs{macro}& |
| 405 |
% Replaces the part of \meta{num} after the decimal point with zeros |
| 406 |
% (keeps the sign unless the result is zero) and stores the result in |
| 407 |
% \cs{macro}.\\ |
| 408 |
% \SpecialUsageIndex{\MFPfrac}^^A |
| 409 |
% \cs{MFPfrac}\mmarg{num}\cs{macro}& |
| 410 |
% Replaces the part of \meta{num} before the decimal point with zero |
| 411 |
% (keeps the sign unless the result is zero) and stores the result in |
| 412 |
% \cs{macro}.\\ |
| 413 |
% \SpecialUsageIndex{\MFPfloor}^^A |
| 414 |
% \cs{MFPfloor}\mmarg{num}\cs{macro}& |
| 415 |
% Stores the largest integer not more than \meta{num} in \cs{macro}.\\ |
| 416 |
% \SpecialUsageIndex{\MFPceil}^^A |
| 417 |
% \cs{MFPceil}\mmarg{num}\cs{macro}& |
| 418 |
% Stores the smallest integer not less than \meta{num} in \cs{macro}.\\ |
| 419 |
% \SpecialUsageIndex{\MFPsgn}^^A |
| 420 |
% \cs{MFPsgn}\mmarg{num}\cs{macro}& |
| 421 |
% Stores $-1$, $0$ or $1$ (in standard form) in \cs{macro} according |
| 422 |
% to whether \meta{num} is negative, zero, or positive.\\ |
| 423 |
% \SpecialUsageIndex{\MFPsq}^^A |
| 424 |
% \cs{MFPsq}\mmarg{num}\cs{macro}& |
| 425 |
% Stores the square of \meta{num} in \cs{macro}.\\ |
| 426 |
% \SpecialUsageIndex{\MFPinv}^^A |
| 427 |
% \cs{MFPinv}\mmarg{num}\cs{macro}& |
| 428 |
% Stores 1/\meta{num}, rounded to 8 places after the decimal point, in |
| 429 |
% \cs{macro}.\\ |
| 430 |
% \SpecialUsageIndex{\MFPincr}^^A |
| 431 |
% \cs{MFPincr}\mmarg{num}\cs{macro}& |
| 432 |
% Stores \meta{num}${}+1$ in \cs{macro}.\\ |
| 433 |
% \SpecialUsageIndex{\MFPdecr}^^A |
| 434 |
% \cs{MFPdecr}\mmarg{num}\cs{macro}& |
| 435 |
% Stores \meta{num}${}-1$ in \cs{macro}.\\ |
| 436 |
% \SpecialUsageIndex{\MFPzero}^^A |
| 437 |
% \cs{MFPzero}\mmarg{num}\cs{macro}& |
| 438 |
% Ignores \meta{num} and stores {0.00000000} in the \cs{macro}.\\ |
| 439 |
% \SpecialUsageIndex{\MFPstore}^^A |
| 440 |
% \cs{MFPstore}\mmarg{num}\cs{macro}& |
| 441 |
% Stores the \meta{num}, converted to standard form, in \cs{macro} |
| 442 |
% \end{tabular}} |
| 443 |
% |
| 444 |
%\bigskip |
| 445 |
% |
| 446 |
% The command \cs{MFPzero} is useful for ``macro programs''. If you want |
| 447 |
% to do something to a number depending on the outcome of a test, you may |
| 448 |
% occasionally want to simply absorbed the number and output a default |
| 449 |
% result. This is more efficient than multiplying by zero (but less |
| 450 |
% efficient than simply defining the \cs{macro} to be zero.) |
| 451 |
% |
| 452 |
% Note that one could easily double, halve, square, increment, |
| 453 |
% decrement or invert a \meta{num} using the binary versions of |
| 454 |
% \cs{MFPadd}, \cs{MFPsub}, \cs{MFPmul} or \cs{MFPdiv}. The commands |
| 455 |
% \cs{MFPdbl}, \cs{MFPhalve}, \cs{MFPsq}, \cs{MFPincr}, \cs{MFPdecr} and |
| 456 |
% \cs{MFPinv} are designed to be more efficient versions, since they are |
| 457 |
% used repeatedly in internal code. |
| 458 |
% |
| 459 |
% Also, multiplication is far more efficient than division, so even if you |
| 460 |
% use the two argument versions, \cs{MFPmul}\mmarg{num}\marg{.5} is faster than |
| 461 |
% \cs{MFPdiv}\mmarg{num}\marg{2}. |
| 462 |
% |
| 463 |
% There is one command that takes no argument and returns no value: |
| 464 |
% |
| 465 |
% \medskip |
| 466 |
% \centerline{% |
| 467 |
% \begin{tabular}{lp{3.4in}} |
| 468 |
% \textit{Do Nothing}&\\[3pt] |
| 469 |
% \hline\hline |
| 470 |
% \textbf{Command}&\textbf{operation}\\ |
| 471 |
% \hline |
| 472 |
% \SpecialUsageIndex{\MFPnoop}\cs{MFPnoop}& Does nothing. |
| 473 |
% \end{tabular}} |
| 474 |
% |
| 475 |
% \bigskip |
| 476 |
% The following are not commands at all, but macros that contain |
| 477 |
% convenient constants. |
| 478 |
% |
| 479 |
% \medskip |
| 480 |
% \centerline{% |
| 481 |
% \begin{tabular}{lp{3.9in}} |
| 482 |
% \textit{Constants}&\\[3pt] |
| 483 |
% \hline\hline |
| 484 |
% \textbf{Constant}&\textbf{value}\\ |
| 485 |
% \hline |
| 486 |
% \SpecialUsageIndex{\MFPpi}^^A |
| 487 |
% \cs{MFPpi}& \texttt{3.14159265}, the eight-digit approximation to |
| 488 |
% $\pi$.\\ |
| 489 |
% \SpecialUsageIndex{\MFPe}^^A |
| 490 |
% \cs{MFPe}& \texttt{2.71828183}, the eight-digit approximation to |
| 491 |
% $e$.\\ |
| 492 |
% \SpecialUsageIndex{\MFPphi}^^A |
| 493 |
% \cs{MFPphi}& \texttt{1.61803399}, the eight-digit approximation to |
| 494 |
% the golden ratio $\phi.$ |
| 495 |
% \end{tabular}} |
| 496 |
% |
| 497 |
% \bigskip |
| 498 |
% There also exist commands to check the sign of a number and the |
| 499 |
% relative size of two numbers: |
| 500 |
% |
| 501 |
% \medskip |
| 502 |
% \indent \SpecialUsageIndex{\MFPchk}\cs{MFPchk}\mmarg{num}\\ |
| 503 |
% \indent \SpecialUsageIndex{\MFPcmp}\cs{MFPcmp}\mmarg{num$_1$}\mmarg{num$_2$} |
| 504 |
% |
| 505 |
% \medskip |
| 506 |
% \noindent These influence the behavior of six commands: |
| 507 |
% |
| 508 |
% \medskip |
| 509 |
% \indent \SpecialUsageIndex{\IFneg}\cs{IFneg}\mmarg{true text}\mmarg{false text}\\ |
| 510 |
% \indent \SpecialUsageIndex{\IFzero}\cs{IFzero}\mmarg{true text}\mmarg{false text}\\ |
| 511 |
% \indent \SpecialUsageIndex{\IFpos}\cs{IFpos}\mmarg{true text}\mmarg{false text}\\ |
| 512 |
% \indent \SpecialUsageIndex{\IFlt}\cs{IFlt}\mmarg{true text}\mmarg{false text}\\ |
| 513 |
% \indent \SpecialUsageIndex{\IFeq}\cs{IFeq}\mmarg{true text}\mmarg{false text}\\ |
| 514 |
% \indent \SpecialUsageIndex{\IFgt}\cs{IFgt}\mmarg{true text}\mmarg{false text} |
| 515 |
% |
| 516 |
% \medskip |
| 517 |
% Issuing \verb$\MFPchk{\X}$ will check the sign of the number stored in |
| 518 |
% the macro \cs{X}. Then \verb$\IFneg{A}{B}$ will produce `\verb$A$' if it |
| 519 |
% is negative and `\verb$B$' if it is zero or positive. Similarly, |
| 520 |
% \verb$\MFPcmp{\X}{1}$ will compare the number stored in \cs{X} to $1$. |
| 521 |
% Afterward, \verb$\IFlt{A}{B}$ will produce `\verb$A$' if \cs{X} is less |
| 522 |
% than $1$ and `\verb$B$' if \cs{X} is equal to or greater than $1$. |
| 523 |
% |
| 524 |
% If users finds it tiresome to type two separate commands, they can |
| 525 |
% easily define a single command that both checks a value and runs |
| 526 |
% \cs{IF...}. For example\\ |
| 527 |
% \indent\verb$\def\IFisneg#1{\MFPchk{#1}\IFneg}$\\ |
| 528 |
% Used like\\ |
| 529 |
% \indent\verb$\IFisneg{\X}{A}{B}$\\ |
| 530 |
% this will check the value of \cs{X} and run \cs{IFneg} on the pair of |
| 531 |
% alternatives that follow. |
| 532 |
% |
| 533 |
% The user might never need to use \cs{MFPchk} because every one of the |
| 534 |
% operators provided by the \mfp{} package runs an internal version of |
| 535 |
% \cs{MFPchk} on the result of the operation before storing it in the |
| 536 |
% \cs{macro}. For example, after \cs{MFPzero} the command \cs{IFzero} will |
| 537 |
% always return the first argument. For this reason one should not insert |
| 538 |
% any \mfp{} operations between a check/compare and the \cs{IF...} command |
| 539 |
% that depends on it. |
| 540 |
% |
| 541 |
% Note: the behavior of all six \cs{IF...} commands is influenced by |
| 542 |
% \emph{both} \cs{MFPchk} and \cs{MFPcmp}. This is because internally |
| 543 |
% \verb$\MFPchk{\X}$ (for example) and \verb$\MFPcmp{\X}{0}$ do |
| 544 |
% essentially the same thing. In fact there are only three internal |
| 545 |
% booleans that govern the behavior of the six \cs{IF...} commands. The |
| 546 |
% different names are for clarity: \cs{IFgt} after a compare is less |
| 547 |
% confusing than the entirely equivalent \cs{IFpos}. |
| 548 |
% |
| 549 |
% It should probably be pointed out that the settings for the \cs{IF...} |
| 550 |
% macros are local to any \TeX{} groups they are contained in. |
| 551 |
% |
| 552 |
% |
| 553 |
% \subsection{Commands to process numbers for printing} |
| 554 |
% |
| 555 |
% After \verb$\MFPadd{1}{2}\X$ one can use \cs{X} anywhere and get |
| 556 |
% $3.00000000$. One might may well prefer $3.0$, and so commands are |
| 557 |
% provided to truncate a number or round it to some number of decimal |
| 558 |
% places. Note: these are provided for printing and they will not invoke |
| 559 |
% the above \cs{MFPchk}. They do not have any stack-based versions. |
| 560 |
% The commands are\\ |
| 561 |
% \indent\SpecialUsageIndex{\MFPtruncate}\cs{MFPtruncate}\mmarg{int}\mmarg{num}\cs{macro}\\ |
| 562 |
% \indent\SpecialUsageIndex{\MFPround}\cs{MFPround}\mmarg{int}\mmarg{num}\cs{macro}\\ |
| 563 |
% \indent\SpecialUsageIndex{\MFPstrip}\cs{MFPstrip}\mmarg{num}\cs{macro}\\ |
| 564 |
% where \meta{int} is a whole number between $-8$ and $8$ (inclusive). The |
| 565 |
% other two arguments are as before. |
| 566 |
% |
| 567 |
% These commands merely process \meta{num} and define \cs{macro} to |
| 568 |
% produce a truncated or rounded version, or one stripped of trailing |
| 569 |
% zeros, or one with added trailing zeros. Note that truncating or |
| 570 |
% rounding a number to a number of digits greater than it already has will |
| 571 |
% actually lengthen it with added zeros. For example, |
| 572 |
% \verb$\MFPround{4}{3.14159}\X$ |
| 573 |
% will cause \cs{X} to be defined to contain \texttt{3.1416}, while |
| 574 |
% \verb$\MFPround{6}{3.14159}\X$ |
| 575 |
% will cause \cs{X} to contain \texttt{3.141590}. |
| 576 |
% If \cs{Y} contains \texttt{3.14159}, then |
| 577 |
% \verb$\MFPtruncate{4}\Y\Y$ |
| 578 |
% will redefine \cs{Y} to contain \texttt{3.1415}. Also, |
| 579 |
% \verb$\MFPstrip{1.20000000}\Z$ |
| 580 |
% will cause \cs{Z} to contain \texttt{1.2}. All these commands first |
| 581 |
% normalize the \meta{num}, so any spaces are removed and redundant signs |
| 582 |
% are discarded. |
| 583 |
% |
| 584 |
% If \meta{int} is negative, places are counted to the left of the decimal |
| 585 |
% point and $0$\,s are substituted for lower order digits. That is, |
| 586 |
% \verb$\MFPtruncate{-2}{1864.3}\X$ |
| 587 |
% will give \cs{X} the value \texttt{1800} and |
| 588 |
% \verb$\MFPround{-2}{1864}\X$ |
| 589 |
% will give \cs{X} the value \texttt{1900}. |
| 590 |
% |
| 591 |
% If the first argument of \cs{MFPround} or \cs{MFPtruncate} is zero or |
| 592 |
% negative then the dot is also omitted from the result. If \cs{MFPstrip} is |
| 593 |
% applied to a number with all zeros after the dot, then one 0 is |
| 594 |
% retained. There is a star form where the dot and the zero are dropped. |
| 595 |
% |
| 596 |
% For these three commands, the sign of the number is irrelevant. That |
| 597 |
% is, the results for negative numbers are the negatives of the results |
| 598 |
% for the absolute values. The processing will remove redundant signs |
| 599 |
% along with redundant leading zeros: \verb$\MFPtruncate{-3}{-+123.456}$ |
| 600 |
% will produce \texttt{0}. The rounding rule is as follows: round up if |
| 601 |
% the digit to the right of the rounding point is $5$ or more, round down if |
| 602 |
% the digit is $4$ or less. |
| 603 |
% |
| 604 |
% |
| 605 |
% \subsection{Stack-based macros}\label{stack} |
| 606 |
% |
| 607 |
% The stack-based macros can only be used in a \mfp{} program group. |
| 608 |
% This group is started by the command \cs{startMFPprogram} and ended by |
| 609 |
% \cs{stopMFPprogram}. None of the stack-based macros takes an argument, |
| 610 |
% but merely operate on values on the stack, replacing them with the |
| 611 |
% results. There are also commands to manipulate the stack and save a |
| 612 |
% value on the stack into a macro. Finally, since all changes to the stack |
| 613 |
% (and to macros) are local and therefore lost after \cs{stopMFPprogram}, |
| 614 |
% there are commands to selectively cause them to be retained. |
| 615 |
% |
| 616 |
% To place numbers on the stack we have \cs{Rpush} and to get them |
| 617 |
% off we have \cs{Rpop}. The syntax is\\ |
| 618 |
% \indent \SpecialUsageIndex{\Rpush}\cs{Rpush}\marg{\meta{num}}\\ |
| 619 |
% \indent \SpecialUsageIndex{\Rpop}\cs{Rpop}\cs{macro}\\ |
| 620 |
% The first will preprocess the \meta{num} as previously discussed and |
| 621 |
% put it on the stack, the second will remove the last number from the stack |
| 622 |
% and define the given macro to have that number as its definition. |
| 623 |
% |
| 624 |
% All the binary operations remove the last two numbers from the stack, |
| 625 |
% operate on them in the order they were put on the stack, and \op{push} the |
| 626 |
% result on the stack. Thus the program |
| 627 |
% \begin{verbatim} |
| 628 |
% \Rpush{1.2} |
| 629 |
% \Rpush{3.4} |
| 630 |
% \Rsub \end{verbatim} |
| 631 |
% will first put \texttt{1.20000000} and \texttt{3.40000000} on the stack |
| 632 |
% and then replace them with \texttt{-2.20000000}. Note the order: $1.2-3.4$. |
| 633 |
% |
| 634 |
% \medskip |
| 635 |
% \centerline{% |
| 636 |
% \begin{tabular}{lp{4.0in}} |
| 637 |
% \textit{Binary Operations}&\\[3pt] |
| 638 |
% \hline\hline |
| 639 |
% \textbf{Command}&\textbf{operation}\\ |
| 640 |
% \hline |
| 641 |
% \SpecialUsageIndex{\Radd}\cs{Radd}& |
| 642 |
% Adds the last two numbers on the stack.\\ |
| 643 |
% \SpecialUsageIndex{\Rsub}\cs{Rsub}& |
| 644 |
% Subtracts the last two numbers on the stack.\\ |
| 645 |
% \SpecialUsageIndex{\Rmul}\cs{Rmul}& |
| 646 |
% Multiplies the last two numbers on the stack, rounding to 8 decimal |
| 647 |
% places.\\ |
| 648 |
% \SpecialUsageIndex{\Rmpy}\cs{Rmpy}& |
| 649 |
% Same as \cs{Rmul}.\\ |
| 650 |
% \SpecialUsageIndex{\Rdiv}\cs{Rdiv}& |
| 651 |
% Divides the last two numbers on the stack, rounding to 8 decimal |
| 652 |
% places.\\ |
| 653 |
% \SpecialUsageIndex{\Rmin}\cs{Rmin}& |
| 654 |
% Replaces the last two elements on the stack with the smaller one.\\ |
| 655 |
% \SpecialUsageIndex{\Rmax}\cs{Rmax}& |
| 656 |
% Replaces the last two elements on the stack with the larger one. |
| 657 |
% \end{tabular}} |
| 658 |
% |
| 659 |
%\bigskip |
| 660 |
% |
| 661 |
% The unary operations replace the last number on the stack with the |
| 662 |
% result of the operation performed on it. |
| 663 |
% |
| 664 |
% \medskip |
| 665 |
% \centerline{% |
| 666 |
% \begin{tabular}{lp{4.0in}} |
| 667 |
% \textit{Unary Operations}&\\[3pt] |
| 668 |
% \hline\hline |
| 669 |
% \textbf{Command}&\textbf{operation}\\ |
| 670 |
% \hline |
| 671 |
% \SpecialUsageIndex{\Rchs}\cs{Rchs}& |
| 672 |
% Changes the sign.\\ |
| 673 |
% \SpecialUsageIndex{\Rabs}\cs{Rabs}& |
| 674 |
% Obtains the absolute value.\\ |
| 675 |
% \SpecialUsageIndex{\Rdbl}\cs{Rdbl}& |
| 676 |
% Doubles the value.\\ |
| 677 |
% \SpecialUsageIndex{\Rhalve}\cs{Rhalve}& |
| 678 |
% Halves the value, rounding to 8 places.\\ |
| 679 |
% \SpecialUsageIndex{\Rint}\cs{Rint}& |
| 680 |
% Replaces the fractional part with zeros. If the result equals $0.0$, any |
| 681 |
% negative sign will be dropped.\\ |
| 682 |
% \SpecialUsageIndex{\Rfrac}\cs{Rfrac}& |
| 683 |
% Replaces the integer part with \texttt{0}. If the result equals |
| 684 |
% $0.0$, any negative sign will be dropped.\\ |
| 685 |
% \SpecialUsageIndex{\Rfloor}\cs{Rfloor}& |
| 686 |
% Obtains the largest integer not greater than the number.\\ |
| 687 |
% \SpecialUsageIndex{\Rceil}\cs{Rceil}& |
| 688 |
% Obtains the smallest integer not less than the number.\\ |
| 689 |
% \SpecialUsageIndex{\Rsgn}\cs{Rsgn}& |
| 690 |
% Obtains $-1$, $0$ or $1$ according to whether the number |
| 691 |
% is negative, zero, or positive. These numbers are pushed onto the |
| 692 |
% stack with the usual decimal point followed by 8 zeros.\\ |
| 693 |
% \SpecialUsageIndex{\Rsq}\cs{Rsq}& |
| 694 |
% Obtains the square. Slightly more efficient than the equivalent |
| 695 |
% \cs{Rdup}\cs{Rmul}. See below for \cs{Rdup}.\\ |
| 696 |
% \SpecialUsageIndex{\Rinv}\cs{Rinv}& |
| 697 |
% Obtains the reciprocal. Slightly more efficient than the equivalent |
| 698 |
% division.\\ |
| 699 |
% \SpecialUsageIndex{\Rincr}\cs{Rincr}& |
| 700 |
% Increases by $1$. Slightly more efficient than the equivalent |
| 701 |
% addition.\\ |
| 702 |
% \SpecialUsageIndex{\Rdecr}\cs{Rdecr}& |
| 703 |
% Decreases by $1$. Slightly more efficient than the equivalent |
| 704 |
% subtraction.\\ |
| 705 |
% \SpecialUsageIndex{\Rzero}\cs{Rzero}& |
| 706 |
% Replaces the number with zero. Slightly more convenient than the |
| 707 |
% equivalent \cs{Rpop}\cs{X} followed by a \cs{Rpush}\marg{0}.\\ |
| 708 |
% \end{tabular}} |
| 709 |
% |
| 710 |
%\bigskip |
| 711 |
% |
| 712 |
% |
| 713 |
% There is one operation, which does not read the stack nor change it |
| 714 |
% (nor do anything else). |
| 715 |
% |
| 716 |
% \medskip |
| 717 |
% \centerline{% |
| 718 |
% \begin{tabular}{lp{3.8in}} |
| 719 |
% \textit{Do Nothing}&\\[3pt] |
| 720 |
% \hline\hline |
| 721 |
% \textbf{Command}&\textbf{operation}\\ |
| 722 |
% \hline |
| 723 |
% \SpecialUsageIndex{\Rnoop}\cs{Rnoop}& |
| 724 |
% Does nothing. |
| 725 |
% \end{tabular}} |
| 726 |
% |
| 727 |
% \bigskip |
| 728 |
% There also exist commands to check the sign of the last number, and the |
| 729 |
% relative size of the last two numbers on the stack:\\ |
| 730 |
% \indent \SpecialUsageIndex{\Rchk}\cs{Rchk}\\ |
| 731 |
% \indent \SpecialUsageIndex{\Rcmp}\cs{Rcmp}\\ |
| 732 |
% They do not remove anything from the stack. |
| 733 |
% Just like the nonstack counterparts, they influence the behavior of |
| 734 |
% six commands: \cs{IFneg}, \cs{IFzero}, \cs{IFpos}, \cs{IFlt}, |
| 735 |
% \cs{IFeq} and \cs{IFgt}. Issuing \verb$\Rchk$ will check the sign of the |
| 736 |
% last number on the stack, while \verb$\Rcmp$ will compare the last two |
| 737 |
% in the order they were pushed. For example, in the following |
| 738 |
% \begin{verbatim} |
| 739 |
% \Rpush{1.3} |
| 740 |
% \Rpush{-2.3} |
| 741 |
% \Rcmp |
| 742 |
% \IFgt{\Radd}{\Rsub} |
| 743 |
% \Rpush\X |
| 744 |
% \Rchk |
| 745 |
% \IFneg{\Radd}{\Rsub} \end{verbatim} |
| 746 |
% \verb$\Rcmp$ will compare $1.3$ to $-2.3$. Since the first is greater |
| 747 |
% than the second, \verb$\IFgt$ will be true and they will be added, |
| 748 |
% producing $-1.0$. Following this the contents of the macro \cs{X} are |
| 749 |
% pushed, it is examined by \verb$\Rchk$ and then either added to or |
| 750 |
% subtracted from $-1.0$. |
| 751 |
% |
| 752 |
% The user might never need to use \cs{Rchk} because every operator that |
| 753 |
% puts something on the stack also runs \cs{Rchk}. In the above program, |
| 754 |
% in fact, \verb$\Rchk$ is redundant since \verb$\Rpush$ will already have |
| 755 |
% run it on the contents of \cs{X}. |
| 756 |
% |
| 757 |
% There exist stack manipulation commands that allow the contents of the |
| 758 |
% stack to be changed without performing any operations. These are really |
| 759 |
% just conveniences, as there effects could be obtained with appropriate |
| 760 |
% combinations of \verb$\Rpop$ and \verb$\Rpush$. These commands, however, do |
| 761 |
% not run \verb$\Rchk$ as \cs{Rpush} would. |
| 762 |
% |
| 763 |
% \medskip |
| 764 |
% \centerline{% |
| 765 |
% \begin{tabular}{lp{3.8in}} |
| 766 |
% \textit{Stack Manipulations}&\\[3pt] |
| 767 |
% \hline\hline |
| 768 |
% \textbf{Command}&\textbf{operation}\\ |
| 769 |
% \hline |
| 770 |
% \SpecialUsageIndex{\Rdup}\cs{Rdup}& |
| 771 |
% Puts another copy of the last element of the stack onto the stack.\\ |
| 772 |
% \SpecialUsageIndex{\Rexch}\cs{Rexch}& |
| 773 |
% Exchanges the last two elements on the stack. |
| 774 |
% \end{tabular}} |
| 775 |
% |
| 776 |
% \bigskip |
| 777 |
% |
| 778 |
% After \cs{stopMFPprogram}, any changes to macros or to the stack are |
| 779 |
% lost, unless arrangements have been made to save them. There are four |
| 780 |
% commands provided. Two act on a macro which is the only argument, the |
| 781 |
% other two have no arguments and act on the stack. The macro must |
| 782 |
% simply contain a value, it cannot be more complicated and certainly |
| 783 |
% cannot take an argument. |
| 784 |
% |
| 785 |
% \medskip |
| 786 |
% \centerline{% |
| 787 |
% \begin{tabular}{lp{3.8in}} |
| 788 |
% \textit{Exporting changed values}&\\[3pt] |
| 789 |
% \hline\hline |
| 790 |
% \textbf{Command}&\textbf{operation}\\ |
| 791 |
% \hline |
| 792 |
% \SpecialUsageIndex{\Export}\cs{Export}\cs{macro}& |
| 793 |
% \raggedright |
| 794 |
% Causes the definition of \cs{macro} to survive the |
| 795 |
% program group.\tabularnewline |
| 796 |
% \SpecialUsageIndex{\Global}\cs{Global}\cs{macro}& |
| 797 |
% Causes the definition of \cs{macro} to be global.\\ |
| 798 |
% \SpecialUsageIndex{\ExportStack}\cs{ExportStack}& |
| 799 |
% \raggedright |
| 800 |
% Causes the contents of the stack to survive the program |
| 801 |
% group.\tabularnewline |
| 802 |
% \SpecialUsageIndex{\GlobalStack}\cs{GlobalStack}& |
| 803 |
% Causes the contents of the stack to be global.\\ |
| 804 |
% \end{tabular}} |
| 805 |
% |
| 806 |
% \bigskip |
| 807 |
% The difference between \cs{Export} and \cs{Global} is solely in how |
| 808 |
% \emph{other} grouping is handled. If the program group is contained in |
| 809 |
% another group (for example, inside an environment), then the result of |
| 810 |
% \cs{Global}\cs{X} is that the definition of \cs{X} survives that group |
| 811 |
% (and all containing groups) as well. On the other hand, after |
| 812 |
% \cs{Export}\cs{X}, then the definition survives the program group, but |
| 813 |
% not other containing groups. |
| 814 |
% |
| 815 |
% If \TeX{} grouping is used \emph{inside} a program group, then using |
| 816 |
% \cs{Export} inside that group has no effect at all, while \cs{Global} |
| 817 |
% works as before. |
| 818 |
% |
| 819 |
% The stack versions are implemented by running \cs{Export} or |
| 820 |
% \cs{Global} on the internal macro that defines the stack, so they |
| 821 |
% have the same behavior. |
| 822 |
% |
| 823 |
% \subsection{Errors} |
| 824 |
% |
| 825 |
% If one tries to \op{pop} from an empty stack, an error message will be |
| 826 |
% issued. Ignoring the error causes the macro to have the value stored |
| 827 |
% in the macro \SpecialUsageIndex{\EndofStack}\verb$\EndofStack$. |
| 828 |
% Its default is \texttt{0.00000000}. |
| 829 |
% |
| 830 |
% If one tries to divide by zero, an error message will be issued. |
| 831 |
% Ignoring the error causes the result to be one of the following: |
| 832 |
% \begin{itemize} |
| 833 |
% \item Dividing $0$ by $0$ gives a result whose integer part is stored |
| 834 |
% in \verb$\ZeroOverZeroInt$\SpecialUsageIndex{\ZeroOverZeroInt} |
| 835 |
% and whose fractional part is stored in |
| 836 |
% \SpecialUsageIndex{\ZeroOverZeroFrac}\verb$\ZeroOverZeroFrac$. |
| 837 |
% The default is \texttt{0.00000000} |
| 838 |
% \item Dividing a nonzero $x$ by $0$ gives a result whose integer part is |
| 839 |
% stored in \SpecialUsageIndex{\xOverZeroInt}\verb$\xOverZeroInt$ |
| 840 |
% and whose fractional part is stored in |
| 841 |
% \SpecialUsageIndex{\xOverZeroFrac}\verb$\xOverZeroFrac$. The |
| 842 |
% defaults are both equal to \texttt{99999999}. The sign of the |
| 843 |
% result will be the sign of $x$. |
| 844 |
% \end{itemize} |
| 845 |
% |
| 846 |
% You can change any of these macros, but make sure they produce a |
| 847 |
% result which is a number in standard form (as described earlier). |
| 848 |
% These macros are copied directly into the result without checking. |
| 849 |
% Then further processing steps may require the result to be a number in |
| 850 |
% standard form. |
| 851 |
% |
| 852 |
% Error messages may result from trying to process numbers given in |
| 853 |
% incorrect format. However, there are so many ways for numbers to be |
| 854 |
% incorrect that this package does not even try to detect them. Thus, they |
| 855 |
% will only be caught if some \TeX{} operation encounters something it |
| 856 |
% cannot handle. (The \LaTeX{} manual calls these ``weird errors'' because |
| 857 |
% the messages tend to be uninformative.) Incorrectly formed numbers may even |
| 858 |
% pass unnoticed, but leave unexpected printed characters on the paper, or odd |
| 859 |
% spacing. |
| 860 |
% |
| 861 |
% \section{Implementation} |
| 862 |
% |
| 863 |
% \subsection{Utility macros} |
| 864 |
% |
| 865 |
% We announce ourself, and our purpose. We save the catcode of |
| 866 |
% \texttt{@} and change it to letter. Several other catcodes are saved |
| 867 |
% and set to other in this file. We also make provisions to load the |
| 868 |
% extra definitions, either directly with \cs{MFPloadextra} or through a |
| 869 |
% declared option in \LaTeX{}. |
| 870 |
% \begin{macrocode} |
| 871 |
%<*sty> |
| 872 |
\expandafter |
| 873 |
\ifx \csname MFP@finish\endcsname\relax |
| 874 |
\else \expandafter\endinput \fi |
| 875 |
\expandafter\edef\csname MFP@finish\endcsname{% |
| 876 |
\catcode64=\the\catcode64 \space |
| 877 |
\catcode46=\the\catcode46 \space |
| 878 |
\catcode60=\the\catcode60 \space |
| 879 |
\catcode62=\the\catcode62 \space}% |
| 880 |
\ifx\ProvidesPackage\UndEfInEd |
| 881 |
\newlinechar`\^^J% |
| 882 |
\message{% |
| 883 |
Package minifp: \MFPfiledate\space v\MFPfileversion. % |
| 884 |
Macros for real number operations % |
| 885 |
^^Jand a stack-based programing language.^^J}% |
| 886 |
\else |
| 887 |
\ProvidesPackage{minifp}[\MFPfiledate\space v\MFPfileversion. % |
| 888 |
Macros for real number operations % |
| 889 |
and a stack-based programing language.]% |
| 890 |
\DeclareOption{extra}{\def\MFPextra{}}% |
| 891 |
\ProcessOptions\relax |
| 892 |
\fi |
| 893 |
\catcode64=11 |
| 894 |
\ifx\MFPextra\UndEfInEd |
| 895 |
\def\MFP@loadextra{}% |
| 896 |
\else |
| 897 |
\def\MFP@loadextra{\input mfpextra\relax}% |
| 898 |
\fi |
| 899 |
\def\MFPloadextra{\input mfpextra\relax}% |
| 900 |
\catcode46=12 |
| 901 |
\catcode60=12 |
| 902 |
\catcode62=12 |
| 903 |
% \end{macrocode} |
| 904 |
% |
| 905 |
% We check for \LaTeX{} (ignoring \LaTeX209); \cs{MFP@ifnoLaTeX}\dots\cs{MFP@end} |
| 906 |
% is skipped in \LaTeX{} and executed otherwise. |
| 907 |
% \begin{macrocode} |
| 908 |
\long\def\gobbleto@MFP@end#1\MFP@end{}% |
| 909 |
\def\MFP@end{\@empty}% |
| 910 |
\ifx\documentclass\UndEfInEd |
| 911 |
\def\MFP@ifnoLaTeX{}% |
| 912 |
\else |
| 913 |
\let\MFP@ifnoLaTeX\gobbleto@MFP@end |
| 914 |
\fi |
| 915 |
% \end{macrocode} |
| 916 |
% |
| 917 |
% We have \LaTeX{}'s \cs{zap@space}. It pretty much \emph{must} be used |
| 918 |
% inside \cs{edef} or other purely expansion context. The rest of these |
| 919 |
% are standard \LaTeX{} internals. Note that the token list that |
| 920 |
% \cs{zap@space} is applied to should probably never contain braces or |
| 921 |
% expandable tokens.\\ |
| 922 |
% \indent Usage: \verb*$\edef\X{\zap@space<tokens> \@empty}$\\ |
| 923 |
% The space is necessary in case none exist; the \cs{@empty} terminates |
| 924 |
% the loop. |
| 925 |
% \begin{macrocode} |
| 926 |
\let\@xp\expandafter |
| 927 |
\def\@XP{\@xp\@xp\@xp}% |
| 928 |
\MFP@ifnoLaTeX |
| 929 |
\def\@empty{}% |
| 930 |
\long\def\@gobble#1{}% |
| 931 |
\def\zap@space#1 #2{#1\ifx#2\@empty\else\@xp\zap@space\fi#2}% |
| 932 |
\long\def\@ifnextchar#1#2#3{% |
| 933 |
\let\reserved@d#1% |
| 934 |
\def\reserved@a{#2}% |
| 935 |
\def\reserved@b{#3}% |
| 936 |
\futurelet\@let@token\@ifnch}% |
| 937 |
\def\@ifnch{% |
| 938 |
\ifx\@let@token\@sptoken |
| 939 |
\let\reserved@c\@xifnch |
| 940 |
\else |
| 941 |
\ifx\@let@token\reserved@d |
| 942 |
\let\reserved@c\reserved@a |
| 943 |
\else |
| 944 |
\let\reserved@c\reserved@b |
| 945 |
\fi |
| 946 |
\fi |
| 947 |
\reserved@c}% |
| 948 |
{% |
| 949 |
\def\:{\global\let\@sptoken= }\: % |
| 950 |
\def\:{\@xifnch}\@xp\gdef\: {\futurelet\@let@token\@ifnch}% |
| 951 |
}% |
| 952 |
\def\@ifstar#1{\@ifnextchar*{\@firstoftwo{#1}}}% |
| 953 |
\long\def\@firstofone #1{#1}% |
| 954 |
\long\def\@firstoftwo #1#2{#1}% |
| 955 |
\long\def\@secondoftwo#1#2{#2}% |
| 956 |
\MFP@end |
| 957 |
% \end{macrocode} |
| 958 |
% |
| 959 |
% We need to divide by both $10^4$ and $10^8$ several times. I could |
| 960 |
% have allocated two count registers, but have taken the approach of |
| 961 |
% reserving those for intermediate calculations. |
| 962 |
% \begin{macrocode} |
| 963 |
\def\MFP@tttfour {10000}% ttt = Ten To The |
| 964 |
\def\MFP@ttteight{100000000}% |
| 965 |
% \end{macrocode} |
| 966 |
% |
| 967 |
% These are for manipulating digits. The \verb$\...ofmany$ commands |
| 968 |
% require a sequence of arguments (brace groups or tokens) followed by |
| 969 |
% \verb$\MFP@end$. The minimum number of required parameters is surely |
| 970 |
% obvious. For example, \cs{MFP@ninthofmany} must be used like\\ |
| 971 |
% \indent\cs{MFP@ninthofmany}\meta{9 or more arguments}\cs{MFP@end}\\ |
| 972 |
% All these are fully expandable. |
| 973 |
% \begin{macrocode} |
| 974 |
\def\MFP@oneofmany#1#2\MFP@end{#1}% |
| 975 |
\def\MFP@fifthofmany#1#2#3#4#5#6\MFP@end{#5}% |
| 976 |
\def\MFP@ninthofmany#1#2#3#4#5#6#7#8{\MFP@oneofmany}% |
| 977 |
\def\MFP@eightofmany#1#2#3#4#5#6#7#8#9\MFP@end{#1#2#3#4#5#6#7#8}% |
| 978 |
% \end{macrocode} |
| 979 |
% |
| 980 |
% \subsection{Processing numbers and the stack} |
| 981 |
% |
| 982 |
% Our stack stores elements in groups, like \\ |
| 983 |
% \indent \verb${-1.234567890}{0.00001234}\MFP@eos$\\ |
| 984 |
% with an end marker. The purpose of the marker is to prevent certain |
| 985 |
% parameter manipulations from stripping off braces. This means we can't |
| 986 |
% use \cs{@empty} to test for an empty stack. At the moment, only |
| 987 |
% \cs{Rpop} actually checks, but all other stack commands (so far) use |
| 988 |
% \cs{Rpop} to get their arguments. |
| 989 |
% \begin{macrocode} |
| 990 |
\let\MFP@eos\relax |
| 991 |
\def\MFP@EOS{\MFP@eos}% |
| 992 |
\def\MFP@initRstack{\def\MFP@Rstack{\MFP@eos}}% |
| 993 |
\MFP@initRstack |
| 994 |
% \end{macrocode} |
| 995 |
% |
| 996 |
% Define some scratch registers for arithmetic operations. We don't care |
| 997 |
% that these might be already in use, as we only use them inside a group. |
| 998 |
% However, we need one counter that will not be messed with by any of |
| 999 |
% our operations. I must be sure not to use commands that change |
| 1000 |
% \cs{MFP@loopctr} in code that depends on it. |
| 1001 |
% \begin{macrocode} |
| 1002 |
\countdef \MFP@tempa 0 |
| 1003 |
\countdef \MFP@tempb 2 |
| 1004 |
\countdef \MFP@tempc 4 |
| 1005 |
\countdef \MFP@tempd 6 |
| 1006 |
\countdef \MFP@tempe 8 |
| 1007 |
\countdef \MFP@tempf 10 |
| 1008 |
\newcount \MFP@loopctr |
| 1009 |
% \end{macrocode} |
| 1010 |
% |
| 1011 |
% The following can only be used where unrestricted expansion is robust. |
| 1012 |
% It will allow results obtained inside a group to survive the group, |
| 1013 |
% but not be unrestrictedly global. |
| 1014 |
% Example: the code\\ |
| 1015 |
% \indent \verb$\MFP@endgroup@after{\def\noexpand\MFP@z@Val{\MFP@z@Val}}$\\ |
| 1016 |
% becomes\\ |
| 1017 |
% \indent \verb$\edef\x{\endgroup\def\noexpand\MFP@z@Val{\MFP@z@Val}}\x$\\ |
| 1018 |
% which gives, upon expansion of \verb$\x$,\\ |
| 1019 |
% \indent |
| 1020 |
% \cs{endgroup}\cs{def}\cs{MFP@z@Val}\marg{\meta{expansion-of-\cs{MFP@z@Val}}}\\ |
| 1021 |
% which defines \cs{MFP@z@Val} outside the current group to equal its expansion |
| 1022 |
% within the current group (provided the group was started with |
| 1023 |
% \cs{begingroup}). |
| 1024 |
% |
| 1025 |
% We define a \cs{MFP@returned@values} to make all the conceivable produced |
| 1026 |
% values survive the group. The \cs{MFPcurr@Sgn} part is to permit testing |
| 1027 |
% the sign of the result and allow conditional code based on it. |
| 1028 |
% |
| 1029 |
% I have been lax at making sure \cs{MFP@z@Ovr} is properly initiallized |
| 1030 |
% and properly checked whenever it could be relevant, and properly |
| 1031 |
% passed on. I think every internal command \cs{MFP@R}\textit{xxx} |
| 1032 |
% should ensure it starts being zero and ends with a numerical value. At |
| 1033 |
% one time division could leave it undefined. |
| 1034 |
% |
| 1035 |
% \cs{MFP@subroutine} executes its argument (typically a single command) with |
| 1036 |
% a wrapper that initializes all the macros that might need initializing, |
| 1037 |
% and returns the necessary results. |
| 1038 |
% \begin{macrocode} |
| 1039 |
\def\MFP@endgroup@after#1{\edef\x{\endgroup#1}\x}% |
| 1040 |
\def\MFP@afterdef{\def\noexpand}% |
| 1041 |
\def\MFP@returned@values{% |
| 1042 |
\MFP@afterdef\MFP@z@Val{\MFP@z@Sign\MFP@z@Int.\MFP@z@Frc}% |
| 1043 |
\MFP@afterdef\MFP@z@Ovr{\MFP@z@Ovr}% |
| 1044 |
\MFP@afterdef\MFP@z@Und{\MFP@z@Und}% |
| 1045 |
\MFP@afterdef\MFPcurr@Sgn{\MFP@z@Sgn}}% |
| 1046 |
\def\MFP@subroutine#1{% |
| 1047 |
\begingroup |
| 1048 |
\MFP@Rzero |
| 1049 |
\def\MFP@z@Ovr{0}% |
| 1050 |
\def\MFP@z@Und{0}% |
| 1051 |
#1% |
| 1052 |
\MFP@endgroup@after\MFP@returned@values}% |
| 1053 |
\def\MFP@Rzero{% |
| 1054 |
\def\MFP@z@Sgn{0}% |
| 1055 |
\def\MFP@z@Int{0}% |
| 1056 |
\def\MFP@z@Frc{00000000}}% |
| 1057 |
% \end{macrocode} |
| 1058 |
% |
| 1059 |
% \DescribeMacro{\EndofStack} |
| 1060 |
% We define here the error messages: popping from an empty stack and |
| 1061 |
% dividing by zero. In addition to the error messages, we provide some |
| 1062 |
% default values that hopefully allow some operations to continue. |
| 1063 |
% |
| 1064 |
% We also have a warning or two. |
| 1065 |
% \begin{macrocode} |
| 1066 |
\def\MFP@errmsg#1#2{% |
| 1067 |
\begingroup |
| 1068 |
\newlinechar`\^^J\let~\space |
| 1069 |
\def\MFP@msgbreak{^^J~~~~~~~~~~~~~~}% |
| 1070 |
\edef\reserved@a{\errhelp{#2}}\reserved@a |
| 1071 |
\errmessage{MiniFP error: #1}% |
| 1072 |
\endgroup}% |
| 1073 |
\def\MFP@popempty@err{% |
| 1074 |
\MFP@errmsg{cannot POP from an empty stack}% |
| 1075 |
{There were no items on the stack for the POP operation. % |
| 1076 |
If you continue, ^^Jthe macro will contain the % |
| 1077 |
value \EndofStack.}}% |
| 1078 |
\def\EndofStack{0.00000000}% |
| 1079 |
\def\MFP@dividebyzero@err{% |
| 1080 |
\MFP@errmsg{division by zero}% |
| 1081 |
{You tried to divide by zero. What were you thinking? % |
| 1082 |
If you continue, ^^Jthe value assigned will be either % |
| 1083 |
\ZeroOverZeroInt.\ZeroOverZeroFrac~(numerator=0) or % |
| 1084 |
^^J+/-\xOverZeroInt.\xOverZeroFrac~(numerator<>0).}}% |
| 1085 |
\def\MFP@warn#1{% |
| 1086 |
\begingroup |
| 1087 |
\newlinechar`\^^J\let~\space |
| 1088 |
\def\MFP@msgbreak{^^J~~~~~~~~~~~~~~~~}% |
| 1089 |
\immediate\write16{^^JMiniFP warning: #1.^^J}% |
| 1090 |
\endgroup}% |
| 1091 |
% \end{macrocode} |
| 1092 |
% |
| 1093 |
% \DescribeMacro{\MaxRealInt}These are the largest possible integer and |
| 1094 |
% fractional parts of a real |
| 1095 |
% \DescribeMacro{\MaxRealFrac}number. They are returned for division by |
| 1096 |
% zero, for logarithm of zero, and when overflow is detected in the |
| 1097 |
% exponential function. |
| 1098 |
% \begin{macrocode} |
| 1099 |
\def\MaxRealInt {99999999}% |
| 1100 |
\def\MaxRealFrac {99999999}% |
| 1101 |
% \end{macrocode} |
| 1102 |
% |
| 1103 |
% \SpecialUsageIndex{\MaxRealInt} |
| 1104 |
% \SpecialUsageIndex{\MaxRealFrac} |
| 1105 |
% These are the results returned when trying to divide by zero. Two are |
| 1106 |
% \DescribeMacro{\xOverZeroInt} |
| 1107 |
% \DescribeMacro{\xOverZeroFrac} |
| 1108 |
% used when dividing a nonzero number by zero and and two when trying to |
| 1109 |
% divide zero by zero. |
| 1110 |
% \DescribeMacro{\ZeroOverZeroInt} |
| 1111 |
% \DescribeMacro{\ZeroOverZeroFrac} |
| 1112 |
% \begin{macrocode} |
| 1113 |
\def\xOverZeroInt {\MaxRealInt}% |
| 1114 |
\def\xOverZeroFrac {\MaxRealFrac}% |
| 1115 |
\def\ZeroOverZeroInt {0}% |
| 1116 |
\def\ZeroOverZeroFrac{00000000}% |
| 1117 |
% \end{macrocode} |
| 1118 |
% |
| 1119 |
% These macros strip the spaces, process a number into sign, integer and |
| 1120 |
% fractional parts, and pad the fractional part out to eight decimals. They |
| 1121 |
% are used in \op{push} so that the stack will only contains reals in a |
| 1122 |
% normalized form. Some of them are also used to preprocess the reals in |
| 1123 |
% the operand versions of commands |
| 1124 |
% |
| 1125 |
% The \cs{MFP@*@Int} and \cs{MFP@*@Frc} parts are always positive, the sign being |
| 1126 |
% stored in \cs{MFP@*@Sgn} as $-1$, $0$ or $1$. |
| 1127 |
% |
| 1128 |
% We strip the spaces and pad the fractional parts separately because |
| 1129 |
% they are unnecessary when processing \op{pop}ped reals (though they wouldn't |
| 1130 |
% hurt). |
| 1131 |
% |
| 1132 |
% The number to be parsed is \arg4 and the macros to contain the parts |
| 1133 |
% are the first three arguments. Since we normally call \cs{MFPparse@real} |
| 1134 |
% with one of two sets of macros, we have two shortcuts for those cases. |
| 1135 |
% \begin{macrocode} |
| 1136 |
\def\MFPparse@real#1#2#3#4{% |
| 1137 |
\MFPnospace@def\MFPtemp@Val{#4}% |
| 1138 |
\MFPprocess@into@parts\MFPtemp@Val#1#2#3% |
| 1139 |
\MFP@padtoeight#3}% |
| 1140 |
\def\MFPparse@x{\MFPparse@real\MFP@x@Sgn\MFP@x@Int\MFP@x@Frc}% |
| 1141 |
\def\MFPparse@y{\MFPparse@real\MFP@y@Sgn\MFP@y@Int\MFP@y@Frc}% |
| 1142 |
% \end{macrocode} |
| 1143 |
% |
| 1144 |
% This macro strips all spaces out of the number (not just before and |
| 1145 |
% after). It takes a macro that will hold the result, followed by the |
| 1146 |
% number (as a macro or a group of actual digits). |
| 1147 |
% \begin{macrocode} |
| 1148 |
\def\MFPnospace@def#1#2{% |
| 1149 |
\edef#1{#2\space}\edef#1{\@xp\zap@space#1\@empty}}% |
| 1150 |
% \end{macrocode} |
| 1151 |
% |
| 1152 |
% This is the process that splits a number into parts. The biggest |
| 1153 |
% difficulty is obtaining the sign. All four arguments are macros, with |
| 1154 |
% the first one holding the number. Following that are the macros to hold |
| 1155 |
% the sign, integer and fractional parts. |
| 1156 |
% \begin{macrocode} |
| 1157 |
\def\MFPprocess@into@parts#1#2#3#4{% |
| 1158 |
\@xp\MFPsplit@dot#1..\MFP@end #3#4% |
| 1159 |
% \end{macrocode} |
| 1160 |
% |
| 1161 |
% At this point \arg3 holds the part before the dot (or the whole thing |
| 1162 |
% if there was no dot) and \arg4 holds the part after the dot, (or |
| 1163 |
% nothing). Now is the first place where having at most eight digits |
| 1164 |
% simplifies things. Note that \arg3 could contain any number of |
| 1165 |
% consecutive signs followed by up to eight digits. It could be zero or |
| 1166 |
% empty, so to avoid losing the sign we append a \texttt{1} (for up to |
| 1167 |
% nine digits). We temporarily define the sign based on the result, but |
| 1168 |
% may need to drop it if both the integer and fractional parts are zero. |
| 1169 |
% |
| 1170 |
% Prepending a zero to the fractional part pemits it to be empty. |
| 1171 |
% In the final \cs{edef}, \arg3 is made positive. |
| 1172 |
% \begin{macrocode} |
| 1173 |
\ifnum#31<0 \def#2{-1}% |
| 1174 |
\else \def#2{1}% |
| 1175 |
\fi |
| 1176 |
\ifnum #30=0 |
| 1177 |
\def#3{0}% |
| 1178 |
\ifnum 0#4=0 \def#2{0}\fi |
| 1179 |
\fi |
| 1180 |
\edef#3{\number \ifnum #2<0 -\fi#3}}% |
| 1181 |
% \end{macrocode} |
| 1182 |
% |
| 1183 |
% This only copies the parts before and after the dot, \arg1 and \arg2, |
| 1184 |
% into macros \arg4 and \arg5. |
| 1185 |
% \begin{macrocode} |
| 1186 |
\def\MFPsplit@dot#1.#2.#3\MFP@end#4#5{\edef#4{#1}\edef#5{#2}}% |
| 1187 |
% \end{macrocode} |
| 1188 |
% |
| 1189 |
% This is used to pad the fractional part to eight places with zeros. If |
| 1190 |
% a number with more than eight digits survives to this point, it gets |
| 1191 |
% truncated. |
| 1192 |
% \begin{macrocode} |
| 1193 |
\def\MFP@padtoeight#1{% |
| 1194 |
\edef#1{\@xp\MFP@eightofmany#100000000\MFP@end}}% |
| 1195 |
% \end{macrocode} |
| 1196 |
% |
| 1197 |
% These take operands off the stack. We know already that there are no |
| 1198 |
% spaces and that the fractional part has eight digits. |
| 1199 |
% \begin{macrocode} |
| 1200 |
\def\MFPgetoperand@x{\Rpop\MFP@x@Val |
| 1201 |
\MFPprocess@into@parts\MFP@x@Val\MFP@x@Sgn\MFP@x@Int\MFP@x@Frc}% |
| 1202 |
\def\MFPgetoperand@y{\Rpop\MFP@y@Val |
| 1203 |
\MFPprocess@into@parts\MFP@y@Val\MFP@y@Sgn\MFP@y@Int\MFP@y@Frc}% |
| 1204 |
% \end{macrocode} |
| 1205 |
% |
| 1206 |
% Concatenate an argument (or two) to the front of stack. The material |
| 1207 |
% must already be in correct format. Note: `front' is where they go |
| 1208 |
% visually (i.e., leftmost) but it can be useful to imagine the stack |
| 1209 |
% growin rightward (or sometimes even downward). |
| 1210 |
% |
| 1211 |
% Note that the result of \verb$\MFP@cattwo{#1}{#2}$ is the same as |
| 1212 |
% \verb$\MFP@cat{#2}$ followed by \verb$\MFP@cat{#1}$. It seemed that |
| 1213 |
% reversing the arguments in \cs{MFP@Rcattwo} confused me more than this |
| 1214 |
% fact. |
| 1215 |
% \begin{macrocode} |
| 1216 |
\def\MFP@Rcat#1{\edef\MFP@Rstack{{#1}\MFP@Rstack}}% |
| 1217 |
\def\MFP@Rcattwo#1#2{\edef\MFP@Rstack{{#1}{#2}\MFP@Rstack}}% |
| 1218 |
% \end{macrocode} |
| 1219 |
% |
| 1220 |
% Convert from a signum (a number) to a sign ($-$ or nothing): |
| 1221 |
% \begin{macrocode} |
| 1222 |
\def\MFP@Sign#1{\ifnum#1<0 -\fi}% |
| 1223 |
\def\MFP@x@Sign{\MFP@Sign\MFP@x@Sgn}% |
| 1224 |
\def\MFP@y@Sign{\MFP@Sign\MFP@y@Sgn}% |
| 1225 |
\def\MFP@z@Sign{\MFP@Sign\MFP@z@Sgn}% |
| 1226 |
% \end{macrocode} |
| 1227 |
% |
| 1228 |
% Sometimes only parts of the number needs changing (used in CHS, ABS). |
| 1229 |
% This copies the integer and fractional parts of $x$ into $z$. |
| 1230 |
% \begin{macrocode} |
| 1231 |
\def\copyMFP@x{\edef\MFP@z@Int{\MFP@x@Int}\edef\MFP@z@Frc{\MFP@x@Frc}}% |
| 1232 |
% \end{macrocode} |
| 1233 |
% |
| 1234 |
% We use \cs{MFPpush@result} to put the result of internal operations onto |
| 1235 |
% the stack. For convenience, we also have it set the sign flags. |
| 1236 |
% \begin{macrocode} |
| 1237 |
\def\MFPpush@result{\MFP@Rchk\MFPcurr@Sgn\MFP@Rcat\MFP@z@Val}% |
| 1238 |
% \end{macrocode} |
| 1239 |
% |
| 1240 |
% When \op{pop} encounters an empty stack it gobbles the code that would |
| 1241 |
% perform the \op{pop} (\arg1) and defines the macro (\arg2) to contain |
| 1242 |
% \cs{EndofStack}. The default meaning for this macro is $0$. |
| 1243 |
% \begin{macrocode} |
| 1244 |
\def\if@EndofStack{% |
| 1245 |
\ifx\MFP@EOS\MFP@Rstack |
| 1246 |
\@xp\@firstoftwo |
| 1247 |
\else |
| 1248 |
\@xp\@secondoftwo |
| 1249 |
\fi}% |
| 1250 |
% \end{macrocode} |
| 1251 |
% |
| 1252 |
% The macro \cs{Rpop} calls \cs{MFP@popit} followed by the contents of the |
| 1253 |
% stack, the token \cs{MFP@end} and the macro to \op{pop} into. If the stack is |
| 1254 |
% not empty, \cs{doMFP@popit} will read the first group \arg1 into that macro |
| 1255 |
% \arg3, and then redefine the stack to be the rest of the argument \arg2. |
| 1256 |
% If the stack is empty, \cs{doMFP@EOS} will equate the macro to |
| 1257 |
% \cs{EndofStack} (initialized to {\tt0.00000000}) after issuing an error |
| 1258 |
% message. |
| 1259 |
% \begin{macrocode} |
| 1260 |
\def\MFP@popit{\if@EndofStack\doMFP@EOS\doMFP@popit}% |
| 1261 |
\def\doMFP@EOS#1\MFP@end#2{\MFP@popempty@err\let#2\EndofStack}% |
| 1262 |
\def\doMFP@popit#1#2\MFP@end#3{\edef\MFP@Rstack{#2}\edef#3{#1}}% |
| 1263 |
% \end{macrocode} |
| 1264 |
% |
| 1265 |
% \subsection{The user-level operations} |
| 1266 |
% |
| 1267 |
% All operations that can be done on arguments as well as the stack will |
| 1268 |
% have a common format: The stack version pops the requisite numbers and |
| 1269 |
% splits them into internal macros (\cs{MFPgetoperand@*}), runs an internal |
| 1270 |
% command that operates on these internal macros, then ``pushes'' the result |
| 1271 |
% returned. The internal commands take care to return the result in proper |
| 1272 |
% form so we don't actually run \cs{Rpush}, but only \cs{MFPpush@result}. |
| 1273 |
% |
| 1274 |
% The operand version processes the operands into normalized form (as if |
| 1275 |
% pushed, using \cs{MFPparse@*}), then proceeds as in the stack version, but |
| 1276 |
% copies the result into the named macro instead of to the stack |
| 1277 |
% (\cs{MFPstore@result}). |
| 1278 |
% |
| 1279 |
% For unary operations we process one argument or stack element. We call |
| 1280 |
% it $x$ and use the \texttt{x} version of all macros. All internal |
| 1281 |
% commands (\arg1) return the results in \texttt{z} versions. |
| 1282 |
% |
| 1283 |
% \DescribeMacro{\MFPchk} |
| 1284 |
% The \cs{MFPchk} command examines its argument and sets a flag according to |
| 1285 |
% its sign. |
| 1286 |
% \begin{macrocode} |
| 1287 |
\def\MFPchk#1{% |
| 1288 |
\MFPparse@x{#1}% |
| 1289 |
\MFP@Rchk\MFP@x@Sgn}% |
| 1290 |
% \end{macrocode} |
| 1291 |
% |
| 1292 |
% We make \cs{MFP@Rchk} a little more general than is strictly needed here, |
| 1293 |
% by giving it an argument (instead of only examining \cs{MFP@x@Sgn}). This is |
| 1294 |
% so we can apply it to the results of operations (which would be in |
| 1295 |
% \cs{MFPcurr@Sgn}). |
| 1296 |
% \begin{macrocode} |
| 1297 |
\def\MFP@Rchk#1{% |
| 1298 |
\MFPclear@flags |
| 1299 |
\ifnum#1>0 \MFP@postrue |
| 1300 |
\else\ifnum#1<0 \MFP@negtrue |
| 1301 |
\else \MFP@zerotrue |
| 1302 |
\fi\fi}% |
| 1303 |
\def\MFPclear@flags{\MFP@zerofalse \MFP@negfalse \MFP@posfalse}% |
| 1304 |
% \end{macrocode} |
| 1305 |
% |
| 1306 |
% \DescribeMacro{\IFzero} |
| 1307 |
% \DescribeMacro{\IFneg} |
| 1308 |
% \DescribeMacro{\IFpos} |
| 1309 |
% These are the user interface to the internal \cs{ifMFP@zero}, |
| 1310 |
% \cs{ifMFP@neg}, \cs{ifMFP@pos} |
| 1311 |
% \begin{macrocode} |
| 1312 |
\def\IFzero{\ifMFP@zero\@xp\@firstoftwo\else\@xp\@secondoftwo\fi}% |
| 1313 |
\def\IFneg {\ifMFP@neg \@xp\@firstoftwo\else\@xp\@secondoftwo\fi}% |
| 1314 |
\def\IFpos {\ifMFP@pos \@xp\@firstoftwo\else\@xp\@secondoftwo\fi}% |
| 1315 |
\newif\ifMFP@zero \newif\ifMFP@neg \newif\ifMFP@pos |
| 1316 |
% \end{macrocode} |
| 1317 |
% |
| 1318 |
% Our comparison commands parallel the check-sign commands. They even |
| 1319 |
% \DescribeMacro{\MFPcmp} |
| 1320 |
% reuse the same internal booleans. The differences: the internal |
| 1321 |
% \DescribeMacro{\IFeq} |
| 1322 |
% \cs{MFP@Rcmp} doesn't take arguments and the comparison test is a little |
| 1323 |
% \DescribeMacro{\IFlt} |
| 1324 |
% more involved. We could simply subtract, which automatically sets the |
| 1325 |
% \DescribeMacro{\IFgt} |
| 1326 |
% internal booleans, but it is way more efficient to compare sizes |
| 1327 |
% directly. |
| 1328 |
% \begin{macrocode} |
| 1329 |
\newif\ifMFPdebug |
| 1330 |
\def\MFPcmp#1#2{\MFPparse@x{#1}\MFPparse@y{#2}\MFP@Rcmp}% |
| 1331 |
\def\MFP@Rcmp{\MFPclear@flags |
| 1332 |
\ifnum \MFP@x@Sign\MFP@x@Int>\MFP@y@Sign\MFP@y@Int\relax |
| 1333 |
\MFP@postrue |
| 1334 |
\else\ifnum \MFP@x@Sign\MFP@x@Int<\MFP@y@Sign\MFP@y@Int\relax |
| 1335 |
\MFP@negtrue |
| 1336 |
\else\ifnum \MFP@x@Sign\MFP@x@Frc>\MFP@y@Sign\MFP@y@Frc\relax |
| 1337 |
\MFP@postrue |
| 1338 |
\else\ifnum \MFP@x@Sign\MFP@x@Frc<\MFP@y@Sign\MFP@y@Frc\relax |
| 1339 |
\MFP@negtrue |
| 1340 |
\else |
| 1341 |
\MFP@zerotrue |
| 1342 |
\fi\fi\fi\fi}% |
| 1343 |
\let\IFeq\IFzero\let\IFlt\IFneg \let\IFgt\IFpos |
| 1344 |
% \end{macrocode} |
| 1345 |
% |
| 1346 |
% Given an operation (\op{pop}, \op{chs}, or whatever), the stack version will |
| 1347 |
% have the same name with ``\texttt{R}'' (for ``real'') prepended. The operand |
| 1348 |
% versions will have the same name with ``\texttt{MFP}'' prepended. The |
| 1349 |
% internal version has the same name as the stack version, with an |
| 1350 |
% ``\texttt{MFP@}'' prepended. |
| 1351 |
% |
| 1352 |
% The unary operations are: |
| 1353 |
% \begin{description} |
| 1354 |
% \item[chs] change sign of $x$. |
| 1355 |
% \item[abs] absolute value of $x$. |
| 1356 |
% \item[dbl] double $x$. |
| 1357 |
% \item[halve] halve $x$. |
| 1358 |
% \item[sgn] $+1$, $-1$ or $0$ depending on the sign of $x$. |
| 1359 |
% \item[sq] square $x$. |
| 1360 |
% \item[int] zero out the fractional part of $x$. |
| 1361 |
% \item[frac] zero out the integer part of $x$. |
| 1362 |
% \item[floor] largest integer not exceeding $x$. |
| 1363 |
% \item[ceil] smallest integer not less than $x$. |
| 1364 |
% \end{description} |
| 1365 |
% |
| 1366 |
% The binary operations are ($x$ represents the first and $y$ second): |
| 1367 |
% \begin{description} |
| 1368 |
% \item[add] add $x$ and $y$. |
| 1369 |
% \item[sub] subtract $y$ from $x$. |
| 1370 |
% \item[mul] multiply $x$ and $y$. |
| 1371 |
% \item[div] divide $x$ by $y$. |
| 1372 |
% \end{description} |
| 1373 |
% |
| 1374 |
% There are also some operations that do not actually change any |
| 1375 |
% values, but may change the stack or the state of some boolean: |
| 1376 |
% \begin{description} |
| 1377 |
% \item[cmp] compare $x$ and $y$ (stack version does not change stack). |
| 1378 |
% \item[chk] examine the sign of $x$ (stack version does not change stack). |
| 1379 |
% \item[dup] stack only, duplicate the top element of the stack. |
| 1380 |
% \item[push] stack only, put a value onto the top of the stack. |
| 1381 |
% \item[pop] stack only, remove the top element of the stack, |
| 1382 |
% store it in a variable. |
| 1383 |
% \item[exch] stack only, exchange top two elements of the stack. |
| 1384 |
% \end{description} |
| 1385 |
% |
| 1386 |
% \DescribeMacro{\startMFPprogram} |
| 1387 |
% The purpose of \cs{startMFPprogram} is to start the group, inside of |
| 1388 |
% which all the stack operations can be used. The ensuing |
| 1389 |
% \DescribeMacro{\stopMFPprogram} |
| 1390 |
% \cs{stopMFPprogram} closes the group. |
| 1391 |
% \begin{macrocode} |
| 1392 |
\def\startMFPprogram{% |
| 1393 |
\begingroup |
| 1394 |
% \end{macrocode} |
| 1395 |
% |
| 1396 |
% \DescribeMacro{\Rchs} |
| 1397 |
% \DescribeMacro{\Rabs} |
| 1398 |
% \DescribeMacro{\Rdbl} |
| 1399 |
% \DescribeMacro{\Rhalve} |
| 1400 |
% \DescribeMacro{\Rsgn} |
| 1401 |
% Then we give definitions to all the stack-based macros. |
| 1402 |
% These definitions are all lost after the group ends. |
| 1403 |
% |
| 1404 |
% \DescribeMacro{\Rsq} |
| 1405 |
% \DescribeMacro{\Rinv} |
| 1406 |
% \DescribeMacro{\Rint} |
| 1407 |
% \DescribeMacro{\Rfrac} |
| 1408 |
% \DescribeMacro{\Rfloor} |
| 1409 |
% \DescribeMacro{\Rceil} |
| 1410 |
% \DescribeMacro{\Rincr} |
| 1411 |
% \DescribeMacro{\Rdecr} |
| 1412 |
% \DescribeMacro{\Rzero} |
| 1413 |
% We start with the unary operations. Note that all they do is call a |
| 1414 |
% wrapper macro \cs{MFP@stack@Unary} with an argument which is the internal |
| 1415 |
% version of the command. |
| 1416 |
% \begin{macrocode} |
| 1417 |
\def\Rchs {\MFP@stack@Unary\MFP@Rchs}% |
| 1418 |
\def\Rabs {\MFP@stack@Unary\MFP@Rabs}% |
| 1419 |
\def\Rdbl {\MFP@stack@Unary\MFP@Rdbl}% |
| 1420 |
\def\Rhalve{\MFP@stack@Unary\MFP@Rhalve}% |
| 1421 |
\def\Rsgn {\MFP@stack@Unary\MFP@Rsgn}% |
| 1422 |
\def\Rsq {\MFP@stack@Unary\MFP@Rsq}% |
| 1423 |
\def\Rinv {\MFP@stack@Unary\MFP@Rinv}% |
| 1424 |
\def\Rint {\MFP@stack@Unary\MFP@Rint}% |
| 1425 |
\def\Rfrac {\MFP@stack@Unary\MFP@Rfrac}% |
| 1426 |
\def\Rfloor{\MFP@stack@Unary\MFP@Rfloor}% |
| 1427 |
\def\Rceil {\MFP@stack@Unary\MFP@Rceil}% |
| 1428 |
\def\Rincr {\MFP@stack@Unary\MFP@Rincr}% |
| 1429 |
\def\Rdecr {\MFP@stack@Unary\MFP@Rdecr}% |
| 1430 |
\def\Rzero {\MFP@stack@Unary\MFP@Rzero}% |
| 1431 |
% \end{macrocode} |
| 1432 |
% |
| 1433 |
% \DescribeMacro{\Radd} |
| 1434 |
% \DescribeMacro{\Rsub} |
| 1435 |
% \DescribeMacro{\Rmul} |
| 1436 |
% \DescribeMacro{\Rmpy} |
| 1437 |
% \DescribeMacro{\Rdiv} |
| 1438 |
% \DescribeMacro{\Rmin} |
| 1439 |
% \DescribeMacro{\Rmax} |
| 1440 |
% Then the binary operations, which again call a wrapper around |
| 1441 |
% the internal version. |
| 1442 |
% \begin{macrocode} |
| 1443 |
\def\Radd {\MFP@stack@Binary\MFP@Radd}% |
| 1444 |
\def\Rmul {\MFP@stack@Binary\MFP@Rmul}% |
| 1445 |
\let\Rmpy\Rmul |
| 1446 |
\def\Rsub {\MFP@stack@Binary\MFP@Rsub}% |
| 1447 |
\def\Rdiv {\MFP@stack@Binary\MFP@Rdiv}% |
| 1448 |
\def\Rmin {\MFP@stack@Binary\MFP@Rmin}% |
| 1449 |
\def\Rmax {\MFP@stack@Binary\MFP@Rmax}% |
| 1450 |
% \end{macrocode} |
| 1451 |
% |
| 1452 |
% \DescribeMacro{\Rnoop} |
| 1453 |
% \DescribeMacro{\Rcmp} |
| 1454 |
% \DescribeMacro{\Rchk} |
| 1455 |
% \DescribeMacro{\Rpush} |
| 1456 |
% \DescribeMacro{\Rpop} |
| 1457 |
% \DescribeMacro{\Rexch} |
| 1458 |
% \DescribeMacro{\Rdup} |
| 1459 |
% And finally some special commands. There is a no-op and commands for |
| 1460 |
% comparing, checking, and manipulation of the stack. Note that |
| 1461 |
% \cs{Rcmp} parses the last two elements on the stack, then puts them back |
| 1462 |
% before calling the internal command that operates on the parsed parts. |
| 1463 |
% The same is true of \cs{Rchk}, but only the last stack element is |
| 1464 |
% examined. |
| 1465 |
% \begin{macrocode} |
| 1466 |
\let\Rnoop\relax |
| 1467 |
\def\Rcmp{% |
| 1468 |
\MFPgetoperand@y\MFPgetoperand@x |
| 1469 |
\MFP@Rcattwo\MFP@y@Val\MFP@x@Val |
| 1470 |
\MFP@Rcmp}% |
| 1471 |
\def\Rchk{% |
| 1472 |
\MFPgetoperand@x |
| 1473 |
\MFP@Rcat\MFP@x@Val |
| 1474 |
\MFP@Rchk\MFP@x@Sgn}% |
| 1475 |
\def\Rpush##1{% |
| 1476 |
\MFPparse@x{##1}% |
| 1477 |
\edef\MFP@z@Val{\MFP@x@Sign\MFP@x@Int.\MFP@x@Frc}% |
| 1478 |
\edef\MFPcurr@Sgn{\MFP@x@Sgn}% |
| 1479 |
\MFPpush@result}% |
| 1480 |
\def\Rpop{\@xp\MFP@popit\MFP@Rstack\MFP@end}% |
| 1481 |
\def\Rexch{% |
| 1482 |
\Rpop\MFP@y@Val\Rpop\MFP@x@Val |
| 1483 |
\MFP@Rcattwo\MFP@x@Val\MFP@y@Val}% |
| 1484 |
\def\Rdup{% |
| 1485 |
\Rpop\MFP@x@Val |
| 1486 |
\MFP@Rcattwo\MFP@x@Val\MFP@x@Val}% |
| 1487 |
% \end{macrocode} |
| 1488 |
% |
| 1489 |
% If \file{mfpextra.tex} is input, then \cs{MFP@Rextra} makes the |
| 1490 |
% additional commands in that file available to an \mfp{} program. |
| 1491 |
% |
| 1492 |
% \DescribeMacro{\Global} |
| 1493 |
% \DescribeMacro{\GlobalStack} |
| 1494 |
% \DescribeMacro{\Export} |
| 1495 |
% \DescribeMacro{\ExportStack} |
| 1496 |
% The last four commands allow computed values to be made available |
| 1497 |
% outside the program group |
| 1498 |
% \begin{macrocode} |
| 1499 |
\MFP@Rextra |
| 1500 |
\let\Global\MFP@Global |
| 1501 |
\let\GlobalStack\MFP@GlobalStack |
| 1502 |
\let\Export\MFP@Export |
| 1503 |
\let\ExportStack\MFP@ExportStack}% |
| 1504 |
\def\stopMFPprogram{\@xp\endgroup\MFPprogram@returns}% |
| 1505 |
\let\MFP@Rextra\@empty |
| 1506 |
\let\MFPprogram@returns\@empty |
| 1507 |
% \end{macrocode} |
| 1508 |
% |
| 1509 |
% \DescribeMacro{\MFPchs} |
| 1510 |
% \DescribeMacro{\MFPabs} |
| 1511 |
% \DescribeMacro{\MFPdbl} |
| 1512 |
% \DescribeMacro{\MFPhalve} |
| 1513 |
% \DescribeMacro{\MFPsgn} |
| 1514 |
% \DescribeMacro{\MFPsq} |
| 1515 |
% \DescribeMacro{\MFPinv} |
| 1516 |
% Now we define the operand versions. These also are defined via a |
| 1517 |
% wrapper command that executes the very same internal commands as the |
| 1518 |
% stack versions. |
| 1519 |
% |
| 1520 |
% \DescribeMacro{\MFPint} |
| 1521 |
% \DescribeMacro{\MFPfrac} |
| 1522 |
% \DescribeMacro{\MFPfloor} |
| 1523 |
% \DescribeMacro{\MFPceil} |
| 1524 |
% \DescribeMacro{\MFPincr} |
| 1525 |
% \DescribeMacro{\MFPdecr} |
| 1526 |
% \DescribeMacro{\MFPzero} |
| 1527 |
% \DescribeMacro{\MFPstore} |
| 1528 |
% First the unary operations. |
| 1529 |
% \begin{macrocode} |
| 1530 |
\def\MFPchs {\MFP@op@Unary\MFP@Rchs}% |
| 1531 |
\def\MFPabs {\MFP@op@Unary\MFP@Rabs}% |
| 1532 |
\def\MFPdbl {\MFP@op@Unary\MFP@Rdbl}% |
| 1533 |
\def\MFPhalve{\MFP@op@Unary\MFP@Rhalve}% |
| 1534 |
\def\MFPsgn {\MFP@op@Unary\MFP@Rsgn}% |
| 1535 |
\def\MFPsq {\MFP@op@Unary\MFP@Rsq}% |
| 1536 |
\def\MFPinv {\MFP@op@Unary\MFP@Rinv}% |
| 1537 |
\def\MFPint {\MFP@op@Unary\MFP@Rint}% |
| 1538 |
\def\MFPfrac {\MFP@op@Unary\MFP@Rfrac}% |
| 1539 |
\def\MFPfloor{\MFP@op@Unary\MFP@Rfloor}% |
| 1540 |
\def\MFPceil {\MFP@op@Unary\MFP@Rceil}% |
| 1541 |
\def\MFPincr {\MFP@op@Unary\MFP@Rincr}% |
| 1542 |
\def\MFPdecr {\MFP@op@Unary\MFP@Rdecr}% |
| 1543 |
\def\MFPzero {\MFP@op@Unary\MFP@Rzero}% |
| 1544 |
\def\MFPstore{\MFP@op@Unary\MFP@Rstore}% |
| 1545 |
% \end{macrocode} |
| 1546 |
% |
| 1547 |
% \DescribeMacro{\MFPadd} |
| 1548 |
% \DescribeMacro{\MFPsub} |
| 1549 |
% \DescribeMacro{\MFPmul} |
| 1550 |
% \DescribeMacro{\MFPmpy} |
| 1551 |
% \DescribeMacro{\MFPdiv} |
| 1552 |
% \DescribeMacro{\MFPmin} |
| 1553 |
% \DescribeMacro{\MFPmax} |
| 1554 |
% Then the binary operations. |
| 1555 |
% \begin{macrocode} |
| 1556 |
\def\MFPadd{\MFP@op@Binary\MFP@Radd}% |
| 1557 |
\def\MFPmul{\MFP@op@Binary\MFP@Rmul}% |
| 1558 |
\let\MFPmpy\MFPmul |
| 1559 |
\def\MFPsub{\MFP@op@Binary\MFP@Rsub}% |
| 1560 |
\def\MFPdiv{\MFP@op@Binary\MFP@Rdiv}% |
| 1561 |
\def\MFPmin{\MFP@op@Binary\MFP@Rmin}% |
| 1562 |
\def\MFPmax{\MFP@op@Binary\MFP@Rmax}% |
| 1563 |
% \end{macrocode} |
| 1564 |
% |
| 1565 |
% A \emph{nullary} operation is one that produces a result with no |
| 1566 |
% operand. Thus, it could return a fixed constant, or it could perform |
| 1567 |
% calculations that obtain input from the system (e.g., current time). At |
| 1568 |
% the moment we don't define any. |
| 1569 |
% \begin{macrocode} |
| 1570 |
\def\MFP@stack@Nullary#1{% |
| 1571 |
\MFP@subroutine{#1}\MFPpush@result}% |
| 1572 |
\def\MFP@op@Nullary#1{% |
| 1573 |
\MFP@subroutine{#1}\MFPstore@result}% |
| 1574 |
% \end{macrocode} |
| 1575 |
% |
| 1576 |
% These are the wrappers for unary operations. The operand versions have a |
| 1577 |
% second argument, the macro that stores the result. But this will be the |
| 1578 |
% argument of \cs{MFPstore@result}. |
| 1579 |
% \begin{macrocode} |
| 1580 |
\def\MFP@stack@Unary#1{% |
| 1581 |
\MFPgetoperand@x |
| 1582 |
\MFP@subroutine{#1}\MFPpush@result}% |
| 1583 |
\def\MFP@op@Unary#1#2{% |
| 1584 |
\MFPparse@x{#2}% |
| 1585 |
\MFP@subroutine{#1}\MFPstore@result}% |
| 1586 |
\def\MFPstore@result#1{\MFP@Rchk\MFPcurr@Sgn\edef#1{\MFP@z@Val}}% |
| 1587 |
% \end{macrocode} |
| 1588 |
% |
| 1589 |
% These are the wrappers for binary operations. The top level definitions |
| 1590 |
% are almost identical to those of the unary operations. The only difference |
| 1591 |
% is they \op{pop} or parse two operands. |
| 1592 |
% \begin{macrocode} |
| 1593 |
\def\MFP@stack@Binary#1{% |
| 1594 |
\MFPgetoperand@y \MFPgetoperand@x |
| 1595 |
\MFP@subroutine{#1}\MFPpush@result}% |
| 1596 |
\def\MFP@op@Binary#1#2#3{% |
| 1597 |
\MFPparse@x{#2}\MFPparse@y{#3}% |
| 1598 |
\MFP@subroutine{#1}\MFPstore@result}% |
| 1599 |
% \end{macrocode} |
| 1600 |
% |
| 1601 |
% \DescribeMacro{\MFPnoop} |
| 1602 |
% We end with a traditional, but generally useless command, the no-op, |
| 1603 |
% which does nothing. It doesn't even have a wrapper. |
| 1604 |
% \begin{macrocode} |
| 1605 |
\let\MFPnoop\relax |
| 1606 |
% \end{macrocode} |
| 1607 |
% |
| 1608 |
% \subsection{The internal computations} |
| 1609 |
% |
| 1610 |
% To change the sign or get the absolute value, we just need to set the |
| 1611 |
% value of \cs{MFP@x@Sgn}. |
| 1612 |
% \begin{macrocode} |
| 1613 |
\def\MFP@Rabs{% |
| 1614 |
\copyMFP@x \edef\MFP@z@Sgn{\ifnum\MFP@x@Sgn=0 0\else1\fi}}% |
| 1615 |
\def\MFP@Rchs{\copyMFP@x \edef\MFP@z@Sgn{\number-\MFP@x@Sgn}}% |
| 1616 |
% \end{macrocode} |
| 1617 |
% |
| 1618 |
% The doubling and halving operations are more efficient ways to |
| 1619 |
% multiply or divide a number by $2$. For doubling, copy $x$ to $y$ |
| 1620 |
% and add. For halving, we use basic \TeX{} integer division, more |
| 1621 |
% efficient than multiplying by $0.5$ and far more than using |
| 1622 |
% \cs{MFP@Rdiv}. |
| 1623 |
% |
| 1624 |
% In \cs{MFP@Rhalve}. we add $1$ to the fractional part for rounding |
| 1625 |
% purposes, and we move any odd 1 from the end of the integer part to the |
| 1626 |
% start of the fractional part. |
| 1627 |
% \begin{macrocode} |
| 1628 |
\def\MFP@Rdbl{\MFP@Rcopy xy\MFP@Radd}% |
| 1629 |
\def\MFP@Rhalve{% |
| 1630 |
\MFP@tempa\MFP@x@Int |
| 1631 |
\MFP@tempb\MFP@x@Frc\relax |
| 1632 |
\ifodd\MFP@tempb |
| 1633 |
\def\MFP@z@Und{5}% |
| 1634 |
\advance\MFP@tempb 1 |
| 1635 |
\ifnum\MFP@ttteight=\MFP@tempb |
| 1636 |
\MFP@tempb0 \advance\MFP@tempa1 |
| 1637 |
\fi |
| 1638 |
\fi |
| 1639 |
\ifodd \MFP@tempa |
| 1640 |
\advance\MFP@tempb \MFP@ttteight\relax |
| 1641 |
\fi |
| 1642 |
\divide\MFP@tempa 2 |
| 1643 |
\divide\MFP@tempb 2 |
| 1644 |
\MFP@Rloadz\MFP@x@Sgn\MFP@tempa\MFP@tempb}% |
| 1645 |
% \end{macrocode} |
| 1646 |
% |
| 1647 |
% The signum is $0.0$, $1.0$ or $-1.0$ to match the sign of $x$. |
| 1648 |
% \begin{macrocode} |
| 1649 |
\def\MFP@Rsgn{\MFP@Rloadz \MFP@x@Sgn{\ifnum\MFP@x@Sgn=0 0\else1\fi}0}% |
| 1650 |
% \end{macrocode} |
| 1651 |
% |
| 1652 |
% The squaring operation just calls \cs{MFP@Rmul} after copying $x$ to |
| 1653 |
% $y$. Its gain in efficiency over a multiplication is that it can skip |
| 1654 |
% preprocessing of the second (identical) operand. |
| 1655 |
% \begin{macrocode} |
| 1656 |
\def\MFP@Rsq{\MFP@Rcopy xy\MFP@Rmul}% |
| 1657 |
% \end{macrocode} |
| 1658 |
% |
| 1659 |
% The inversion operation just calls \cs{MFP@Rdiv} after copying $x$ to |
| 1660 |
% $y$ and $1$ to $x$. Its advantage over a divide is it skips the |
| 1661 |
% preprocessing of $1$ as an operand. |
| 1662 |
% \begin{macrocode} |
| 1663 |
\def\MFP@Rinv{\MFP@Rcopy xy\MFP@Rload x110\MFP@Rdiv}% |
| 1664 |
% \end{macrocode} |
| 1665 |
% |
| 1666 |
% Integer part: replace fractional part with zeros. |
| 1667 |
% \begin{macrocode} |
| 1668 |
\def\MFP@Rint{% |
| 1669 |
\MFP@Rloadz {\ifnum\MFP@x@Int=0 0\else\MFP@x@Sgn\fi}\MFP@x@Int 0}% |
| 1670 |
% \end{macrocode} |
| 1671 |
% |
| 1672 |
% Fractional part: replace integer part with a zero. |
| 1673 |
% \begin{macrocode} |
| 1674 |
\def\MFP@Rfrac{% |
| 1675 |
\MFP@Rloadz {\ifnum\MFP@x@Frc=0 0\else\MFP@x@Sgn\fi}0\MFP@x@Frc}% |
| 1676 |
% \end{macrocode} |
| 1677 |
% |
| 1678 |
% To increment and decrement by $1$, except in border cases, we need only |
| 1679 |
% address the integer part of a number. This doesn't seem so simple |
| 1680 |
% written out but, even so, it is more efficient than full-blown addition. |
| 1681 |
% It would be very slightly more efficient if \cs{MFP@Rdecr} did not call |
| 1682 |
% \cs{MFP@Rincr}, but instead was similarly coded. |
| 1683 |
% \begin{macrocode} |
| 1684 |
\def\MFP@Rincr{% |
| 1685 |
\ifnum\MFP@x@Sgn<0 |
| 1686 |
\ifcase\MFP@x@Int |
| 1687 |
\MFP@tempa\MFP@ttteight |
| 1688 |
\advance\MFP@tempa -\MFP@x@Frc\relax |
| 1689 |
\MFP@Rloadz 10\MFP@tempa |
| 1690 |
\or |
| 1691 |
\MFP@Rloadz{\ifnum\MFP@x@Frc=0 0\else -1\fi}0\MFP@x@Frc |
| 1692 |
\else |
| 1693 |
\MFP@tempa\MFP@x@Int |
| 1694 |
\advance\MFP@tempa -1 |
| 1695 |
\MFP@Rloadz{-1}\MFP@tempa\MFP@x@Frc |
| 1696 |
\fi |
| 1697 |
\else |
| 1698 |
\MFP@tempa\MFP@x@Int |
| 1699 |
\advance\MFP@tempa 1 |
| 1700 |
\MFP@Rloadz 1\MFP@tempa\MFP@x@Frc |
| 1701 |
\fi}% |
| 1702 |
\def\MFP@Rdecr{% |
| 1703 |
\edef\MFP@x@Sgn{\number -\MFP@x@Sgn}\MFP@Rincr |
| 1704 |
\edef\MFP@z@Sgn{\number -\MFP@z@Sgn}}% |
| 1705 |
\def\MFP@Rstore{\MFP@Rcopy xz}% |
| 1706 |
% \end{macrocode} |
| 1707 |
% |
| 1708 |
% The floor of a real number $x$ is the largest integer not larger than |
| 1709 |
% $x$. The ceiling is the smallest integer not less than $x$. For |
| 1710 |
% positive $x$, floor is the same as integer part. Not true for negative |
| 1711 |
% $x$. Example: $\mathop{\mathrm{int}}(-1.5) = -1$ but |
| 1712 |
% $\mathop{\mathrm{floor}}=-2$ |
| 1713 |
% |
| 1714 |
% We use the same code to get floor or ceiling, the |
| 1715 |
% appropriate inequality character being its argument. |
| 1716 |
% \begin{macrocode} |
| 1717 |
\def\MFP@Rfloorceil#1{% |
| 1718 |
\MFP@tempa\MFP@x@Int\relax |
| 1719 |
\ifnum \MFP@x@Sgn #10 |
| 1720 |
\ifnum\MFP@x@Frc=0 |
| 1721 |
\else |
| 1722 |
\advance\MFP@tempa1 |
| 1723 |
\fi |
| 1724 |
\fi |
| 1725 |
\MFP@Rloadz{\ifnum\MFP@x@Int=0 0\else\MFP@x@Sgn\fi}\MFP@tempa0}% |
| 1726 |
\def\MFP@Rfloor{\MFP@Rfloorceil<}% |
| 1727 |
\def\MFP@Rceil {\MFP@Rfloorceil>}% |
| 1728 |
% \end{macrocode} |
| 1729 |
% |
| 1730 |
% For multiplication, after the usual break into integer and fractional |
| 1731 |
% parts, we further split these parts into $4$-digit pieces with |
| 1732 |
% \cs{MFP@split}. The first argument (\arg1) holds the eight digit number, |
| 1733 |
% then \arg2 is a macro that will hold the top four digits and \arg3 will |
| 1734 |
% hold the bottom four. |
| 1735 |
% \begin{macrocode} |
| 1736 |
\def\MFP@split#1#2#3{% |
| 1737 |
\begingroup |
| 1738 |
\MFP@tempa#1\relax |
| 1739 |
\MFP@tempb\MFP@tempa |
| 1740 |
\divide\MFP@tempb by\MFP@tttfour |
| 1741 |
\edef#2{\number\MFP@tempb}% |
| 1742 |
\multiply\MFP@tempb by\MFP@tttfour |
| 1743 |
\advance\MFP@tempa-\MFP@tempb |
| 1744 |
\MFP@endgroup@after{% |
| 1745 |
\MFP@afterdef#2{#2}% |
| 1746 |
\MFP@afterdef#3{\number\MFP@tempa}% |
| 1747 |
}}% |
| 1748 |
% |
| 1749 |
\def\MFP@@split{% |
| 1750 |
\MFP@split\MFP@x@Int\MFP@x@Int@ii\MFP@x@Int@i |
| 1751 |
\MFP@split\MFP@x@Frc\MFP@x@Frc@i\MFP@x@Frc@ii |
| 1752 |
\MFP@split\MFP@y@Int\MFP@y@Int@ii\MFP@y@Int@i |
| 1753 |
\MFP@split\MFP@y@Frc\MFP@y@Frc@i\MFP@y@Frc@ii}% |
| 1754 |
% \end{macrocode} |
| 1755 |
% |
| 1756 |
% We will store the intermediate and final products in \cs{MFP@z@*}. Each one |
| 1757 |
% is ultimately reduced to four digits, like the parts of $x$ and $y$. As each |
| 1758 |
% base-$10000$ digit of $y$ is multiplied by a digit of $x$, we add the |
| 1759 |
% result to the appropriate digit of the partial result $z$. |
| 1760 |
% |
| 1761 |
% The underflow ends up in \cs{MFP@z@Frc@iv} and \cs{MFP@z@Frc@iii}. |
| 1762 |
% Overflow will be in \cs{MFP@z@Int@iii}. Unlike the rest, it can be up to |
| 1763 |
% eight digits because we do not need to carry results out of it. |
| 1764 |
% |
| 1765 |
% This command prepends zeros so a number fills four slots. Here \arg1 is |
| 1766 |
% a macro holding the value and it is redefined to contain the result. A |
| 1767 |
% macro that calls this should ensure that \arg1 is not empty and is less |
| 1768 |
% than 10,000. |
| 1769 |
% \begin{macrocode} |
| 1770 |
\def\makeMFP@fourdigits#1{% |
| 1771 |
\edef#1{\@xp\MFP@fifthofmany\number#1{}{0}{00}{000}\MFP@end\number#1}}% |
| 1772 |
% \end{macrocode} |
| 1773 |
% |
| 1774 |
% This is the same, but produces eight digits. Similarly \arg1 should be |
| 1775 |
% nonempty and less than 100,000,000. |
| 1776 |
% \begin{macrocode} |
| 1777 |
\def\makeMFP@eightdigits#1{% |
| 1778 |
\edef#1{\@xp\MFP@ninthofmany\number#1% |
| 1779 |
{}{0}{00}{000}{0000}{00000}{000000}{0000000}\MFP@end\number#1}}% |
| 1780 |
% \end{macrocode} |
| 1781 |
% |
| 1782 |
% The following macros implement carrying. The macros \cs{MFP@carrya} and |
| 1783 |
% \cs{MFP@carrym} should be followed by two macros that hold numbers. The |
| 1784 |
% first number can have too many digits. These macros remove extra digits |
| 1785 |
% from the front and add their value to the number in the second macro |
| 1786 |
% (the ``carry''). Both act by calling \cs{MFP@carry}, which is told the |
| 1787 |
% number of digits to keep via \arg1 (10,000 for four digits, |
| 1788 |
% 100,000,000 for eight). The ``\texttt{a}'' in \cs{MFP@carrya} is for |
| 1789 |
% addition and ``\texttt{m}'' is for multiplication, which indicates where |
| 1790 |
% these will mainly be used. |
| 1791 |
% \begin{macrocode} |
| 1792 |
\def\MFP@carrya{\MFP@carry\MFP@ttteight}% |
| 1793 |
\def\MFP@carrym{\MFP@carry\MFP@tttfour}% |
| 1794 |
\def\MFP@carry#1#2#3{% |
| 1795 |
\begingroup |
| 1796 |
\MFP@carryi{#1}#2#3% |
| 1797 |
\MFP@endgroup@after{% |
| 1798 |
\MFP@afterdef#3{\number\MFP@tempa}% |
| 1799 |
\MFP@afterdef#2{\number\MFP@tempb}% |
| 1800 |
}}% |
| 1801 |
% \end{macrocode} |
| 1802 |
% |
| 1803 |
% This is the ``internal'' carry. \arg1, \arg2, and \arg3 are as in |
| 1804 |
% \cs{MFP@carry}. Its advantage is that it can be used used where \arg2 |
| 1805 |
% and \arg3 are not macros, leaving the result in \cs{MFP@tempa} and |
| 1806 |
% \cs{MFP@tempb} with \cs{MFP@tempb} in the correct range, |
| 1807 |
% $[0,\mbox{\arg1})$. Its disadvantage is it does not protect temporary |
| 1808 |
% registers. Warning: do not use it with \arg2=\cs{MFP@tempa} and do not |
| 1809 |
% use it without grouping if you want to preserve the values in these |
| 1810 |
% temporary count registers. |
| 1811 |
% \begin{macrocode} |
| 1812 |
\def\MFP@carryi#1#2#3{% |
| 1813 |
\MFP@tempa=#3\relax |
| 1814 |
\MFP@tempb=#2\relax |
| 1815 |
\MFP@tempc=\MFP@tempb |
| 1816 |
\divide \MFP@tempc #1\relax |
| 1817 |
\advance \MFP@tempa \MFP@tempc |
| 1818 |
\multiply\MFP@tempc #1\relax |
| 1819 |
\advance \MFP@tempb -\MFP@tempc}% |
| 1820 |
% \end{macrocode} |
| 1821 |
% |
| 1822 |
% This adds \arg1 to \arg2, the result goes into macro \arg3. This does no |
| 1823 |
% checking. It is basicly used to add with macros instead of registers. |
| 1824 |
% \begin{macrocode} |
| 1825 |
\def\MFP@addone#1#2#3{% |
| 1826 |
\begingroup |
| 1827 |
\MFP@tempa#1% |
| 1828 |
\advance\MFP@tempa#2\relax |
| 1829 |
\MFP@endgroup@after{% |
| 1830 |
\MFP@afterdef#3{\number\MFP@tempa}% |
| 1831 |
}}% |
| 1832 |
% \end{macrocode} |
| 1833 |
% |
| 1834 |
% Multiply \arg1 by \cs{MFP@tempb} and add to \arg2. \cs{MFP@tempb} is one digit |
| 1835 |
% (base=10000) of $y$ in multiplying $x\times y$, \arg1 (usually a macro) |
| 1836 |
% holds one digit of $x$. \arg2 is a macro that will hold one digit of the |
| 1837 |
% final product $z$. The product is added to it (overflow is taken care of |
| 1838 |
% later by the carry routines). |
| 1839 |
% \begin{macrocode} |
| 1840 |
\def\MFP@multiplyone#1#2{% |
| 1841 |
\MFP@tempa#1% |
| 1842 |
\multiply\MFP@tempa\MFP@tempb |
| 1843 |
\advance\MFP@tempa#2% |
| 1844 |
\edef#2{\number\MFP@tempa}}% |
| 1845 |
% \end{macrocode} |
| 1846 |
% |
| 1847 |
% This does the above multiplication-addition for all four ``digits'' of |
| 1848 |
% $x$. This is where \cs{MFP@tempb} is initialized for \cs{MFP@multiplyone}. The |
| 1849 |
% first argument represents a digit of $y$, the remaining four arguments |
| 1850 |
% are macros representing the digits of $z$ that are involved in |
| 1851 |
% multiplying the digits of $x$ by \arg1. |
| 1852 |
% \begin{macrocode} |
| 1853 |
\def\MFP@multiplyfour#1#2#3#4#5{% |
| 1854 |
\MFP@tempb #1\relax |
| 1855 |
\MFP@multiplyone\MFP@x@Int@ii #2% |
| 1856 |
\MFP@multiplyone\MFP@x@Int@i #3% |
| 1857 |
\MFP@multiplyone\MFP@x@Frc@i #4% |
| 1858 |
\MFP@multiplyone\MFP@x@Frc@ii #5}% |
| 1859 |
% \end{macrocode} |
| 1860 |
% |
| 1861 |
% Now we begin the internal implementations of the binary operations. All |
| 1862 |
% four expect macros \cs{MFP@x@Sgn}, \cs{MFP@x@Int}, \cs{MFP@x@Frc}, \cs{MFP@y@Sgn}, |
| 1863 |
% \cs{MFP@y@Int} and \cs{MFP@y@Frc} to be the normalized parts of two real numbers |
| 1864 |
% $x$ and $y$. |
| 1865 |
% |
| 1866 |
% \cs{MFP@Rsub} just changes the sign of $y$ and then calls \cs{MFP@Radd}. |
| 1867 |
% |
| 1868 |
% \cs{MFP@Radd} checks whether $x$ and $y$ have same or different signs. In |
| 1869 |
% the first case we need only add absolute values and the sign of the |
| 1870 |
% result will match that of the operands. In the second case, finding the |
| 1871 |
% sign of the result is a little more involve (and ``borrowing'' may be |
| 1872 |
% needed). |
| 1873 |
% \begin{macrocode} |
| 1874 |
\def\MFP@Rsub{\edef\MFP@y@Sgn{\number-\MFP@y@Sgn}\MFP@Radd}% |
| 1875 |
\def\MFP@Radd{% |
| 1876 |
\MFP@tempa\MFP@x@Sgn |
| 1877 |
\multiply\MFP@tempa\MFP@y@Sgn\relax |
| 1878 |
\ifcase\MFP@tempa |
| 1879 |
\ifnum \MFP@x@Sgn=0 |
| 1880 |
\MFP@Rcopy yz% |
| 1881 |
\else |
| 1882 |
\MFP@Rcopy xz% |
| 1883 |
\fi |
| 1884 |
\or |
| 1885 |
\@xp\MFP@Radd@same |
| 1886 |
\else |
| 1887 |
\@xp\MFP@Radd@diff |
| 1888 |
\fi}% |
| 1889 |
% \end{macrocode} |
| 1890 |
% |
| 1891 |
% \cs{MFP@Radd@same} adds two numbers which have the same sign. The sign |
| 1892 |
% of the result is the common sign. The fractional and integer parts are |
| 1893 |
% added separately, then a carry is invoked. The overflow (\cs{MFP@z@Ovr}) |
| 1894 |
% could be only a single digit 0 or 1. |
| 1895 |
% \begin{macrocode} |
| 1896 |
\def\MFP@Radd@same{% |
| 1897 |
\MFP@addone\MFP@x@Frc\MFP@y@Frc\MFP@z@Frc |
| 1898 |
\MFP@addone\MFP@x@Int\MFP@y@Int\MFP@z@Int |
| 1899 |
\MFP@carrya\MFP@z@Frc\MFP@z@Int |
| 1900 |
\MFP@carrya\MFP@z@Int\MFP@z@Ovr |
| 1901 |
\makeMFP@eightdigits\MFP@z@Frc |
| 1902 |
\edef\MFP@z@Sgn{\MFP@x@Sgn}}% |
| 1903 |
% \end{macrocode} |
| 1904 |
% |
| 1905 |
% We are now adding two numbers with opposite sign. Since $x\ne 0$ this |
| 1906 |
% is the same as $\sgn(x)(|x| - |y|)$ . So we subtract absolute values, |
| 1907 |
% save the result in \cs{MFP@z@Sgn}, \cs{MFP@z@Int} and \cs{MFP@z@Frc} |
| 1908 |
% (with the last two nonnegative, as usual), then change the sign of |
| 1909 |
% \cs{MFP@z@Sgn} if \cs{MFP@x@Sgn} is negative. Since the difference |
| 1910 |
% between numbers in $[0,10^8)$ has absolute value in that range, there is |
| 1911 |
% no carrying. However, there may be borrowing. |
| 1912 |
% \begin{macrocode} |
| 1913 |
\def\MFP@Radd@diff{% |
| 1914 |
\MFP@addone\MFP@x@Frc{-\MFP@y@Frc}\MFP@z@Frc |
| 1915 |
\MFP@addone\MFP@x@Int{-\MFP@y@Int}\MFP@z@Int |
| 1916 |
% \end{macrocode} |
| 1917 |
% |
| 1918 |
% Now we need to establish the sign and arrange the borrow. The sign of |
| 1919 |
% the result is the sign of \cs{MFP@z@Int} unless it is 0; in that case |
| 1920 |
% it, is the sign of \cs{MFP@z@Frc}. There must be a simpler coding, |
| 1921 |
% though. |
| 1922 |
% \begin{macrocode} |
| 1923 |
\MFP@tempa=\MFP@z@Int |
| 1924 |
\MFP@tempb=\MFP@z@Frc\relax |
| 1925 |
\ifnum\MFP@tempa=0 \else \MFP@tempa=\MFP@Sign\MFP@tempa 1 \fi |
| 1926 |
\ifnum\MFP@tempb=0 \else \MFP@tempb=\MFP@Sign\MFP@tempb 1 \fi |
| 1927 |
\ifnum\MFP@tempa=0 \MFP@tempa=\MFP@tempb \fi |
| 1928 |
% \end{macrocode} |
| 1929 |
% |
| 1930 |
% Now we have the sign of $|x| - |y|$ in \cs{MFP@tempa}, and we multiply |
| 1931 |
% that sign by the sign of $x$ to get \cs{MFP@z@Sgn}. Then we multiply the |
| 1932 |
% current value of $z$ by that sign to get the absolute value, stored in |
| 1933 |
% \cs{MFP@tempa} and \cs{MFP@tempb}. |
| 1934 |
% \begin{macrocode} |
| 1935 |
\edef\MFP@z@Sgn{\number\MFP@x@Sign\MFP@tempa}% |
| 1936 |
\MFP@tempb\MFP@tempa |
| 1937 |
\multiply\MFP@tempa \MFP@z@Int |
| 1938 |
\multiply\MFP@tempb \MFP@z@Frc\relax |
| 1939 |
% \end{macrocode} |
| 1940 |
% |
| 1941 |
% What we should have now is a positive number which might still be |
| 1942 |
% represented with a negative fractional part. A human being performing |
| 1943 |
% the subtraction would have borrowed first. Being a computer, we do it |
| 1944 |
% last, and we're done. |
| 1945 |
% \begin{macrocode} |
| 1946 |
\ifnum\MFP@tempb<0 |
| 1947 |
\advance\MFP@tempb\MFP@ttteight |
| 1948 |
\advance\MFP@tempa-1 |
| 1949 |
\fi |
| 1950 |
\edef\MFP@z@Int{\number\MFP@tempa}% |
| 1951 |
\edef\MFP@z@Frc{\number\MFP@tempb}% |
| 1952 |
\makeMFP@eightdigits\MFP@z@Frc}% |
| 1953 |
% \end{macrocode} |
| 1954 |
% |
| 1955 |
% \cs{MFP@Rmul} first computes the (theoretical) sign of the product: if |
| 1956 |
% it is zero, return zero, otherwise provisionally set the sign of the product |
| 1957 |
% and call \cs{MFP@@Rmul}. |
| 1958 |
% \begin{macrocode} |
| 1959 |
\def\MFP@Rmul{% |
| 1960 |
\ifnum\MFP@x@Sgn=0 \MFP@Rzero |
| 1961 |
\else\ifnum\MFP@y@Sgn=0 \MFP@Rzero |
| 1962 |
\else \edef\MFP@z@Sgn{\number\MFP@x@Sign\MFP@y@Sgn}% |
| 1963 |
\@XP\MFP@@Rmul |
| 1964 |
\fi\fi}% |
| 1965 |
% \end{macrocode} |
| 1966 |
% |
| 1967 |
% \cs{MFP@@Rmul} first initializes the macros that will hold the |
| 1968 |
% base-10000 digits of $z$. Then it splits the four expected macros into |
| 1969 |
% eight macros that hold the base-10000 digits for each of $x$ and $y$. |
| 1970 |
% Then each digit of $y$ is used to multiply the four digits of $x$ and the |
| 1971 |
% results are added to corresponding digits of $z$. |
| 1972 |
% \begin{macrocode} |
| 1973 |
\def\MFP@@Rmul{% |
| 1974 |
\def\MFP@z@Frc@iv {0}\def\MFP@z@Frc@iii{0}% |
| 1975 |
\def\MFP@z@Frc@ii {0}\def\MFP@z@Frc@i {0}% |
| 1976 |
\def\MFP@z@Int@i {0}\def\MFP@z@Int@ii {0}% |
| 1977 |
\def\MFP@z@Int@iii{0}% |
| 1978 |
\MFP@@split |
| 1979 |
\MFP@multiplyfour \MFP@y@Frc@ii \MFP@z@Frc@i |
| 1980 |
\MFP@z@Frc@ii \MFP@z@Frc@iii \MFP@z@Frc@iv |
| 1981 |
\MFP@multiplyfour \MFP@y@Frc@i \MFP@z@Int@i |
| 1982 |
\MFP@z@Frc@i \MFP@z@Frc@ii \MFP@z@Frc@iii |
| 1983 |
\MFP@multiplyfour \MFP@y@Int@i \MFP@z@Int@ii |
| 1984 |
\MFP@z@Int@i \MFP@z@Frc@i \MFP@z@Frc@ii |
| 1985 |
\MFP@multiplyfour \MFP@y@Int@ii \MFP@z@Int@iii |
| 1986 |
\MFP@z@Int@ii \MFP@z@Int@i \MFP@z@Frc@i |
| 1987 |
% \end{macrocode} |
| 1988 |
% Now apply the carry routines on the underflow digits\dots |
| 1989 |
% \begin{macrocode} |
| 1990 |
\MFP@carrym\MFP@z@Frc@iv\MFP@z@Frc@iii |
| 1991 |
\MFP@carrym\MFP@z@Frc@iii\MFP@z@Frc@ii |
| 1992 |
% \end{macrocode} |
| 1993 |
% \dots and pause to round the lowest digit that will be kept\dots |
| 1994 |
% \begin{macrocode} |
| 1995 |
\ifnum\MFP@z@Frc@iii<5000 \else |
| 1996 |
\MFP@tempb\MFP@z@Frc@ii |
| 1997 |
\advance\MFP@tempb1 |
| 1998 |
\edef\MFP@z@Frc@ii{\number\MFP@tempb}% |
| 1999 |
\fi |
| 2000 |
% \end{macrocode} |
| 2001 |
% \dots and continue carrying. |
| 2002 |
% \begin{macrocode} |
| 2003 |
\MFP@carrym\MFP@z@Frc@ii\MFP@z@Frc@i |
| 2004 |
\MFP@carrym\MFP@z@Frc@i \MFP@z@Int@i |
| 2005 |
\MFP@carrym\MFP@z@Int@i \MFP@z@Int@ii |
| 2006 |
\MFP@carrym\MFP@z@Int@ii\MFP@z@Int@iii |
| 2007 |
% \end{macrocode} |
| 2008 |
% To end, we arrange for all macros to hold four digits (except |
| 2009 |
% \cs{MFP@z@Int@ii} and \cs{MFP@z@Int@iii} which don't need leading 0s) |
| 2010 |
% and load them into the appropriate 8-digit macros. The underflow digits |
| 2011 |
% are stored in \cs{MFP@z@Und} in case we ever need to examine them (we |
| 2012 |
% now do: in our unit conversion routine \cs{MFP@DPmul}), and the overflow |
| 2013 |
% in \cs{MFP@z@Ovr} in case we ever want to implement an overflow error. |
| 2014 |
% Theoretically $z \ne 0$, but it is possible that $z=0$ after reducing to |
| 2015 |
% eight places. If so, we must reset \cs{MFP@z@Sgn}. |
| 2016 |
% \begin{macrocode} |
| 2017 |
\makeMFP@fourdigits\MFP@z@Frc@iv |
| 2018 |
\makeMFP@fourdigits\MFP@z@Frc@iii |
| 2019 |
\makeMFP@fourdigits\MFP@z@Frc@ii |
| 2020 |
\makeMFP@fourdigits\MFP@z@Frc@i |
| 2021 |
\makeMFP@fourdigits\MFP@z@Int@i |
| 2022 |
\edef\MFP@z@Int{\number\MFP@z@Int@ii\MFP@z@Int@i}% |
| 2023 |
\edef\MFP@z@Frc{\MFP@z@Frc@i\MFP@z@Frc@ii}% |
| 2024 |
\edef\MFP@z@Ovr{\number\MFP@z@Int@iii}% |
| 2025 |
\edef\MFP@z@Und{\MFP@z@Frc@iii\MFP@z@Frc@iv}% |
| 2026 |
\ifnum\MFP@z@Int>0 |
| 2027 |
\else\ifnum\MFP@z@Frc>0 |
| 2028 |
\else \def\MFP@z@Sgn{0}% |
| 2029 |
\fi\fi}% |
| 2030 |
% \end{macrocode} |
| 2031 |
% |
| 2032 |
% For division, we will obtain the result one digit at a time until the |
| 2033 |
% $9$th digit after the decimal is found. That $9$th will be used to round |
| 2034 |
% to eight digits (and stored as underflow). We normalize the denominator |
| 2035 |
% by shifting left until the integer part is eight digits. We do the same for |
| 2036 |
% the numerator. The integer quotient of the integer parts will be one digit |
| 2037 |
% (possibly a 0). If the denominator is shifted $d$ digits left and the |
| 2038 |
% numerator $n$ digits left, the quotient will have to be shifted $n-d$ |
| 2039 |
% places right or $d-n$ places left. Since the result is supposed to have |
| 2040 |
% $9$ digits after the dot, our quotient needs $9+d-n+1$ total digits. |
| 2041 |
% Since $d$ can be as high as $15$ and $n$ as low as $0$, we could need |
| 2042 |
% $25$ repetitions. However, that extreme would put $15$ or $16$ digits in |
| 2043 |
% the integer part, a $7$ or $8$ digit overflow. (It can be argued that |
| 2044 |
% only $16$ significant digits should be retained in any case.) If $d$ is |
| 2045 |
% $0$ and $n$ is $15$ we would need $-5$ digits. That means the first |
| 2046 |
% nonzero digit is in the 15th or 16th place after the dot and the |
| 2047 |
% quotient is effectively zero. |
| 2048 |
% |
| 2049 |
% Here I explain why we normalize the parts in this way. If a numerator |
| 2050 |
% has the form $n_1.n_2$ and the denominator has the form $d_1.d_2$ then |
| 2051 |
% \TeX{} can easily obtain the integer part of $n_1/d_1$, because these |
| 2052 |
% are within its range for integers. The resulting quotient (let's call it |
| 2053 |
% $q_1$) is the largest integer satisfying $q_1d_1 \le n_1$. What we seek, |
| 2054 |
% however is the largest integer $q$ such that $q(d_1.d_2) \le n_1.n_2$. |
| 2055 |
% It can easily be shown that $q \le q_1$. It is true, but not so easily |
| 2056 |
% shown, that $q \ge q_1 - 1$. This is only true if $d_1$ is large enough, |
| 2057 |
% in our case it has to be at least five digits. Thus we only have to do one |
| 2058 |
% simple division and decide if we need to reduce the quotient by one. If |
| 2059 |
% we arrange for $d_1$ to have eight digits, then $q_1$ will be one digit and |
| 2060 |
% the test for whether we need to reduce it becomes easier. |
| 2061 |
% |
| 2062 |
% This test is done as follows. The first trial quotient, $q_1$, will work |
| 2063 |
% if |
| 2064 |
% \[ |
| 2065 |
% q_1 d_1 (10)^8 + q_1 d_2 \le n_1 (10)^8 + n_2 |
| 2066 |
% \] |
| 2067 |
% This means |
| 2068 |
% \begin{equation}\label{crucial} |
| 2069 |
% 0 \le (n_1 - q_1 d_1) (10)^8 + n_2 - q_1 d_2 . |
| 2070 |
% \end{equation} |
| 2071 |
% Since $d_2$ is no more than eight digits, $q_1 d_2$ is less than $9 |
| 2072 |
% (10)^8$. Inequality (\ref{crucial}) is therefore satisfied if $n_1 - q_1 |
| 2073 |
% d_1 \ge 9$. If that is not the case then the right side of |
| 2074 |
% (\ref{crucial}) is computable within \TeX's integer ranges and we can |
| 2075 |
% easily test the inequality. If the inequality holds, then $q = q_1$, |
| 2076 |
% otherwise $q = q_1 - 1$. |
| 2077 |
% |
| 2078 |
% Note also that when $q = q_1$, then both terms in (\ref{crucial}) |
| 2079 |
% (ignoring the $10^8$ factor) will be needed to calculate the remainder. |
| 2080 |
% If $q = q_1 - 1$, we simply add $d_1$ and $d_2$ to the respective parts. |
| 2081 |
% Thus we will save these values for that use. |
| 2082 |
% |
| 2083 |
% Now I need to get it organized. \cs{MFP@Rdiv} will have \cs{MFP@x@*} and |
| 2084 |
% \cs{MFP@y@*} available. One step (could be first or last). Is to calculate |
| 2085 |
% the sign. Let's do it first (because we need to check for zero anyway). |
| 2086 |
% |
| 2087 |
% We invoke an error message upon division by zero, but nevertheless return |
| 2088 |
% a value. By default it is $0$ for $0/0$ and the maximum possible real |
| 2089 |
% for $x/0$ when $x$ is not zero. If the numerator is zero and the |
| 2090 |
% denominator not, we could do nothing as $z$ was initialized to be zero. |
| 2091 |
% However, we play it safe by explicitly setting $z$ to zero. |
| 2092 |
% |
| 2093 |
% If neither is zero, we calculate the sign of the result and call |
| 2094 |
% \cs{MFP@@Rdiv} to divide the absolute values. |
| 2095 |
% \begin{macrocode} |
| 2096 |
\def\MFP@Rdiv{% |
| 2097 |
\ifnum\MFP@y@Sgn=0 \MFP@dividebyzero@err |
| 2098 |
\ifnum\MFP@x@Sgn=0 |
| 2099 |
\edef\MFP@z@Int{\ZeroOverZeroInt}% |
| 2100 |
\edef\MFP@z@Frc{\ZeroOverZeroFrac}% |
| 2101 |
\else |
| 2102 |
\edef\MFP@z@Int{\xOverZeroInt}% |
| 2103 |
\edef\MFP@z@Frc{\xOverZeroFrac}% |
| 2104 |
\fi |
| 2105 |
\edef\MFP@z@Sgn{\MFP@x@Sgn}% |
| 2106 |
\else\ifnum\MFP@x@Sgn=0 \MFP@Rzero |
| 2107 |
\else \edef\MFP@z@Sgn{\number\MFP@x@Sign\MFP@y@Sgn}\MFP@@Rdiv |
| 2108 |
\fi\fi}% |
| 2109 |
% \end{macrocode} |
| 2110 |
% |
| 2111 |
% Now we have two positive values to divide. Our first step is to shift |
| 2112 |
% the denominator ($y$) left and keep track of how many places. We store |
| 2113 |
% the shift in \cs{MFP@tempa}. This actually changes the value of $y$, |
| 2114 |
% but knowing the shift will give us the correct quotient in the end. |
| 2115 |
% |
| 2116 |
% We first arrange that \cs{MFP@y@Int} is nonzero by making it \cs{MFP@y@Frc} if |
| 2117 |
% it is zero (a shift of eight digits). Then the macro |
| 2118 |
% \cs{MFP@numdigits@toshift} computes $8$ minus the number of digits in |
| 2119 |
% \cs{MFP@y@Int}, which is how many positions left $y$ will be shifted. |
| 2120 |
% We then call \cs{MFP@doshift@y} on the concatenation of the digits in |
| 2121 |
% the integer and fractional parts (padded with zeros to ensure there are |
| 2122 |
% at least 16). All this macro does is read the first eight digits into |
| 2123 |
% \cs{MFP@y@Int} and the next eight into \cs{MFP@y@Frc}. |
| 2124 |
% \begin{macrocode} |
| 2125 |
\def\MFP@@Rdiv{% |
| 2126 |
\ifnum\MFP@y@Int=0 |
| 2127 |
\edef\MFP@y@Int{\number\MFP@y@Frc}% |
| 2128 |
\def\MFP@y@Frc{00000000}% |
| 2129 |
\MFP@tempa=8 |
| 2130 |
\else |
| 2131 |
\MFP@tempa=0 |
| 2132 |
\fi |
| 2133 |
\advance\MFP@tempa\MFP@numdigits@toshift\MFP@y@Int\relax |
| 2134 |
\@XP\MFP@doshift@y\@xp\MFP@y@Int\MFP@y@Frc0000000\MFP@end |
| 2135 |
% \end{macrocode} |
| 2136 |
% |
| 2137 |
% We repeat all that on the numerator $x$, except shifting its digits |
| 2138 |
% left means the final outcome will need a corresponding \emph{right} |
| 2139 |
% shift. We record that fact by reducing \cs{MFP@tempa}, which ends up |
| 2140 |
% holding the net shift necesary. |
| 2141 |
% |
| 2142 |
% This has the advantage that we know the result will be in the range |
| 2143 |
% $[0.1, 10)$. It also means we can reduce the number of places we will |
| 2144 |
% need to shift left as well as reduce the number of iterations of the |
| 2145 |
% loop that calculates the digits. |
| 2146 |
% \begin{macrocode} |
| 2147 |
\ifnum\MFP@x@Int=0 |
| 2148 |
\edef\MFP@x@Int{\number\MFP@x@Frc}% |
| 2149 |
\def\MFP@x@Frc{00000000}% |
| 2150 |
\advance\MFP@tempa -8 |
| 2151 |
\fi |
| 2152 |
\advance\MFP@tempa-\MFP@numdigits@toshift\MFP@x@Int\relax |
| 2153 |
\@XP\MFP@doshift@x\@xp\MFP@x@Int\MFP@x@Frc0000000\MFP@end |
| 2154 |
% \end{macrocode} |
| 2155 |
% |
| 2156 |
% Since our result will have at most one digit in the integer part, a |
| 2157 |
% rightward shift of $10$ places will make every digit $0$ including the |
| 2158 |
% rounding digit, so we return $0$. |
| 2159 |
% \begin{macrocode} |
| 2160 |
\ifnum\MFP@tempa<-9 |
| 2161 |
\MFP@Rzero |
| 2162 |
\else |
| 2163 |
% \end{macrocode} |
| 2164 |
% |
| 2165 |
% Now we perform the division, which is a loop repeated $10 + |
| 2166 |
% {}$\cs{MFP@tempa} times. Therefore, we add 10 to \cs{MFP@tempa} in |
| 2167 |
% \cs{MFP@tempf}, our loop counter. We also initialize the macro that |
| 2168 |
% will store the digits and then, after the division, shift and split it |
| 2169 |
% into parts. |
| 2170 |
% \begin{macrocode} |
| 2171 |
\MFP@tempf\MFP@tempa |
| 2172 |
\advance\MFP@tempf 10 |
| 2173 |
\def\MFP@z@digits{}% |
| 2174 |
\MFP@Rdivloop |
| 2175 |
\MFPshiftandsplit@z@digits |
| 2176 |
% \end{macrocode} |
| 2177 |
% |
| 2178 |
% The last remaining step is to round and carry and get the fractional |
| 2179 |
% part in the appropriate 8-digit form.. |
| 2180 |
% \begin{macrocode} |
| 2181 |
\ifnum\MFP@z@Und>4 |
| 2182 |
\MFP@addone\MFP@z@Frc1\MFP@z@Frc |
| 2183 |
\MFP@carrya\MFP@z@Frc\MFP@z@Int |
| 2184 |
\MFP@carrya\MFP@z@Int\MFP@z@Ovr |
| 2185 |
\makeMFP@eightdigits\MFP@z@Frc |
| 2186 |
\fi |
| 2187 |
\fi}% |
| 2188 |
% \end{macrocode} |
| 2189 |
% |
| 2190 |
% If \arg1 of \cs{MFP@numdigits@toshift}, has $n$ digits then |
| 2191 |
% \cs{MFP@numdigits@toshift} picks out the value $8-n$. \cs{MFP@doshift@x} |
| 2192 |
% reads the first eight digits into \cs{MFP@x@Int} and then pulls out eight more |
| 2193 |
% from the rest (\arg9) inside \cs{MFP@x@Frc}. The same with |
| 2194 |
% \cs{MFP@doshift@y}. |
| 2195 |
% \begin{macrocode} |
| 2196 |
\def\MFP@numdigits@toshift#1{\@xp\MFP@ninthofmany#101234567\MFP@end}% |
| 2197 |
\def\MFP@doshift@x#1#2#3#4#5#6#7#8#9\MFP@end{% |
| 2198 |
\def\MFP@x@Int{#1#2#3#4#5#6#7#8}% |
| 2199 |
\edef\MFP@x@Frc{\MFP@eightofmany#9\MFP@end}}% |
| 2200 |
\def\MFP@doshift@y#1#2#3#4#5#6#7#8#9\MFP@end{% |
| 2201 |
\def \MFP@y@Int{#1#2#3#4#5#6#7#8}% |
| 2202 |
\edef\MFP@y@Frc{\MFP@eightofmany#9\MFP@end}}% |
| 2203 |
% \end{macrocode} |
| 2204 |
% |
| 2205 |
% The loop counter is \cs{MFP@tempf}, \cs{MFP@tempa} is reserved for the |
| 2206 |
% shift required later, the quotient digit will be \cs{MFP@tempb}. The |
| 2207 |
% remainder will be calculated in \cs{MFP@tempc} and \cs{MFP@tempd}. |
| 2208 |
% \cs{MFP@tempe} will hold the value whose size determines whether the |
| 2209 |
% quotient needs to be reduced. |
| 2210 |
% \begin{macrocode} |
| 2211 |
\def\MFP@Rdivloop{% |
| 2212 |
\MFP@tempb\MFP@x@Int % \MFP@tempb = n_1 |
| 2213 |
\MFP@tempc\MFP@y@Int % \MFP@tempc = d_1 |
| 2214 |
\divide\MFP@tempb \MFP@tempc % \MFP@tempb = n_1/d_1 = q_1 |
| 2215 |
\multiply \MFP@tempc \MFP@tempb % \MFP@tempc = q_1 d_1 |
| 2216 |
\MFP@tempd \MFP@y@Frc % \MFP@tempd = d_2 |
| 2217 |
\multiply \MFP@tempd \MFP@tempb % \MFP@tempd = q_1 d_2 |
| 2218 |
\MFP@tempe \MFP@tempc |
| 2219 |
\advance \MFP@tempe -\MFP@x@Int\relax % \MFP@tempe = -n_1 + q_1 d_1 |
| 2220 |
\ifnum \MFP@tempe > -9 % n_1 - q_1 d_1 < 9 |
| 2221 |
\multiply \MFP@tempe\MFP@ttteight % -(n_1 - q_1 d_1)(10)^8 |
| 2222 |
\advance \MFP@tempe \MFP@tempd % add q_1 d_2 |
| 2223 |
\advance \MFP@tempe -\MFP@x@Frc\relax % add -n_2 |
| 2224 |
\ifnum \MFP@tempe>0 % Crucial inequality fails |
| 2225 |
\advance\MFP@tempb -1 % new q = q_1 - 1 |
| 2226 |
\advance\MFP@tempc -\MFP@y@Int % q_1 d_1 - d_1 = q d_1 |
| 2227 |
\advance\MFP@tempd -\MFP@y@Frc\relax% q_1 d_2 - d_2 = q d_2 |
| 2228 |
\fi |
| 2229 |
\fi |
| 2230 |
\edef\MFP@z@digits{\MFP@z@digits\number\MFP@tempb}% |
| 2231 |
% \end{macrocode} |
| 2232 |
% |
| 2233 |
% It remains to: |
| 2234 |
% \begin{itemize} |
| 2235 |
% \item Do the carry from \cs{MFP@tempd} to \cs{MFP@tempc}. Then |
| 2236 |
% \cs{MFP@tempc.}\cs{MFP@tempd} will represent $q\cdot y$. |
| 2237 |
% \item Subtract them from \cs{MFP@x@Int} and \cs{MFP@x@Frc} (i.e. remainder = |
| 2238 |
% $x - qy$). |
| 2239 |
% \item Borrow, if needed, and we will have the remainder in |
| 2240 |
% \cs{MFP@x@Int.}\cs{MFP@x@Frc}. |
| 2241 |
% \end{itemize} |
| 2242 |
% Then we decrement the loop counter, and decide whether to repeat this |
| 2243 |
% loop. If so, we need to shift the remainder right one digit (multiply |
| 2244 |
% by 10). We don't use \cs{MFP@carrya} since it requires macros; its |
| 2245 |
% internal code, \cs{MFP@carryi} just leaves the results in |
| 2246 |
% \cs{MFP@tempa.}\cs{MFP@tempb}. |
| 2247 |
% \begin{macrocode} |
| 2248 |
\begingroup |
| 2249 |
\MFP@carryi\MFP@ttteight\MFP@tempd\MFP@tempc |
| 2250 |
\MFP@endgroup@after{% |
| 2251 |
\MFP@tempc=\number\MFP@tempa |
| 2252 |
\MFP@tempd=\number\MFP@tempb\relax |
| 2253 |
}% |
| 2254 |
% subtract |
| 2255 |
\MFP@addone\MFP@x@Int{-\MFP@tempc}\MFP@x@Int |
| 2256 |
\MFP@addone\MFP@x@Frc{-\MFP@tempd}\MFP@x@Frc |
| 2257 |
% borrow |
| 2258 |
\ifnum\MFP@x@Frc<0 |
| 2259 |
\MFP@addone\MFP@x@Frc\MFP@ttteight\MFP@x@Frc |
| 2260 |
\MFP@addone\MFP@x@Int{-1}\MFP@x@Int |
| 2261 |
\fi |
| 2262 |
\advance\MFP@tempf -1 |
| 2263 |
\ifnum\MFP@tempf>0 |
| 2264 |
\edef\MFP@x@Int{\MFP@x@Int0}% |
| 2265 |
\edef\MFP@x@Frc{\MFP@x@Frc0}% |
| 2266 |
\MFP@carrya\MFP@x@Frc\MFP@x@Int |
| 2267 |
\@xp\MFP@Rdivloop |
| 2268 |
\fi}% |
| 2269 |
% \end{macrocode} |
| 2270 |
% |
| 2271 |
% Now \cs{MFPshiftandsplit@z@digits}. At this point, the digits of the |
| 2272 |
% quotient are stored in \cs{MFP@z@digits}. We need to shift the decimal |
| 2273 |
% \cs{MFP@tempa} places left, and perform the rounding. There are |
| 2274 |
% \cs{MFP@tempa}${}+10$ digits. This could be as little as $1$ or as great |
| 2275 |
% as $25$. In the first case \cs{MFP@tempa} is $-9$, and this (rightward) |
| 2276 |
% shift produces $0$ plus a rounding digit. In the latter case \cs{MFP@tempa} |
| 2277 |
% is $15$, and the shift produces $8$ digits overflow, an $8$-digit |
| 2278 |
% integer part, an $8$-digit fractional part and a rounding digit. In the |
| 2279 |
% example $0123456$, \cs{MFP@tempa}${}+10$ is $7$, so \cs{MFP@tempa} is $-3$. |
| 2280 |
% The shift produces $0.0001\,2345\,6$. The rounding digit ($6$) makes the |
| 2281 |
% answer $0.0001\,2346$. |
| 2282 |
% |
| 2283 |
% We take two cases: |
| 2284 |
% \begin{itemize} |
| 2285 |
% \item \cs{MFP@tempa}${}\le 7$, prepend $7-{}$\cs{MFP@tempa} zeros. The first |
| 2286 |
% $8$ digits will become the integer part, and there should be |
| 2287 |
% exactly $9$ more digits. |
| 2288 |
% \item \cs{MFP@tempa}${} > 7$, pluck \cs{MFP@tempa}${}-7$ digits for |
| 2289 |
% overflow, the next $8$ for integer part, leaving $9$ more digits |
| 2290 |
% \end{itemize} |
| 2291 |
% In either case, the $9$ last digits will be processed into a fractional |
| 2292 |
% part (with possible carry if the rounding increases it to $10^8$). |
| 2293 |
% |
| 2294 |
% After this, we will return to \cs{MFP@Rdiv} so overwriting \cs{MFP@temp*} |
| 2295 |
% won't cause any problems. |
| 2296 |
% \begin{macrocode} |
| 2297 |
\def\MFPshiftandsplit@z@digits{% |
| 2298 |
\advance \MFP@tempa -7 |
| 2299 |
\ifnum\MFP@tempa>0 |
| 2300 |
\def\MFP@z@Ovr{}% |
| 2301 |
\@xp\MFPget@Ovrdigits\MFP@z@digits\MFP@end |
| 2302 |
\else |
| 2303 |
\ifnum\MFP@tempa<-7 |
| 2304 |
\edef\MFP@z@digits{00000000\MFP@z@digits}% |
| 2305 |
\advance\MFP@tempa8 |
| 2306 |
\fi |
| 2307 |
\ifnum\MFP@tempa<-3 |
| 2308 |
\edef\MFP@z@digits{0000\MFP@z@digits}% |
| 2309 |
\advance\MFP@tempa4 |
| 2310 |
\fi |
| 2311 |
\edef\MFP@z@digits{% |
| 2312 |
\ifcase-\MFP@tempa\or |
| 2313 |
0\or |
| 2314 |
00\or |
| 2315 |
000\or |
| 2316 |
0000\else |
| 2317 |
00000% |
| 2318 |
\fi \MFP@z@digits}% |
| 2319 |
\@xp\MFPget@Intdigits\MFP@z@digits\MFP@end |
| 2320 |
\fi}% |
| 2321 |
% \end{macrocode} |
| 2322 |
% |
| 2323 |
% The macro \cs{MFPget@Ovrdigits} is a loop that loads the first \cs{MFP@tempa} |
| 2324 |
% digits of what follows into \cs{MFP@z@Ovr}. It does this one digit (\arg1) |
| 2325 |
% at a time. Once the counter reaches $0$, we call the macro that |
| 2326 |
% processes the integer part digits. |
| 2327 |
% \begin{macrocode} |
| 2328 |
\def\MFPget@Ovrdigits#1{% |
| 2329 |
\edef\MFP@z@Ovr{\MFP@z@Ovr#1}% |
| 2330 |
\advance\MFP@tempa -1 |
| 2331 |
\ifnum\MFP@tempa>0 |
| 2332 |
\@xp\MFPget@Ovrdigits |
| 2333 |
\else |
| 2334 |
\@xp\MFPget@Intdigits |
| 2335 |
\fi}% |
| 2336 |
% \end{macrocode} |
| 2337 |
% |
| 2338 |
% The macro \cs{MFPget@Intdigits} should have exactly 17 digits following it. |
| 2339 |
% It puts eight of them in \cs{MFP@z@Int}, then calls \cs{MFPget@Frcdigits} to |
| 2340 |
% read the fractional part. That requires exactly nine digits follow it, |
| 2341 |
% putting eight in \cs{MFP@z@Frc} and the last in \cs{MFP@z@Und}. Still, to |
| 2342 |
% allow a graceful exit should there be more, we gobble the rest of the |
| 2343 |
% digits. |
| 2344 |
% \begin{macrocode} |
| 2345 |
\def\MFPget@Intdigits#1#2#3#4#5#6#7#8{% |
| 2346 |
\def\MFP@z@Int{\number#1#2#3#4#5#6#7#8}% |
| 2347 |
\MFPget@Frcdigits}% |
| 2348 |
\def\MFPget@Frcdigits#1#2#3#4#5#6#7#8#9{% |
| 2349 |
\def\MFP@z@Frc{#1#2#3#4#5#6#7#8}% |
| 2350 |
\def\MFP@z@Und{#9}\gobbleto@MFP@end}% |
| 2351 |
% \end{macrocode} |
| 2352 |
% |
| 2353 |
% The max amd min operations simply run the compare operation and use |
| 2354 |
% and use the resultant booleans to copy $x$ or $y$ to $z$. |
| 2355 |
% \begin{macrocode} |
| 2356 |
\def\MFP@Rmax{% |
| 2357 |
\MFP@Rcmp \ifMFP@neg \MFP@Rcopy yz\else\MFP@Rcopy xz\fi}% |
| 2358 |
\def\MFP@Rmin{% |
| 2359 |
\MFP@Rcmp \ifMFP@pos \MFP@Rcopy yz\else\MFP@Rcopy xz\fi}% |
| 2360 |
% \end{macrocode} |
| 2361 |
% |
| 2362 |
% \subsection{Commands to format for printing} |
| 2363 |
% |
| 2364 |
% \DescribeMacro{\MFPtruncate} |
| 2365 |
% This first runs the parsing command so the fractional part has exactly |
| 2366 |
% eight digits. These become the arguments of \cs{MFP@@Rtrunc}, which just |
| 2367 |
% keeps the right number. For negative truncations we prepend zeros to the |
| 2368 |
% integer part so it too is exactly eight digits. These become the |
| 2369 |
% arguments of \cs{MFP@@iRtrunc}, which substitutes 0 for the last |
| 2370 |
% \texttt{-}\cs{MFP@tempa} of them. |
| 2371 |
% |
| 2372 |
% The macro to store the result in follows \arg2. It is read and |
| 2373 |
% defined by either \cs{MFP@Rtrunc} or \cs{MFP@iRtrunc}. |
| 2374 |
% \begin{macrocode} |
| 2375 |
\def\MFPtruncate#1#2{% |
| 2376 |
\begingroup |
| 2377 |
\MFP@tempa#1\relax |
| 2378 |
\MFPparse@x{#2}% |
| 2379 |
\ifnum\MFP@tempa<1 |
| 2380 |
\@xp\MFP@iRtrunc |
| 2381 |
\else |
| 2382 |
\@xp\MFP@Rtrunc |
| 2383 |
\fi}% |
| 2384 |
\def\MFP@Rtrunc#1{% |
| 2385 |
\edef\MFP@x@Frc{\@xp\MFP@@Rtrunc\MFP@x@Frc\MFP@end}% |
| 2386 |
\ifnum\MFP@x@Int=0 |
| 2387 |
\ifnum\MFP@x@Frc=0 |
| 2388 |
\def\MFP@x@Sgn{0}% |
| 2389 |
\fi |
| 2390 |
\fi |
| 2391 |
\MFP@endgroup@after{% |
| 2392 |
\MFP@afterdef#1{\MFP@x@Sign\MFP@x@Int.\MFP@x@Frc}}}% |
| 2393 |
\def\MFP@@Rtrunc#1#2#3#4#5#6#7#8#9\MFP@end{% |
| 2394 |
\ifcase\MFP@tempa\or |
| 2395 |
#1\or |
| 2396 |
#1#2\or |
| 2397 |
#1#2#3\or |
| 2398 |
#1#2#3#4\or |
| 2399 |
#1#2#3#4#5\or |
| 2400 |
#1#2#3#4#5#6\or |
| 2401 |
#1#2#3#4#5#6#7\else |
| 2402 |
#1#2#3#4#5#6#7#8\fi}% |
| 2403 |
\def\MFP@iRtrunc#1{% |
| 2404 |
\makeMFP@eightdigits\MFP@x@Int |
| 2405 |
\edef\MFP@x@Val{\number\MFP@x@Sign\@xp\MFP@@iRtrunc\MFP@x@Int\MFP@end}% |
| 2406 |
\MFP@endgroup@after{\MFP@afterdef#1{\MFP@x@Val}}}% |
| 2407 |
\def\MFP@@iRtrunc#1#2#3#4#5#6#7#8#9\MFP@end{% |
| 2408 |
\ifcase-\MFP@tempa |
| 2409 |
#1#2#3#4#5#6#7#8\or |
| 2410 |
#1#2#3#4#5#6#70\or |
| 2411 |
#1#2#3#4#5#600\or |
| 2412 |
#1#2#3#4#5000\or |
| 2413 |
#1#2#3#40000\or |
| 2414 |
#1#2#300000\or |
| 2415 |
#1#2000000\or |
| 2416 |
#10000000\else |
| 2417 |
00000000\fi}% |
| 2418 |
% \end{macrocode} |
| 2419 |
% |
| 2420 |
% \DescribeMacro{\MFPround} |
| 2421 |
% For rounding we simply add the appropriate fraction and truncate. |
| 2422 |
% The macro in which to store the result will follow \arg2, and be |
| 2423 |
% picked up by the \cs{MFPtruncate} command. |
| 2424 |
% \begin{macrocode} |
| 2425 |
\def\MFPround#1#2{% |
| 2426 |
\begingroup |
| 2427 |
\MFP@tempa#1\relax |
| 2428 |
\ifnum 0>\MFP@tempa |
| 2429 |
\edef\MFP@y@Tmp{% |
| 2430 |
\ifcase-\MFP@tempa\or |
| 2431 |
5\or |
| 2432 |
50\or |
| 2433 |
500\or |
| 2434 |
5000\or |
| 2435 |
50000\or |
| 2436 |
500000\or |
| 2437 |
5000000\else |
| 2438 |
50000000\fi |
| 2439 |
}% |
| 2440 |
\else |
| 2441 |
\edef\MFP@y@Tmp{% |
| 2442 |
\ifcase\MFP@tempa |
| 2443 |
.5\or |
| 2444 |
.05\or |
| 2445 |
.005\or |
| 2446 |
.0005\or |
| 2447 |
.00005\or |
| 2448 |
.000005\or |
| 2449 |
.0000005\or |
| 2450 |
.00000005\else |
| 2451 |
0\fi |
| 2452 |
}% |
| 2453 |
\fi |
| 2454 |
\MFPchk{#2}\ifMFP@neg\edef\MFP@y@Tmp{-\MFP@y@Tmp}\fi |
| 2455 |
\MFPadd{#2}\MFP@y@Tmp\MFP@z@Tmp |
| 2456 |
\MFP@endgroup@after{\MFP@afterdef\MFP@z@Tmp{\MFP@z@Tmp}}% |
| 2457 |
\MFPtruncate{#1}\MFP@z@Tmp}% |
| 2458 |
% \end{macrocode} |
| 2459 |
% |
| 2460 |
% \DescribeMacro{\MFPstrip} |
| 2461 |
% Stripping zeros from the right end of the fractional part. The star form |
| 2462 |
% differs only in the handling of a zero fractional part. So we check |
| 2463 |
% whether it is zero and when it is, we either append `\texttt{.0}' or |
| 2464 |
% nothing. The rest of the code grabs a digit at a time and stops when the |
| 2465 |
% rest are zero. |
| 2466 |
% \begin{macrocode} |
| 2467 |
\def\MFPstrip{% |
| 2468 |
\@ifstar{\MFP@strip{}}{\MFP@strip{.0}}}% |
| 2469 |
\def\MFP@strip#1#2#3{% |
| 2470 |
\MFPparse@x{#2}% |
| 2471 |
\ifnum \MFP@x@Frc=0 |
| 2472 |
\edef#3{\MFP@x@Sign\MFP@x@Int#1}% |
| 2473 |
\else |
| 2474 |
\edef#3{\MFP@x@Sign\MFP@x@Int.\@xp\MFP@@strip\MFP@x@Frc\MFP@end}% |
| 2475 |
\fi}% |
| 2476 |
\def\MFP@@strip#1#2\MFP@end{% |
| 2477 |
#1% |
| 2478 |
\ifnum 0#2>0 |
| 2479 |
\@xp\MFP@@strip |
| 2480 |
\else |
| 2481 |
\@xp\gobbleto@MFP@end |
| 2482 |
\fi#2\MFP@end}% |
| 2483 |
% \end{macrocode} |
| 2484 |
% |
| 2485 |
% \subsection{Miscellaneous} |
| 2486 |
% |
| 2487 |
% Here is the code that allows definitions to survive after |
| 2488 |
% \cs{stopMFPprogram}. The \cs{Global} variants are easiest. |
| 2489 |
% \begin{macrocode} |
| 2490 |
\def\MFP@Global#1{\toks@\@xp{#1}\xdef#1{\the\toks@}}% |
| 2491 |
\def\MFP@GlobalStack{\MFP@Global\MFP@Rstack}% |
| 2492 |
% \end{macrocode} |
| 2493 |
% |
| 2494 |
% The \cs{Export} command adds the command and its definition to a macro |
| 2495 |
% that is executed after the closing group of the program. |
| 2496 |
% \begin{macrocode} |
| 2497 |
\def\MFP@Export#1{% |
| 2498 |
\begingroup |
| 2499 |
\toks@\@xp{\MFPprogram@returns}% |
| 2500 |
\MFP@endgroup@after{% |
| 2501 |
\MFP@afterdef\MFPprogram@returns{\the\toks@ \MFP@afterdef#1{#1}}% |
| 2502 |
}}% |
| 2503 |
\def\MFP@ExportStack{\MFP@Export\MFP@Rstack}% |
| 2504 |
% \end{macrocode} |
| 2505 |
% |
| 2506 |
% The various operations \cs{MFP@R...} together make up a ``microcode'' in |
| 2507 |
% terms of which the stack language and the operand language are both |
| 2508 |
% defined. As a language in its own right, it lacks only convenient ways |
| 2509 |
% to move numbers around, as well as a few extra registers for saving |
| 2510 |
% intermediate results. In this language, numbers are represented by a |
| 2511 |
% three part data structure, consisting of a signum, an integer part and a |
| 2512 |
% fractional part. |
| 2513 |
% |
| 2514 |
% Here we define extra commands to remedy this lack, starting with a way |
| 2515 |
% to load a number (or rather, a three part data structure representing a |
| 2516 |
% number) directly into a register. Here \arg1 is a register name (we |
| 2517 |
% always us a single letter) and the remaining arguments are the signum, |
| 2518 |
% the integer part and the fractional part (automatically normalized to 8 |
| 2519 |
% digits). The ``register'' is just a set of three macros created from the |
| 2520 |
% name given. |
| 2521 |
% |
| 2522 |
% We make loading a number into a register a little more general than |
| 2523 |
% strictly needed, allowing the parts to be specified as anything \TeX{} |
| 2524 |
% recognizes as a number and allowing any register name. This generality |
| 2525 |
% might reduce efficiency but it simplifies code. Because register |
| 2526 |
% \reg{z} is by far the most common one to load, we make more efficient |
| 2527 |
% version of it. |
| 2528 |
% \begin{macrocode} |
| 2529 |
\def\MFP@Rload #1#2#3#4{% |
| 2530 |
\@xp\edef\csname MFP@#1@Sgn\endcsname{\number#2}% |
| 2531 |
\@xp\edef\csname MFP@#1@Int\endcsname{\number#3}% |
| 2532 |
\@xp\edef\csname MFP@#1@Frc\endcsname{\number#4}% |
| 2533 |
\@xp\makeMFP@eightdigits\csname MFP@#1@Frc\endcsname}% |
| 2534 |
\def\MFP@Rcopy#1#2{% |
| 2535 |
\MFP@Rload #2{\csname MFP@#1@Sgn\endcsname}% |
| 2536 |
{\csname MFP@#1@Int\endcsname}% |
| 2537 |
{\csname MFP@#1@Frc\endcsname}}% |
| 2538 |
\def\MFP@Rloadz#1#2#3{% |
| 2539 |
\edef\MFP@z@Sgn{\number#1}% |
| 2540 |
\edef\MFP@z@Int{\number#2}% |
| 2541 |
\edef\MFP@z@Frc{\number#3}% |
| 2542 |
\makeMFP@eightdigits\MFP@z@Frc}% |
| 2543 |
% \end{macrocode} |
| 2544 |
% |
| 2545 |
% \DescribeMacro{\MFPpi} |
| 2546 |
% These are some miscellaneous constants. The 8-digit approximation to |
| 2547 |
% $\pi$, is \cs{MFPpi} and the constant mathematicians call $e$ is |
| 2548 |
% \DescribeMacro{\MFPe} |
| 2549 |
% \cs{MFPe}. Finally, the golden ratio (often called $\phi$) is obtained |
| 2550 |
% by |
| 2551 |
% \DescribeMacro{\MFPphi} |
| 2552 |
% \cs{MFPphi}. |
| 2553 |
% \begin{macrocode} |
| 2554 |
\def\MFPpi{3.14159265}% |
| 2555 |
\def\MFPe{2.71828183}% |
| 2556 |
\def\MFPphi{1.61803399}% |
| 2557 |
% \end{macrocode} |
| 2558 |
% Load (conditionally) \file{mfpextra.tex}. |
| 2559 |
% \begin{macrocode} |
| 2560 |
\MFP@loadextra |
| 2561 |
\MFP@finish |
| 2562 |
%</sty> |
| 2563 |
% \end{macrocode} |
| 2564 |
% |
| 2565 |
% \section{Extras}\label{extras} |
| 2566 |
% |
| 2567 |
% The extras consist so far of sine, cosine, angle, logarithm, powers, |
| 2568 |
% square root, and random number. For completeness, here is the table of |
| 2569 |
% user-level commands available. |
| 2570 |
% |
| 2571 |
% \medskip |
| 2572 |
% \centerline{% |
| 2573 |
% \begin{tabular}{lp{3in}} |
| 2574 |
% \textit{Operand versions}&\\[3pt] |
| 2575 |
% \hline\hline |
| 2576 |
% \textbf{Command}&\textbf{operation}\\ |
| 2577 |
% \hline |
| 2578 |
% \SpecialUsageIndex{\MFPsin}^^A |
| 2579 |
% \cs{MFPsin}\mmarg{num}\cs{macro}& |
| 2580 |
% Stores $\sin(\meta{num})$ in \cs{macro}, where \meta{num} is an |
| 2581 |
% angle in degrees.\\ |
| 2582 |
% \SpecialUsageIndex{\MFPcos}^^A |
| 2583 |
% \cs{MFPcos}\mmarg{num}\cs{macro}& |
| 2584 |
% Stores $\cos(\meta{num})$ in \cs{macro}, where \meta{num} is an |
| 2585 |
% angle in degrees.\\ |
| 2586 |
% \SpecialUsageIndex{\MFPangle}^^A |
| 2587 |
% \cs{MFPangle}\mmarg{$x$}\mmarg{$y$}\cs{macro}& |
| 2588 |
% Stores in \cs{macro} the polar angle coordinate $\theta$ of the point |
| 2589 |
% $(x,y)$, where $-180<\theta\le 180$.\\ |
| 2590 |
% \SpecialUsageIndex{\MFPrad}^^A |
| 2591 |
% \cs{MFPrad}\mmarg{num}\cs{macro}& |
| 2592 |
% The angle \meta{num} in degrees is converted to radians, |
| 2593 |
% and result is stored in \cs{macro}.\\ |
| 2594 |
% \SpecialUsageIndex{\MFPdeg}^^A |
| 2595 |
% \cs{MFPdeg}\mmarg{num}\cs{macro}& |
| 2596 |
% The angle \meta{num} in radians is converted to degrees, |
| 2597 |
% and result is stored in \cs{macro}.\\ |
| 2598 |
% \SpecialUsageIndex{\MFPlog}^^A |
| 2599 |
% \cs{MFPlog}\mmarg{num}\cs{macro}& |
| 2600 |
% Stores $\log(\meta{num})$ in \cs{macro} (base 10 logarithm).\\ |
| 2601 |
% \SpecialUsageIndex{\MFPln}^^A |
| 2602 |
% \cs{MFPln}\mmarg{num}\cs{macro}& |
| 2603 |
% Stores $\ln(\meta{num})$ in \cs{macro} (natural logarithm).\\ |
| 2604 |
% \SpecialUsageIndex{\MFPexp}^^A |
| 2605 |
% \cs{MFPexp}\mmarg{num}\cs{macro}& |
| 2606 |
% Stores $\exp(\meta{num})$ (i.e., $e^x$) in \cs{macro}.\\ |
| 2607 |
% \SpecialUsageIndex{\MFPsqrt}^^A |
| 2608 |
% \cs{MFPsqrt}\mmarg{num}\cs{macro}& |
| 2609 |
% Stores the square root of \meta{num} in \cs{macro}.\\ |
| 2610 |
% \SpecialUsageIndex{\MFPrand}^^A |
| 2611 |
% \cs{MFPrand}\mmarg{num}\cs{macro}& |
| 2612 |
% Stores a random real number between $0$ amd \meta{num} in |
| 2613 |
% \cs{macro}. If \meta{num} is negative, so is the result.\\ |
| 2614 |
% \SpecialUsageIndex{\MFPpow}^^A |
| 2615 |
% \cs{MFPpow}\mmarg{num}\mmarg{int}\cs{macro}& |
| 2616 |
% Stores the \meta{int} power of \meta{num} in \cs{macro}. The |
| 2617 |
% second operand must be an integer (positive or negative). |
| 2618 |
% \end{tabular}} |
| 2619 |
% |
| 2620 |
% In addition, there is \SpecialUsageIndex{\MFPsetseed}\cs{MFPsetseed} for |
| 2621 |
% setting the internal random number seed. It takes one argument, the seed |
| 2622 |
% value, which must be an integer greater than or equal to $1$ and less |
| 2623 |
% than or equal to $2^{31}-2 = 2\,147\,483\,646$. If the seed is set to |
| 2624 |
% zero or a negative number then the first use of the random number |
| 2625 |
% generator will replace it with a seed value based on the current time |
| 2626 |
% and date. The randum number seed is a global value. |
| 2627 |
% |
| 2628 |
% There are actually three random number generators and they can be |
| 2629 |
% selected with the commands |
| 2630 |
% \SpecialUsageIndex{\MFPrandgenA}\cs{MFPrandgenA}, |
| 2631 |
% \SpecialUsageIndex{\MFPrandgenB}\cs{MFPrandgenB}, or |
| 2632 |
% \SpecialUsageIndex{\MFPrandgenC}\cs{MFPrandgenC}. The first uses the |
| 2633 |
% code and multiplier value from the well-known macro file |
| 2634 |
% \file{random.tex}. It is the default. The other two use different |
| 2635 |
% multipliers which are alleged to have better statistical behavior. If |
| 2636 |
% any of these commands is used inside a group, that generator is in force |
| 2637 |
% during that group only. |
| 2638 |
% |
| 2639 |
% \bigskip |
| 2640 |
% \centerline{% |
| 2641 |
% \begin{tabular}{lp{3.9in}} |
| 2642 |
% \textit{Stack versions}&\\[3pt] |
| 2643 |
% \hline\hline |
| 2644 |
% \textbf{Command}&\textbf{operation}\\ |
| 2645 |
% \hline |
| 2646 |
% \SpecialUsageIndex{\Rsin}\cs{Rsin}& |
| 2647 |
% The number is interpreted as degrees, and its sine is computed.\\ |
| 2648 |
% \SpecialUsageIndex{\Rcos}\cs{Rcos}& |
| 2649 |
% The number is interpreted as degrees, and its cosine is computed.\\ |
| 2650 |
% \SpecialUsageIndex{\Rangle}\cs{Rangle}& |
| 2651 |
% The top two numbers are interpreted as coordinates of a point $P$ |
| 2652 |
% in the order they were pushed. The polar angle coordinate $\theta$ |
| 2653 |
% of $P$, with $-180 < \theta \le 180$ is computed.\\ |
| 2654 |
% \SpecialUsageIndex{\Rrad}\cs{Rrad}& |
| 2655 |
% The number of degrees is converted to radians.\\ |
| 2656 |
% \SpecialUsageIndex{\Rdeg}\cs{Rdeg}& |
| 2657 |
% The number of radians is converted to degrees.\\ |
| 2658 |
% \SpecialUsageIndex{\Rlog}\cs{Rlog}& |
| 2659 |
% Computes the base-10 logarithm.\\ |
| 2660 |
% \SpecialUsageIndex{\Rln}\cs{Rln}& |
| 2661 |
% Computes the natural logarithm.\\ |
| 2662 |
% \SpecialUsageIndex{\Rexp}\cs{Rexp}& |
| 2663 |
% Computes the exponential of the number (i.e., $e^x$).\\ |
| 2664 |
% \SpecialUsageIndex{\Rsqrt}\cs{Rsqrt}& |
| 2665 |
% Computes the square root of the number.\\ |
| 2666 |
% \SpecialUsageIndex{\Rrand}\cs{Rrand}& |
| 2667 |
% Returns a random real number between $0$ and the number, keeping the |
| 2668 |
% sign.\\ |
| 2669 |
% \SpecialUsageIndex{\Rpow}\cs{Rpow}& |
| 2670 |
% Computes $x^y$. The last number pushed ($y$) must be an |
| 2671 |
% integer. |
| 2672 |
% \end{tabular}} |
| 2673 |
% |
| 2674 |
% \bigskip |
| 2675 |
% The user could easily convert between radians and degrees using |
| 2676 |
% multiplication and/or division. One could similarly convert between |
| 2677 |
% natural logarithms and base ten logarithms. The commands \cs{Rdeg}, |
| 2678 |
% \cs{Rrad}, \cs{Rlog} and \cs{Rln} (and their \cs{MFP...} counterparts) |
| 2679 |
% aim for more accurate results. |
| 2680 |
% |
| 2681 |
% \subsection{Loading the extras} |
| 2682 |
% |
| 2683 |
% \DescribeMacro{\Rsin}\DescribeMacro{\Rcos} |
| 2684 |
% \DescribeMacro{\Rangle} |
| 2685 |
% \DescribeMacro{\Rrad}\DescribeMacro{\Rdeg} |
| 2686 |
% \DescribeMacro{\Rlog}\DescribeMacro{\Rln} |
| 2687 |
% \DescribeMacro{\Rexp}\DescribeMacro{\Rsqrt} |
| 2688 |
% \DescribeMacro{\Rrand}\DescribeMacro{\Rpow} |
| 2689 |
% We start \file{mfpextra} with the hook \cs{MFP@Rextra} that |
| 2690 |
% \cs{startMFPprogram} will call to make available the extra operations |
| 2691 |
% defined here. If \file{minifp.sty} has been loaded, this macro is |
| 2692 |
% \cs{@empty}, otherwise it should be undefined. If it is undefined we |
| 2693 |
% load \file{minifp.sty}. If it is then not \cs{@empty} we assume |
| 2694 |
% \file{mfpextra.tex} was previously loaded and end input here. |
| 2695 |
% \begin{macrocode} |
| 2696 |
%<*extra> |
| 2697 |
% check if mfpextra already loaded: |
| 2698 |
\expandafter\ifx\csname MFP@xfinish\endcsname\relax |
| 2699 |
\else \expandafter\endinput\fi |
| 2700 |
\expandafter\edef\csname MFP@xfinish\endcsname{% |
| 2701 |
\catcode64=\the\catcode64 \space |
| 2702 |
\catcode46=\the\catcode46 \space |
| 2703 |
\catcode60=\the\catcode60 \space |
| 2704 |
\catcode62=\the\catcode62 \space}% |
| 2705 |
\catcode64=11 % @ |
| 2706 |
\catcode46=12 % . (period) |
| 2707 |
\catcode60=12 % < |
| 2708 |
\catcode62=12 % > |
| 2709 |
\ifx\MFP@Rextra\UndEfInEd \input minifp.sty \fi |
| 2710 |
\ifx\MFP@Rextra\@empty |
| 2711 |
\else |
| 2712 |
\immediate\write16{mfpextra.tex: already loaded.^^J}% |
| 2713 |
\MFP@xfinish |
| 2714 |
\expandafter\endinput |
| 2715 |
\fi |
| 2716 |
\immediate\write16{% |
| 2717 |
mfpextra.tex: extra operations for the MiniFP package.^^J}% |
| 2718 |
\def\MFP@Rextra{% |
| 2719 |
\def\Rcos {\MFP@stack@Unary\MFP@Rcos }% |
| 2720 |
\def\Rsin {\MFP@stack@Unary\MFP@Rsin }% |
| 2721 |
\def\Rangle{\MFP@stack@Binary\MFP@Rangle}% |
| 2722 |
\def\Rrad {\MFP@stack@Unary\MFP@Rrad }% |
| 2723 |
\def\Rdeg {\MFP@stack@Unary\MFP@Rdeg }% |
| 2724 |
\def\Rlog {\MFP@stack@Unary\MFP@Rlog }% |
| 2725 |
\def\Rln {\MFP@stack@Unary\MFP@Rln }% |
| 2726 |
\def\Rexp {\MFP@stack@Unary\MFP@Rexp }% |
| 2727 |
\def\Rsqrt {\MFP@stack@Unary\MFP@Rsqrt}% |
| 2728 |
\def\Rrand {\MFP@stack@Unary\MFP@Rrand}% |
| 2729 |
\def\Rpow {\MFP@stack@Binary\MFP@Rpow}}% |
| 2730 |
% \end{macrocode} |
| 2731 |
% |
| 2732 |
% \DescribeMacro{\MFPsin}\DescribeMacro{\MFPcos} |
| 2733 |
% \DescribeMacro{\MFPrad}\DescribeMacro{\MFPdeg} |
| 2734 |
% \DescribeMacro{\MFPlog}\DescribeMacro{\MFPln} |
| 2735 |
% \DescribeMacro{\MFPexp}\DescribeMacro{\MFPsqrt} |
| 2736 |
% \DescribeMacro{\MFPrand}\DescribeMacro{\MFPpow} |
| 2737 |
% Then the wrappers for the operand versions. |
| 2738 |
% \begin{macrocode} |
| 2739 |
\def\MFPcos {\MFP@op@Unary\MFP@Rcos }% |
| 2740 |
\def\MFPsin {\MFP@op@Unary\MFP@Rsin }% |
| 2741 |
\def\MFPangle {\MFP@op@Binary\MFP@Rangle}% |
| 2742 |
\def\MFPrad {\MFP@op@Unary\MFP@Rrad }% |
| 2743 |
\def\MFPdeg {\MFP@op@Unary\MFP@Rdeg }% |
| 2744 |
\def\MFPlog {\MFP@op@Unary\MFP@Rlog }% |
| 2745 |
\def\MFPln {\MFP@op@Unary\MFP@Rln }% |
| 2746 |
\def\MFPexp {\MFP@op@Unary\MFP@Rexp }% |
| 2747 |
\def\MFPsqrt {\MFP@op@Unary\MFP@Rsqrt}% |
| 2748 |
\def\MFPrand {\MFP@op@Unary\MFP@Rrand}% |
| 2749 |
\def\MFPpow {\MFP@op@Binary\MFP@Rpow}% |
| 2750 |
% \end{macrocode} |
| 2751 |
% |
| 2752 |
% \subsection{Error messages} |
| 2753 |
% |
| 2754 |
% These extra commands come with a few possible new warnings and errors. |
| 2755 |
% |
| 2756 |
% \DescribeMacro{\LogOfZeroInt} |
| 2757 |
% \DescribeMacro{\LogOfZeroFrac} |
| 2758 |
% Trying to take the logarithm of zero will result in an error message. |
| 2759 |
% If one allows \TeX{} to continue, the returned value will be negative, |
| 2760 |
% with an integer part whose absolute value is equal to the contents of |
| 2761 |
% \cs{LogOfZeroInt} and a fractional part equal to the contents of |
| 2762 |
% \cs{LogOfZeroFrac}. The defaults are both $99999999$. |
| 2763 |
% |
| 2764 |
% Trying to take the logarithm of a negative number will produce the |
| 2765 |
% warning |
| 2766 |
% \begin{verbatim} |
| 2767 |
% MFP warning: Log of a negative number is complex. |
| 2768 |
% Only the real part will be computed. \end{verbatim} |
| 2769 |
% The log of the absolute value is returned. |
| 2770 |
% |
| 2771 |
% Trying to take the square root of a negative number has similar |
| 2772 |
% behavior. It produces a warning and returns $0$. |
| 2773 |
% |
| 2774 |
% \SpecialUsageIndex{\MaxRealInt} |
| 2775 |
% \SpecialUsageIndex{\MaxRealFrac} |
| 2776 |
% Trying to take the exponential of a number larger than about $18.42$ |
| 2777 |
% will cause an error and the number returned has integer part |
| 2778 |
% $99999999$ and fractional part $99999999$. |
| 2779 |
% |
| 2780 |
% Trying to take a negative power of $0$ produces an error and returns |
| 2781 |
% the same value as trying to divide $1$ by $0$. |
| 2782 |
% |
| 2783 |
% Messages for errors related to impossible powers and logarithms. |
| 2784 |
% \begin{macrocode} |
| 2785 |
\def\MFP@logofzero@err{% |
| 2786 |
\MFP@errmsg{logarithm of zero}% |
| 2787 |
{You tried to take the logarithm of zero. What were you % |
| 2788 |
thinking? If you ^^Jcontinue, the value % |
| 2789 |
assigned will be -\LogOfZeroInt.\LogOfZeroFrac.}}% |
| 2790 |
\def\LogOfZeroInt {\MaxRealInt}% |
| 2791 |
\def\LogOfZeroFrac{\MaxRealFrac}% |
| 2792 |
\def\MFP@expoverflow@err{% |
| 2793 |
\MFP@errmsg{Power too large}% |
| 2794 |
{The power you tried to calculate is too large for % |
| 2795 |
8 digits. If you continue, ^^Jthe value assigned will be % |
| 2796 |
\MaxRealInt.\MaxRealFrac.}}% |
| 2797 |
\def\MFP@badpower@err{% |
| 2798 |
\MFP@errmsg{negative power of zero}% |
| 2799 |
{You tried to take a negative power of zero. What were you |
| 2800 |
thinking? If you ^^Jcontinue, the value assigned will be % |
| 2801 |
\xOverZeroInt.\xOverZeroFrac.}}% |
| 2802 |
% \end{macrocode} |
| 2803 |
% |
| 2804 |
% A debugging utility, \cs{MFPshowreg} displays the contents of a |
| 2805 |
% register. |
| 2806 |
% \begin{macrocode} |
| 2807 |
\def\MFPshowreg #1{% |
| 2808 |
\ifMFPdebug |
| 2809 |
\begingroup |
| 2810 |
\edef\theregister{% |
| 2811 |
#1 = \expandafter \MFP@Sign |
| 2812 |
\csname MFP@#1@Sgn\endcsname % |
| 2813 |
\csname MFP@#1@Int\endcsname.% |
| 2814 |
\csname MFP@#1@Frc\endcsname}% |
| 2815 |
\show\theregister |
| 2816 |
\endgroup |
| 2817 |
\fi}% |
| 2818 |
% \end{macrocode} |
| 2819 |
% |
| 2820 |
% \subsection{Sine and Cosine} |
| 2821 |
% |
| 2822 |
% For iterated code, the most common register to copy is $z$ and |
| 2823 |
% the most common place to copy it is to $x$ or $y$ so we |
| 2824 |
% make single commands to do those. |
| 2825 |
% \begin{macrocode} |
| 2826 |
\def\MFP@Rcopyz#1{\MFP@Rload {#1}\MFP@z@Sgn\MFP@z@Int\MFP@z@Frc}% |
| 2827 |
\def\MFP@Rcopyzx{\MFP@Rcopyz x}% |
| 2828 |
\def\MFP@Rcopyzy{\MFP@Rcopyz y}% |
| 2829 |
% \end{macrocode} |
| 2830 |
% |
| 2831 |
% Our code assumes the number $x$ is an angle in degrees. To get sine and |
| 2832 |
% cosine of numbers as radians, simply convert your radians to degrees |
| 2833 |
% using \cs{MFPdeg} or \cs{Rdeg}. Then find the sine or cosine of the |
| 2834 |
% result. For example, if \cs{X} holds the angle in in radians and you |
| 2835 |
% want the result to be stored in \cs{S}: |
| 2836 |
% \begin{verbatim} |
| 2837 |
% \MFPdeg\X\Y \MFPsin\Y\S \end{verbatim} |
| 2838 |
% |
| 2839 |
% For unit conversions such as radian to degree we try to be more accurate |
| 2840 |
% than a multiplication by an eight-digit conversion factor allows. |
| 2841 |
% If $x$ is large and the factor is off by $0.5\times 10^{-8}$, then the |
| 2842 |
% result can be significantly off. But if we are able to give the |
| 2843 |
% conversion factor 16 digits precision, then only the imprecision of $x$ |
| 2844 |
% will significantly affect the result. |
| 2845 |
% |
| 2846 |
% We express the conversion factor as an integer part and two eight-digit |
| 2847 |
% fractional parts. We multiply $x$ by the integer and first fractional |
| 2848 |
% part (\arg1 and \arg2) with a normal \cs{MFP@Rmul}, but we save the |
| 2849 |
% underflow digits and undo the rounding that occured at the 8th digit. |
| 2850 |
% Together these give us an essentially exact result. Then we multiply $x$ |
| 2851 |
% by the second fractional part (\arg3) and add the saved underflow to the |
| 2852 |
% result. Finally, we round and add the result to the first product. |
| 2853 |
% Argument \arg3, as well as the underflow digits, represent numbers less |
| 2854 |
% than $10^{-8}$, so we effectively scale them up by $10^8$, round the |
| 2855 |
% result to an integer and scale that back down. |
| 2856 |
% |
| 2857 |
% The registers $w$ and $v$ are used to save intermediate results. |
| 2858 |
% The ``\texttt{DP}'' in \cs{MFP@DPmul} refers to the fact that we are |
| 2859 |
% multiplying by a ``double precision'' real. The conversion factors are |
| 2860 |
% required to be positive. |
| 2861 |
% \begin{macrocode} |
| 2862 |
\def\MFP@DPmul#1#2#3{% |
| 2863 |
\ifnum\MFP@x@Sgn=0 |
| 2864 |
\MFP@Rzero |
| 2865 |
\else |
| 2866 |
\MFP@Rcopy xv% |
| 2867 |
\MFP@Rload y1{#1}{#2}\MFP@Rmul |
| 2868 |
\edef\MFP@w@Und{\MFP@z@Und}% |
| 2869 |
\ifnum\MFP@z@Frc@iii>4999 |
| 2870 |
\MFP@tempa\MFP@z@Frc \advance\MFP@tempa-1 |
| 2871 |
\edef\MFP@z@Frc{\number\MFP@tempa}% |
| 2872 |
\makeMFP@eightdigits\MFP@z@Frc |
| 2873 |
\fi |
| 2874 |
\MFP@Rcopyz w% |
| 2875 |
\MFP@Rcopy vx\MFP@Rload y10{#3}\MFP@Rmul |
| 2876 |
\MFP@Rcopyzx\MFP@Rload y\MFP@v@Sgn 0{\MFP@w@Und}\MFP@Radd |
| 2877 |
\MFP@tempa\MFP@z@Int\relax |
| 2878 |
\ifnum\MFP@z@Frc<50000000 \else \advance\MFP@tempa 1 \fi |
| 2879 |
\ifnum\MFP@tempa<\MFP@ttteight\relax |
| 2880 |
\MFP@Rload x{\ifnum\MFP@tempa>0 \MFP@z@Sgn\else0\fi}0\MFP@tempa |
| 2881 |
\else |
| 2882 |
\MFP@Rload x\MFP@z@Sgn10% |
| 2883 |
\fi |
| 2884 |
\MFP@Rcopy wy\MFP@Radd |
| 2885 |
\fi}% |
| 2886 |
% \end{macrocode} |
| 2887 |
% |
| 2888 |
% Conversion factors: |
| 2889 |
% \begin{itemize} |
| 2890 |
% \item radians to degrees: $57.2957795130823209$ |
| 2891 |
% \item degrees to radians: $0.0174532925199433$ |
| 2892 |
% \item natural log to common log: $0.4342944819032518$ |
| 2893 |
% \item common log to natural log: $2.3025850929940457$ |
| 2894 |
% \end{itemize} |
| 2895 |
% |
| 2896 |
% Note that the comparatively large size of the first number means that |
| 2897 |
% the $\pm0.5\cdot10^{-8}$ imprecision that $x$ implicitly carries will |
| 2898 |
% be multiplied to approximately $\pm29.6\cdot 10^{-8}$ in the result. |
| 2899 |
% The only way around this would be to operate with higher precision |
| 2900 |
% internally. We do that in the code for computing angles. |
| 2901 |
% \begin{macrocode} |
| 2902 |
\def\MFP@Rdeg{\MFP@DPmul{57}{29577951}{30823209}}% |
| 2903 |
\def\MFP@Rrad{\MFP@DPmul{0}{01745329}{25199433}}% |
| 2904 |
\def\MFP@RbaseX{\MFP@DPmul{0}{43429448}{19032518}}% |
| 2905 |
\def\MFP@RbaseE{\MFP@DPmul{2}{30258509}{29940457}}% |
| 2906 |
% \end{macrocode} |
| 2907 |
% |
| 2908 |
% There are very few angles that are expressible in eight digits whose sine |
| 2909 |
% or cosine can be expressed exactly in eight digits. For these, we do obtain |
| 2910 |
% an exact result. Other values produce inexact results. It would be nice |
| 2911 |
% if we could at least obtain these correctly rounded to eight decimals, but |
| 2912 |
% unfortunately our methods will often produce a result off by $1$ in the |
| 2913 |
% eighth decimal from the correctly rounded value. Anything that |
| 2914 |
% involves the addition of two or more rounded results can have this |
| 2915 |
% problem. The only way to get correctly rounded results is to carry out |
| 2916 |
% all operations internally to additional places. Even then, there will be |
| 2917 |
% the occasional $.4999\dots$ that should round to $0$ but rounds to $1$ |
| 2918 |
% instead. |
| 2919 |
% |
| 2920 |
% For the cosine, just compute $\sin(90-x)$. |
| 2921 |
% \begin{macrocode} |
| 2922 |
\def\MFP@Rcos{% |
| 2923 |
\MFP@Rcopy xy\MFP@Rload x1{90}0\MFP@Rsub |
| 2924 |
\MFP@Rcopyzx\MFP@Rsin}% |
| 2925 |
% \end{macrocode} |
| 2926 |
% |
| 2927 |
% Reduce $|x|$ by subtracting $180$ from the integer part until it is less |
| 2928 |
% than $180$. Of course, $\sin x = \sgn(x)\sin(|x|)$ so we only need to |
| 2929 |
% compute $\sin(|x|)$. The sign will be that of $x$; each reduction by |
| 2930 |
% $180$ changes the sign, but the reduction code keeps track of that. If |
| 2931 |
% $x$ is 0 after the reduction, return zero. |
| 2932 |
% \begin{macrocode} |
| 2933 |
\def\MFP@Rsin{% |
| 2934 |
\MFP@tempa\MFP@x@Int |
| 2935 |
\MFP@tempb\MFP@x@Frc |
| 2936 |
\MFP@tempc\MFP@x@Sgn\relax |
| 2937 |
\MFP@reduce@angle |
| 2938 |
\ifnum\MFP@tempa>0 \MFP@@Rsin |
| 2939 |
\else\ifnum\MFP@tempb>0 \MFP@@Rsin |
| 2940 |
\else \MFP@Rzero |
| 2941 |
\fi\fi}% |
| 2942 |
% \end{macrocode} |
| 2943 |
% |
| 2944 |
% This following reduces $|x|$ to the case $0 \le |x| < 180$. It assumes |
| 2945 |
% the integer part is in count register \cs{MFP@tempa}, the sign in |
| 2946 |
% \cs{MFP@tempc}. |
| 2947 |
% \begin{macrocode} |
| 2948 |
\def\MFP@reduce@angle{% |
| 2949 |
\ifnum\MFP@tempa<180 |
| 2950 |
\else |
| 2951 |
\advance\MFP@tempa-180 |
| 2952 |
\MFP@tempc-\MFP@tempc |
| 2953 |
\@xp\MFP@reduce@angle |
| 2954 |
\fi}% |
| 2955 |
% \end{macrocode} |
| 2956 |
% |
| 2957 |
% At this point, $|x|$ is represented by \cs{MFP@tempa} (integer part) and |
| 2958 |
% \cs{MFP@tempb} (fractional part). Also, we already know the sign stored |
| 2959 |
% in \cs{MFP@tempc}. Moreover $0 < {}$\cs{MFP@tempa}${} < 180$. We now |
| 2960 |
% reduce to $0 < |x| \le 90$ using $\sin(x) = \sin(180-|x|)$, and return |
| 2961 |
% $1$ if equal to $90$. |
| 2962 |
% |
| 2963 |
% The calculation of $180-x$ is optimized, taking advantage of the fact |
| 2964 |
% that both $x$ and the result are known to be positive. If the fractional |
| 2965 |
% part is positive, we borrow $1$ by reducing $180$ to $179$. |
| 2966 |
% \begin{macrocode} |
| 2967 |
\def\MFP@@Rsin{% |
| 2968 |
\ifnum\MFP@tempa<90 |
| 2969 |
\else |
| 2970 |
\MFP@tempa -\MFP@tempa |
| 2971 |
\ifnum\MFP@tempb>0 |
| 2972 |
\MFP@tempb -\MFP@tempb |
| 2973 |
\advance\MFP@tempb \MFP@ttteight\relax |
| 2974 |
\advance\MFP@tempa 179 |
| 2975 |
\else \advance\MFP@tempa 180 |
| 2976 |
\fi |
| 2977 |
\fi |
| 2978 |
\ifnum\MFP@tempa=90 |
| 2979 |
\MFP@Rloadz \MFP@tempc10% |
| 2980 |
\else |
| 2981 |
% \end{macrocode} |
| 2982 |
% |
| 2983 |
% We would need to convert $x$ to radians (multiply by $\pi/180$) to use |
| 2984 |
% the standard power series, but instead we will incorporate the |
| 2985 |
% conversion factor into the power series coefficients. |
| 2986 |
% |
| 2987 |
% We will, however, try to increase accuracy by reducing the size of $x$ |
| 2988 |
% and correspondingly increasing the appropriate factors. Since the |
| 2989 |
% number of significant figures of a product is limited by the least |
| 2990 |
% number of significant figures of the two factors, the bottleneck on |
| 2991 |
% accuracy is that of the smaller term: all our numbers have eight digits |
| 2992 |
% so if a number is small, the number of nonzero digits is small. |
| 2993 |
% |
| 2994 |
% Dividing by 100 seems a good choice (so our units are |
| 2995 |
% ``hectodegrees''). This makes $0 < x < .9$ and the integer part |
| 2996 |
% (\cs{MFP@tempa}) will be henceforth ignored. |
| 2997 |
% |
| 2998 |
% The addition of 50 is for rounding purposes. After that, our |
| 2999 |
% computations amount to concatenating the top six digits of |
| 3000 |
% \cs{MFP@tempb} to the digits of \cs{MFP@tempa}. This will produce the |
| 3001 |
% integer form of the fractional part of $x/100$ (the integer part of |
| 3002 |
% $x/100$ is zero). |
| 3003 |
% |
| 3004 |
% Division by $100$ can turn a number into $0$. This is one place we can |
| 3005 |
% lose accuracy (up to $\pm1$ in the last digit of the result). In |
| 3006 |
% compensation, the rest of the calculations become very much more |
| 3007 |
% accurate. |
| 3008 |
% \begin{macrocode} |
| 3009 |
\advance\MFP@tempb 50 \divide\MFP@tempb 100 |
| 3010 |
\multiply\MFP@tempa 1000000 \advance\MFP@tempb\MFP@tempa |
| 3011 |
\ifnum\MFP@tempb=0 |
| 3012 |
\MFP@Rzero |
| 3013 |
\else |
| 3014 |
% \end{macrocode} |
| 3015 |
% |
| 3016 |
% We save some multiplications by working with $t=x^2$. As we don't need |
| 3017 |
% the original $x$ anymore, we simply replace it with the newly reduced |
| 3018 |
% value. We also save this reduced $x$ in another register, $s$, as |
| 3019 |
% we will need it again at the end, and our intermediate calculations do |
| 3020 |
% not preserve the $x$ register. Then we square $x$ and, if that |
| 3021 |
% square is $0$ we can skip all the power series and simply return $x$ |
| 3022 |
% converted to radians. If $x^2$ is not zero, we save it in temporary |
| 3023 |
% register $t$ and call our power series. When this program is |
| 3024 |
% finished, all that remains is the final multiplication by a conversion |
| 3025 |
% factor (\cs{MFP@DPmul}). |
| 3026 |
% \begin{macrocode} |
| 3027 |
\MFP@Rload s\MFP@tempc0\MFP@tempb |
| 3028 |
\MFP@Rcopy sx% |
| 3029 |
\MFP@Rsq |
| 3030 |
\ifnum \MFP@z@Frc>0 |
| 3031 |
\MFP@Rcopyz t\MFP@Rsin@prog |
| 3032 |
\else |
| 3033 |
\MFP@Rcopy sx% |
| 3034 |
\fi |
| 3035 |
\MFP@DPmul 1{74532925}{19943296}% |
| 3036 |
\fi |
| 3037 |
\fi}% |
| 3038 |
% \end{macrocode} |
| 3039 |
% |
| 3040 |
% \cs{MFP@Rsin@prog} is the power series computation. The power series |
| 3041 |
% need only go to the $x^{13}$ term as the next is less than $10^{-9}$ and |
| 3042 |
% in our 8-place computations is indistingushable from $0$. Our series is: |
| 3043 |
% $$ |
| 3044 |
% rx(1 - r^2t/3! + r^4t^2/5! - r^6t^3/7! + r^8t^4/9! - r^{10}t^5/11! + |
| 3045 |
% r^{12}t^6/13!) |
| 3046 |
% $$ |
| 3047 |
% where $r$ is the factor that converts $x$ to radian measure |
| 3048 |
% (hectodegrees to radians). When $x$ is so small as to produce $t = 0$ we |
| 3049 |
% have skipped all this. |
| 3050 |
% |
| 3051 |
% We minimize any multiplications of tiny numbers by computing this as |
| 3052 |
% $$ |
| 3053 |
% rx(1 - ft(1 - et(1 - dt(1 - ct(1 - bt(1 - at)))))). |
| 3054 |
% $$ |
| 3055 |
% In this format, additional terms might actually make a difference, |
| 3056 |
% because $at$ is not particularly small. However, the more computations |
| 3057 |
% we have, the more errors accumulate. Therefore we take the fewest that |
| 3058 |
% produce acceptable accuracy. |
| 3059 |
% |
| 3060 |
% Now $r = 1.7453292519943296$ and $a$, $b$, etc., have formulas: |
| 3061 |
% $$ |
| 3062 |
% \vcenter{\centering |
| 3063 |
% $\displaystyle a = r^2/13/12,\ b = r^2/11/10,\ c = r^2/9/8$,\\ |
| 3064 |
% $\displaystyle d = r^2/7/6,\ e = r^2/5/4,\ f = r^2/3/2$.\par |
| 3065 |
% } |
| 3066 |
% $$ |
| 3067 |
% An alternative method would be to accumulate a sum, computing each term |
| 3068 |
% from the previous one (e.g., if $u = t^3/7!$ is the fourth term, the next |
| 3069 |
% one is $u*t*(1/(8*9))$). This is a bit more complicated to code and requires |
| 3070 |
% moving values around more. It would have the advantage that we can stop |
| 3071 |
% whenever a term evaluates to zero, making computation faster for small |
| 3072 |
% values of $x$. I have not determined whether it would compromise |
| 3073 |
% accuracy. |
| 3074 |
% |
| 3075 |
% We avoid divisions by precomputing the coefficients $a$, $b$, $c$, etc. |
| 3076 |
% Note that without the reduction in $x$, the value of $a$ for example |
| 3077 |
% would be $0.00000195$, with only three significant figures of accuracy. |
| 3078 |
% Now we can have seven, and the accuracy is more-or-less determined by that |
| 3079 |
% of the reduced x. |
| 3080 |
% $$ |
| 3081 |
% \vcenter{\centering |
| 3082 |
% $\displaystyle a = 0.01952676,\ b = 0.02769249,\ c = 0.04230797,$,\\ |
| 3083 |
% $\displaystyle d = 0.07252796,\ e = 0.15230871,\ f = 0.50769570$.\par |
| 3084 |
% } |
| 3085 |
% $$ |
| 3086 |
% It is important to note that the following operations step all over |
| 3087 |
% the \cs{MFP@temp}\textit{x} \cs{count} registers, so we have made sure |
| 3088 |
% that we no longer need them. |
| 3089 |
% |
| 3090 |
% The \cs{MFP@flipz} computes $1-z$, where $z$ is the result of the |
| 3091 |
% previous operation. Instead of simply subtracting, we optimize based |
| 3092 |
% on the fact that $z$ is known to be nonnegative and not larger than $1$. |
| 3093 |
% |
| 3094 |
% The macro \cs{MFP@com@iter} `flipz' the previous result then multiplies |
| 3095 |
% by $t$ and the indicated coefficient. (The name of this macro stands for |
| 3096 |
% ``common iterated'' code; it is reused for some other power series.) |
| 3097 |
% |
| 3098 |
% For extra efficiency, the power series uses a ``small'' version of |
| 3099 |
% multiplication \cs{MFP@Rsmul}, used only when the factors are sure to |
| 3100 |
% lie in $[0,1]$. This does not take into account the sign of $x$, |
| 3101 |
% whence the ending \cs{edef}. |
| 3102 |
% \begin{macrocode} |
| 3103 |
\def\MFP@Rsin@prog{% |
| 3104 |
\MFP@Rcopy tx\MFP@Rload y10{01952676}\MFP@Rsmul% |
| 3105 |
\MFP@com@iter{02769249}\MFP@com@iter{04230797}\MFP@com@iter{07252796}% |
| 3106 |
\MFP@com@iter{15230871}\MFP@com@iter{50769570}\MFP@flipz \MFP@Rcopyzx |
| 3107 |
\MFP@Rcopy sy\MFP@Rsmul\MFP@Rcopyzx\edef\MFP@x@Sgn{\MFP@s@Sgn}}% |
| 3108 |
\def\MFP@flipz{% |
| 3109 |
\ifnum\MFP@z@Sgn=0 |
| 3110 |
\MFP@Rloadz 110% |
| 3111 |
\else |
| 3112 |
\MFP@tempa\MFP@ttteight |
| 3113 |
\advance\MFP@tempa-\MFP@z@Frc\relax |
| 3114 |
\MFP@Rloadz{\ifcase\MFP@tempa 0\else1\fi}0\MFP@tempa |
| 3115 |
\fi}% |
| 3116 |
\def\MFP@com@iter#1{\MFP@flipz |
| 3117 |
\MFP@Rcopyzx\MFP@Rcopy ty\MFP@Rsmul |
| 3118 |
\MFP@Rcopyzx\MFP@Rload y10{#1}\MFP@Rsmul}% |
| 3119 |
% \end{macrocode} |
| 3120 |
% |
| 3121 |
% As to the accuracy of these computations, we can certainly lose accuracy |
| 3122 |
% at each step. In principle, if $x$ is known to 10 significant figures |
| 3123 |
% ($x \ge 10$~degrees), then even though we lose two figures with division |
| 3124 |
% by 100, the accuracy bottleneck is the fact that our coefficients have |
| 3125 |
% only seven figures. Now we have 17 multiplications, and while products |
| 3126 |
% are said to have the same number of significant figures as the factors, |
| 3127 |
% in the worse case we can accumulate inaccuracy of about $.5\times |
| 3128 |
% 10^{-8}$ per multiplication. So we are not guaranteed an accuracy of |
| 3129 |
% more than about $\pm 10^{-7}$. Numerical tests, however, show that it |
| 3130 |
% isn't that bad, probably because the direction of inaccuracies usually |
| 3131 |
% varies randomly, and inaccuracies in one direction compensate for those |
| 3132 |
% going the other way. I have not seen a case where the result is off by |
| 3133 |
% more than $1$ in the last decimal place (i.e., $\pm 1.5\times 10^{-8}$). |
| 3134 |
% In the case where we can know the result exactly, $x=30$, we get an |
| 3135 |
% exact answer, even though we don't single it out (as we do $0$, $90$ and |
| 3136 |
% $180$). |
| 3137 |
% |
| 3138 |
% The following is the ``small'' version of \cs{MFP@Rmul}. Limited to |
| 3139 |
% non-negative numbers less than or equal to $1$. Theoretically all the |
| 3140 |
% numbers are strictly between $0$ and $1$, but in practice a |
| 3141 |
% multiplication could round to $0$ and then, after subtraction, a $1$ |
| 3142 |
% could occur. We handle those easy cases separately, so that in |
| 3143 |
% \cs{MFP@@Rsmul} we don't have to worry about the integer parts at all. |
| 3144 |
% |
| 3145 |
% Also, since these are completely internal, we don't even define the |
| 3146 |
% overflow and underflow macros. |
| 3147 |
% \begin{macrocode} |
| 3148 |
\def\MFP@Rsmul{% |
| 3149 |
\ifnum \MFP@x@Sgn=0 \MFP@Rzero |
| 3150 |
\else\ifnum \MFP@y@Sgn=0 \MFP@Rzero |
| 3151 |
\else\ifnum\MFP@x@Int>0 \MFP@Rcopy yz% |
| 3152 |
\else\ifnum\MFP@y@Int>0 \MFP@Rcopy xz% |
| 3153 |
\else \MFP@@Rsmul |
| 3154 |
\fi\fi\fi\fi}% |
| 3155 |
\def\MFP@@Rsmul{% |
| 3156 |
\MFP@split\MFP@x@Frc\MFP@x@Frc@i\MFP@x@Frc@ii |
| 3157 |
\MFP@split\MFP@y@Frc\MFP@y@Frc@i\MFP@y@Frc@ii |
| 3158 |
\def\MFP@z@Frc@i {0}\def\MFP@z@Frc@ii {0}% |
| 3159 |
\def\MFP@z@Frc@iii{0}\def\MFP@z@Frc@iv {0}% |
| 3160 |
\MFP@tempb\MFP@y@Frc@ii\relax |
| 3161 |
\MFP@multiplyone\MFP@x@Frc@ii\MFP@z@Frc@iv |
| 3162 |
\MFP@multiplyone\MFP@x@Frc@i\MFP@z@Frc@iii |
| 3163 |
\MFP@tempb\MFP@y@Frc@i\relax |
| 3164 |
\MFP@multiplyone\MFP@x@Frc@ii\MFP@z@Frc@iii |
| 3165 |
\MFP@multiplyone\MFP@x@Frc@i\MFP@z@Frc@ii |
| 3166 |
\MFP@carrym\MFP@z@Frc@iv\MFP@z@Frc@iii |
| 3167 |
\MFP@carrym\MFP@z@Frc@iii\MFP@z@Frc@ii |
| 3168 |
\ifnum\MFP@z@Frc@iii<5000 \else |
| 3169 |
\MFP@tempb\MFP@z@Frc@ii |
| 3170 |
\advance\MFP@tempb1 |
| 3171 |
\edef\MFP@z@Frc@ii{\number\MFP@tempb}\fi |
| 3172 |
\MFP@carrym\MFP@z@Frc@ii\MFP@z@Frc@i |
| 3173 |
\makeMFP@fourdigits\MFP@z@Frc@ii |
| 3174 |
\makeMFP@fourdigits\MFP@z@Frc@i |
| 3175 |
\def\MFP@z@Int{0}% |
| 3176 |
\edef\MFP@z@Frc{\MFP@z@Frc@i\MFP@z@Frc@ii}% |
| 3177 |
\edef\MFP@z@Sgn{\ifnum\MFP@z@Frc=0 0\else 1\fi}}% |
| 3178 |
% \end{macrocode} |
| 3179 |
% |
| 3180 |
% \subsection{Polar angle} |
| 3181 |
% |
| 3182 |
% Instead of supplying the arcsine and arccosine functions, we supply the |
| 3183 |
% more general angle function. This is a binary operation that accepts |
| 3184 |
% the two coordinates of a point and computes its angle in polar |
| 3185 |
% coordinates. One then has, for example, $\arctan x = |
| 3186 |
% \mathop{\mathrm{angle}}(1,x)$ and $\arccos x = \mathop{\mathrm{angle}} |
| 3187 |
% (x, \sqrt{1-x^2})$. |
| 3188 |
% |
| 3189 |
% We start, as usual, with a few reductions. When the $y$-part is $0$, we |
| 3190 |
% immediately return $0$ or $180$. If the $y$-part is negative, we compute |
| 3191 |
% the angle for $(x,|y|)$ and negate it. If the $x$-part is negative, we |
| 3192 |
% compute the angle for $|x|$ and subtract it from $180$. Finally, |
| 3193 |
% reduced to both coordinates positive, if $y>x$ we compute the angle of |
| 3194 |
% $(y,x)$ and subtract that from $90$. Ultimately, we apply a power |
| 3195 |
% series formula for $\mathop{\mathrm{angle}}(1,y/x)$ and get convergence |
| 3196 |
% when the argument is less than $1$, but convergence is poor unless the |
| 3197 |
% argument is less than $1/2$. When that is not the case, conceptually, we |
| 3198 |
% rotate the picture clockwise by the arctangent of $1/2$, compute the |
| 3199 |
% angle of the new point and then add a precomputed value of |
| 3200 |
% $\arctan(1/2)$. |
| 3201 |
% \begin{macrocode} |
| 3202 |
\def\MFP@Rangle{% |
| 3203 |
\ifcase\MFP@y@Sgn\relax |
| 3204 |
\ifcase\MFP@x@Sgn\relax |
| 3205 |
\MFP@warn{Point (0,0) has no angle. Returning 0 anyway}% |
| 3206 |
\MFP@Rzero |
| 3207 |
\or |
| 3208 |
\MFP@Rzero |
| 3209 |
\else |
| 3210 |
\MFP@Rloadz 1{180}0% |
| 3211 |
\fi |
| 3212 |
\@xp\@gobble |
| 3213 |
\or |
| 3214 |
\def\MFP@angle@Sgn{1}\@xp\@firstofone |
| 3215 |
\else |
| 3216 |
\def\MFP@y@Sgn{1}% |
| 3217 |
\def\MFP@angle@Sgn{-1}\@xp\@firstofone |
| 3218 |
\fi |
| 3219 |
{\ifcase\MFP@x@Sgn\relax |
| 3220 |
\MFP@Rloadz1{90}0% |
| 3221 |
\or \MFP@@Rangle |
| 3222 |
\else |
| 3223 |
\def\MFP@x@Sgn{1}\MFP@@Rangle |
| 3224 |
\MFP@Rcopyzy\MFP@Rload x1{180}0\MFP@Rsub |
| 3225 |
\fi |
| 3226 |
\let\MFP@z@Sgn\MFP@angle@Sgn}}% |
| 3227 |
\def\MFP@@Rangle{% |
| 3228 |
\MFP@Rcmp |
| 3229 |
\ifMFP@neg |
| 3230 |
\MFP@Rcopy xw\MFP@Rcopy yx\MFP@Rcopy wy% |
| 3231 |
\MFP@@@Rangle |
| 3232 |
\MFP@Rload x1{90}0\MFP@Rcopyzy\MFP@Rsub |
| 3233 |
\else |
| 3234 |
\MFP@@@Rangle |
| 3235 |
\fi}% |
| 3236 |
% \end{macrocode} |
| 3237 |
% |
| 3238 |
% Precisely what we do when we are finally in the case $0<y<x$ is perform |
| 3239 |
% a couple of reductions. Ultimately we want to compute the arctan of |
| 3240 |
% $z = y/x$. We once again use a power series but, for fast convergence, |
| 3241 |
% we require $z$ to be considerably less than $1$. For reasons we discuss |
| 3242 |
% later, we won't be able to use the more efficient \cs{MFP@Rsmul} so we |
| 3243 |
% want to keep the number of iterations of our power series calculations |
| 3244 |
% low. |
| 3245 |
% |
| 3246 |
% So we start with two iterations of the algorithm used by Knuth: if $y/x |
| 3247 |
% > 1/2$ we transform the pair $(x,y)$ to a new one whose angle has been |
| 3248 |
% reduced by $\arctan (1/2)$. The new pair is $(x',y') = (2x+y, 2y-x)$. |
| 3249 |
% If we still have $y/x > 1/4$, we perform $(x'',y'') = (4x + y, 4y - x)$, |
| 3250 |
% which then satisfies $y''/x'' \le 1/4$. When either of these |
| 3251 |
% transformations is performed, we add the corresponding angle to the |
| 3252 |
% ``angle-so-far'' in register $a$. |
| 3253 |
% |
| 3254 |
% We could continue this iteration 32 times to get (theoretically) the |
| 3255 |
% angle in degrees to $\pm 10^{-8}$. That seems a bit long, plus the |
| 3256 |
% accumulation of errors over $32$ iterations could (in the worst case) |
| 3257 |
% produce less than $\pm10^{-7}$ accuracy. |
| 3258 |
% |
| 3259 |
% To get the accuracy we need, we work in ``scaled reals''. That is, we |
| 3260 |
% get 10 effective decimal places of accuracy by letting an $x$ in the |
| 3261 |
% range $0< x < 100$ stand for $0< x/100 < 1$. |
| 3262 |
% |
| 3263 |
% Our initial reductions can increase $x$ by a factor of about 13. |
| 3264 |
% Moreover, we ultimately need to scale y by 100 when we convert to |
| 3265 |
% scaled computations. Thus, if we make sure $x$ is less than |
| 3266 |
% $1\,000\,000$, we will prevent overflow in both cases. |
| 3267 |
% \begin{macrocode} |
| 3268 |
\def\MFP@Rquad{\MFP@Rdbl\MFP@Rcopyzx\MFP@Rdbl}% |
| 3269 |
\def\MFP@@@Rangle{% |
| 3270 |
\MFP@Rcopy xs\MFP@Rcopy yt% |
| 3271 |
\ifnum\MFP@x@Int<1000000 |
| 3272 |
\else |
| 3273 |
\MFP@RdivC \MFP@Rcopyz s% |
| 3274 |
\MFP@Rcopy tx\MFP@RdivC \MFP@Rcopyz t% |
| 3275 |
\fi |
| 3276 |
\ifnum\MFP@t@Sgn=0 \MFP@Rzero |
| 3277 |
\else |
| 3278 |
\MFP@Rcopy tx\MFP@Rdbl\MFP@Rcopyzx\MFP@Rcopy sy\MFP@Rcmp |
| 3279 |
\ifMFP@pos |
| 3280 |
\MFP@Rsub\MFP@Rcopyz u\MFP@Rcopy sx\MFP@Rdbl |
| 3281 |
\MFP@Rcopyzx\MFP@Rcopy ty\MFP@Radd |
| 3282 |
\MFP@Rcopyz s\MFP@Rcopy ut% |
| 3283 |
\MFP@Rload a1{2656}{50511771}% |
| 3284 |
\else |
| 3285 |
\MFP@Rload a000% |
| 3286 |
\fi |
| 3287 |
\MFP@Rcopy tx\MFP@Rquad\MFP@Rcopyzx\MFP@Rcopy sy\MFP@Rcmp |
| 3288 |
\ifMFP@pos |
| 3289 |
\MFP@Rsub\MFP@Rcopyz u\MFP@Rcopy sx\MFP@Rquad |
| 3290 |
\MFP@Rcopyzx\MFP@Rcopy ty\MFP@Radd |
| 3291 |
\MFP@Rcopyz s\MFP@Rcopy ut% |
| 3292 |
\MFP@Rcopy ax\MFP@Rload y1{1403}{62434679}% |
| 3293 |
\MFP@Radd\MFP@Rcopy za% |
| 3294 |
\fi |
| 3295 |
\MFP@Rcopy tx\MFP@RmulC |
| 3296 |
\MFP@Rcopyzx\MFP@Rcopy sy\MFP@Rdiv |
| 3297 |
\MFP@Rcopyzx\MFP@Ratanc |
| 3298 |
\MFP@Rcopyzx\MFP@Rdeg |
| 3299 |
\MFP@Rcopyzx\MFP@Rcopy ay\MFP@Radd |
| 3300 |
\MFP@Rcopyzx\MFP@RdivC |
| 3301 |
\fi}% |
| 3302 |
% \end{macrocode} |
| 3303 |
% |
| 3304 |
% Here are fast multiplication and division by 100. We need these because |
| 3305 |
% we are going to compute the arctangent in radians to ten decimal places. |
| 3306 |
% We do this by computing with scaled reals in which, for example, $0.5$ |
| 3307 |
% is represented by $50.0$. When we do this, multiplication requires a |
| 3308 |
% division by 100: $.5\times.5 = .25$ would be computed as $(50\times50) / |
| 3309 |
% 100 = 25$. |
| 3310 |
% \begin{macrocode} |
| 3311 |
\def\MFP@twoofmany#1#2#3\MFP@end{#1#2}% |
| 3312 |
\def\MFP@gobbletwo#1#2{}% |
| 3313 |
\def\MFP@RmulC{% |
| 3314 |
\edef\MFP@z@Int{\MFP@x@Int\@xp\MFP@twoofmany\MFP@x@Frc\MFP@end}% |
| 3315 |
\edef\MFP@z@Frc{\@xp\MFP@gobbletwo\MFP@x@Frc00}% |
| 3316 |
\edef\MFP@z@Sgn{\MFP@x@Sgn}}% |
| 3317 |
\def\MFP@RdivC{% |
| 3318 |
\makeMFP@eightdigits\MFP@x@Int |
| 3319 |
\makeMFP@eightdigits\MFP@x@Frc |
| 3320 |
\@XP\MFP@@RdivC\@xp\MFP@x@Int\MFP@x@Frc\MFP@end}% |
| 3321 |
\def\MFP@@RdivC#1#2#3#4#5#6{% |
| 3322 |
\edef\MFP@z@Int{\number#1#2#3#4#5#6}% |
| 3323 |
\MFP@@@RdivC}% |
| 3324 |
\def\MFP@@@RdivC#1#2#3#4#5#6#7#8#9\MFP@end{% |
| 3325 |
\MFP@tempa#1#2#3#4#5#6#7#8\relax |
| 3326 |
\ifnum#9>49 \advance\MFP@tempa1 \fi |
| 3327 |
\edef\MFP@z@Frc{\number\MFP@tempa}% |
| 3328 |
\makeMFP@eightdigits\MFP@z@Frc |
| 3329 |
\edef\MFP@z@Sgn{\MFP@x@Sgn}% |
| 3330 |
\ifnum\MFP@tempa=0 |
| 3331 |
\ifnum\MFP@z@Int=0 \def\MFP@z@Sgn{0}\fi |
| 3332 |
\fi}% |
| 3333 |
% \end{macrocode} |
| 3334 |
% |
| 3335 |
% Finally, we compute the arctan of a scaled real producing a result |
| 3336 |
% as a scaled number (i..e., as ``centiradians''---$100$ times the number |
| 3337 |
% of radians) using a power series. Since that number could be |
| 3338 |
% around $0.25$ (represented by $25.0$), we have to sum to at least its |
| 3339 |
% $15$th power ($4^{-15}/15 \approx .6\times 10^{-10}$ and the next term |
| 3340 |
% in the series is effectively $0$). Fortunately, the power series has |
| 3341 |
% only odd terms, so there are only eight terms we actually need to calculate. |
| 3342 |
% The calculation proceeds much like the one for the sine, starting with |
| 3343 |
% the sum |
| 3344 |
% $$ |
| 3345 |
% x\left(1 - \frac{u}{3} + \frac{u^2}{5} - \frac{u^3}{7} + \cdots |
| 3346 |
% - \frac{u^7}{15}\right), |
| 3347 |
% $$ |
| 3348 |
% where $u = x^2$. |
| 3349 |
% |
| 3350 |
% We start with the common iterated code. It assumes a scaled value in $x$ |
| 3351 |
% to be multiplied by the saved (scaled) value of $x^2$ (in register $u$) |
| 3352 |
% and by a coefficient (supplied in separate integer and fractional |
| 3353 |
% parts). It ends with the new value in $x$. |
| 3354 |
% \begin{macrocode} |
| 3355 |
\def\MFP@scaledmul{\MFP@Rmul\MFP@Rcopyzx\MFP@RdivC}% |
| 3356 |
\def\MFP@atan@iter#1#2{% |
| 3357 |
\MFP@Rcopy uy\MFP@scaledmul |
| 3358 |
\MFP@Rcopyzx\MFP@Rload y1{#1}{#2}\MFP@scaledmul |
| 3359 |
\MFP@Rcopyzy\MFP@Rload x1{100}{00000000}% |
| 3360 |
\MFP@Rsub\MFP@Rcopyzx}% |
| 3361 |
\def\MFP@Ratanc{% |
| 3362 |
\MFP@Rcopy xs\MFP@Rcopy xy\MFP@scaledmul |
| 3363 |
\ifnum \MFP@z@Sgn=0 |
| 3364 |
\MFP@Rcopy sz% |
| 3365 |
\else |
| 3366 |
\MFP@Rcopyz u\MFP@Rcopyzx |
| 3367 |
\MFP@Rload y1{86}{66666667}\MFP@scaledmul |
| 3368 |
\MFP@Rcopyzy\MFP@Rload x1{100}{00000000}\MFP@Rsub\MFP@Rcopyzx |
| 3369 |
\MFP@atan@iter{84}{61538462}\MFP@atan@iter{81}{81818182}% |
| 3370 |
\MFP@atan@iter{77}{77777778}\MFP@atan@iter{71}{42857143}% |
| 3371 |
\MFP@atan@iter{60}{00000000}\MFP@atan@iter{33}{33333333}% |
| 3372 |
\MFP@Rcopy sy\MFP@scaledmul |
| 3373 |
\fi}% |
| 3374 |
% \end{macrocode} |
| 3375 |
% |
| 3376 |
% \subsection{Logarithms} |
| 3377 |
% |
| 3378 |
% Now for logarithms. We are going to compute both common logarithms |
| 3379 |
% (base $10$) and natural logarithms (base $e$). The first step of the |
| 3380 |
% calculation is be essentially trivial and works with base 10: to |
| 3381 |
% get the integer part of the log for numbers with positive integer part, |
| 3382 |
% count the digits in the integer part and subtract $1$. For numbers less |
| 3383 |
% than one, count the number of zeros at the beginning of the fractional |
| 3384 |
% part and add $1$ (subtract this from the result of the second part). This |
| 3385 |
% reduces the problem to numbers $1 \le x < 10$. A few divisions (when |
| 3386 |
% necessary) reduce to the case where $x = 1 + u$ with $u$ small enough |
| 3387 |
% that the power series for $\log (1 + u)$ can be computed accurately in |
| 3388 |
% an acceptable number of of terms. Then we proceed as in the code for |
| 3389 |
% sine. |
| 3390 |
% |
| 3391 |
% The power series produces a logarithm in base $e$ so we ultimately get |
| 3392 |
% the answer in two parts, with the parts calculated for different bases. |
| 3393 |
% The last step for the common log is to multiply the second part by a |
| 3394 |
% conversion factor and add the first to it. For natural log, convert the |
| 3395 |
% first and add the second. Which one is to be returned is passed as a |
| 3396 |
% boolean. |
| 3397 |
% |
| 3398 |
% We keep the value-so-far in register $s$ and the modified |
| 3399 |
% $x$-value in register $t$. |
| 3400 |
% \begin{macrocode} |
| 3401 |
\newif\ifMFP@natural |
| 3402 |
\def\MFP@Rlog{\MFP@naturalfalse\MFP@Rlog@}% |
| 3403 |
\def\MFP@Rln{\MFP@naturaltrue\MFP@Rlog@}% |
| 3404 |
\def\MFP@Rlog@{% |
| 3405 |
\ifnum\MFP@x@Sgn=0 |
| 3406 |
\MFP@logofzero@err |
| 3407 |
\MFP@Rloadz{-1}\LogOfZeroInt\LogOfZeroFrac |
| 3408 |
\else |
| 3409 |
\ifnum \MFP@x@Sgn<0 |
| 3410 |
\MFP@warn{The logarithm of a negative number is complex. |
| 3411 |
\MFP@msgbreak Only the real part will be computed}% |
| 3412 |
\def\MFP@x@Sgn{1}% |
| 3413 |
\fi |
| 3414 |
\MFP@Rload s000% |
| 3415 |
% \end{macrocode} |
| 3416 |
% |
| 3417 |
% If the integer part is $0$, the fractional part is not. Save the |
| 3418 |
% number of places that will be shifted in \cs{MFP@tempa}. We use |
| 3419 |
% \cs{number} to strip the leading zeros and (essentially) we count |
| 3420 |
% the number of digits that remain. Then we shift left, putting the first |
| 3421 |
% digit into the integer part of \reg{s} and the rest into the |
| 3422 |
% fractional part. |
| 3423 |
% \begin{macrocode} |
| 3424 |
\ifnum \MFP@x@Int=0 |
| 3425 |
\edef\MFP@x@Tmp{\number\MFP@x@Frc}% |
| 3426 |
\MFP@tempa=\MFP@numshiftL\MFP@x@Tmp\relax |
| 3427 |
\def\MFP@s@Sgn{-1}% |
| 3428 |
\edef\MFP@t@Int{\@xp\MFP@oneofmany\MFP@x@Tmp\MFP@end}% |
| 3429 |
\edef\MFP@t@Frc{\@xp\@gobble\MFP@x@Tmp0}% |
| 3430 |
\MFP@padtoeight\MFP@t@Frc |
| 3431 |
\else |
| 3432 |
% \end{macrocode} |
| 3433 |
% When the integer part is not $0$, we get the number of digits to |
| 3434 |
% shift again in \cs{MFP@tempa}. It will be one less than the number of |
| 3435 |
% integer digits. |
| 3436 |
% \begin{macrocode} |
| 3437 |
\MFP@tempa\MFP@numshiftR\MFP@x@Int |
| 3438 |
\edef\MFP@x@Tmp{\MFP@x@Int\MFP@x@Frc}% |
| 3439 |
\ifnum\MFP@tempa>0 \def\MFP@s@Sgn{1}\fi |
| 3440 |
\edef\MFP@t@Int{\@xp\MFP@oneofmany\MFP@x@Tmp\MFP@end}% |
| 3441 |
\edef\MFP@x@Tmp{\@xp\@gobble\MFP@x@Tmp}% |
| 3442 |
\edef\MFP@t@Frc{\@xp\MFP@eightofmany\MFP@x@Tmp\MFP@end}% |
| 3443 |
\fi |
| 3444 |
% \end{macrocode} |
| 3445 |
% |
| 3446 |
% Now the integer part of $\log_{10} x$ is known. We save it in $s$. |
| 3447 |
% Also set the sign of the reduced argument (positive). Then call |
| 3448 |
% \cs{MFP@Rlog@reduce}, which reduces $x$ to less than $1.161\,$ while |
| 3449 |
% possibly increasing $s$. For the natural log, we convert the value in |
| 3450 |
% $s$. |
| 3451 |
% |
| 3452 |
% If the reduced $x$ is $1$, return the value in $s$, otherwise call the |
| 3453 |
% power series program (discarding the integer part of $t$, which should |
| 3454 |
% be a $1$). Finally, convert the returned result if necessary and add |
| 3455 |
% register $s$ to it. |
| 3456 |
% \begin{macrocode} |
| 3457 |
\edef\MFP@s@Int{\number\MFP@tempa}% |
| 3458 |
\def\MFP@t@Sgn{1}% |
| 3459 |
\MFP@Rlog@reduce |
| 3460 |
\ifMFP@natural \MFP@Rcopy sx\MFP@RbaseE \MFP@Rcopy zs\fi |
| 3461 |
\ifnum\MFP@t@Frc=0 |
| 3462 |
\MFP@Rcopy sz% |
| 3463 |
\else |
| 3464 |
\def\MFP@t@Int{0}\MFP@Rlog@prog |
| 3465 |
\ifMFP@natural\else \MFP@Rcopyzx \MFP@RbaseX \fi |
| 3466 |
\MFP@Rcopy sy\MFP@Rcopyzx\MFP@Radd |
| 3467 |
\fi |
| 3468 |
\fi}% |
| 3469 |
% \end{macrocode} |
| 3470 |
% |
| 3471 |
% We determine the size of a right shift by lining up the digits in |
| 3472 |
% the integer part, followed by the possible numbers, and picking out the |
| 3473 |
% ninth argument. Similarly, to get a left shift we line up the digits |
| 3474 |
% of the fractional part (minus the leading zeros) followed by the |
| 3475 |
% possible numbers, and again picking the ninth. |
| 3476 |
% \begin{macrocode} |
| 3477 |
\def\MFP@numshiftR#1{\@xp\MFP@ninthofmany#176543210\MFP@end}% |
| 3478 |
\def\MFP@numshiftL#1{\@xp\MFP@ninthofmany#112345678\MFP@end}% |
| 3479 |
% \end{macrocode} |
| 3480 |
% |
| 3481 |
% In \cs{MFP@Rlog@reduce} we divide by the square root of 10 if the number |
| 3482 |
% is significantly larger than that (adding $.5$ to value-so-far). We |
| 3483 |
% repeat with the 4th, 8th and 16th roots. It seems that this could be |
| 3484 |
% where errors can accumulate, so the divisions are done with double |
| 3485 |
% precision multiplication and $x$ is scaled by 100. Our check whether |
| 3486 |
% $x > \sqrt{10}$ is rather rough: comparing the first three digits only, |
| 3487 |
% but even in the worst case, the final $x$ is less than $1.1605$, so at |
| 3488 |
% most $0.161$ is fed to the power series. |
| 3489 |
% \begin{macrocode} |
| 3490 |
\def\MFP@Rlog@reduce{% |
| 3491 |
\MFP@Rcopy tx\MFP@RmulC\MFP@Rcopyz t% |
| 3492 |
\MFP@reduceonce {316}{31622776}{60168379}{50000000}% |
| 3493 |
\MFP@reduceonce {177}{56234132}{51903491}{25000000}% |
| 3494 |
\MFP@reduceonce {133}{74989420}{93324558}{12500000}% |
| 3495 |
\MFP@reduceonce {115}{86596432}{33600654}{06250000}% |
| 3496 |
\MFP@Rcopy tx\MFP@RdivC\MFP@Rcopyz t}% |
| 3497 |
\def\MFP@reduceonce#1#2#3#4{% |
| 3498 |
\ifnum\MFP@t@Int>#1\relax |
| 3499 |
\MFP@Rcopy tx% |
| 3500 |
\MFP@DPmul 0{#2}{#3}\MFP@Rcopyz t% |
| 3501 |
\MFP@Rcopy sx\MFP@Rload y10{#4}\MFP@Radd |
| 3502 |
\MFP@Rcopyz s% |
| 3503 |
\fi}% |
| 3504 |
% \end{macrocode} |
| 3505 |
% |
| 3506 |
% Now we have a value for $t$ of the form $1 + u$ with $0\le u < 0.161$. |
| 3507 |
% We will use the formula |
| 3508 |
% $$ |
| 3509 |
% \ln (1 + u) = \sum_{n=1}^\infty (-1)^{n-1} \frac{u^n}{n}. |
| 3510 |
% $$ |
| 3511 |
% We only need to carry it far enough to assure that the remaining terms |
| 3512 |
% would be zero in our finite resolution arithmetic, that is |
| 3513 |
% $(.161)^k/k < .5\times 10^{-8}$. This is satisfied by $k=10$. |
| 3514 |
% So we carry the sum to 9 places. |
| 3515 |
% |
| 3516 |
% Again, we compute this by |
| 3517 |
% $$ |
| 3518 |
% u(1-au(1-bu(1-cu(1-du(1-eu(1-fu(1-gu(1-hu)))))))) |
| 3519 |
% $$ |
| 3520 |
% where $a= 1/2$, $b = 2/3$,\dots, $g=7/8$, and $h=8/9$ |
| 3521 |
% This arrangement allows us to reuse \cs{MFP@com@iter}. |
| 3522 |
% \begin{macrocode} |
| 3523 |
\def\MFP@Rlog@prog{% |
| 3524 |
\MFP@Rcopy tx\MFP@Rload y10{88888889}\MFP@Rsmul |
| 3525 |
\MFP@com@iter{87500000}\MFP@com@iter{85714286}\MFP@com@iter{83333333}% |
| 3526 |
\MFP@com@iter{80000000}\MFP@com@iter{75000000}\MFP@com@iter{66666667}% |
| 3527 |
\MFP@com@iter{50000000}\MFP@flipz\MFP@Rcopyzx\MFP@Rcopy ty\MFP@Rsmul}% |
| 3528 |
% \end{macrocode} |
| 3529 |
% |
| 3530 |
% \subsection{Powers} |
| 3531 |
% |
| 3532 |
% With the exponential function we immediately return $1$ if $x=0$. We |
| 3533 |
% call two separate handlers for positive and negative $x$. This is |
| 3534 |
% because the issues are different between positive and negative |
| 3535 |
% exponents. |
| 3536 |
% \begin{macrocode} |
| 3537 |
\def\MFP@Rexp{% |
| 3538 |
\ifcase\MFP@x@Sgn\relax |
| 3539 |
\MFP@Rloadz 110% |
| 3540 |
\or |
| 3541 |
\MFP@Rexp@pos |
| 3542 |
\else |
| 3543 |
\def\MFP@x@Sgn{1}% |
| 3544 |
\MFP@Rexp@neg |
| 3545 |
\fi}% |
| 3546 |
% \end{macrocode} |
| 3547 |
% |
| 3548 |
% One issue for positive exponents is overflow, so we issue an error |
| 3549 |
% message for that case. The largest mumber that will not produce |
| 3550 |
% overflow is $18.42068074$ so we first compare to that; if larger, |
| 3551 |
% issue the error message and return $99999999.99999999$. |
| 3552 |
% |
| 3553 |
% We compute the integer power first, using an \cs{ifcase}. Because there |
| 3554 |
% are only 19 cases to consider a table lookup is faster than |
| 3555 |
% multiplications. |
| 3556 |
% |
| 3557 |
% Then, we examine the first digit $d$ after the decimal and compute |
| 3558 |
% $e^{0.d}$, again by cases. This is multiplied by the integer power |
| 3559 |
% previously found. What remains is the rest of the fractional part of |
| 3560 |
% $x$, which is strictly less than $0.1$. The exponential of this is |
| 3561 |
% computed using the first several terms of the power series for $e^x$. |
| 3562 |
% \begin{macrocode} |
| 3563 |
\def\MFP@Rexp@pos{% |
| 3564 |
\MFP@Rload y1{18}{42068074}\MFP@Rcmp |
| 3565 |
\ifMFP@pos |
| 3566 |
\MFP@expoverflow@err |
| 3567 |
\MFP@Rloadz 1\MaxRealInt\MaxRealFrac |
| 3568 |
\else |
| 3569 |
\MFP@tempa\MFP@x@Int |
| 3570 |
\edef\MFP@powerof@e{% |
| 3571 |
1\ifcase\MFP@tempa |
| 3572 |
10\or |
| 3573 |
2{71828183}\or |
| 3574 |
7{38905610}\or |
| 3575 |
{20}{08553692}\or |
| 3576 |
{54}{59815003}\or |
| 3577 |
{148}{41315910}\or |
| 3578 |
{403}{42879349}\or |
| 3579 |
{1096}{63315843}\or |
| 3580 |
{2980}{95798704}\or |
| 3581 |
{8103}{08392758}\or |
| 3582 |
{22026}{46579481}\or |
| 3583 |
{59874}{14171520}\or |
| 3584 |
{162754}{79141900}\or |
| 3585 |
{442413}{39200892}\or |
| 3586 |
{1202604}{28416478}\or |
| 3587 |
{3269017}{37247211}\or |
| 3588 |
{8886110}{52050787}\or |
| 3589 |
{24154952}{75357530}\or |
| 3590 |
{65659969}{13733051}\else |
| 3591 |
{\MaxRealInt}{\MaxRealFrac}\fi}% |
| 3592 |
\@xp\MFP@Rloadz\MFP@powerof@e |
| 3593 |
\ifnum\MFP@x@Frc=0 |
| 3594 |
\else |
| 3595 |
\MFP@Rcopyz s% |
| 3596 |
\MFP@tempa=\@xp\MFP@oneofmany\MFP@x@Frc\MFP@end |
| 3597 |
\edef\MFP@powerof@e{% |
| 3598 |
y1\ifcase\MFP@tempa |
| 3599 |
10\or |
| 3600 |
1{10517092}\or |
| 3601 |
1{22140276}\or |
| 3602 |
1{34985881}\or |
| 3603 |
1{49182470}\or |
| 3604 |
1{64872127}\or |
| 3605 |
1{82211880}\or |
| 3606 |
2{01375271}\or |
| 3607 |
2{22554093}\or |
| 3608 |
2{45960311}\else |
| 3609 |
10\fi}% |
| 3610 |
\edef\MFP@t@Frc{0\@xp\@gobble\MFP@x@Frc}% |
| 3611 |
\MFP@Rcopy sx\@xp\MFP@Rload\MFP@powerof@e\MFP@Rmul |
| 3612 |
\ifnum\MFP@t@Frc=0 |
| 3613 |
\else |
| 3614 |
\MFP@Rcopyz s\MFP@Rload t10\MFP@t@Frc |
| 3615 |
\MFP@Rexp@pos@prog |
| 3616 |
\MFP@Rcopy sx\MFP@Rcopyzy\MFP@Rmul |
| 3617 |
\fi |
| 3618 |
\fi |
| 3619 |
\fi}% |
| 3620 |
% \end{macrocode} |
| 3621 |
% |
| 3622 |
% Since the $x$ value is now less than $0.1$, we can get eight places of |
| 3623 |
% accuracy with only six terms of the power series. We can also arrange to |
| 3624 |
% use the more efficient \cs{MFP@Rsmul} for multiplication. |
| 3625 |
% |
| 3626 |
% We organize the computation thusly |
| 3627 |
% $$ |
| 3628 |
% 1 + (x + x/2(x + x/3(x + x/4(x + x/5(x + x/6))))) |
| 3629 |
% $$ |
| 3630 |
% We start by loading $x$ (now in register $t$) into register |
| 3631 |
% $z$, then repeatedly run \cs{MFP@Rexp@iter} feeding it the |
| 3632 |
% successive values of $1/n$. This iterator first multiplies the most |
| 3633 |
% recent result (the $z$ register) by $1/n$, then that by $x$ and |
| 3634 |
% then adds $x$ to that. The final step is to add $1$. |
| 3635 |
% \begin{macrocode} |
| 3636 |
\def\MFP@Rexp@pos@prog{% |
| 3637 |
\MFP@Rcopy tz\MFP@Rexp@iter{14285714}\MFP@Rexp@iter{16666667}% |
| 3638 |
\MFP@Rexp@iter{20000000}\MFP@Rexp@iter{25000000}% |
| 3639 |
\MFP@Rexp@iter{33333333}\MFP@Rexp@iter{50000000}\MFP@Rcopyzx |
| 3640 |
\MFP@Rincr}% |
| 3641 |
\def\MFP@Rexp@iter#1{% |
| 3642 |
\MFP@Rcopyzx\MFP@Rload y10{#1}\MFP@Rsmul |
| 3643 |
\MFP@Rcopyzx\MFP@Rcopy ty\MFP@Rsmul |
| 3644 |
\MFP@Rcopyzx\MFP@Rcopy ty\MFP@Radd}% |
| 3645 |
% \end{macrocode} |
| 3646 |
% It is impossible to get accuracy to the last digit when $e^x$ is large. |
| 3647 |
% This is because an absolute error in $x$ converts to a relative error |
| 3648 |
% in $e^x$, That is, knowing $x$ only to $10^{-8}$ means $e^x$ is off by |
| 3649 |
% (about) $e^x\cdot 10^{-8}$. Roughly speaking, this means only about $8$ |
| 3650 |
% places of $e^x$ are accurate, so if (for example) the integer part of |
| 3651 |
% $e^x$ has six places then only two places after the decimal are |
| 3652 |
% significant. |
| 3653 |
% |
| 3654 |
% \bigskip |
| 3655 |
% The first issue with negative exponents is that it doesn't take much to |
| 3656 |
% produce a value of $e^{-x}$ that rounds to $0$. Any $x > 19.11382792$. So |
| 3657 |
% we start by comparing to that value and simply return zero if $x$ is |
| 3658 |
% larger. |
| 3659 |
% |
| 3660 |
% We perform exactly the same reductions as for positive exponents, |
| 3661 |
% handling the integer part and the first decimal separately. Then we call |
| 3662 |
% the power series program (not the same). |
| 3663 |
% \begin{macrocode} |
| 3664 |
\def\MFP@Rexp@neg{% |
| 3665 |
\MFP@Rload y1{19}{11382792}% |
| 3666 |
\MFP@Rcmp |
| 3667 |
\ifMFP@pos |
| 3668 |
\MFP@Rloadz 000% |
| 3669 |
\else |
| 3670 |
\MFP@tempa\MFP@x@Int |
| 3671 |
\edef\MFP@powerof@e{% |
| 3672 |
\ifcase\MFP@tempa |
| 3673 |
11{0}\or |
| 3674 |
10{36787944}\or |
| 3675 |
10{13533528}\or |
| 3676 |
10{04978707}\or |
| 3677 |
10{01831564}\or |
| 3678 |
10{00673795}\or |
| 3679 |
10{00247875}\or |
| 3680 |
10{00091188}\or |
| 3681 |
10{00033546}\or |
| 3682 |
10{00012341}\or |
| 3683 |
10{00004540}\or |
| 3684 |
10{00001670}\or |
| 3685 |
10{00000614}\or |
| 3686 |
10{00000226}\or |
| 3687 |
10{00000083}\or |
| 3688 |
10{00000031}\or |
| 3689 |
10{00000011}\or |
| 3690 |
10{00000004}\or |
| 3691 |
10{00000002}\or |
| 3692 |
10{00000001}\else |
| 3693 |
000\fi}% |
| 3694 |
\@xp\MFP@Rloadz\MFP@powerof@e |
| 3695 |
\ifnum\MFP@x@Frc=0 |
| 3696 |
\else |
| 3697 |
\MFP@Rcopyz s% |
| 3698 |
\MFP@tempa=\@xp\MFP@oneofmany\MFP@x@Frc\MFP@end |
| 3699 |
\edef\MFP@powerof@e{% |
| 3700 |
y1\ifcase\MFP@tempa |
| 3701 |
10\or |
| 3702 |
0{90483742}\or |
| 3703 |
0{81873075}\or |
| 3704 |
0{74081822}\or |
| 3705 |
0{67032005}\or |
| 3706 |
0{60653066}\or |
| 3707 |
0{54881164}\or |
| 3708 |
0{49658530}\or |
| 3709 |
0{44932896}\or |
| 3710 |
0{40656966}\else |
| 3711 |
10\fi}% |
| 3712 |
\edef\MFP@t@Frc{0\@xp\@gobble\MFP@x@Frc}% |
| 3713 |
\MFP@Rcopy sx\@xp\MFP@Rload\MFP@powerof@e\MFP@Rmul |
| 3714 |
\ifnum\MFP@t@Frc=0 |
| 3715 |
\else |
| 3716 |
\MFP@Rcopyz s\MFP@Rload t10\MFP@t@Frc |
| 3717 |
\MFP@Rexp@neg@prog |
| 3718 |
\MFP@Rcopyzx\MFP@Rcopy sy\MFP@Rmul |
| 3719 |
\fi |
| 3720 |
\fi |
| 3721 |
\fi}% |
| 3722 |
% \end{macrocode} |
| 3723 |
% |
| 3724 |
% Since $x$ is now positive we calculate $e^{-x}$. Again we need only up |
| 3725 |
% to the 6th power, organized as follows |
| 3726 |
% $$ |
| 3727 |
% 1 - x(1 - x/2(1 - x/3(1 - x/4(1 - x/5(1 - x/6))))) |
| 3728 |
% $$ |
| 3729 |
% Since this has exactly the same form as the the power series calculation |
| 3730 |
% for $\log$ and $\sin$, we can reuse the code in \cs{MFP@com@iter}. We |
| 3731 |
% end with the final multiplication by $x$ and the subtraction from 1 |
| 3732 |
% rather than call \cs{MFP@com@iter} with a useless multiplication by $1$. |
| 3733 |
% \begin{macrocode} |
| 3734 |
\def\MFP@Rexp@neg@prog{% |
| 3735 |
\MFP@Rcopy tx\MFP@Rload y10{14285712}\MFP@Rsmul |
| 3736 |
\MFP@com@iter{16666667}\MFP@com@iter{20000000}% |
| 3737 |
\MFP@com@iter{25000000}\MFP@com@iter{33333333}% |
| 3738 |
\MFP@com@iter{50000000}\MFP@flipz\MFP@Rcopyzx |
| 3739 |
\MFP@Rcopy ty\MFP@Rsmul\MFP@flipz}% |
| 3740 |
% \end{macrocode} |
| 3741 |
% |
| 3742 |
% The most efficient way to take an integer power of a number $x$ is to |
| 3743 |
% scan the binary code for the exponent. Each digit $1$ in this code |
| 3744 |
% corresponds to a $2^k$ power of $x$, which can be computed by repeatedly |
| 3745 |
% squaring $x$. These \emph{dyadic} powers are mutiplied together. We can |
| 3746 |
% convert this idea to a simple loop illustrated by this example of |
| 3747 |
% finding $x^{13}$ ($13 = 1101$ in base $2$). Here $p$ holds the current |
| 3748 |
% product and $q$ holds the current dyadic power of $x$, initialized with |
| 3749 |
% $p=1$ and $q=x$: |
| 3750 |
% \begin{enumerate} |
| 3751 |
% \item Rightmost digit 1: update $p\leftarrow pq = x$ and $q\leftarrow |
| 3752 |
% q^2 = x^2$. |
| 3753 |
% \item Next digit 0: Just update $q\leftarrow q^2 = x^4$. |
| 3754 |
% \item Next digit 1: update $p \leftarrow pq = x^5$ and $q\leftarrow |
| 3755 |
% q^2 = x^8$. |
| 3756 |
% \item Next digit 1: update $p \leftarrow pq = x^{13}$, detect that we |
| 3757 |
% are at the end and skip the update of $q$. Return $p$. |
| 3758 |
% \end{enumerate} |
| 3759 |
% Of course, this requires the binary digits of the exponent $n$. But the |
| 3760 |
% rightmost digit of $n$ is $1$ if and only if $n$ is odd, and we can |
| 3761 |
% examine each digit in turn if we divide $n$ by $2$ (discarding the |
| 3762 |
% remainder) at each stage. We detect the end when $n$ is reduced to $1$. |
| 3763 |
% |
| 3764 |
% Accuracy is partly a function of the number of multiplications. |
| 3765 |
% The above scheme requires at most $\lfloor\log_2 n\rfloor$ squarings |
| 3766 |
% and at most $\lceil \log_2 n \rceil$ multiplications for $x^n$, while |
| 3767 |
% directly multiplying $x\cdot x \cdots x$ would require $n-1$ |
| 3768 |
% multiplications. |
| 3769 |
% |
| 3770 |
% I have tested with an exponents around $8000$, which has 13 binary |
| 3771 |
% digits. Each squaring could double the relative error. For that |
| 3772 |
% large a power, the base has to be near 1 to avoid overflow or underflow. |
| 3773 |
% So the relative error is about $.5(10)^{-8}$. Doubling that 12 times |
| 3774 |
% would increase it to about $.00004$, and the result could have as little |
| 3775 |
% as 4 or 5 significant figures. In these tests, the results were actually |
| 3776 |
% accurate to 5 or 6 significant figures, starting with 8 figures. Raising |
| 3777 |
% to this power takes only about $25$ times as long as a single |
| 3778 |
% multiplication (rather than $7999$ times). |
| 3779 |
% |
| 3780 |
% For negative powers we can either find the positive power of $x$ and |
| 3781 |
% take its reciprocal or take the reciprocal of $x$ and find its positive |
| 3782 |
% power. We do the second so that overflow can be detected in |
| 3783 |
% \cs{MFP@@Rpow}. |
| 3784 |
% \begin{macrocode} |
| 3785 |
\def\MFP@Rpow{% |
| 3786 |
\ifnum\MFP@y@Frc>0 |
| 3787 |
\MFP@warn{The "pow" function requires an integer power. |
| 3788 |
\MFP@msgbreak The fractional part will be ignored}% |
| 3789 |
\fi |
| 3790 |
\MFP@loopctr=\MFP@y@Int\relax |
| 3791 |
\ifnum\MFP@loopctr=0 |
| 3792 |
\MFP@Rloadz 110% |
| 3793 |
\else |
| 3794 |
\ifnum\MFP@x@Sgn=0 |
| 3795 |
\ifnum\MFP@y@Sgn>0 |
| 3796 |
\MFP@Rloadz 000% |
| 3797 |
\else |
| 3798 |
\MFP@badpower@err |
| 3799 |
\MFP@Rloadz 1\xOverZeroInt\xOverZeroFrac |
| 3800 |
\fi |
| 3801 |
\else |
| 3802 |
\ifnum\MFP@x@Sgn>0 |
| 3803 |
\def\MFP@power@Sgn{1}% |
| 3804 |
\else |
| 3805 |
\edef\MFP@power@Sgn{\ifodd\MFP@loopctr -\fi 1}% |
| 3806 |
\fi |
| 3807 |
\ifnum\MFP@y@Sgn<0 \MFP@Rinv \MFP@Rcopyzx\fi |
| 3808 |
\ifnum\MFP@loopctr=1 |
| 3809 |
\MFP@Rloadz \MFP@power@Sgn\MFP@x@Int\MFP@x@Frc |
| 3810 |
\else |
| 3811 |
\MFP@@Rpow |
| 3812 |
\fi |
| 3813 |
\fi |
| 3814 |
\fi}% |
| 3815 |
% \end{macrocode} |
| 3816 |
% |
| 3817 |
% This implements the algorithm discussed above. We save $x$ in register |
| 3818 |
% $q$, initialize the starting value of $1$ in \reg{p} and then |
| 3819 |
% run the loop. If the binary digit just read is a 1 (i.e., \cs{ifodd} is |
| 3820 |
% true), it multiplies $p$ and $q$. It also saves the last product (copies |
| 3821 |
% \reg{z} to \reg{p}). This need not be done on the last iteration, |
| 3822 |
% but must not be moved out of the \cs{ifodd} conditional because |
| 3823 |
% intervening computations modify $z$. If there are more iterations to do |
| 3824 |
% (i.e., the \cs{ifnum} is true), this squares $q$ and reduces the |
| 3825 |
% counter. Note that the exponents $0$ and $1$ do not occur since we have |
| 3826 |
% handled them separately. |
| 3827 |
% |
| 3828 |
% In case of overflow (either the multiplication or the squaring) we |
| 3829 |
% break the loop and return $\pm\infty$. |
| 3830 |
% \begin{macrocode} |
| 3831 |
\def\MFP@@Rpow{% |
| 3832 |
\MFP@Rcopy xq% |
| 3833 |
\MFP@Rload p110% |
| 3834 |
\MFP@Rpow@loop}% |
| 3835 |
\def\MFP@Rpow@loop{% |
| 3836 |
\ifodd\MFP@loopctr |
| 3837 |
\MFP@Rcopy px\MFP@Rcopy qy\MFP@Rmul |
| 3838 |
\ifnum \MFP@z@Ovr>0 \MFP@handle@expoverflow |
| 3839 |
\else |
| 3840 |
\ifnum\MFP@loopctr>1 \MFP@Rcopyz p\fi |
| 3841 |
\fi |
| 3842 |
\fi |
| 3843 |
\ifnum\MFP@loopctr>1 |
| 3844 |
\MFP@Rcopy qx\MFP@Rsq |
| 3845 |
\ifnum \MFP@z@Ovr>0 \MFP@handle@expoverflow |
| 3846 |
\else |
| 3847 |
\MFP@Rcopyz q% |
| 3848 |
\divide\MFP@loopctr 2 |
| 3849 |
\@XP\MFP@Rpow@loop |
| 3850 |
\fi |
| 3851 |
\fi}% |
| 3852 |
\def\MFP@handle@expoverflow{% |
| 3853 |
\MFP@expoverflow@err |
| 3854 |
\MFP@loopctr=0 |
| 3855 |
\MFP@Rloadz\MFP@power@Sgn\MaxRealInt\MaxRealFrac}% |
| 3856 |
% \end{macrocode} |
| 3857 |
% |
| 3858 |
% \subsection{The square root} |
| 3859 |
% |
| 3860 |
% One can combine logarithms and exponentials to get any power: to get |
| 3861 |
% $x^y$, compute $e^{y\ln x}$. This has the disadvantage that it doesn't |
| 3862 |
% work if $x$ is negative. Most powers of negative numbers are not |
| 3863 |
% defined, but certainly integer powers are. Thus we have defined |
| 3864 |
% \cs{MFPpow} and \cs{Rpow} for that case. |
| 3865 |
% |
| 3866 |
% If we enforce a positive $x$, then $y$ can have any value. However, |
| 3867 |
% the computation of $e^{.5\ln x}$ cannot give a result as good as one can |
| 3868 |
% get from a special purpose algorithm for the square root. For example, |
| 3869 |
% the inaccuracies in computing $\ln x$ will make $e^{.5\ln 9}$ inexact, |
| 3870 |
% while the square root function we implement below will produce exactly |
| 3871 |
% $\sqrt{9} = 3$. In fact, if a square root can be expressed exactly |
| 3872 |
% within our 8-digit precision, our code will find it. |
| 3873 |
% |
| 3874 |
% For the square root we return zero if $x$ is not positive. If the integer |
| 3875 |
% part of $x$ is $0$, we copy the fractional part to the integer part |
| 3876 |
% (that is, we multiply by $10^{8}$, remembering to multiply by $10^{-4}$ |
| 3877 |
% later). This makes the square root of such numbers rather more |
| 3878 |
% accurate. (To get around some other rare but annoying inaccuracies, we |
| 3879 |
% go through a similar process when the integer part of $x$ is at most $4$ |
| 3880 |
% digits, multiplying by $10^4$ before and by $10^{-2}$ after.) |
| 3881 |
% |
| 3882 |
% We then compute the square root using an algorithm that will |
| 3883 |
% be exact whenever possible. We perform one additional processing step. |
| 3884 |
% To explain it, note that our algorithm actually produces the largest |
| 3885 |
% number $s$ with four digits right of the decimal place that satisfies $s^2 |
| 3886 |
% \le x$. That is |
| 3887 |
% $$ |
| 3888 |
% s^2 \le x < \left( s + 10^{-4} \right)^2 |
| 3889 |
% $$ |
| 3890 |
% From this it follows that $x = (s+\epsilon)^2 = s^2 + 2s\epsilon + |
| 3891 |
% \epsilon^2$ with $\epsilon < 10^{-4}$ (and so $\epsilon^2 < 10^{-8}$). |
| 3892 |
% We estimate this $\epsilon$ and add that estimate to $s$. The estimate |
| 3893 |
% we use is obtained by discarding the very small $\epsilon^2$ and solving |
| 3894 |
% for the remaining $\epsilon$ get |
| 3895 |
% $$ |
| 3896 |
% \epsilon \approx \bar\epsilon = \frac{x-s^2}{2s} |
| 3897 |
% $$ |
| 3898 |
% With this value, $s + \bar\epsilon$ misses the exact square root by at |
| 3899 |
% most $\epsilon^2/(2s) < .5\cdot 10^{-8}$, because $s \ge 1$. |
| 3900 |
% The final result $s + \bar\epsilon$ is equivalent to computing the |
| 3901 |
% average of $s$ and $x/s$. This, possibly divided by $10^4$ or $10^2$ is the |
| 3902 |
% returned value. |
| 3903 |
% |
| 3904 |
% By tests, with rare exceptions, our computations produces a result |
| 3905 |
% correct in all eight decimal places. In the rare exception, the last |
| 3906 |
% place is within $1$ of the correct value. |
| 3907 |
% \begin{macrocode} |
| 3908 |
\def\MFP@Rsqrt{% |
| 3909 |
\ifcase\MFP@x@Sgn\relax |
| 3910 |
\MFP@Rzero |
| 3911 |
\or |
| 3912 |
\ifnum\MFP@x@Int=0 |
| 3913 |
\def\MFP@sqrt@reduce{2}% |
| 3914 |
\edef\MFP@x@Int{\number\MFP@x@Frc}% |
| 3915 |
\edef\MFP@x@Frc{00000000}% |
| 3916 |
\else\ifnum\MFP@x@Int<10000 |
| 3917 |
\def\MFP@sqrt@reduce{1}% |
| 3918 |
\edef\MFP@x@Int{\MFP@x@Int\@xp\MFP@fourofmany\MFP@x@Frc\MFP@end}% |
| 3919 |
\edef\MFP@x@Frc{\@xp\MFP@gobblefour\MFP@x@Frc0000}% |
| 3920 |
\else |
| 3921 |
\def\MFP@sqrt@reduce{0}% |
| 3922 |
\fi\fi |
| 3923 |
\MFP@Rcopy xt% |
| 3924 |
\MFP@Isqrt |
| 3925 |
\MFP@Rcopyz s\MFP@Rcopyzy |
| 3926 |
\MFP@Rcopy tx\MFP@Rdiv |
| 3927 |
\MFP@Rcopy sx\MFP@Rcopyzy\MFP@Radd |
| 3928 |
\MFP@Rcopyzx\MFP@Rhalve |
| 3929 |
\ifcase \MFP@sqrt@reduce\relax |
| 3930 |
\or |
| 3931 |
\MFP@Rcopyzx\MFP@Rload y10{01000000}\MFP@Rmul |
| 3932 |
\or |
| 3933 |
\MFP@Rcopyzx\MFP@Rload y10{00010000}\MFP@Rmul |
| 3934 |
\fi |
| 3935 |
\else |
| 3936 |
\MFP@warn{Square root of a negative number. Zero will be returned.}% |
| 3937 |
\MFP@Rzero |
| 3938 |
\fi}% |
| 3939 |
\def\MFP@fourofmany#1#2#3#4#5\MFP@end{#1#2#3#4}% |
| 3940 |
\def\MFP@gobblefour#1#2#3#4{}% |
| 3941 |
% \end{macrocode} |
| 3942 |
% |
| 3943 |
% There is a rather straightforward pencil and paper algorithm that |
| 3944 |
% provides the square root digit by digit, and it produces an exact answer |
| 3945 |
% when that is possible. Unfortunately, the decimal version is not easy to |
| 3946 |
% code. Fortunately the same algorithm works in any number base and it is |
| 3947 |
% rather simple to code the binary version (because we only need to decide |
| 3948 |
% at each stage whether the ``next digit'' is $0$ or $1$. This produces a |
| 3949 |
% square root in binary digits, from which it is easy to compute the |
| 3950 |
% number itself. The result is exact if the answer would be a finite |
| 3951 |
% number of binary digits. We apply it to the integer $10^8 x$. While this |
| 3952 |
% number is too large for \TeX{} to handle as an integer, it is not that |
| 3953 |
% hard to convert it to a string of binary digits stored in a macro. |
| 3954 |
% |
| 3955 |
% The algorithm simplifies somewhat if we proces a base 4 integer, |
| 3956 |
% producing a base 2 result. Also, instead of producing the square root |
| 3957 |
% encoded in a string of binary digits, we simply build the numerical |
| 3958 |
% result as we discover the binary digits (multiply previous value by two |
| 3959 |
% and add the new digit.) Fortunately, the square root of $10^8 x$ (and |
| 3960 |
% the temporary scratch registers used in the code) will never exceed |
| 3961 |
% \TeX{}'s limit for integers. |
| 3962 |
% |
| 3963 |
% The macro \cs{MFP@ItoQ} implements the conversion to base-4 digits. |
| 3964 |
% The two arguments are the integer and fractional part of $x$. The |
| 3965 |
% result is stored in \cs{MFP@ItoQ@Tmp}, which is so far only used by the |
| 3966 |
% square root code. |
| 3967 |
% |
| 3968 |
% The test \cs{ifodd}\cs{MFP@tempb} is used to get the binary digits. |
| 3969 |
% Combining two of them yields the quadrenary digits. The |
| 3970 |
% \cs{ifodd}\cs{MFP@tempa} tests are there to check whether there |
| 3971 |
% will be a remainder after division by $2$, which should then be |
| 3972 |
% inserted at the front of \cs{MFP@tempb} before division by $2$. Two |
| 3973 |
% divisions by $2$ each iteration amounts to division by $4$. This is slightly |
| 3974 |
% more efficient than dividing by $4$ and determining the remainder. |
| 3975 |
% \begin{macrocode} |
| 3976 |
\def\MFP@ItoQ#1#2{% |
| 3977 |
\MFP@tempa#1\relax\MFP@tempb#2\relax |
| 3978 |
\def\MFP@ItoQ@Tmp{}\MFP@ItoQ@loop}% |
| 3979 |
\def\MFP@ItoQ@loop{% |
| 3980 |
\ifodd\MFP@tempb |
| 3981 |
\ifodd\MFP@tempa \advance\MFP@tempb \MFP@ttteight\relax\fi |
| 3982 |
\divide\MFP@tempa2 \divide\MFP@tempb2 |
| 3983 |
\edef\MFP@ItoQ@Tmp{\ifodd\MFP@tempb 3\else 1\fi\MFP@ItoQ@Tmp}% |
| 3984 |
\else |
| 3985 |
\ifodd\MFP@tempa \advance\MFP@tempb \MFP@ttteight\relax\fi |
| 3986 |
\divide\MFP@tempa2 \divide\MFP@tempb2 |
| 3987 |
\edef\MFP@ItoQ@Tmp{\ifodd\MFP@tempb 2\else 0\fi\MFP@ItoQ@Tmp}% |
| 3988 |
\fi |
| 3989 |
\ifodd\MFP@tempa \advance\MFP@tempb \MFP@ttteight\relax\fi |
| 3990 |
\divide\MFP@tempa 2 \divide\MFP@tempb 2 |
| 3991 |
\ifnum\MFP@tempa>0 |
| 3992 |
\@xp\MFP@ItoQ@loop |
| 3993 |
\else\ifnum\MFP@tempb>0 |
| 3994 |
\@XP\MFP@ItoQ@loop |
| 3995 |
\fi\fi}% |
| 3996 |
% \end{macrocode} |
| 3997 |
% |
| 3998 |
% This integer square root $n$ is $10^4$ times the largest number $y$ |
| 3999 |
% satisfying $y^2 \le x$ and having at most four decimal places. The rest of |
| 4000 |
% the code after the \cs{MFP@Isqrt@loop} is intended to divide $n$ |
| 4001 |
% (returned in \cs{MFP@tempc}) by $10^4$ in order to get the number $y$ |
| 4002 |
% itself. |
| 4003 |
% \begin{macrocode} |
| 4004 |
\def\MFP@Isqrt{% |
| 4005 |
\MFP@ItoQ\MFP@x@Int\MFP@x@Frc |
| 4006 |
\MFP@tempa=0 \MFP@tempb=0 \MFP@tempc=0 |
| 4007 |
\expandafter\MFP@Isqrt@loop\MFP@ItoQ@Tmp\MFP@end |
| 4008 |
\MFP@tempa=\MFP@tempc |
| 4009 |
\divide\MFP@tempc\MFP@tttfour |
| 4010 |
\edef\MFP@z@Int{\number\MFP@tempc}% |
| 4011 |
\multiply\MFP@tempc \MFP@tttfour |
| 4012 |
\advance\MFP@tempa -\MFP@tempc |
| 4013 |
\edef\MFP@z@Frc{\number\MFP@tempa}% |
| 4014 |
\makeMFP@fourdigits\MFP@z@Frc |
| 4015 |
\edef\MFP@z@Frc{\MFP@z@Frc0000}% |
| 4016 |
\def\MFP@z@Sgn{1}}% |
| 4017 |
% \end{macrocode} |
| 4018 |
% |
| 4019 |
% The following is a loop that essentially performs a base-2 version of |
| 4020 |
% the base-10 algorithm that I learned at age 12 from my father |
| 4021 |
% (apparently it was taught in eighth or ninth grade in his day, but not |
| 4022 |
% in mine). Seeing it written out, I am surprise at how concise and |
| 4023 |
% elegant it is! |
| 4024 |
% \begin{macrocode} |
| 4025 |
\def\MFP@Isqrt@loop#1{% |
| 4026 |
\ifx\MFP@end #1% |
| 4027 |
\else |
| 4028 |
\multiply\MFP@tempa 2 \multiply\MFP@tempb 4 \multiply\MFP@tempc 2 |
| 4029 |
\advance \MFP@tempb#1\relax |
| 4030 |
\ifnum\MFP@tempa<\MFP@tempb |
| 4031 |
\advance\MFP@tempc 1 \advance\MFP@tempa 1 |
| 4032 |
\advance\MFP@tempb -\MFP@tempa |
| 4033 |
\advance\MFP@tempa 1 |
| 4034 |
\fi |
| 4035 |
\expandafter\MFP@Isqrt@loop |
| 4036 |
\fi}% |
| 4037 |
% \end{macrocode} |
| 4038 |
% |
| 4039 |
%^^A For my own benefit: the above code finds the next binary digit and |
| 4040 |
%^^A updates the square root (in \cs{MFP@tempc}) by appending that digit. The |
| 4041 |
%^^A new digit is also appended to the end of \cs{MFP@tempa}. This is |
| 4042 |
%^^A subtracted from \cs{MFP@tempb}, but only if the last digit is a 1. Then |
| 4043 |
%^^A the next quadrenary digit is appended to \cs{MFP@tempb}. Finally, the |
| 4044 |
%^^A last binary digit found is added (not appended) to \cs{MFP@tempa}. The |
| 4045 |
%^^A ``appending'' of a digit means a multiplication by $2$ (or $4$) and the |
| 4046 |
%^^A addition of the digit. We perform such additions only if the digit is a |
| 4047 |
%^^A 1, and we determine if the digit is 1 or 0 by the \cs{ifnum} test. |
| 4048 |
% |
| 4049 |
% \subsection{Random numbers} |
| 4050 |
% |
| 4051 |
% We borrow the code of \file{random.tex} to generate a random integer in |
| 4052 |
% the range $1$ to $2^{31}-2$, inclusive. Mathematically, this works |
| 4053 |
% because the modulus $m = 2^{31}-1$ is a prime number, and the |
| 4054 |
% multiplicative group of nonzero elements of $\mathbb{Z}_m$ is cyclic. |
| 4055 |
% The multiplier chosen (in our cases $16\,807$, $48\,271$, or $69\,621$) |
| 4056 |
% has to be a generator of that group. |
| 4057 |
% |
| 4058 |
% The first step is the code for \cs{nextrandom} from \file{random.tex}. |
| 4059 |
% We could omit this if it is already defined, or we could even input |
| 4060 |
% \file{random.tex} but, for better control, we define it ourselves with |
| 4061 |
% an internal name. This code leaves the next random number in |
| 4062 |
% \cs{MFP@randseed}. The initial seed is calculated from the time and |
| 4063 |
% date if it was not positive |
| 4064 |
% \begin{macrocode} |
| 4065 |
\newcount\MFP@randseed % the random number (and starting seed) |
| 4066 |
\def\MFP@nextrand{\begingroup |
| 4067 |
\ifnum\MFP@randseed<1 |
| 4068 |
\global\MFP@randseed\time |
| 4069 |
\global\multiply\MFP@randseed388 \global\advance\MFP@randseed\year |
| 4070 |
\global\multiply\MFP@randseed31 \global\advance\MFP@randseed\day |
| 4071 |
\global\multiply\MFP@randseed97 \global\advance\MFP@randseed\month |
| 4072 |
\MFP@nextrand \MFP@nextrand \MFP@nextrand |
| 4073 |
\fi |
| 4074 |
\MFP@tempa\MFP@randseed |
| 4075 |
\divide\MFP@tempa \MFP@rand@q % modulus = m*q + r |
| 4076 |
\MFP@tempb\MFP@tempa |
| 4077 |
\multiply\MFP@tempa \MFP@rand@q |
| 4078 |
\global\advance\MFP@randseed-\MFP@tempa % seed mod q |
| 4079 |
\global\multiply\MFP@randseed \MFP@rand@m |
| 4080 |
\multiply\MFP@tempb \MFP@rand@r |
| 4081 |
\global\advance\MFP@randseed-\MFP@tempb |
| 4082 |
\ifnum\MFP@randseed<\z@ \global\advance\MFP@randseed "7FFFFFFF\relax\fi |
| 4083 |
\endgroup}% |
| 4084 |
% \end{macrocode} |
| 4085 |
% |
| 4086 |
% \DescribeMacro{\MFPrandgenA}\DescribeMacro{\MFPrandgenB} |
| 4087 |
% \DescribeMacro{\MFPrandgenC} |
| 4088 |
% We have paametrized \cs{MFP@nextrand} so that any suitable multiplier |
| 4089 |
% can be used. The following commands each select one of the three |
| 4090 |
% multipliers that we provide, plus precomputed values for the quotient |
| 4091 |
% and remainder. We default to generator ``A''. |
| 4092 |
% \begin{macrocode} |
| 4093 |
\def\MFPrandgenA{\def\MFP@rand@m{16807 }\def\MFP@rand@q{127773 }% |
| 4094 |
\def\MFP@rand@r{2836 }}% |
| 4095 |
\def\MFPrandgenB{\def\MFP@rand@m{48271 }\def\MFP@rand@q{44488 }% |
| 4096 |
\def\MFP@rand@r{3399 }}% |
| 4097 |
\def\MFPrandgenC{\def\MFP@rand@m{69621 }\def\MFP@rand@q{30845 }% |
| 4098 |
\def\MFP@rand@r{23902 }}% |
| 4099 |
\MFPrandgenA |
| 4100 |
% \end{macrocode} |
| 4101 |
% |
| 4102 |
% The command \verb$\MFPranr{$\meta{x}\verb$}\X$ will take a parameter $x$ |
| 4103 |
% and define \cs{X} to contain a (pseudo)random real number in the |
| 4104 |
% interval $[0,x]$. Theoretically, the number should lie in $[0,x)$, but |
| 4105 |
% rounding will make $x$ itself a possible value. Similarly, \cs{Rrand} |
| 4106 |
% will replace the $x$ on top of the stack with this random value. To get |
| 4107 |
% the result, we call \cs{MFP@getrand} twice to produce two random |
| 4108 |
% integers in the range $[0,99999999]$ and assemble them into a double |
| 4109 |
% precision multiplier less than $1$. Then we multiply $x$ by that with |
| 4110 |
% our \cs{MDP@DPmul}. |
| 4111 |
% |
| 4112 |
% The test at the end of \cs{MFP@getrand} fails only about 1 time in 50, |
| 4113 |
% so the odds are vanishingly small that more than a few tries are needed. |
| 4114 |
% \begin{macrocode} |
| 4115 |
\def\MFP@getrand{% leaves result in \MFP@tempa |
| 4116 |
\MFP@nextrand |
| 4117 |
\MFP@tempa\MFP@randseed |
| 4118 |
\advance\MFP@tempa-1 |
| 4119 |
\divide\MFP@tempa 21 % (2^31-3)= 100000000*21 + r |
| 4120 |
\ifnum \MFP@ttteight> \MFP@tempa |
| 4121 |
\else \@xp\MFP@getrand\fi}% |
| 4122 |
\def\MFP@Rrand{% |
| 4123 |
\MFP@getrand \edef\MFP@a@Tmp{\number\MFP@tempa}% |
| 4124 |
\MFP@getrand \edef\MFP@b@Tmp{\number\MFP@tempa}% |
| 4125 |
\MFP@DPmul0\MFP@a@Tmp\MFP@b@Tmp}% |
| 4126 |
% \end{macrocode} |
| 4127 |
% |
| 4128 |
% \DescribeMacro{\MFPsetseed} |
| 4129 |
% Finally, a user-level command to set the seed value. |
| 4130 |
% \begin{macrocode} |
| 4131 |
\def\MFPsetseed#1{\global\MFP@randseed #1\relax}% |
| 4132 |
\MFP@xfinish |
| 4133 |
%</extra> |
| 4134 |
% \end{macrocode} |
| 4135 |
%\Finale |
| 4136 |
% |