diff options
author | Karl Berry <karl@freefriends.org> | 2015-01-04 23:05:16 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2015-01-04 23:05:16 +0000 |
commit | 90561dbec0fcb33dc7607338973c174774a32aba (patch) | |
tree | c30a0b0c035d2e8a62f14f0dd372ed7461f4b71c /Master/texmf-dist | |
parent | a720b5b8835a3527a1454d218a856e45643611d5 (diff) |
apnum (4jan15)
git-svn-id: svn://tug.org/texlive/trunk@35958 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist')
-rw-r--r-- | Master/texmf-dist/doc/generic/apnum/README | 13 | ||||
-rw-r--r-- | Master/texmf-dist/doc/generic/apnum/apnum.d | 133 | ||||
-rw-r--r-- | Master/texmf-dist/doc/generic/apnum/apnum.pdf | bin | 498436 -> 488569 bytes | |||
-rw-r--r-- | Master/texmf-dist/tex/generic/apnum/apnum.tex | 139 |
4 files changed, 106 insertions, 179 deletions
diff --git a/Master/texmf-dist/doc/generic/apnum/README b/Master/texmf-dist/doc/generic/apnum/README index 3abf6cad9fb..4f838b6a01c 100644 --- a/Master/texmf-dist/doc/generic/apnum/README +++ b/Master/texmf-dist/doc/generic/apnum/README @@ -3,12 +3,12 @@ apnum -- Arbitrary precision numbers implemented by TeX macros Petr Olsak Nov. 2014 The basic operations (addition, subtraction, multiplication, division, power -to an integer) are implemented by TeX macros in this package. Operands can be -numbers with arbitrary number of digits (or scientific notation is allowed). -The expression scanner is provided too. The exhaustive documentation -(including detail TeXnical documentation) is included. The macro includes many -optimizations and uses only TeX primitives (from classic TeX) and \newcount -macro. +to an integer) are implemented by TeX macros in this package. Operands can +be numbers with arbitrary number of digits (or scientific notation is +allowed). The expression scanner is provided too. The exhaustive +documentation (including detail TeXnical documentation) is included. The +macro includes many optimizations and uses only TeX primitives (from classic +TeX) and \newcount macro. Files: README ... this file apnum.tex ... TeX macro @@ -16,4 +16,3 @@ Files: README ... this file apnum.d ... documentation (source for apnum.pdf) License: Public domain. - diff --git a/Master/texmf-dist/doc/generic/apnum/apnum.d b/Master/texmf-dist/doc/generic/apnum/apnum.d index 4c7b65572f2..bb54c03037f 100644 --- a/Master/texmf-dist/doc/generic/apnum/apnum.d +++ b/Master/texmf-dist/doc/generic/apnum/apnum.d @@ -10,7 +10,7 @@ \input utf8off \clearmubyte % use pdfcsplain -\def\projectversion{1.0 2014} +\def\projectversion{1.1 Jan 2015} \def\headtitle{Arbitrary Precision Numbers} \widowpenalty=10000 @@ -503,11 +503,11 @@ If there is a problem (level of brackets less than zero, level of brackets not equal to zero at the end of the expression, unknown operator) we print an error using "\apEVALerror" macro. -The "\next" is set to "\apEVALb", i.e.\ scanner returns back to the state of +The "\apNext" is set to "\apEVALb", i.e.\ scanner returns back to the state of reading the operand. But exceptions exist: if the ")" is found then priority is decreased and the macro "\apEVALo" is executed again. If the end of the "<expression>" is found then the loop is ended by -"\let\next=\relax". +"\let\apNext=\relax". \inext{apEVALo}{\count=2 ^^B\cbrace}{++} @@ -545,7 +545,7 @@ executed after group by the "\apEVALa" macro. The macro \db apEVALerror "<string>" prints an error message. We decide to be better to print only "\message", no "\errmessage". The "\tmpb" is -prepared to create "\OUT" as "??" and the "\next" macro is set in order to skip +prepared to create "\OUT" as "??" and the "\apNext" macro is set in order to skip the rest of the scanned "<expression>". \inext{apEVALerror}{^^B\cbrace}{++} @@ -736,7 +736,7 @@ The macro \db apPLUSa does the following work: \ilabel[plus:ba] {apPLUSg} \ilabel[plus:bb] {apnumC=-} \ilabel[plus:G] {apnumG=0} -\ilabel[plus:next] {next=} +\ilabel[plus:next] {apNext=} \ilabel[plus:X] {apnumX=0} \ilabel[plus:fa] {00123} \ilabel[plus:fb] {apPLUSy} @@ -784,10 +784,10 @@ The macro \db apPLUSa does the following work: \item * The transmission calculation is different for addition and subtraction. If the subtraction is processed then the sign of the result is set (using the value "\apnumG") and the "\apPLUSm" for transmissions is - prepared. Else the "\apPLUSp" for transmissions is prepared as the "\next" macro + prepared. Else the "\apPLUSp" for transmissions is prepared as the "\apNext" macro (line~\cite[plus:next]) \item * The result of the first pass is expanded in the input stream and the - "\next" (i.e.\ transmissions calculation) is activated at line~\cite[plus:X]. + "\apNext" (i.e.\ transmissions calculation) is activated at line~\cite[plus:X]. \item * if the result is in the form ".000123", then the decimal point and the trailing zeros have to be inserted. Else the trailing zeros from the left side of the result have to be removed by "\apPLUSy". This macro adds @@ -839,7 +839,7 @@ sign of the result to the "\apnumG" and (maybe) exchanges the "\apPLUSxA" and "\apPLUSxB" macros (by the \db apPLUSg macro) in order to the internal result of the subtraction will be always non-negative. -If the end of input stream is reached, then "\next" (used at line~\cite[plus:nn]) +If the end of input stream is reached, then "\apNext" (used at line~\cite[plus:nn]) is reset from its original value "\apPLUSc" to the \db apPLUSd where the "\apnumY" is simply set to zero. The reading from input stream is finished. This occurs when there are more Digits after decimal point in the second @@ -848,7 +848,7 @@ operand than in the first one. If the end of input stream is reached and the \db apPLUSf macro removes the rest of input stream and the first pass of the calculation is done. -\ilabel[plus:nn] {next^^E} +\ilabel[plus:nn] {apNext^^E} \inext{apPLUSc}{def\nb apPLUSh}{++} Why there is a complication about reading one parameter from input stream @@ -1252,7 +1252,7 @@ macros and does the following work: \ilabel[div:outa] {edef\nb OUT} \ilabel[div:outb] {apADDzeros} \ilabel[div:z] {apnumZ=} -\ilabel[div:next] {next=} +\ilabel[div:next] {apNext=} \ilabel[div:add] {apnumZ=\nb apnumD} \ilabel[div:rema] {apDIVv} \ilabel[div:remb] {apROLLa} @@ -1413,7 +1413,7 @@ case, we put it to the input stream using "\expandafter\apDIVh\tmpa" followed by four "\apNL"s (which simply expands zero digit) followed by stop-mark. The "\apDIVh" reads one Digit from input stream. Else we put only the stop-mark to the input stream and run the -"\apDIVi". The "\nexti" is set to the "\apDIVi", so the macro "\apDIVh" will +"\apDIVi". The "\apNexti" is set to the "\apDIVi", so the macro "\apDIVh" will be skipped forever and no new Digit is read from input stream. \inext{apDIVg}{^^B\cbrace}{++} @@ -1421,7 +1421,7 @@ be skipped forever and no new Digit is read from input stream. The macro \db apDIVh reads one Digit from data stream (from the rest of the dividend) and saves it to the "\apnumZ" register. If the stop-mark is reached (this is recognized that the last digit is the "\apNL"), -then "\nexti" is set to "\apDIVi", so the "\apDIVh" is never +then "\apNexti" is set to "\apDIVi", so the "\apDIVh" is never processed again. \inext{apDIVh}{^^B\cbrace}{++} @@ -1541,42 +1541,26 @@ is simple but slow. The goal of this section is to present the power to the integer with some optimizations. Let $a$ is the base of the powering computation and $d_1, d_2, d_3, \dots, -d_n$ are decimal digits of the exponent (in reverse order). Then +d_n$ are binary digits of the exponent (in reverse order). Then $$ - p = a^{1\,{d_1}+10\,{d_2}+100\,{d_3}+\cdots+10^{n-1}\,{d_n}} = - (a^1)^{d_1}\cdot(a^{10})^{d_2}\cdot(a^{100})^{d_3}\cdot (a^{10^{n-1}})^{d_n}. + p = a^{1\,{d_1}+2\,{d_2}+2^2\,{d_3}+\cdots+2^{n-1}\,{d_n}} = + (a^1)^{d_1}\cdot(a^2)^{d_2}\cdot(a^{2^2})^{d_3}\cdot (a^{2^{n-1}})^{d_n}. $$ -We can see from previous equation that the $p$ can be computed by the +If $d_i=0$ then $z^{d_i}$ is one and this can be omitted from the queue of +multiplications. If $d_i=1$ then we keep $z^{d_i}$ as $z$ in the queue. +We can see from this that the $p$ can be computed by the following algorithm: \begtt -p := 1 -for (i=1; i<n+1; i++) { - if (i>1) a := a^10; - if (d[i]>0) p := p * a^d[i]; +(* "a" is initialized as the base, "e" as the exponent *) +p := 1; +while (e>0) { + if (e%2) p := p*a; + e := e/2; + if (e>0) a := a*a; } +(* "p" includes the result *) \endtt -% -We have a problem that we are playing with the number system of the base 10. -The calculation of "a:=a^10" is not such trivial as a similar task in -binary system. We have to calculate all new decimal digits by -multiplications. But we needn't to do 9 multiplications, because $a^{10} = -(a\,(a^2)^2)^2$, i.e. only four multiplications are needed. Suppose two functions -"pow2(x){x:=x*x}" and "mul(x){x:=a*x}". We can reuse the information from the -previous step where "a^d[i-1]" is already computed. For example $d_{i-1}$ -from previous step was 4 and this was computed by "b:=a;" "pow2(b);" "pow2(b);". -Then we can compute $a^{10}$ from the previous step by -"mul(b);" "pow(b);" "a:=b". The algorithm above can be more specified by: - -\begtt -p := 1; a := base; b := a -for (i=1; i<n+1; i++) { - if (i>1) { use previous value of b and functions mul(b), pow2(b) - in order to b = a^10; - a := b; } - if (d[i]>0) { use functions mul(b), pow2(b) in order to b = a^d[i]; - p := p * b; } -\endtt The macro \db apPOWa does the following work. @@ -1591,7 +1575,6 @@ The macro \db apPOWa does the following work. \ilabel[pow:ab] {ifcase} \ilabel[pow:ba] {apMULb} \ilabel[pow:bb] {*.\nb OUT} -\ilabel[pow:c] {apMULc} \ilabel[pow:p] {1*} \ilabel[pow:rev] {apREV} \ilabel[pow:b] {apPOWb} @@ -1622,19 +1605,12 @@ The macro \db apPOWa does the following work. (lines~\cite[pow:aa] to~\cite[pow:ab]). \item * The base is saved in interleaved reversed format (like in "\apMULa") into the "\OUT" macro by the "\apMULb" macro. Let it be the - $b$ value from our algorithm described above + $a$ value from our algorithm described above (lines~\cite[pow:ba] and~\cite[pow:bb]). -\item * The same base is saved to "\tmpa" in reversed (but non-interleaved) - format by "\apMULc". Let it be the $a$ value from our algorithm - (line~\cite[pow:c]). \item * The initial value of $p=1$ from our algorithm is set in interleaved format into "\tmpc" macro (line~\cite[pow:p]). -\item * In brief: $a={}$"\tmpa", $b={}$"\OUT", $p={}$"\tmpc". -\item * The decimal digits of the exponent in "\tmpb" are reversed - (line~\cite[pow:rev]). -\item * The main loop described above is processed by putting decimal digits - of the exponent into input stream in reversed order and processing the "\apPOWb" macro. +\item * The main loop described above is processed by "\apPOWb" macro. (line~\cite[pow:b]). \item * The result in "\tmpc" is converted into human readable form by the "\apPOWg" macro and it is stored into the "\OUT" macro @@ -1650,50 +1626,18 @@ The macro \db apPOWa does the following work. \inext{apPOWa}{^^B\cbrace}{++} -The macro \db apPOWb is the body of the loop in the algorithm described above. The part -"\ifnum\apnumE"{\tt\char`<}"10" is processed only for second and next digits because the -"\apnumE" is initialized as 10 in the "\apPOWa" macro and it includes the digit of -the exponent from the previous step when next step is processed. -This is equivalent to "if(i"{\tt\char`>}"1)" from -algorithm described above. The sequence of functions "mul(b)" and "pow2(b)" -are set for each value of the digit of the exponent in the "\ifcase" -arguments. For example, the digit from the previous step was~4, i.e.\ -$b=a^4$. Then "\apPOWc"~"mp." is processed. This expands to "\apPOWm" "\apPOWp" -which are macros where the functions "mul(b)" and "pow2(b)" are -implemented. The following operations is processed: "b:=a*b" ($b=a^5$) and "b:=b*b" ($b=a^{10}$). -Second example: the previous digit was zero. Then we need to -calculate $b^{10}$ which is done by "\apPOWc"~"ppmp", i.e.\ -"\apPOWp\apPOWp\apPOWm\apPOWp", i.e. "b:=b*b;" "b:=b*b", "b:=a*b", "b:=b*b". - -The line~\cite[pow:b1] is equivalent to "a:=b". The non-interleaved format is -extracted from "\OUT" by "\apPOWd" and saved to "\tmpb" and then the "\tmpa" -is set to "\tmpb". - -The second part of "\apPOWb" (lines~\cite[pow:b2] to~\cite[pow:b3]) is equivalent to the -second part of the loop in the algorithm described above. The -multiplication "p:=p*b" is done at lines \cite[pow:b4]~to~\cite[pow:b3]: first the -"\OUT" (i.e.\ the $b$ value) is converted to non-interleaved format by "\apPOWd" and saved to -"\tmpb". This is needed because because "\apMULb" does the multiplication -"\OUT:=\OUT*\tmpb" between interleaved and non-interleaved format (both reversed). -Then the result is ``normalized'' to the initial value of the interleaved format -by the "\apPOWn" macro. All these calculations are processed over "\OUT", -but we set "\OUT:=\tmpc" (i.e.\ the $p$ value) temporary for this multiplication. - -The loop is repeated until the stop-mark "@" is reached. - -\ilabel[pow:b1] {tmpa=} -\ilabel[pow:b2] {apnumE=0} -\ilabel[pow:b3] {OUT=\nb tmpd} -\ilabel[pow:b4] {tmpd=} +The macro \db apPOWb is the body of the loop in the algorithm described above. +The code part after "\ifodd\apnumE" does "p := p*a". In order to do this, we need +to convert "\OUT" (where "a" is stored) into normal format using "\apPOWd". +The result is saved in "\tmpb". Then the multiplication is done by "\apMULd" +and the result is normalized by the "\apPOWn" macro. Because "\apMULd" +works with "\OUT" macro, we temporary set "\tmpc" to "\OUT". +The code part after "\ifnum\apnumE"{\tt\char`<}"0" does "a := a*a" using the "\apPOWt" +macro. The result is normalized by the "\apPOWn" macro. \inext{apPOWb}{^^B\cbrace}{++} -The \db apPOWc "<letters><dot>" does "\apPOW<letter>" "\apPOW<letter>" etc. -For example "\apPOWc ppm." processes "\apPOWp\apPOWp\apPOWm". - -\inext{apPOWc}{}{++} - The macro \db apPOWd "<initialized interleaved reversed format>" extracts the Digits from its argument and saves them to the "\tmpb" macro. @@ -1710,13 +1654,6 @@ It ignores the first two elements from the format and runs \db apPOWh. \inext{apPOWg}{^^B\cbrace}{++} -The macro \db apPOWm does "b:=a*b", i.e.\ "\OUT:=\tmpa*\OUT". The \db apPOWp -does "b:=b*b", i.e.\ "\OUT:=\OUT*\OUT". The ``normalization'' of "\OUT" is -done by the "\apPOWn" macro after the calculation is finished. It means that -the interleaved format is reinitialized. - -\inext{apPOWm}{apPOWp}{++} - The normalization to the initialized interleaved format of the "\OUT" is done by the \db apPOWn "<data>@" macro. The \db apPOWna reads the first part of the "<data>" (to the first "*", where the Digits are non-interleaved. The diff --git a/Master/texmf-dist/doc/generic/apnum/apnum.pdf b/Master/texmf-dist/doc/generic/apnum/apnum.pdf Binary files differindex 4f5d07b0f38..ea0568bac84 100644 --- a/Master/texmf-dist/doc/generic/apnum/apnum.pdf +++ b/Master/texmf-dist/doc/generic/apnum/apnum.pdf diff --git a/Master/texmf-dist/tex/generic/apnum/apnum.tex b/Master/texmf-dist/tex/generic/apnum/apnum.tex index cb891d55bcc..28f1dea9557 100644 --- a/Master/texmf-dist/tex/generic/apnum/apnum.tex +++ b/Master/texmf-dist/tex/generic/apnum/apnum.tex @@ -1,10 +1,10 @@ % Arbitrary precision numbers %%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% 2014 Petr Olsak +% 2014, 2015 Petr Olsak % See the documentation apnum.pdf or apnum.d for more information -\def\apnumversion{1.0 <Nov. 2014>} +\def\apnumversion{1.1 <Jan. 2015>} \message{The Arbitrary Precision Numbers, \apnumversion} %%%%%%%%%%%% Internal registers: @@ -55,17 +55,17 @@ \apTESTdigit#1\iftrue \ifx E#1\let\tmpb=\tmpa \expandafter\apEVALd\expandafter\apEVALk \else \edef\tmpb{\tmpa#1}\expandafter\apEVALd\expandafter\apEVALn\fi\fi - \edef\tmpb{\tmpa\noexpand#1}\futurelet\next\apEVALg + \edef\tmpb{\tmpa\noexpand#1}\futurelet\apNext\apEVALg } -\def\apEVALd#1\fi#2\next\apEVALg{\fi#1} +\def\apEVALd#1\fi#2\apNext\apEVALg{\fi#1} \def\apEVALe{% \ifx\tmpa\empty \else \ifnum\tmpa1<0 \def\tmpb{-1}\apEVALp \MUL 4\fi\fi \advance\apnumA by4 \apEVALb } \def\apEVALf#1#2{\expandafter\def\expandafter\tmpb\expandafter{\tmpa#1#2}\apEVALo} -\def\apEVALg{\ifx\next \bgroup \expandafter\apEVALh \else \expandafter\apEVALo \fi} -\def\apEVALh#1{\expandafter\def\expandafter\tmpb\expandafter{\tmpb{#1}}\futurelet\next\apEVALg} +\def\apEVALg{\ifx\apNext \bgroup \expandafter\apEVALh \else \expandafter\apEVALo \fi} +\def\apEVALh#1{\expandafter\def\expandafter\tmpb\expandafter{\tmpb{#1}}\futurelet\apNext\apEVALg} \def\apEVALk{\afterassignment\apEVALm\apE=} \def\apEVALm{\edef\tmpb{\tmpb E\the\apE}\apEVALo} \def\apEVALn#1{\apTESTdigit#1% @@ -73,22 +73,22 @@ \else\edef\tmpb{\tmpb#1}\expandafter\expandafter\expandafter\apEVALn\fi \else \expandafter\apEVALo\expandafter#1\fi } -\def\apEVALo#1{\let\next=\apEVALb +\def\apEVALo#1{\let\apNext=\apEVALb \ifx+#1\apEVALp \apEPLUS 1\fi \ifx-#1\apEVALp \apEMINUS 1\fi \ifx*#1\apEVALp \apEMUL 2\fi \ifx/#1\apEVALp \apEDIV 2\fi \ifx^#1\apEVALp \apEPOW 3\fi - \ifx)#1\advance\apnumA by-4 \let\next=\apEVALo \let\tmpa=\relax + \ifx)#1\advance\apnumA by-4 \let\apNext=\apEVALo \let\tmpa=\relax \ifnum\apnumA<0 \apEVALerror{many brackets ")"}\fi \fi \ifx\end#1% \ifnum\apnumA>0 \apEVALerror{missing bracket ")"}% \else \apEVALp\END 0\fi - \let\next=\relax + \let\apNext=\relax \fi \ifx\tmpa\relax \else \apEVALerror{unknown operator "\string#1"}\fi - \apnumE=0 \next + \apnumE=0 \apNext } \def\apEVALp#1#2{% \apnumB=#2 \advance\apnumB by\apnumA @@ -112,7 +112,7 @@ \fi\fi } \def\apEVALerror#1{\message{\noexpand\evaldef ERROR: #1.}% - \def\tmpb{\def\OUT{??}}\def\next##1\end{}% + \def\tmpb{\def\OUT{??}}\def\apNext##1\end{}% } \def\apTESTdigit#1#2{% \ifx E#1\apXtrue \else @@ -166,12 +166,12 @@ } \def\apPPs#1#2#3{\def\tmpc{#3}\expandafter\apPPt\expandafter#1#2.@#2} \def\apPPt#1#2{% - \ifx-#2\apnumG=-1 \def\next{#1}% - \else \ifx0#2\apnumG=0 \def\next{\apPPu#1}\else \apnumG=1 \def\next{#1#2}\fi\fi - \next + \ifx-#2\apnumG=-1 \def\apNext{#1}% + \else \ifx0#2\apnumG=0 \def\apNext{\apPPu#1}\else \apnumG=1 \def\apNext{#1#2}\fi\fi + \apNext } \def\apPPu#1#2.@#3{\ifx@#2@\apnumG=0 \ifx#1\apROUNDa\def\XOUT{}\fi - \else\def\next{\apPPt#1#2.@#3}\expandafter\next\fi + \else\def\apNext{\apPPt#1#2.@#3}\expandafter\apNext\fi } \def\apEVALone#1#2{\evaldef\OUT{#2}\ifnum\apSIGN<0 \expandafter\apNOminus\OUT@\OUT\fi #1} \def\apEVALtwo#1#2#3{% @@ -201,8 +201,8 @@ \else \apnumC=-\apnumC \apPLUSb \tmpa\apnumE \tmpb\apnumF \apnumA \fi \ifnum\apnumG=0 \def\OUT{0}\apSIGN=0 \apE=0 \else - \ifnum\apSIGN=0 \apSIGN=\apnumG \let\next=\apPLUSm \else \let\next=\apPLUSp \fi - \apnumX=0 \edef\OUT{\expandafter}\expandafter \next \OUT@% second pass + \ifnum\apSIGN=0 \apSIGN=\apnumG \let\apNext=\apPLUSm \else \let\apNext=\apPLUSp \fi + \apnumX=0 \edef\OUT{\expandafter}\expandafter \apNext \OUT@% second pass \ifnum\apnumD<1 % result in the form .000123 \apnumZ=-\apnumD \def\tmpa{.}% @@ -215,11 +215,11 @@ \def\apPLUSb#1#2#3#4#5{% \edef\tmpd{\ifcase#4\or{}{}{}\or{}{}\or{}\fi#3}% \edef\tmpc{\ifcase#2\or{}{}{}\or{}{}\or{}\fi}% - \let\next=\apPLUSc \apnumD=#5\advance\apnumD by4 \apnumG=0 \apnumZ=0 \def\OUT{}% + \let\apNext=\apPLUSc \apnumD=#5\advance\apnumD by4 \apnumG=0 \apnumZ=0 \def\OUT{}% \expandafter\expandafter\expandafter\apPLUSc\expandafter\tmpc#1\apNL\apNL\apNL\apNL@% } \def\apPLUSc#1#2#3#4{\apnumY=\apPLUSxA#1#2#3#4\relax - \ifx\apNL#4\let\next=\apPLUSd\fi + \ifx\apNL#4\let\apNext=\apPLUSd\fi \ifx\apNL#1\relax \ifx\tmpd\empty \expandafter\expandafter\expandafter\apPLUSf \fi\fi \apPLUSe } @@ -230,7 +230,7 @@ \ifnum\apnumZ=0 \apPLUSh \fi \edef\OUT{{\the\apnumY}\OUT}% \advance\apnumD by-4 - \next + \apNext } \def\apPLUSf#1@{} \def\apPLUSg{\let\tmpc=\apPLUSxA \let\apPLUSxA=\apPLUSxB \let\apPLUSxB=\tmpc} @@ -371,7 +371,7 @@ \edef\OUT{\ifnum\apSIGN<0-\fi}% \ifnum\apnumD<0 \edef\OUT{\OUT.}\apnumZ=-\apnumD \apADDzeros\OUT \fi \apnumE=1 \apnumZ=0 - \let\next=\apDIVg \next % <--- the main calculation loop is here + \let\apNext=\apDIVg \apNext % <--- the main calculation loop is here \ifnum\apnumD>0 \apnumZ=\apnumD \apADDzeros\OUT \fi \ifnum\apnumE=0 \def\XOUT{0}\else % extracting remainder from \XOUT \edef\XOUT{\expandafter}\expandafter\apDIVv\XOUT @@ -383,8 +383,8 @@ \def\apDIVcomp#1#2{% \expandafter\def\expandafter\tmpc\expandafter{#1\apNL\apNL\apNL\apNL\apNL\apNL\apNL\apNL@}% \expandafter\def\expandafter\tmpd\expandafter{#2\apNL\apNL\apNL\apNL\apNL\apNL\apNL\apNL@}% - \def\next{\expandafter\expandafter\expandafter\apDIVcompA\expandafter\tmpc\tmpd}% - \apXtrue \next + \def\apNext{\expandafter\expandafter\expandafter\apDIVcompA\expandafter\tmpc\tmpd}% + \apXtrue \apNext } \def\apDIVcompA#1#2#3#4#5#6#7#8#9@{% \ifx#8\apNL \def\tmpc{0000000\apNL@}\else\def\tmpc{#9@}\fi @@ -392,28 +392,29 @@ \apDIVcompB } \def\apDIVcompB#1#2#3#4#5#6#7#8#9@{% - \ifnum\apnumX<#1#2#3#4#5#6#7#8 \let\next=\relax \apXfalse \else - \ifnum\apnumX>#1#2#3#4#5#6#7#8 \let\next=\relax \apXtrue + \ifnum\apnumX<#1#2#3#4#5#6#7#8 \let\apNext=\relax \apXfalse \else + \ifnum\apnumX>#1#2#3#4#5#6#7#8 \let\apNext=\relax \apXtrue \fi\fi - \ifx\next\relax\else - \ifx#8\apNL \def\tmpd{0000000\apNL@}\ifx\tmpc\tmpd\let\next=\relax\fi \else\def\tmpd{#9@}\fi + \ifx\apNext\relax\else + \ifx#8\apNL \def\tmpd{0000000\apNL@}\ifx\tmpc\tmpd\let\apNext=\relax\fi + \else\def\tmpd{#9@}\fi \fi - \next + \apNext } \def\apDIVg{% \ifx\tmpb\empty - \ifx\tmpa\empty \def\next{\apDIVi!}\let\nexti=\apDIVi - \else \def\next{\expandafter\apDIVh\tmpa\apNL\apNL\apNL\apNL!}\let\nexti=\apDIVh + \ifx\tmpa\empty \def\apNext{\apDIVi!}\let\apNexti=\apDIVi + \else \def\apNext{\expandafter\apDIVh\tmpa\apNL\apNL\apNL\apNL!}\let\apNexti=\apDIVh \fi\fi - \ifx\next\apDIVg + \ifx\apNext\apDIVg \apIVread\tmpa \apnumA=\apnumX \apIVread\tmpb \edef\XOUT{{\the\apnumX}{\the\apnumA}\XOUT}% \fi - \next + \apNext } \def\apDIVh#1#2#3#4{\apnumZ=#1#2#3#4 - \ifx\apNL#4\let\nexti=\apDIVi\fi + \ifx\apNL#4\let\apNexti=\apDIVi\fi \apDIVi } \def\apDIVi{% @@ -422,7 +423,7 @@ \expandafter\apDIVp\XOUT \advance\apnumC by-4 \apnumZ=0 - \expandafter\nexti + \expandafter\apNexti \else \expandafter\apDIVj \fi @@ -432,7 +433,7 @@ \apnumA=\apDIVxA \divide\apnumA by\apDIVxB \def\apOUTl{}\apnumO=1 \apnumL=0 \apnumX=0 \apnumB=0 \apnumE=0 - \let\next=\apDIVq \next 0\apnumZ + \let\apNext=\apDIVq \apNext 0\apnumZ } \def\apDIVq#1#2#3{% B A B \advance\apnumO by-1 \ifnum\apnumO=0 \apOUTx \fi @@ -446,15 +447,15 @@ \expandafter \edef\csname apOUT:\apOUTn\endcsname{\csname apOUT:\apOUTn\endcsname{#3}{\the\apnumB}}% \ifnum\apnumE<\apnumB \apnumE=\apnumB \fi - \ifx@#3\let\next=\apDIVr \fi - \next{#3}% + \ifx@#3\let\apNext=\apDIVr \fi + \apNext{#3}% } \def\apDIVr#1#2{% \ifnum\apnumX=#2 % the calculated Digit is OK, we save it \edef\XOUT{\expandafter\apOUTs\apOUTl.,}% \edef\tmpa{\ifnum\apnumF=4 \expandafter\apIVwrite\else \expandafter\the\fi\apnumA}% \ifnum\apnumD<\apnumF \ifnum\apnumD>-1 \apDIVt \fi\fi %adding dot - \ifx\nexti\apDIVh \apnumE=1 \fi + \ifx\apNexti\apDIVh \apnumE=1 \fi \ifnum\apnumE=0 \apDIVu % removing zeros \advance\apnumD by-\apNUMdigits\tmpa \relax \else \advance\apnumD by-\apnumF \apnumF=4 \fi @@ -463,9 +464,9 @@ \else % we need do correction and run the remainder calculation again \advance\apnumA by-1 \apnumX=0 \apnumB=0 \apnumE=0 \def\apOUTl{}\apnumO=1 \apnumL=0 - \def\next{\let\next=\apDIVq - \expandafter\next\expandafter0\expandafter\apnumZ\XOUT}% - \expandafter\next + \def\apNext{\let\apNext=\apDIVq + \expandafter\apNext\expandafter0\expandafter\apnumZ\XOUT}% + \expandafter\apNext \fi } \def\apDIVt{\edef\tmpa{\apIVdot\apnumD\tmpa}\edef\tmpa{\tmpa}} @@ -499,11 +500,8 @@ \edef\tmpc{\ifcase\apnumA\or{}{}{}\or{}{}\or{}\fi}\def\OUT{}% \expandafter\expandafter\expandafter \apMULb \expandafter \tmpc \tmpa @@@@% \edef\OUT{*.\OUT}% \OUT := \tmpa in interleaved format - \let\tmpd=\tmpa \def\tmpa{}% - \expandafter\expandafter\expandafter \apMULc \expandafter \tmpc \tmpd @@@@% - \def\tmpc{*.1*}\apnumE=10 - \edef\tmpb{\apREV\tmpb}% - \expandafter\apPOWb\tmpb@% do multiplications + \def\tmpc{*.1*}% + \apnumE=\tmpb\relax \apPOWb \expandafter\apPOWg \tmpc % \OUT := \tmpc in human raedable form \ifnum\apnumD=0 \ifnum \apSIGN<0 \edef\OUT{-\OUT}\fi \else \def\tmpc{-\apnumD}\apnumG=\apSIGN \expandafter\apROLLa\OUT.@\OUT\fi @@ -511,26 +509,17 @@ \relax \fi\fi } -\def\apPOWb#1{% - \ifx@#1\else - \ifnum\apnumE<10 - \expandafter\apPOWc\ifcase\apnumE - ppmp.\or ppmp.\or pmp.\or mmp.\or mp.\or p.\or mmmm.\or mmm.\or mm.\or m.\or .\fi - \def\tmpb{}\expandafter\apPOWd\OUT \let\tmpa=\tmpb - \fi - \apnumE=#1 - \ifnum\apnumE=0 \else - \expandafter\apPOWc\ifcase\apnumE - \or .\or p.\or pm.\or pp.\or ppm.\or pmp.\or pmpm.\or ppp.\or pppm.\fi - \def\tmpb{}\expandafter\apPOWd\OUT - \let\tmpd=\OUT \let\OUT=\tmpc - \expandafter\apMULd \tmpb@\expandafter\apPOWn\OUT@% - \let\tmpc=\OUT \let\OUT=\tmpd - \fi - \expandafter\apPOWb +\def\apPOWb{% + \ifodd\apnumE \def\tmpb{}\expandafter\apPOWd\OUT + \let\tmpd=\OUT \let\OUT=\tmpc + \expandafter\apMULd \tmpb@\expandafter\apPOWn\OUT@% + \let\tmpc=\OUT \let\OUT=\tmpd \fi -} -\def\apPOWc#1{\ifx.#1\else \csname apPOW#1\endcsname \expandafter\apPOWc\fi} + \divide\apnumE by2 + \ifnum\apnumE>0 \expandafter\apPOWt\OUT \expandafter\apPOWn\OUT@% + \expandafter\apPOWb + \fi +} \def\apPOWd#1#2{% \apPOWd <spec format> => \tmpb (in simple reverse format) \ifx*#1\expandafter\apPOWd \else \edef\tmpb{\tmpb{#1}}% @@ -542,8 +531,6 @@ \def\apPOWh#1#2{\apnumA=#1 \ifx*#2\edef\OUT{\the\apnumA\OUT}\else \edef\OUT{\apIVwrite\apnumA\OUT}\expandafter\apPOWh\fi } -\def\apPOWm{\expandafter\apMULd\tmpa@\expandafter\apPOWn\OUT@} -\def\apPOWp{\expandafter\apPOWt\OUT \expandafter\apPOWn\OUT@} \def\apPOWn#1{\def\OUT{*}\apPOWna} \def\apPOWna#1{\ifx*#1\expandafter\apPOWnn\else \edef\OUT{\OUT0{#1}}\expandafter\apPOWna\fi} \def\apPOWnn#1#2{\ifx*#1\edef\OUT{\OUT*}\else\edef\OUT{\OUT0{#1}}\expandafter\apPOWnn\fi} @@ -608,13 +595,13 @@ \else \expandafter\apROUNDb \fi } -\def\apROUNDb#1.{\edef\tmpc{#1}\apnumX=0 \def\tmpd{}\let\next=\apROUNDc \next} -\def\apROUNDc#1{\ifx@#1\def\next{\apROUNDd.@}% +\def\apROUNDb#1.{\edef\tmpc{#1}\apnumX=0 \def\tmpd{}\let\apNext=\apROUNDc \apNext} +\def\apROUNDc#1{\ifx@#1\def\apNext{\apROUNDd.@}% \else \advance\apnumD by-1 - \ifnum\apnumD<0 \def\next{\apROUNDd#1}% + \ifnum\apnumD<0 \def\apNext{\apROUNDd#1}% \else \ifx.#1\else \advance\apnumX by#1 \edef\tmpd{\tmpd#1}\fi \fi - \fi \next + \fi \apNext } \def\apROUNDd#1.@#2{\def\XOUT{#1}% \ifnum\apnumX=0 \def\tmpd{}\fi @@ -667,9 +654,9 @@ \else \expandafter\apDIGc \expandafter#1\fi } \def\apDIGc#1.{\def\tmpd{#1}% - \ifx\tmpc\empty \let\next=\apDIGe - \else \def\next{\expandafter\apDIGd\tmpd@}% - \fi \next + \ifx\tmpc\empty \let\apNext=\apDIGe + \else \def\apNext{\expandafter\apDIGd\tmpd@}% + \fi \apNext } \def\apDIGd#1{\ifx@#1\expandafter\apDIGe \else \tmpc \expandafter\apDIGd \fi} \def\apDIGe#1@#2{% @@ -728,3 +715,7 @@ \catcode`\@=\apnumZ \endinput +1.0 <Nov. 2014> - First version released +1.1 <Jan. 2015> + - POW implemented more simple (by base 2 of exponent) + - \next renamed in order to avoid name conflict |