% \iffalse meta-comment % % Copyright (C) 2007 by % Heiko Oberdiek % % This work may be distributed and/or modified under the % conditions of the LaTeX Project Public License, either % version 1.3c of this license or (at your option) any later % version. This version of this license is in % http://www.latex-project.org/lppl/lppl-1-3c.txt % and the latest version of this license is in % http://www.latex-project.org/lppl.txt % and version 1.3 or later is part of all distributions of % LaTeX version 2005/12/01 or later. % % This work has the LPPL maintenance status "maintained". % % This Current Maintainer of this work is Heiko Oberdiek. % % The Base Interpreter refers to any `TeX-Format', % because some files are installed in TDS:tex/generic//. % % This work consists of the main source file intcalc.dtx % and the derived files % intcalc.sty, intcalc.pdf, intcalc.ins, intcalc.drv, % intcalc-test1.tex, intcalc-test2.tex, intcalc-test3.tex, % intcalc-test4.tex. % % Distribution: % CTAN:macros/latex/contrib/oberdiek/intcalc.dtx % CTAN:macros/latex/contrib/oberdiek/intcalc.pdf % % Unpacking: % (a) If intcalc.ins is present: % tex intcalc.ins % (b) Without intcalc.ins: % tex intcalc.dtx % (c) If you insist on using LaTeX % latex \let\install=y\input{intcalc.dtx} % (quote the arguments according to the demands of your shell) % % Documentation: % (a) If intcalc.drv is present: % latex intcalc.drv % (b) Without intcalc.drv: % latex intcalc.dtx; ... % The class ltxdoc loads the configuration file ltxdoc.cfg % if available. Here you can specify further options, e.g. % use A4 as paper format: % \PassOptionsToClass{a4paper}{article} % % Programm calls to get the documentation (example): % pdflatex intcalc.dtx % makeindex -s gind.ist intcalc.idx % pdflatex intcalc.dtx % makeindex -s gind.ist intcalc.idx % pdflatex intcalc.dtx % % Installation: % TDS:tex/generic/oberdiek/intcalc.sty % TDS:doc/latex/oberdiek/intcalc.pdf % TDS:doc/latex/oberdiek/test/intcalc-test1.tex % TDS:doc/latex/oberdiek/test/intcalc-test2.tex % TDS:doc/latex/oberdiek/test/intcalc-test3.tex % TDS:doc/latex/oberdiek/test/intcalc-test4.tex % TDS:source/latex/oberdiek/intcalc.dtx % %<*ignore> \begingroup \def\x{LaTeX2e}% \expandafter\endgroup \ifcase 0\ifx\install y1\fi\expandafter \ifx\csname processbatchFile\endcsname\relax\else1\fi \ifx\fmtname\x\else 1\fi\relax \else\csname fi\endcsname % %<*install> \input docstrip.tex \Msg{************************************************************************} \Msg{* Installation} \Msg{* Package: intcalc 2007/09/27 v1.1 Expandable integer calculations (HO)} \Msg{************************************************************************} \keepsilent \askforoverwritefalse \let\MetaPrefix\relax \preamble This is a generated file. Copyright (C) 2007 by Heiko Oberdiek This work may be distributed and/or modified under the conditions of the LaTeX Project Public License, either version 1.3c of this license or (at your option) any later version. This version of this license is in http://www.latex-project.org/lppl/lppl-1-3c.txt and the latest version of this license is in http://www.latex-project.org/lppl.txt and version 1.3 or later is part of all distributions of LaTeX version 2005/12/01 or later. This work has the LPPL maintenance status "maintained". This Current Maintainer of this work is Heiko Oberdiek. The Base Interpreter refers to any `TeX-Format', because some files are installed in TDS:tex/generic//. This work consists of the main source file intcalc.dtx and the derived files intcalc.sty, intcalc.pdf, intcalc.ins, intcalc.drv, intcalc-test1.tex, intcalc-test2.tex, intcalc-test3.tex, intcalc-test4.tex. \endpreamble \let\MetaPrefix\DoubleperCent \generate{% \file{intcalc.ins}{\from{intcalc.dtx}{install}}% \file{intcalc.drv}{\from{intcalc.dtx}{driver}}% \usedir{tex/generic/oberdiek}% \file{intcalc.sty}{\from{intcalc.dtx}{package}}% \usedir{doc/latex/oberdiek/test}% \file{intcalc-test1.tex}{\from{intcalc.dtx}{test1}}% \file{intcalc-test2.tex}{\from{intcalc.dtx}{test2,etex}}% \file{intcalc-test3.tex}{\from{intcalc.dtx}{test2,noetex}}% \file{intcalc-test4.tex}{\from{intcalc.dtx}{test4,etex}}% } \obeyspaces \Msg{************************************************************************} \Msg{*} \Msg{* To finish the installation you have to move the following} \Msg{* file into a directory searched by TeX:} \Msg{*} \Msg{* intcalc.sty} \Msg{*} \Msg{* And install the following script file:} \Msg{*} \Msg{* } \Msg{*} \Msg{* To produce the documentation run the file `intcalc.drv'} \Msg{* through LaTeX.} \Msg{*} \Msg{* Happy TeXing!} \Msg{*} \Msg{************************************************************************} \endbatchfile % %<*ignore> \fi % %<*driver> \NeedsTeXFormat{LaTeX2e} \ProvidesFile{intcalc.drv}% [2007/09/27 v1.1 Expandable integer calculations (HO)]% \documentclass{ltxdoc} \usepackage{holtxdoc}[2008/08/11] \usepackage{wasysym} \let\iint\relax \let\iiint\relax \usepackage[fleqn]{amsmath} \DeclareMathOperator{\opInv}{Inv} \DeclareMathOperator{\opAbs}{Abs} \DeclareMathOperator{\opSgn}{Sgn} \DeclareMathOperator{\opMin}{Min} \DeclareMathOperator{\opMax}{Max} \DeclareMathOperator{\opCmp}{Cmp} \DeclareMathOperator{\opInc}{Inc} \DeclareMathOperator{\opDec}{Dec} \DeclareMathOperator{\opAdd}{Add} \DeclareMathOperator{\opSub}{Sub} \DeclareMathOperator{\opShl}{Shl} \DeclareMathOperator{\opShr}{Shr} \DeclareMathOperator{\opMul}{Mul} \DeclareMathOperator{\opSqr}{Sqr} \DeclareMathOperator{\opFac}{Fac} \DeclareMathOperator{\opPow}{Pow} \DeclareMathOperator{\opDiv}{Div} \DeclareMathOperator{\opMod}{Mod} \DeclareMathOperator{\opInt}{Int} \DeclareMathOperator{\opOdd}{ifodd} \newcommand*{\Def}{% \ensuremath{% \mathrel{\mathop{:}}=% }% } \newcommand*{\op}[1]{% \textsf{#1}% } \begin{document} \DocInput{intcalc.dtx}% \end{document} % % \fi % % \CheckSum{3149} % % \CharacterTable % {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 % 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 % Digits \0\1\2\3\4\5\6\7\8\9 % Exclamation \! Double quote \" Hash (number) \# % Dollar \$ Percent \% Ampersand \& % Acute accent \' Left paren \( Right paren \) % Asterisk \* Plus \+ Comma \, % Minus \- Point \. Solidus \/ % Colon \: Semicolon \; Less than \< % Equals \= Greater than \> Question mark \? % Commercial at \@ Left bracket \[ Backslash \\ % Right bracket \] Circumflex \^ Underscore \_ % Grave accent \` Left brace \{ Vertical bar \| % Right brace \} Tilde \~} % % \GetFileInfo{intcalc.drv} % % \title{The \xpackage{intcalc} package} % \date{2007/09/27 v1.1} % \author{Heiko Oberdiek\\\xemail{heiko.oberdiek at googlemail.com}} % % \maketitle % % \begin{abstract} % This package provides expandable arithmetic operations % with integers. % \end{abstract} % % \tableofcontents % % \section{Documentation} % % \subsection{Introduction} % % Package \xpackage{intcalc} defines arithmetic operations % that deal with integers. Integers mean numbers in \TeX. % The same restrictions apply, the range is limited to % [-2147483647, 2147483647]. % % The operations have the form of macros that take one or % two integers as parameter and return the integer result. % The macro name is a three letter operation name prefixed % by the package name, e.g. \cs{intcalcAdd}|{10}{43}| returns % |53|. % % The macros are fully expandable, exactly two expansion % steps generate the result. Therefore the operations % may be used nearly everywhere in \TeX, even inside % \cs{number}, \cs{csname}, file names, or other % expandable contexts. % % The package contains two implementations of the operations. % If \eTeX\ is detected then the macros are implemented % using its features (\cs{numexpr}). Otherwise the slower % implementation without \eTeX's help is choosen. % % \subsection{Conditions} % % \subsubsection{Preconditions} % % \begin{itemize} % \item % Arguments can be anything that \TeX\ interprets as ``number''. % Examples: plain numbers, count or length register, macros % that expands to a number. % \item % The arguments are limited to the range % -2147483647 until 2147483647. These numbers belong to the range. % Note that some operations % have additionals restrictions to the range. % \item % The argument may be expressions that \cs{numexpr} % understands if \eTeX\ is available. % \item % The resulting number must fit in the allowed range. % \end{itemize} % % \subsubsection{Postconditions} % % Additional properties of the macros apart from calculating % a correct result (of course \smiley): % \begin{itemize} % \item % The macros are fully expandable. Thus they can % be used inside \cs{edef}, \cs{csname}, after \cs{number}, % for example. % \item % Furthermore exactly two expansion steps calculate the result. % \item % The number consists of one optional minus sign and one to % ten digits. The first digit is larger than zero for % numbers that consists of more than one digit. % % In short, the number format is exactly the same as % \cs{number} generates. And the tokens (minus sign, digits) % have catcode 12 (other). % \item % Call by value is simulated. First the arguments are % converted to numbers. Then these numbers are used % in the calculations. % % Remember that arguments % may contain expensive macros or \eTeX\ expressions. % This strategy avoids multiple evaluations of such % arguments. % \end{itemize} % % \subsection{Error handling} % % There are two kinds of errors if a precondition is violated: % Some errors are detected by the macros, example: division by zero. % In this cases an undefined control sequence is called and causes % a TeX error message, example: \cs{IntCalcError:DivisionByZero}. % The name of the control sequence contains % the reason for the error. The \TeX\ error may be ignored. % Then the operation returns zero as result. % Because the macros are supposed to work in expandible contexts. % An traditional error message, however, is not expandable and % would break these contexts. % % If a number exceeds the range of -2147483647 until 2147483647, then % \TeX\ throws an error ``Number too big'' and recovers by % using biggest allowed value. % Example for the negative number -3000000000 is replaced % by -2147483647. % % \subsection{Operations} % % Some definition equations below use the function $\opInt$ % that converts a real number to an integer. The number % is truncated that means rounding to zero: % \begin{gather*} % \opInt(x) \Def % \begin{cases} % \lfloor x\rfloor & \text{if $x\geq0$}\\ % \lceil x\rceil & \text{otherwise} % \end{cases} % \end{gather*} % % \subsubsection{\op{Num}} % % \begin{declcs}{intcalcNum} \M{x} % \end{declcs} % Macro \cs{intcalcNum} converts its argument to a normalized integer % number without unnecessary leading zeros or signs. % The result matches the regular expression: %\begin{quote} %\begin{verbatim} %0|-?[1-9][0-9]* %\end{verbatim} %\end{quote} % % \subsubsection{\op{Inv}, \op{Abs}, \op{Sgn}} % % \begin{declcs}{intcalcInv} \M{x} % \end{declcs} % Macro \cs{intcalcInv} switches the sign. % \begin{gather*} % \opInv(x) \Def -x % \end{gather*} % % \begin{declcs}{intcalcAbs} \M{x} % \end{declcs} % Macro \cs{intcalcAbs} returns the absolute value % of integer \meta{x}. % \begin{gather*} % \opAbs(x) \Def \vert x\vert % \end{gather*} % % \begin{declcs}{intcalcSgn} \M{x} % \end{declcs} % Macro \cs{intcalcSgn} encodes the sign of \meta{x} as number. % \begin{gather*} % \opSgn(x) \Def % \begin{cases} % -1& \text{if $x<0$}\\ % 0& \text{if $x=0$}\\ % 1& \text{if $x>0$} % \end{cases} % \end{gather*} % These return values can easily be distinguished by \cs{ifcase}: %\begin{quote} %\begin{verbatim} %\ifcase\intcalcSgn{} % $x=0$ %\or % $x>0$ %\else % $x<0$ %\fi %\end{verbatim} %\end{quote} % % \subsubsection{\op{Min}, \op{Max}, \op{Cmp}} % % \begin{declcs}{intcalcMin} \M{x} \M{y} % \end{declcs} % Macro \cs{intcalcMin} returns the smaller of the two integers. % \begin{gather*} % \opMin(x,y) \Def % \begin{cases} % x & \text{if $xy$}\\ % y & \text{otherwise} % \end{cases} % \end{gather*} % % \begin{declcs}{intcalcCmp} \M{x} \M{y} % \end{declcs} % Macro \cs{intcalcCmp} encodes the comparison result as number: % \begin{gather*} % \opCmp(x,y) \Def % \begin{cases} % -1 & \text{if $xy$} % \end{cases} % \end{gather*} % These values can be distinguished by \cs{ifcase}: %\begin{quote} %\begin{verbatim} %\ifcase\intcalcCmp{}{} % $x=y$ %\or % $x>y$ %\else % $x % \end{macrocode} % % \subsection{Reload check and package identification} % Reload check, especially if the package is not used with \LaTeX. % \begin{macrocode} \begingroup \catcode44 12 % , \catcode45 12 % - \catcode46 12 % . \catcode58 12 % : \catcode64 11 % @ \catcode123 1 % { \catcode125 2 % } \expandafter\let\expandafter\x\csname ver@intcalc.sty\endcsname \ifx\x\relax % plain-TeX, first loading \else \def\empty{}% \ifx\x\empty % LaTeX, first loading, % variable is initialized, but \ProvidesPackage not yet seen \else \catcode35 6 % # \expandafter\ifx\csname PackageInfo\endcsname\relax \def\x#1#2{% \immediate\write-1{Package #1 Info: #2.}% }% \else \def\x#1#2{\PackageInfo{#1}{#2, stopped}}% \fi \x{intcalc}{The package is already loaded}% \aftergroup\endinput \fi \fi \endgroup % \end{macrocode} % Package identification: % \begin{macrocode} \begingroup \catcode35 6 % # \catcode40 12 % ( \catcode41 12 % ) \catcode44 12 % , \catcode45 12 % - \catcode46 12 % . \catcode47 12 % / \catcode58 12 % : \catcode64 11 % @ \catcode91 12 % [ \catcode93 12 % ] \catcode123 1 % { \catcode125 2 % } \expandafter\ifx\csname ProvidesPackage\endcsname\relax \def\x#1#2#3[#4]{\endgroup \immediate\write-1{Package: #3 #4}% \xdef#1{#4}% }% \else \def\x#1#2[#3]{\endgroup #2[{#3}]% \ifx#1\@undefined \xdef#1{#3}% \fi \ifx#1\relax \xdef#1{#3}% \fi }% \fi \expandafter\x\csname ver@intcalc.sty\endcsname \ProvidesPackage{intcalc}% [2007/09/27 v1.1 Expandable integer calculations (HO)] % \end{macrocode} % % \subsection{Catcodes} % % \begin{macrocode} \begingroup \catcode123 1 % { \catcode125 2 % } \def\x{\endgroup \expandafter\edef\csname InCa@AtEnd\endcsname{% \catcode35 \the\catcode35\relax \catcode64 \the\catcode64\relax \catcode123 \the\catcode123\relax \catcode125 \the\catcode125\relax }% }% \x \catcode35 6 % # \catcode64 11 % @ \catcode123 1 % { \catcode125 2 % } \def\TMP@EnsureCode#1#2{% \edef\InCa@AtEnd{% \InCa@AtEnd \catcode#1 \the\catcode#1\relax }% \catcode#1 #2\relax } \TMP@EnsureCode{33}{12}% ! \TMP@EnsureCode{40}{12}% ( \TMP@EnsureCode{41}{12}% ) \TMP@EnsureCode{42}{12}% * \TMP@EnsureCode{43}{12}% + \TMP@EnsureCode{45}{12}% - \TMP@EnsureCode{47}{12}% / \TMP@EnsureCode{58}{11}% : (letter!) \TMP@EnsureCode{60}{12}% < \TMP@EnsureCode{61}{12}% = \TMP@EnsureCode{62}{12}% > \TMP@EnsureCode{63}{14}% ? (comment!) \begingroup\expandafter\expandafter\expandafter\endgroup \expandafter\ifx\csname InCa@TestMode\endcsname\relax \else \catcode63=9 % ? (ignore) \fi ? \let\InCa@@TestMode\InCa@TestMode % \end{macrocode} % % \subsection{Macros independent of \eTeX} % % \subsubsection{\op{Abs}, \op{Sgn}} % % \begin{macro}{\InCa@Abs} % \begin{macrocode} \def\InCa@Abs#1#2!{% \ifx#1-% #2% \else #1#2% \fi } % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@Sgn} % \begin{macrocode} \def\InCa@Sgn#1#2!{% \ifx#1-% -1% \else \ifx#10% 0% \else 1% \fi \fi } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Min}, \op{Max}, \op{Cmp}} % % \begin{macro}{\InCa@Min} % \begin{macrocode} \def\InCa@Min#1!#2!{% \ifnum#1<#2 % #1% \else #2% \fi } % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@Max} % \begin{macrocode} \def\InCa@Max#1!#2!{% \ifnum#1>#2 % #1% \else #2% \fi } % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@Cmp} % \begin{macrocode} \def\InCa@Cmp#1!#2!{% \ifnum#1=#2 % 0% \else \ifnum#1<#2 % -% \fi 1% \fi } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Fac}} % % \begin{macro}{\InCa@Fac} % It does not make much sense to calculate the % faculty by an general algorithm. The allowed % range of arguments is too low because of the % limited integer domain. % \begin{macrocode} \def\InCa@Fac#1!{% \ifcase#1 1% 0! \or 1% 1! \or 2% 2! \or 6% 3! \or 24% 4! \or 120% 5! \or 720% 6! \or 5040% 7! \or 40320% 8! \or 362880% 9! \or 3628800% 10! \or 39916800% 11! \or 479001600% 12! \else \ifnum#1<\z@ 0\IntCalcError:FacNegative% \else 0\IntCalcError:FacOverflow% \fi \fi } % \end{macrocode} % \end{macro} % % \subsection{Implementation based on \eTeX} % % Only \cs{numexpr} is used from \eTeX. % \begin{macrocode} \begingroup\expandafter\expandafter\expandafter\endgroup \expandafter\ifx\csname numexpr\endcsname\relax \else % \end{macrocode} % % \subsubsection{\op{Num}} % % \begin{macro}{\intcalcNum} % \begin{macrocode} \def\intcalcNum#1{% \the\numexpr#1\relax }% % \end{macrocode} % \end{macro} % % \subsubsection{\op{Inv}, \op{Abs}, \op{Sgn}} % % \begin{macro}{\intcalcInv} % \begin{macrocode} \def\intcalcInv#1{% \number-\intcalcNum{#1} % }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcAbs} % \begin{macrocode} \def\intcalcAbs#1{% \number\expandafter\InCa@Abs\the\numexpr#1! % }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcSgn} % \begin{macrocode} \def\intcalcSgn#1{% \number\expandafter\InCa@Sgn\the\numexpr#1! % }% % \end{macrocode} % \end{macro} % % \subsubsection{\op{Min}, \op{Max}, \op{Cmp}} % % \begin{macro}{\intcalcMin} % \begin{macrocode} \def\intcalcMin#1#2{% \number\expandafter\InCa@Min \the\numexpr#1\expandafter!% \the\numexpr#2! % }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcMax} % \begin{macrocode} \def\intcalcMax#1#2{% \number\expandafter\InCa@Max \the\numexpr#1\expandafter!% \the\numexpr#2! % }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcCmp} % \begin{macrocode} \def\intcalcCmp#1#2{% \number\expandafter\InCa@Cmp \the\numexpr#1\expandafter!\the\numexpr#2! % }% % \end{macrocode} % \end{macro} % % \subsubsection{\op{Inc}, \op{Dec}} % % \begin{macro}{\intcalcInc} % \begin{macrocode} \def\intcalcInc#1{% \the\numexpr#1+1\relax }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcDec} % \begin{macrocode} \def\intcalcDec#1{% \the\numexpr#1-1\relax }% % \end{macrocode} % \end{macro} % % \begin{macro}{\IntCalcInc} % \begin{macrocode} \def\IntCalcInc#1!{% \the\numexpr#1+1\relax }% % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcDec} % \begin{macrocode} \def\IntCalcDec#1!{% \the\numexpr#1-1\relax }% % \end{macrocode} % \end{macro} % % \subsubsection{\op{Add}, \op{Sub}} % % \begin{macro}{\intcalcAdd} % \begin{macrocode} \def\intcalcAdd#1#2{% \the\numexpr#1+(#2)\relax }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcSub} % \begin{macrocode} \def\intcalcSub#1#2{% \the\numexpr#1-(#2)\relax }% % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcAdd} % \begin{macrocode} \def\IntCalcAdd#1!#2!{% \the\numexpr#1+#2\relax }% % \end{macrocode} % \end{macro} % % \begin{macro}{\IntCalcSub} % \begin{macrocode} \def\IntCalcSub#1!#2!{% \the\numexpr#1-#2\relax }% % \end{macrocode} % \end{macro} % % \subsubsection{\op{Shl}, \op{Shr}} % % \begin{macro}{\intcalcShl} % \begin{macrocode} \def\intcalcShl#1{% \the\numexpr(#1)*2\relax }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcShr} % \begin{macrocode} \def\intcalcShr#1{% \number\expandafter\InCa@Shr\the\numexpr#1! % }% % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcShl} % \begin{macrocode} \def\IntCalcShl#1!{% \the\numexpr#1*2\relax }% % \end{macrocode} % \end{macro} % % \begin{macro}{\IntCalcShr} % \begin{macrocode} \def\IntCalcShr#1!{% \the\numexpr\ifodd#1 (#1-1)\else#1\fi/2\relax }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Shr} % \begin{macrocode} \def\InCa@Shr#1#2!{% \ifx#1-% -\InCa@Shr#2!% \else \ifodd#1#2 % \the\numexpr(#1#2-1)/2\relax \else \the\numexpr#1#2/2\relax \fi \fi }% % \end{macrocode} % \end{macro} % % \subsubsection{\op{Mul}, \op{Sqr}, \op{Fac}} % % \begin{macro}{\intcalcMul} % \begin{macrocode} \def\intcalcMul#1#2{% \the\numexpr(#1)*(#2)\relax }% % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcMul} % \begin{macrocode} \def\IntCalcMul#1!#2!{% \the\numexpr#1*#2\relax }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcSqr} % \begin{macrocode} \def\intcalcSqr#1{% \number\expandafter\InCa@Sqr\the\numexpr#1! % }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Sqr} % \begin{macrocode} \def\InCa@Sqr#1!{% \the\numexpr#1*#1\relax }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcFac} % \begin{macrocode} \def\intcalcFac#1{% \number\expandafter\InCa@Fac\the\numexpr#1! % }% % \end{macrocode} % \end{macro} % % \subsubsection{\op{Pow}} % % \begin{macro}{\intcalcPow} % \begin{macrocode} \def\intcalcPow#1#2{% \number\expandafter\InCa@Pow \the\numexpr#1\expandafter!% \the\numexpr#2! % }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Pow} % \begin{macrocode} \def\InCa@Pow#1#2!#3#4!{% \ifcase#3#4 % power = 0 1% \or % power = 1 #1#2% \or % power = 2 \the\numexpr#1#2*#1#2\relax \else \ifcase#1#2 % basis = 0, power <> 0 0% \ifx#3-% power < 0 0\IntCalcError:DivisionByZero% \fi \or 1% basis = 1 \else \ifnum#1#2=\m@ne % basis = -1 \ifodd#3#4 % -% \fi 1% \else % |basis| > 1 \ifx#3-% power < 0 0% \else % power > 2 \InCa@PowRec#1#2!#3#4!1!% \fi \fi \fi \fi }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@PowRec} %\begin{quote} %\begin{verbatim} %Pow(b, p) { % PowRec(b, p, 1) %} %PowRec(b, p, r) { % if p == 1 then % return r*b % else % ifodd p then % return PowRec(b*b, (p-1)/2, r*b) % p div 2 = (p-1)/2 % else % return PowRec(b*b, (p-1)/2, r) % fi % fi %} %\end{verbatim} %\end{quote} % \begin{macrocode} \def\InCa@PowRec#1!#2!#3!{% \ifnum#2=\@ne \the\numexpr#1*#3\relax \else \ifodd#2 % \expandafter\InCa@PowRec \the\numexpr#1*#1\expandafter!% \the\numexpr(#2-1)/2\expandafter!% \the\numexpr#1*#3\expandafter\expandafter\expandafter!% \else \expandafter\InCa@PowRec \the\numexpr#1*#1\expandafter!% \the\numexpr(#2-1)/2\expandafter!% \number#3\expandafter\expandafter\expandafter!% \fi \fi }% % \end{macrocode} % \end{macro} % % \subsubsection{\op{Div}, \op{Mod}} % % \TeX's \cs{divide} truncates, % \eTeX's \cs{numexpr} rounds the result of a division. % The rounding method is called ``Symmetric Arithmetic Rounding'' % or ``Round-Half-Up'' (``Kaufm\"annisches Runden'' in German): % \begin{quote} % | 1 = 3 divide 2 = 1.5 = numexpr 3/2 = 2|\\ % |-1 = -3 divide 2 = -1.5 = numexpr -3/2 = -2| % \end{quote} % Macro \cs{intcalcDiv} follows \TeX\ and truncates. % The calculation is done by the following formula: % \begin{gather} % \opDiv(X,Y) = (X - (Y-1)/2)/Y\qquad\text{for $X, Y > 0$} % \end{gather} % The operator `/' is \cs{numexpr}'s division. % \begin{macro}{\intcalcDiv} % \begin{macrocode} \def\intcalcDiv#1#2{% \number\expandafter\InCa@Div \the\numexpr#1\expandafter!% \the\numexpr#2! % }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Div} % \begin{macrocode} \def\InCa@Div#1!#2!{% \ifcase#2 % 0\IntCalcError:DivisionByZero% \else \ifcase#1 % 0% \else \expandafter\InCa@@Div \romannumeral 0% \ifnum#1<\z@ \expandafter-\number-#1% \else \expandafter+\number#1% \fi \expandafter!% \romannumeral 0% \ifnum#2<\z@ \expandafter-\number-#2% \else \expandafter+\number#2% \fi !% \fi \fi }% % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcDiv} % \begin{macrocode} \def\InCa@Temp#1{% \def\IntCalcDiv##1!##2!{% \number \ifcase##2 % 0\IntCalcError:DivisionByZero% \else \ifcase##1 % 0% \else \the\numexpr(##1-(##2-1)/2)/##2\relax \fi \fi #1% }% }% \InCa@Temp{ }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@@Div} % \begin{macrocode} \def\InCa@@Div#1#2!#3#4!{% #1#3% \the\numexpr(#2-(#4-1)/2)/#4\relax }% % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcMod} % \begin{macrocode} \def\intcalcMod#1#2{% \number\expandafter\InCa@Mod \the\numexpr#1\expandafter!% \the\numexpr#2! % }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Mod} % \begin{macrocode} \def\InCa@Mod#1!#2!{% \ifcase#2 % 0\IntCalcError:DivisionByZero% \else \ifcase#1 % 0% \else \expandafter\InCa@@Mod \romannumeral 0% \ifnum#1<\z@ \expandafter-\number-#1% \else \expandafter+\number#1% \fi \expandafter!% \romannumeral 0% \ifnum#2<\z@ \expandafter-\number-#2% \else \expandafter+\number#2% \fi !% \fi \fi }% % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcMod} % \begin{macrocode} \def\InCa@Temp#1{% \def\IntCalcMod##1!##2!{% \number \ifcase##2 % 0\IntCalcError:DivisionByZero% \else \ifcase##1 % 0% \else \the\numexpr##1-(##1-(##2-1)/2)/##2*##2\relax \fi \fi #1% }% }% \InCa@Temp{ }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@@Mod} % \begin{macrocode} \def\InCa@@Mod#1#2!#3#4!{% \if#3+% \if#1+% \the\numexpr#2-\InCa@@Div+#2!+#4!*#4\relax \else \expandafter\InCa@ModX \the\numexpr-#2+\InCa@@Div+#2!+#4!*#4!#4!% \fi \else -% \if#1+% \expandafter\InCa@ModX \the\numexpr-#2+\InCa@@Div+#2!+#4!*#4!#4!% \else \the\numexpr#2-\InCa@@Div+#2!+#4!*#4\relax \fi \fi }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@ModX} % \begin{macrocode} \def\InCa@ModX#1!#2!{% \ifcase#1 % 0% \else \the\numexpr#1+#2\relax \fi }% % \end{macrocode} % \end{macro} % % \begin{macrocode} \InCa@AtEnd \expandafter\endinput \fi % \end{macrocode} % % \subsection{Implementation without \eTeX} % % \subsubsection{\op{Num}} % % \begin{macro}{\intcalcNum} % \begin{macrocode} \def\intcalcNum#1{% \number\expandafter\InCa@FirstOfOne\number#1! % } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Inv}, \op{Abs}, \op{Sgn}} % % \begin{macro}{\intcalcInv} % \begin{macrocode} \def\intcalcInv#1{% \number\expandafter\InCa@FirstOfOne\number-#1! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@FirstOfOne} % \begin{macrocode} \def\InCa@FirstOfOne#1!{#1} % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcAbs} % \begin{macrocode} \def\intcalcAbs#1{% \number\expandafter\InCa@Abs\number#1! % } % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcSgn} % \begin{macrocode} \def\intcalcSgn#1{% \number\expandafter\InCa@Sgn\number#1! % } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Min}, \op{Max}, \op{Cmp}} % % \begin{macro}{\intcalcMin} % \begin{macrocode} \def\intcalcMin#1#2{% \number\expandafter\InCa@Min \number\number#1\expandafter!\number#2! % } % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcMax} % \begin{macrocode} \def\intcalcMax#1#2{% \number\expandafter\InCa@Max \number\number#1\expandafter!\number#2! % } % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcCmp} % \begin{macrocode} \def\intcalcCmp#1#2{% \number\expandafter\InCa@Cmp \number\number#1\expandafter!\number#2! % }% % \end{macrocode} % \end{macro} % % \subsubsection{\op{Inc}, \op{Dec}} % % \begin{macro}{\intcalcInc} % \begin{macrocode} \def\intcalcInc#1{% \number\expandafter\InCa@IncSwitch\number#1! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@IncSwitch} % \begin{macrocode} \def\InCa@IncSwitch#1#2!{% \ifx#1-% -% \csname InCa@Empty% \InCa@Dec#2!% \else \csname InCa@Empty% \InCa@Inc#1#2!% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\intcalcDec} % \begin{macrocode} \def\intcalcDec#1{% \number\expandafter\InCa@DecSwitch\number#1! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@DecSwitch} % \begin{macrocode} \def\InCa@DecSwitch#1#2!{% \ifx#1-% -% \csname InCa@Empty% \expandafter\InCa@Inc#2!% \else \ifx#10% -1% \else \csname InCa@Empty% \InCa@Dec#1#2!% \fi \fi } % \end{macrocode} % \end{macro} % % \begin{macro}{\IntCalcInc} % \begin{macrocode} \def\IntCalcInc#1!{% \number\csname InCa@Empty\InCa@Inc#1! % } % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcDec} % \begin{macrocode} \def\IntCalcDec#1!{% \number\csname InCa@Empty\InCa@Dec#1! % } % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@Inc} % \begin{macrocode} \def\InCa@Inc#1#2{% \ifx#2!% \csname InCa@IncDigit#1\endcsname1% \else \csname InCa@IncDigit#1% \expandafter\InCa@Inc\expandafter#2% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@IncDigit[0-8]} % \begin{macrocode} \def\InCa@Temp#1#2{% \expandafter\def\csname InCa@IncDigit#1\endcsname##1{% \endcsname 0% \ifcase##1 % #1% \else #2% \fi }% } \InCa@Temp 01 \InCa@Temp 12 \InCa@Temp 23 \InCa@Temp 34 \InCa@Temp 45 \InCa@Temp 56 \InCa@Temp 67 \InCa@Temp 78 \InCa@Temp 89 % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@IncDigit9} % \begin{macrocode} \expandafter\def\csname InCa@IncDigit9\endcsname#1{% \expandafter\endcsname \ifcase#1 % 09% \else 10% \fi } % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@Dec} % \begin{macrocode} \def\InCa@Dec#1#2{% \ifx#2!% \csname InCa@DecDigit#1\endcsname1% \else \csname InCa@DecDigit#1% \expandafter\InCa@Dec\expandafter#2% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@DecDigit[1-9]} % \begin{macrocode} \def\InCa@Temp#1#2{% \expandafter\def\csname InCa@DecDigit#1\endcsname##1{% \endcsname 0% \ifcase##1 % #1% \else #2% \fi }% } \InCa@Temp 98 \InCa@Temp 87 \InCa@Temp 76 \InCa@Temp 65 \InCa@Temp 54 \InCa@Temp 43 \InCa@Temp 32 \InCa@Temp 21 \InCa@Temp 10 % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@DecDigit0} % \begin{macrocode} \expandafter\def\csname InCa@DecDigit0\endcsname#1{% \expandafter\endcsname \ifcase#1 % 00% \else 19% \fi } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Add}, \op{Sub}} % % \begin{macro}{\intcalcAdd} % \begin{macrocode} \def\intcalcAdd#1#2{% \number \expandafter\InCa@AddSwitch \number\number#1\expandafter!% \number#2! % } % \end{macrocode} % \end{macro} % \begin{macro}{\intcalcSub} % \begin{macrocode} \def\intcalcSub#1#2{% \number \expandafter\InCa@AddSwitch \number\number#1\expandafter!% \number-\number#2! % } % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@AddSwitch} % Decision table for \cs{InCa@AddSwitch}. % The sign of negative numbers can be removed % by a simple \cs{@gobble} % instead of the more expensive |\number-|. % \begin{quote} % \begin{tabular}[t]{@{}|l|l|l|l|l|@{}} % \hline % $x<0$ & $y<0$ & $ x< y$ & $-$ & $\opAdd(-x,-y)$\\ % \cline{3-3}\cline{5-5} % & & else & & $\opAdd(-y,-x)$\\ % \cline{2-5} % & else & $-x> y$ & $-$ & $\opSub(-x, y)$\\ % \cline{3-5} % & & else & $+$ & $\opSub( y,-x)$\\ % \hline % else & $y<0$ & $ x>-y$ & $+$ & $\opSub( x,-y)$\\ % \cline{3-5} % & & else & $-$ & $\opSub(-y, x)$\\ % \cline{2-5} % & else & $ x> y$ & $+$ & $\opAdd( x, y)$\\ % \cline{3-3}\cline{5-5} % & & else & & $\opAdd( y, x)$\\ % \hline % \end{tabular} % \end{quote} % \begin{macrocode} \def\InCa@AddSwitch#1!#2!{% \ifnum#1<\z@ \ifnum#2<\z@ -% \ifnum#1<#2 % \expandafter\InCa@Add\number-#1\expandafter!% \@gobble#2!% \else \expandafter\InCa@Add\number-#2\expandafter!% \@gobble#1!% \fi \else \ifnum-#1>#2 % -% \expandafter\InCa@Sub\@gobble#1!#2!% \else \expandafter\InCa@Sub\number#2\expandafter!% \@gobble#1!% \fi \fi \else \ifnum#2<\z@ \ifnum#1>-#2 % \expandafter\InCa@Sub\number#1\expandafter!% \@gobble#2!% \else -% \expandafter\InCa@Sub\@gobble#2!#1!% \fi \else \ifnum#1>#2 % \InCa@Add#1!#2!% \else \InCa@Add#2!#1!% \fi \fi \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcAdd} % \begin{macrocode} \def\IntCalcAdd#1!#2!{% \number\InCa@Add#1!#2! % } % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcSub} % \begin{macrocode} \def\IntCalcSub#1!#2!{% \number\InCa@Sub#1!#2! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Space} % \begin{macrocode} \begingroup \def\x#1{\endgroup \let\InCa@Space= #1% }% \x{ } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Add} % \begin{macrocode} \def\InCa@Add#1!#2!{% \ifcase#2 % #1% \else \InCa@@Add#1!#2!000000000\InCa@Space \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Sub} % \begin{macrocode} \def\InCa@Sub#1!#2!{% \ifnum#1=#2 % 0% \else \InCa@@Sub#1!#2!000000000\InCa@Space \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@@Add} % \begin{macrocode} \def\InCa@@Add#1!#2#3!{% \ifx\InCa@Empty#3\InCa@Empty \@ReturnAfterElseFi{% \InCa@@@Add!!#1!#2% }% \else \@ReturnAfterFi{% \InCa@@Add#1!#3!#2% }% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@@Sub} % \begin{macrocode} \def\InCa@@Sub#1!#2#3!{% \ifx\InCa@Empty#3\InCa@Empty \@ReturnAfterElseFi{% \InCa@@@Sub!!#1!#2% }% \else \@ReturnAfterFi{% \InCa@@Sub#1!#3!#2% }% \fi } % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@@@Add} % \begin{macrocode} \def\InCa@@@Add#1!#2!#3#4!#5{% \ifx\InCa@Empty#4\InCa@Empty \csname InCa@Empty% \@ReturnAfterElseFi{% \InCa@ProcessAdd#1#3!#5#2% }% \else \@ReturnAfterFi{% \InCa@@@Add#1#3!#5#2!#4!% }% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@@@Sub} % \begin{macrocode} \def\InCa@@@Sub#1!#2!#3#4!#5{% \ifx\InCa@Empty#4\InCa@Empty \csname @gobble% \@ReturnAfterElseFi{% \InCa@ProcessSub#1#3!#5#2% }% \else \@ReturnAfterFi{% \InCa@@@Sub#1#3!#5#2!#4!% }% \fi } % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@ProcessAdd} % \begin{macrocode} \def\InCa@ProcessAdd#1#2!#3#4{% \ifx\InCa@Empty#2\InCa@Empty \csname InCa@AddDigit#1\endcsname#3% \romannumeral0#4% \else \csname InCa@AddDigit#1\csname InCa@DigitCarry#3% \@ReturnAfterFi{% \InCa@ProcessAdd#2!#4% }% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@ProcessSub} % \begin{macrocode} \def\InCa@ProcessSub#1#2!#3#4{% \ifx\InCa@Empty#2\InCa@Empty \csname InCa@SubDigit#1\endcsname#3% \romannumeral0#4% \else \csname InCa@SubDigit#1\csname InCa@DigitCarry#3% \@ReturnAfterFi{% \InCa@ProcessSub#2!#4% }% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@DigitCarry[0-9]} % \begin{macrocode} \def\InCa@Temp#1#2{% \expandafter\def\csname InCa@DigitCarry#1\endcsname##1{% \ifcase##1 % \endcsname#1% \else \endcsname#2% \fi }% } \InCa@Temp 01 \InCa@Temp 12 \InCa@Temp 23 \InCa@Temp 34 \InCa@Temp 45 \InCa@Temp 56 \InCa@Temp 67 \InCa@Temp 78 \InCa@Temp 89 \InCa@Temp 9{{10}} % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@AddDigit0} % \begin{macrocode} \expandafter\def\csname InCa@AddDigit0\endcsname#1{% \ifnum#1>9 % \endcsname10% \else \endcsname0#1% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@AddDigit[1-9]} % \begin{macrocode} \def\InCa@Temp#1#2#3{% \expandafter\def\csname InCa@AddDigit#1\endcsname##1{% \ifnum##1>#2 % \endcsname 1% \else \endcsname 0% \fi \ifcase##1 #1% 0 #3% \else #1% 10 \fi }% } \InCa@Temp 18{% \or 2% 1 \or 3% 2 \or 4% 3 \or 5% 4 \or 6% 5 \or 7% 6 \or 8% 7 \or 9% 8 \or 0% 9 }% \InCa@Temp 27{% \or 3% 1 \or 4% 2 \or 5% 3 \or 6% 4 \or 7% 5 \or 8% 6 \or 9% 7 \or 0% 8 \or 1% 9 }% \InCa@Temp 36{% \or 4% 1 \or 5% 2 \or 6% 3 \or 7% 4 \or 8% 5 \or 9% 6 \or 0% 7 \or 1% 8 \or 2% 9 }% \InCa@Temp 45{% \or 5% 1 \or 6% 2 \or 7% 3 \or 8% 4 \or 9% 5 \or 0% 6 \or 1% 7 \or 2% 8 \or 3% 9 }% \InCa@Temp 54{% \or 6% 1 \or 7% 2 \or 8% 3 \or 9% 4 \or 0% 5 \or 1% 6 \or 2% 7 \or 3% 8 \or 4% 9 }% \InCa@Temp 63{% \or 7% 1 \or 8% 2 \or 9% 3 \or 0% 4 \or 1% 5 \or 2% 6 \or 3% 7 \or 4% 8 \or 5% 9 }% \InCa@Temp 72{% \or 8% 1 \or 9% 2 \or 0% 3 \or 1% 4 \or 2% 5 \or 3% 6 \or 4% 7 \or 5% 8 \or 6% 9 }% \InCa@Temp 81{% \or 9% 1 \or 0% 2 \or 1% 3 \or 2% 4 \or 3% 5 \or 4% 6 \or 5% 7 \or 6% 8 \or 7% 9 }% \InCa@Temp 90{% \or 0% 1 \or 1% 2 \or 2% 3 \or 3% 4 \or 4% 5 \or 5% 6 \or 6% 7 \or 7% 8 \or 8% 9 }% % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@SubDigit[0-9]} % \begin{macrocode} \def\InCa@Temp#1#2{% \expandafter\def\csname InCa@SubDigit#1\endcsname##1{% \ifnum##1>#1 % \endcsname 1% \else \endcsname 0% \fi \ifcase##1 #1% 0 #2% \else #1% 10 \fi }% } \InCa@Temp 0{% \or 9% 1 \or 8% 2 \or 7% 3 \or 6% 4 \or 5% 5 \or 4% 6 \or 3% 7 \or 2% 8 \or 1% 9 } \InCa@Temp 1{% \or 0% 1 \or 9% 2 \or 8% 3 \or 7% 4 \or 6% 5 \or 5% 6 \or 4% 7 \or 3% 8 \or 2% 9 } \InCa@Temp 2{% \or 1% 1 \or 0% 2 \or 9% 3 \or 8% 4 \or 7% 5 \or 6% 6 \or 5% 7 \or 4% 8 \or 3% 9 } \InCa@Temp 3{% \or 2% 1 \or 1% 2 \or 0% 3 \or 9% 4 \or 8% 5 \or 7% 6 \or 6% 7 \or 5% 8 \or 4% 9 } \InCa@Temp 4{% \or 3% 1 \or 2% 2 \or 1% 3 \or 0% 4 \or 9% 5 \or 8% 6 \or 7% 7 \or 6% 8 \or 5% 9 } \InCa@Temp 5{% \or 4% 1 \or 3% 2 \or 2% 3 \or 1% 4 \or 0% 5 \or 9% 6 \or 8% 7 \or 7% 8 \or 6% 9 } \InCa@Temp 6{% \or 5% 1 \or 4% 2 \or 3% 3 \or 2% 4 \or 1% 5 \or 0% 6 \or 9% 7 \or 8% 8 \or 7% 9 } \InCa@Temp 7{% \or 6% 1 \or 5% 2 \or 4% 3 \or 3% 4 \or 2% 5 \or 1% 6 \or 0% 7 \or 9% 8 \or 8% 9 } \InCa@Temp 8{% \or 7% 1 \or 6% 2 \or 5% 3 \or 4% 4 \or 3% 5 \or 2% 6 \or 1% 7 \or 0% 8 \or 9% 9 } \InCa@Temp 9{% \or 8% 1 \or 7% 2 \or 6% 3 \or 5% 4 \or 4% 5 \or 3% 6 \or 2% 7 \or 1% 8 \or 0% 9 } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Shl}, \op{Shr}} % % \begin{macro}{\intcalcShl} % \begin{macrocode} \def\intcalcShl#1{% \number\expandafter\InCa@ShlSwitch\number#1! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@ShlSwitch} % \begin{macrocode} \def\InCa@ShlSwitch#1#2!{% \ifx#1-% -\csname InCa@Empty% \InCa@Shl#2!% \else \csname InCa@Empty% \InCa@Shl#1#2!% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcShl} % \begin{macrocode} \def\IntCalcShl#1!{% \number \csname InCa@Empty% \InCa@Shl#1! % } % \end{macrocode} % \end{macro} % \begin{macro}{\IntCal@ShlDigit} % \begin{macrocode} \def\InCa@Shl#1#2{% \ifx#2!% \csname InCa@ShlDigit#1\endcsname0% \else \csname InCa@ShlDigit#1% \@ReturnAfterFi{% \InCa@Shl#2% }% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@ShlDigit0} % \begin{macrocode} \expandafter\def\csname InCa@ShlDigit0\endcsname{% \endcsname0% } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@ShlDigit[1-9]} % \begin{macrocode} \def\InCa@Temp#1#2#3#4#5{% \expandafter\def\csname InCa@ShlDigit#1\endcsname##1{% \expandafter\endcsname \ifcase##1 % #2#3% \else #4#5% \fi }% } \InCa@Temp 10203 \InCa@Temp 20405 \InCa@Temp 30607 \InCa@Temp 40809 \InCa@Temp 51011 \InCa@Temp 61213 \InCa@Temp 71415 \InCa@Temp 81617 \InCa@Temp 91819 % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcShr} % \begin{macrocode} \def\intcalcShr#1{% \number\expandafter\InCa@ShrSwitch\number#1! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@ShrSwitch} % \begin{macrocode} \def\InCa@ShrSwitch#1#2!{% \ifx#1-% -\InCa@Shr#2!% \else \InCa@Shr#1#2!% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcShr} % \begin{macrocode} \def\IntCalcShr#1!{% \number\InCa@Shr#1! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Shr} % \begin{macrocode} \def\InCa@Shr#1#2{% \InCa@ShrDigit#1!% \ifx#2!% \else \@ReturnAfterFi{% \ifodd#1 % \@ReturnAfterElseFi{% \InCa@Shr{1#2}% }% \else \expandafter\InCa@Shr\expandafter#2% \fi }% \fi } % \end{macrocode} % \end{macro} % \begin{macrocode} \def\InCa@ShrDigit#1!{% \ifcase#1 0% 0 \or 0% 1 \or 1% 2 \or 1% 3 \or 2% 4 \or 2% 5 \or 3% 6 \or 3% 7 \or 4% 8 \or 4% 9 \or 5% 10 \or 5% 11 \or 6% 12 \or 6% 13 \or 7% 14 \or 7% 15 \or 8% 16 \or 8% 17 \or 9% 18 \or 9% 19 \fi } % \end{macrocode} % % \subsubsection{\cs{InCa@Tim}} % % \begin{macro}{\InCa@Tim} % Macro \cs{InCa@Tim} implements % ``Number \emph{tim}es digit''. % \begin{macrocode} \def\InCa@Temp#1{% \def\InCa@Tim##1##2{% \number \ifcase##2 % 0 0% \or % 1 ##1% \else % 2-9 \csname InCa@Empty% \InCa@ProcessTim##2##1!% \fi #1% }% } \InCa@Temp{ } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@ProcessTim} % \begin{macrocode} \def\InCa@ProcessTim#1#2#3{% \ifx#3!% \csname InCa@TimDigit#2\endcsname#10% \else \csname InCa@TimDigit#2\csname InCa@Param#1% \@ReturnAfterFi{% \InCa@ProcessTim#1#3% }% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Param[0-9]} % \begin{macrocode} \def\InCa@Temp#1{% \expandafter\def\csname InCa@Param#1\endcsname{% \endcsname#1% }% } \InCa@Temp 0% \InCa@Temp 1% \InCa@Temp 2% \InCa@Temp 3% \InCa@Temp 4% \InCa@Temp 5% \InCa@Temp 6% \InCa@Temp 7% \InCa@Temp 8% \InCa@Temp 9% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@TimDigit0} % \begin{macrocode} \expandafter\def\csname InCa@TimDigit0\endcsname#1#2{% \endcsname 0#2% } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@TimDigit1} % \begin{macrocode} \expandafter\def\csname InCa@TimDigit1\endcsname#1#2{% \ifcase#2 % \endcsname 0#1% \else \csname InCa@AddDigit#1\endcsname #2% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@TimDigit[2-9]} % \begin{macrocode} \def\InCa@Temp#1#2{% \expandafter\def\csname InCa@TimDigit#1\endcsname##1{% \expandafter\InCa@TimDigitCarry \number \ifcase##1 0% 0 #2% \fi !% }% } \InCa@Temp 2{% \or 2% 1 \or 4% 2 \or 6% 3 \or 8% 4 \or 10% 5 \or 12% 6 \or 14% 7 \or 16% 8 \or 18% 9 } \InCa@Temp 3{% \or 3% 1 \or 6% 2 \or 9% 3 \or 12% 4 \or 15% 5 \or 18% 6 \or 21% 7 \or 24% 8 \or 27% 9 } \InCa@Temp 4{% \or 4% 1 \or 8% 2 \or 12% 3 \or 16% 4 \or 20% 5 \or 24% 6 \or 28% 7 \or 32% 8 \or 36% 9 } \InCa@Temp 5{% \or 5% 1 \or 10% 2 \or 15% 3 \or 20% 4 \or 25% 5 \or 30% 6 \or 35% 7 \or 40% 8 \or 45% 9 } \InCa@Temp 6{% \or 6% 1 \or 12% 2 \or 18% 3 \or 24% 4 \or 30% 5 \or 36% 6 \or 42% 7 \or 48% 8 \or 54% 9 } \InCa@Temp 7{% \or 7% 1 \or 14% 2 \or 21% 3 \or 28% 4 \or 35% 5 \or 42% 6 \or 49% 7 \or 56% 8 \or 63% 9 } \InCa@Temp 8{% \or 8% 1 \or 16% 2 \or 24% 3 \or 32% 4 \or 40% 5 \or 48% 6 \or 56% 7 \or 64% 8 \or 72% 9 } \InCa@Temp 9{% \or 9% 1 \or 18% 2 \or 27% 3 \or 36% 4 \or 45% 5 \or 54% 6 \or 63% 7 \or 72% 8 \or 81% 9 } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@TimDigitCarry} % \begin{macrocode} \def\InCa@TimDigitCarry#1!{% \ifnum#1<10 % \csname InCa@AddDigit#1\expandafter\endcsname \else \@ReturnAfterFi{% \InCa@@TimDigitCarry#1!% }% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@@TimDigitCarry} % \begin{macrocode} \def\InCa@@TimDigitCarry#1#2!#3{% \csname InCa@DigitCarry#1% \csname InCa@AddDigit#2\endcsname #3% } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Mul}} % % \begin{macro}{\intcalcMul} % \begin{macrocode} \def\intcalcMul#1#2{% \number \expandafter\InCa@MulSwitch \number\number#1\expandafter!% \number#2! % } % \end{macrocode} % \end{macro} % % \begin{macro}{\InCa@MulSwitch} % Decision table for \cs{InCa@MulSwitch}. % \begin{quote} % \begin{tabular}[t]{@{}|l|l|l|l|l|@{}} % \hline % $x<0$ & $y<0$ & $ x< y$ & $+$ & $\opMul(-x,-y)$\\ % \cline{3-3}\cline{5-5} % & & else & & $\opMul(-y,-x)$\\ % \cline{2-5} % & else & $-x> y$ & $-$ & $\opMul(-x, y)$\\ % \cline{3-3}\cline{5-5} % & & else & & $\opMul( y,-x)$\\ % \hline % else & $y<0$ & $ x>-y$ & $-$ & $\opMul( x,-y)$\\ % \cline{3-3}\cline{5-5} % & & else & & $\opMul(-y, x)$\\ % \cline{2-5} % & else & $ x> y$ & $+$ & $\opMul( x, y)$\\ % \cline{3-3}\cline{5-5} % & & else & & $\opMul( y, x)$\\ % \hline % \end{tabular} % \end{quote} % \begin{macrocode} \def\InCa@MulSwitch#1!#2!{% \ifnum#1<\z@ \ifnum#2<\z@ \ifnum#1<#2 % \expandafter\InCa@Mul\number-#1\expandafter!% \@gobble#2!% \else \expandafter\InCa@Mul\number-#2\expandafter!% \@gobble#1!% \fi \else -% \ifnum-#1>#2 % \expandafter\InCa@Mul\@gobble#1!#2!% \else \expandafter\InCa@Mul\number#2\expandafter!% \@gobble#1!% \fi \fi \else \ifnum#2<\z@ -% \ifnum#1>-#2 % \expandafter\InCa@Mul\number#1\expandafter!% \@gobble#2!% \else \expandafter\InCa@Mul\@gobble#2!#1!% \fi \else \ifnum#1>#2 % \InCa@Mul#1!#2!% \else \InCa@Mul#2!#1!% \fi \fi \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcMul} % \begin{macrocode} \def\IntCalcMul#1!#2!{% \number\InCa@Mul#1!#2! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Mul} % \begin{macrocode} \def\InCa@Mul#1!#2!{% \ifcase#2 % 0% \or #1% \or \csname InCa@Empty% \expandafter\InCa@Shl#1!% \else \ifnum#2<10 % \InCa@Tim{#1}#2% \else \InCa@ProcessMul!#2!#1!% \fi \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Mul} % \begin{macrocode} \def\InCa@ProcessMul#1!#2#3!#4!{% \ifx\InCa@Empty#3\InCa@Empty \expandafter\InCa@Add\number #10\expandafter\expandafter\expandafter!% \InCa@Tim{#4}#2!% \else \ifx\InCa@Empty#1\InCa@Empty \expandafter\expandafter\expandafter\InCa@ProcessMul \InCa@Tim{#4}#2!% #3!#4!% \else \expandafter\InCa@ProcessMul\number \expandafter\InCa@Add\number% #10\expandafter\expandafter\expandafter!% \InCa@Tim{#4}#2!!% #3!#4!% \fi \fi } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Sqr}, \op{Fac}} % % \begin{macro}{\intcalcSqr} % \begin{macrocode} \def\intcalcSqr#1{% \number\expandafter\InCa@Sqr\number#1! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Sqr} % \begin{macrocode} \def\InCa@Sqr#1#2!{% \ifx#1-% \InCa@Mul#2!#2!% \else \InCa@Mul#1#2!#1#2!% \fi } % \end{macrocode} % \end{macro} % % \begin{macro}{\intcalcFac} % \begin{macrocode} \def\intcalcFac#1{% \number\expandafter\InCa@Fac\number#1! % } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Pow}} % % \begin{macro}{\intcalcPow} % \begin{macrocode} \def\intcalcPow#1#2{% \number\expandafter\InCa@Pow \number\number#1\expandafter!% \number#2! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Pow} % \begin{macrocode} \def\InCa@Pow#1#2!#3#4!{% \ifcase#3#4 % power = 0 1% \or % power = 1 #1#2% \or % power = 2 \ifx#1-% \InCa@Mul#2!#2!% \else \InCa@Mul#1#2!#1#2!% \fi \else \ifcase#1#2 % basis = 0, power <> 0 0% \ifx#3-% power < 0 0\IntCalcError:DivisionByZero% \fi \or 1% basis = 1 \else \ifnum#1#2=\m@ne % basis = -1 \ifodd#3#4 % -% \fi 1% \else % |basis| > 1 \ifx#3-% power < 0 0% \else % power > 2 \ifx#1-% basis < 0 \ifodd#3#4 % -% \fi \InCa@PowRec#2!#3#4!1!% \else \InCa@PowRec#1#2!#3#4!1!% \fi \fi \fi \fi \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@PowRec} %\begin{quote} %\begin{verbatim} %Pow(b, p) { % PowRec(b, p, 1) %} %PowRec(b, p, r) { % if p == 1 then % return r % else % ifodd p then % return PowRec(b*b, p div 2, r*b) % p div 2 = (p-1)/2 % else % return PowRec(b*b, p div 2, r) % fi % fi %} %\end{verbatim} %\end{quote} % \begin{macrocode} \def\InCa@PowRec#1!#2!#3!{% \ifnum#2=\@ne \ifnum#1>#3 % \InCa@Mul#1!#3!% \else \InCa@Mul#3!#1!% \fi \else \expandafter\InCa@PowRec \number\InCa@Mul#1!#1!\expandafter!% \number\intcalcShr{#2}\expandafter!% \number \ifodd#2 % \ifnum#1>#3 % \InCa@Mul#1!#3!% \else \InCa@Mul#3!#1!% \fi \else #3% \fi \expandafter!% \fi } % \end{macrocode} % \end{macro} % % \subsubsection{\op{Div}} % % \begin{macro}{\intcalcDiv} % \begin{macrocode} \def\intcalcDiv#1#2{% \number\expandafter\InCa@Div \number\number#1\expandafter!% \number#2! % } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@Div} % \begin{macrocode} \def\InCa@Div#1!#2!{% \ifcase#2 % 0\IntCalcError:DivisionByZero% \else \ifcase#1 % 0% \else \expandafter\InCa@DivSwitch \number#1\expandafter!% \number#2!% \fi \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcDiv} % \begin{macrocode} \def\InCa@Temp#1{% \def\IntCalcDiv##1!##2!{% \number \ifcase##2 % 0\IntCalcError:DivisionByZero% \else \ifcase##1 % 0% \else \InCa@@Div##1!##2!% \fi \fi #1% }% } \InCa@Temp{ }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@DivSwitch} % Decision table for \cs{InCa@DivSwitch}. % \begin{quote} % \begin{tabular}[t]{@{}|l|l|l|l|@{}} % \hline % $x<0$ & $y<0$ & $+$ & $\opDiv(-x,-y)$\\ % \cline{2-4} % & else & $-$ & $\opDiv(-x, y)$\\ % \hline % else & $y<0$ & $-$ & $\opDiv( x,-y)$\\ % \cline{2-4} % & else & $+$ & $\opDiv( x, y)$\\ % \hline % \end{tabular} % \end{quote} % \begin{macrocode} \def\InCa@DivSwitch#1!#2!{% \ifnum#1<\z@ \ifnum#2<\z@ \expandafter\InCa@@Div\number-#1\expandafter!% \@gobble#2!% \else -% \expandafter\InCa@@Div\@gobble#1!#2!% \fi \else \ifnum#2<\z@ -% \expandafter\InCa@@Div\number#1\expandafter!% \@gobble#2!% \else \InCa@@Div#1!#2!% \fi \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@@Div} % \begin{macrocode} \def\InCa@@Div#1!#2!{% \ifnum#1>#2 % \ifcase#2 % 0 already catched ? \IntCalcError:ThisCannotHappen% \or % 1 #1% \or % 2 \InCa@Shr#1!% \else \InCa@DivStart!#1!#2!#2!% \fi \else \ifnum#1=#2 % 1% \else 0% \fi \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@DivStart} % \begin{macrocode} \def\InCa@DivStart#1!#2#3!#4#5{% \ifx#5!% \@ReturnAfterElseFi{% \InCa@DivStartI{#1#2}#3=!% }% \else \@ReturnAfterFi{% \InCa@DivStart{#1#2}!#3!#5% }% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@StartI} % \begin{macrocode} \def\InCa@DivStartI#1!#2!{% \expandafter\InCa@DivStartII \number#2\expandafter\expandafter\expandafter!% \intcalcShl{#2}!% #1!% } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@StartII} % \begin{macrocode} \def\InCa@DivStartII#1!#2!{% \expandafter\InCa@DivStartIII \number#1\expandafter!% \number#2\expandafter\expandafter\expandafter!% \intcalcShl{#2}!% } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@StartIII} % \begin{macrocode} \def\InCa@DivStartIII#1!#2!#3!{% \expandafter\InCa@DivStartIV \number#1\expandafter!% \number#2\expandafter!% \number#3\expandafter!% \number\InCa@Add#3!#2!\expandafter\expandafter\expandafter!% \intcalcShl{#3}!% } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@StartIV} % \begin{macrocode} \def\InCa@DivStartIV#1!#2!#3!#4!#5!#6!{% \InCa@ProcessDiv#6!#1!#2!#3!#4!#5!/% } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@ProcessDiv} % \begin{macrocode} \def\InCa@ProcessDiv#1#2#3!#4!#5!#6!#7!#8!#9/{% #9% \ifnum#1<#4 % 0 0% \ifx#2=% \else \InCa@ProcessDiv{#1#2}#3!#4!#5!#6!#7!#8!% \fi \else % 1-9 \ifnum#1<#5 % 1 1% \ifx#2=% \else \expandafter\InCa@ProcessDiv\expandafter{% \number\InCa@Sub#1!#4!% #2% }#3!#4!#5!#6!#7!#8!% \fi \else % 2-9 \ifnum#1<#7 % 2 3 4 5 \ifnum#1<#6 % 2 3 \@ReturnAfterElseFi{% \expandafter\InCa@@ProcessDiv \number\InCa@Sub#1!#5!!% 23% }% \else % 4 5 \@ReturnAfterFi{% \expandafter\InCa@@ProcessDiv \number\InCa@Sub#1!#6!!% 45% }% \fi #2#3!#4!#5!#6!#7!#8!% \else % 6 7 8 9 \ifnum#1<#8 % 6 7 \@ReturnAfterElseFi{% \expandafter\InCa@@ProcessDiv \number\InCa@Sub#1!#7!!% 67% }% \else % 8 9 \@ReturnAfterFi{% \expandafter\InCa@@ProcessDiv \number\InCa@Sub#1!#8!!% 89% }% \fi #2#3!#4!#5!#6!#7!#8!% \fi \fi \fi \ifx#2=% \expandafter\@gobble \fi /% } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@@ProcessDiv} % \begin{macrocode} \def\InCa@@ProcessDiv#1!#2#3#4#5!#6!{% \ifnum#1<#6 % #2% \@ReturnAfterElseFi{% \ifx#4=% \expandafter\InCa@CleanupIV \else \@ReturnAfterFi{% \InCa@ProcessDiv{#1#4}#5!#6!% }% \fi }% \else #3% \@ReturnAfterFi{% \ifx#4=% \expandafter\InCa@CleanupIV \else \@ReturnAfterFi{% \expandafter\InCa@ProcessDiv\expandafter{% \number\InCa@Sub#1!#6! % #4% }#5!#6!% }% \fi }% \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@CleanupIV} % \begin{macrocode} \def\InCa@CleanupIV#1!#2!#3!#4!{} % \end{macrocode} % \end{macro} % % \subsubsection{\op{Mod}} % % \begin{macro}{\intcalcMod} % \begin{macrocode} \def\intcalcMod#1#2{% \number\expandafter\InCa@Mod \number\number#1\expandafter!% \number#2! % } % \end{macrocode} % \end{macro} % \begin{macro}{\intcalc@Mod} % Pseudocode/decision table for \cs{intcalc@Mod}. % \begin{quote} % \begin{tabular}{@{}lll@{}} % if & $y=0$ & DivisionByZero\\ % elsif & $y<0$ & $-\opMod(-x,-y)$\\ % elsif & $x=0$ & 0\\ % elsif & $y=1$ & 0\\ % elsif & $y=2$ & $\opOdd(x)\mathbin{?}1\mathbin{:}0$\\ % elsif & $x<0$ & % $z\leftarrow x-(x/y)*y;\quad (z<0)\mathbin{?}z+y\mathbin{:}z$\\ % else & & $x-(x/y)*y$\\ % \\ % \end{tabular} % \end{quote} % \begin{macrocode} \def\InCa@Mod#1!#2!{% \ifcase#2 % 0\IntCalcError:DivisionByZero% \else \ifnum#2<\z@ -% \expandafter\InCa@Mod \number-#1\expandafter!% \number-#2!% \else \ifcase#1 % 0% \else \ifcase#2 % 0 already catched ? \IntCalcError:ThisCannotHappen% \or % 1 0% \or % 2 \ifodd#1 1\else 0\fi \else \ifnum#1<\z@ \expandafter\InCa@ModShift \number-% \expandafter\InCa@Sub \number\@gobble#1\expandafter!% \number\intcalcMul{#2}{% \expandafter\InCa@Div\@gobble#1!#2!% }!% !#2!% \else \expandafter\InCa@Sub\number#1\expandafter!% \number\intcalcMul{#2}{\InCa@Div#1!#2!}!% \fi \fi \fi \fi \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\IntCalcMod} % \begin{macrocode} \def\InCa@Temp#1{% \def\IntCalcMod##1!##2!{% \number \ifcase##2 % 0\IntCalcError:DivisionByZero% \else \ifcase##1 % 0% \else \ifcase##2 % 0 already catched ? \IntCalcError:ThisCannotHappen \or % 1 0% \or % 2 \ifodd ##1 1\else 0\fi \else \expandafter\InCa@Sub\number##1\expandafter!% \number\intcalcMul{##2}{\InCa@Div##1!##2!}!% \fi \fi \fi #1% }% } \InCa@Temp{ }% % \end{macrocode} % \end{macro} % \begin{macro}{\InCa@ModShift} % \begin{macrocode} \def\InCa@ModShift#1!#2!{% \ifnum#1<\z@ \expandafter\InCa@Sub\number#2\expandafter!% \@gobble#1!% \else #1% \fi } % \end{macrocode} % \end{macro} % % \subsubsection{Help macros} % % \begin{macro}{\InCa@Empty} % \begin{macrocode} \def\InCa@Empty{} % \end{macrocode} % \end{macro} % \begin{macro}{\@gobble} % \begin{macrocode} \expandafter\ifx\csname @gobble\endcsname\relax \long\def\@gobble#1{}% \fi % \end{macrocode} % \end{macro} % \begin{macro}{\@ReturnAfterFi} % \begin{macrocode} \long\def\@ReturnAfterFi#1\fi{\fi#1}% % \end{macrocode} % \end{macro} % \begin{macro}{\@ReturnAfterElseFi} % \begin{macrocode} \long\def\@ReturnAfterElseFi#1\else#2\fi{\fi#1}% % \end{macrocode} % \end{macro} % % \begin{macrocode} \InCa@AtEnd % \end{macrocode} % % \begin{macrocode} % % \end{macrocode} % % \section{Test} % % \subsection{Catcode checks for loading} % % \begin{macrocode} %<*test1> % \end{macrocode} % \begin{macrocode} \catcode`\{=1 % \catcode`\}=2 % \catcode`\#=6 % \catcode`\@=11 % \expandafter\ifx\csname count@\endcsname\relax \countdef\count@=255 % \fi \expandafter\ifx\csname @gobble\endcsname\relax \long\def\@gobble#1{}% \fi \expandafter\ifx\csname @firstofone\endcsname\relax \long\def\@firstofone#1{#1}% \fi \expandafter\ifx\csname loop\endcsname\relax \expandafter\@firstofone \else \expandafter\@gobble \fi {% \def\loop#1\repeat{% \def\body{#1}% \iterate }% \def\iterate{% \body \let\next\iterate \else \let\next\relax \fi \next }% \let\repeat=\fi }% \def\RestoreCatcodes{} \count@=0 % \loop \edef\RestoreCatcodes{% \RestoreCatcodes \catcode\the\count@=\the\catcode\count@\relax }% \ifnum\count@<255 % \advance\count@ 1 % \repeat \def\RangeCatcodeInvalid#1#2{% \count@=#1\relax \loop \catcode\count@=15 % \ifnum\count@<#2\relax \advance\count@ 1 % \repeat } \expandafter\ifx\csname LoadCommand\endcsname\relax \def\LoadCommand{\input intcalc.sty\relax}% \fi \def\Test{% \RangeCatcodeInvalid{0}{47}% \RangeCatcodeInvalid{58}{64}% \RangeCatcodeInvalid{91}{96}% \RangeCatcodeInvalid{123}{255}% \catcode`\@=12 % \catcode`\\=0 % \catcode`\{=1 % \catcode`\}=2 % \catcode`\#=6 % \catcode`\[=12 % \catcode`\]=12 % \catcode`\%=14 % \catcode`\ =10 % \catcode13=5 % \LoadCommand \RestoreCatcodes } \Test \csname @@end\endcsname \end % \end{macrocode} % \begin{macrocode} % % \end{macrocode} % % \subsection{Macro tests} % % \subsubsection{Preamble with test macro definitions} % % \begin{macrocode} %<*test2|test4> \NeedsTeXFormat{LaTeX2e} \nofiles \documentclass{article} %\let\SavedNumexpr\numexpr %\let\numexpr\UNDEFINED \makeatletter \chardef\InCa@TestMode=1 % \makeatother \usepackage{intcalc}[2007/09/27] %\let\numexpr\SavedNumexpr \usepackage{qstest} \IncludeTests{*} \LogTests{log}{*}{*} % %<*test2> \newcommand*{\TestSpaceAtEnd}[1]{% % \let\SavedNumexpr\numexpr % \let\numexpr\UNDEFINED \edef\resultA{#1}% \edef\resultB{#1 }% % \let\numexpr\SavedNumexpr \Expect*{\resultA\space}*{\resultB}% } \newcommand*{\TestResult}[2]{% % \let\SavedNumexpr\numexpr % \let\numexpr\UNDEFINED \edef\result{#1}% % \let\numexpr\SavedNumexpr \Expect*{\result}{#2}% } \newcommand*{\TestResultTwoExpansions}[2]{% %<*noetex> \begingroup \let\numexpr\UNDEFINED \expandafter\expandafter\expandafter \endgroup % \expandafter\expandafter\expandafter\Expect \expandafter\expandafter\expandafter{#1}{#2}% } \newcount\TestCount %\newcommand*{\TestArg}[1]{\numexpr#1\relax} %\newcommand*{\TestArg}[1]{#1} \newcommand*{\TestTeXDivide}[2]{% \TestCount=\TestArg{#1}\relax \divide\TestCount by \TestArg{#2}\relax \Expect*{\intcalcDiv{#1}{#2}}*{\the\TestCount}% } \newcommand*{\Test}[2]{% \TestResult{#1}{#2}% \TestResultTwoExpansions{#1}{#2}% \TestSpaceAtEnd{#1}% } \newcommand*{\TestExch}[2]{\Test{#2}{#1}} \newcommand*{\TestInv}[2]{% \Test{\intcalcInv{#1}}{#2}% } \newcommand*{\TestNum}[2]{% \Test{\intcalcNum{#1}}{#2}% } \newcommand*{\TestAbs}[2]{% \Test{\intcalcAbs{#1}}{#2}% } \newcommand*{\TestSgn}[2]{% \Test{\intcalcSgn{#1}}{#2}% } \newcommand*{\TestMin}[3]{% \Test{\intcalcMin{#1}{#2}}{#3}% } \newcommand*{\TestMax}[3]{% \Test{\intcalcMax{#1}{#2}}{#3}% } \newcommand*{\TestCmp}[3]{% \Test{\intcalcCmp{#1}{#2}}{#3}% } \newcommand*{\TestInc}[2]{% \Test{\intcalcInc{#1}}{#2}% \ifnum\intcalcNum{#1}>-1 % \edef\x{% \noexpand\Test{% \noexpand\IntCalcInc\intcalcNum{#1}!% }{#2}% }% \x \fi } \newcommand*{\TestDec}[2]{% \Test{\intcalcDec{#1}}{#2}% \ifnum\intcalcNum{#1}>0 % \edef\x{% \noexpand\Test{% \noexpand\IntCalcDec\intcalcNum{#1}!% }{#2}% }% \x \fi } \newcommand*{\TestAdd}[3]{% \Test{\intcalcAdd{#1}{#2}}{#3}% \ifnum\intcalcNum{#1}>0 % \ifnum\intcalcNum{#2}> 0 % \ifnum\intcalcCmp{#1}{#2}>0 % \edef\x{% \noexpand\Test{% \noexpand\IntCalcAdd \intcalcNum{#1}!\intcalcNum{#2}!% }{#3}% }% \x \else \edef\x{% \noexpand\Test{% \noexpand\IntCalcAdd \intcalcNum{#2}!\intcalcNum{#1}!% }{#3}% }% \x \fi \fi \fi } \newcommand*{\TestSub}[3]{% \Test{\intcalcSub{#1}{#2}}{#3}% \ifnum\intcalcNum{#1}>0 % \ifnum\intcalcNum{#2}> 0 % \ifnum\intcalcCmp{#1}{#2}>0 % \edef\x{% \noexpand\Test{% \noexpand\IntCalcSub \intcalcNum{#1}!\intcalcNum{#2}!% }{#3}% }% \x \fi \fi \fi } \newcommand*{\TestShl}[2]{% \Test{\intcalcShl{#1}}{#2}% \edef\x{% \noexpand\Test{% \noexpand\IntCalcShl\intcalcAbs{#1}!% }{\intcalcAbs{#2}}% }% \x } \newcommand*{\TestShr}[2]{% \Test{\intcalcShr{#1}}{#2}% \edef\x{% \noexpand\Test{% \noexpand\IntCalcShr\intcalcAbs{#1}!% }{\intcalcAbs{#2}}% }% \x } \newcommand*{\TestMul}[3]{% \Test{\intcalcMul{#1}{#2}}{#3}% \edef\x{% \noexpand\Test{% \noexpand\IntCalcMul\intcalcAbs{#1}!\intcalcAbs{#2}!% }{\intcalcAbs{#3}}% }% \x } \newcommand*{\TestSqr}[2]{% \Test{\intcalcSqr{#1}}{#2}% } \newcommand*{\TestFac}[2]{% \expandafter\TestExch\expandafter{\the\numexpr#2}{\intcalcFac{#1}}% } \newcommand*{\TestPow}[3]{% \Test{\intcalcPow{#1}{#2}}{#3}% } \newcommand*{\TestDiv}[3]{% \Test{\intcalcDiv{#1}{#2}}{#3}% \TestTeXDivide{#1}{#2}% \edef\x{% \noexpand\Test{% \noexpand\IntCalcDiv\intcalcAbs{#1}!\intcalcAbs{#2}!% }{\intcalcAbs{#3}}% }% } \newcommand*{\TestMod}[3]{% \Test{\intcalcMod{#1}{#2}}{#3}% \ifcase\ifcase\intcalcSgn{#1} 0% \or \ifcase\intcalcSgn{#2} 1% \or 0% \else 1% \fi \else \ifcase\intcalcSgn{#2} 1% \or 1% \else 0% \fi \fi\relax \edef\x{% \noexpand\Test{% \noexpand\IntCalcMod \intcalcAbs{#1}!\intcalcAbs{#2}!% }{\intcalcAbs{#3}}% }% \x \fi } % % \end{macrocode} % % \subsubsection{Time} % % \begin{macrocode} %<*test2> \begingroup\expandafter\expandafter\expandafter\endgroup \expandafter\ifx\csname pdfresettimer\endcsname\relax \else \makeatletter \newcount\SummaryTime \newcount\TestTime \SummaryTime=\z@ \newcommand*{\PrintTime}[2]{% \typeout{% [Time #1: \strip@pt\dimexpr\number#2sp\relax\space s]% }% }% \newcommand*{\StartTime}[1]{% \renewcommand*{\TimeDescription}{#1}% \pdfresettimer }% \newcommand*{\TimeDescription}{}% \newcommand*{\StopTime}{% \TestTime=\pdfelapsedtime \global\advance\SummaryTime\TestTime \PrintTime\TimeDescription\TestTime }% \let\saved@qstest\qstest \let\saved@endqstest\endqstest \def\qstest#1#2{% \saved@qstest{#1}{#2}% \StartTime{#1}% }% \def\endqstest{% \StopTime \saved@endqstest }% \AtEndDocument{% \PrintTime{summary}\SummaryTime }% \makeatother \fi % % \end{macrocode} % % \subsubsection{Test 4: additional mod/div operations} % % \begin{macrocode} %<*test4> \newcommand*{\TestDo}[2]{% \ifcase\numexpr#2\relax \else \edef\temp{\intcalcMod{#1}{#2}}% \Expect*{% \the\numexpr \intcalcMul{% \intcalcDiv{\intcalcAbs{#1}}{\intcalcAbs{#2}}% }{\intcalcAbs{#2}}% +\intcalcMod{\intcalcAbs{#1}}{\intcalcAbs{#2}}\relax }*{\the\numexpr\intcalcAbs{#1}\relax}% \fi } \newcommand*{\TestOne}[2]{% \TestDo{#1}{#1}% } \newcommand*{\TestTwo}[3]{% \TestDo{#1}{#2}% \TestDo{#2}{#1}% } \let\TestNum\TestOne \let\TestInv\TestOne \let\TestAbs\TestOne \let\TestSgn\TestOne \let\TestMin\TestTwo \let\TestMax\TestTwo \let\TestCmp\TestTwo \let\TestInc\TestOne \let\TestDec\TestOne \let\TestAdd\TestTwo \let\TestSub\TestTwo \let\TestShl\TestOne \let\TestShr\TestOne \let\TestMul\TestTwo \let\TestSqr\TestOne \def\TestFac#1#2{} \let\TestPow\TestTwo \let\TestDiv\TestTwo \let\TestMod\TestTwo % % \end{macrocode} % % \subsubsection{Test sets} % % \begin{macrocode} %<*test2|test4> \makeatletter \begin{qstest}{num}{num}% \TestNum{0}{0}% \TestNum{1}{1}% \TestNum{-1}{-1}% \TestNum{10}{10}% \TestNum{-10}{-10}% \TestNum{2147483647}{2147483647}% \TestNum{-2147483647}{-2147483647}% \TestNum{ 0 }{0}% \TestNum{ 1 }{1}% \TestNum{--1}{1}% \TestNum{ - + - + 4 }{4}% \TestNum{\z@}{0}% \TestNum{\@ne}{1}% \TestNum{\m@ne}{-1}% %<*etex> \TestNum{-10+30}{20}% \TestNum{10-30}{-20}% % \end{qstest} \begin{qstest}{inv}{inv}% \TestInv{0}{0}% \TestInv{1}{-1}% \TestInv{-1}{1}% \TestInv{10}{-10}% \TestInv{-10}{10}% \TestInv{2147483647}{-2147483647}% \TestInv{-2147483647}{2147483647}% \TestInv{ 0 }{0}% \TestInv{ 1 }{-1}% \TestInv{--1}{-1}% \TestInv{\z@}{0}% \TestInv{\@ne}{-1}% \TestInv{\m@ne}{1}% %<*etex> \TestInv{-10+30}{-20}% \TestInv{10-30}{20}% % \end{qstest} \begin{qstest}{abs}{abs}% \TestAbs{0}{0}% \TestAbs{1}{1}% \TestAbs{-1}{1}% \TestAbs{10}{10}% \TestAbs{-10}{10}% \TestAbs{2147483647}{2147483647}% \TestAbs{-2147483647}{2147483647}% \TestAbs{ 0 }{0}% \TestAbs{ 1 }{1}% \TestAbs{--1}{1}% \TestAbs{\z@}{0}% \TestAbs{\@ne}{1}% \TestAbs{\m@ne}{1}% %<*etex> \TestAbs{-10+30}{20}% \TestAbs{10-30}{20}% % \end{qstest} \begin{qstest}{sign}{sign}% \TestSgn{0}{0}% \TestSgn{1}{1}% \TestSgn{-1}{-1}% \TestSgn{10}{1}% \TestSgn{-10}{-1}% \TestSgn{2147483647}{1}% \TestSgn{-2147483647}{-1}% \TestSgn{ 0 }{0}% \TestSgn{ 2 }{1}% \TestSgn{ -2 }{-1}% \TestSgn{--2}{1}% \TestSgn{\z@}{0}% \TestSgn{\@ne}{1}% \TestSgn{\m@ne}{-1}% %<*etex> \TestSgn{-10+30}{1}% \TestSgn{10-30}{-1}% % \end{qstest} \begin{qstest}{min}{min}% \TestMin{0}{1}{0}% \TestMin{1}{0}{0}% \TestMin{-10}{-20}{-20}% \TestMin{ 1 }{ 2 }{1}% \TestMin{ 2 }{ 1 }{1}% \TestMin{1}{1}{1}% \TestMin{\z@}{\@ne}{0}% \TestMin{\@ne}{\m@ne}{-1}% %<*etex> \TestMin{1+2}{3+4}{3}% % \end{qstest} \begin{qstest}{max}{max}% \TestMax{0}{1}{1}% \TestMax{1}{0}{1}% \TestMax{-10}{-20}{-10}% \TestMax{ 1 }{ 2 }{2}% \TestMax{ 2 }{ 1 }{2}% \TestMax{1}{1}{1}% \TestMax{\z@}{\@ne}{1}% \TestMax{\@ne}{\m@ne}{1}% %<*etex> \TestMax{1+2}{3+4}{7}% % \end{qstest} \begin{qstest}{cmp}{cmp}% \TestCmp{0}{0}{0}% \TestCmp{-21}{17}{-1}% \TestCmp{3}{4}{-1}% \TestCmp{-10}{-10}{0}% \TestCmp{-10}{-11}{1}% \TestCmp{100}{5}{1}% \TestCmp{2147483647}{-2147483647}{1}% \TestCmp{-2147483647}{2147483647}{-1}% \TestCmp{2147483647}{2147483647}{0}% \TestCmp{\z@}{\@ne}{-1}% \TestCmp{\@ne}{\m@ne}{1}% \TestCmp{ 4 }{ 5 }{-1}% \TestCmp{ -3 }{ -7 }{1}% %<*etex> \TestCmp{1+2}{3+4}{-1}% % \end{qstest} \begin{qstest}{fac}{fac} \TestFac{0}{1}% \TestFac{1}{1}% \TestFac{2}{2}% \TestFac{3}{2*3}% \TestFac{4}{2*3*4}% \TestFac{5}{2*3*4*5}% \TestFac{6}{2*3*4*5*6}% \TestFac{7}{2*3*4*5*6*7}% \TestFac{8}{2*3*4*5*6*7*8}% \TestFac{9}{2*3*4*5*6*7*8*9}% \TestFac{10}{2*3*4*5*6*7*8*9*10}% \TestFac{11}{2*3*4*5*6*7*8*9*10*11}% \TestFac{12}{2*3*4*5*6*7*8*9*10*11*12}% \end{qstest} \begin{qstest}{inc}{inc}% \TestInc{0}{1}% \TestInc{1}{2}% \TestInc{-1}{0}% \TestInc{10}{11}% \TestInc{-10}{-9}% \TestInc{999}{1000}% \TestInc{-1000}{-999}% \TestInc{129}{130}% \TestInc{2147483646}{2147483647}% \TestInc{-2147483647}{-2147483646}% \end{qstest} \begin{qstest}{dec}{dec}% \TestDec{0}{-1}% \TestDec{1}{0}% \TestDec{-1}{-2}% \TestDec{10}{9}% \TestDec{-10}{-11}% \TestDec{1000}{999}% \TestDec{-999}{-1000}% \TestDec{130}{129}% \TestDec{2147483647}{2147483646}% \TestDec{-2147483646}{-2147483647}% \end{qstest} \begin{qstest}{add}{add}% \TestAdd{0}{0}{0}% \TestAdd{1}{0}{1}% \TestAdd{0}{1}{1}% \TestAdd{1}{2}{3}% \TestAdd{-1}{-1}{-2}% \TestAdd{2147483646}{1}{2147483647}% \TestAdd{-2147483647}{2147483647}{0}% \TestAdd{20}{-5}{15}% \TestAdd{-4}{-1}{-5}% \TestAdd{-1}{-4}{-5}% \TestAdd{-4}{1}{-3}% \TestAdd{-1}{4}{3}% \TestAdd{4}{-1}{3}% \TestAdd{1}{-4}{-3}% \TestAdd{-4}{-1}{-5}% \TestAdd{-1}{-4}{-5}% \TestAdd{ -4 }{ -1 }{-5}% \TestAdd{ -1 }{ -4 }{-5}% \TestAdd{ -4 }{ 1 }{-3}% \TestAdd{ -1 }{ 4 }{3}% \TestAdd{ 4 }{ -1 }{3}% \TestAdd{ 1 }{ -4 }{-3}% \TestAdd{ -4 }{ -1 }{-5}% \TestAdd{ -1 }{ -4 }{-5}% \TestAdd{876543210}{111111111}{987654321}% \TestAdd{999999999}{2}{1000000001}% %<*etex> \TestAdd{100}{50+150}{300}% \TestAdd{2147483647}{10-2147483647}{10}% % \end{qstest} \begin{qstest}{sub}{sub} \TestSub{0}{0}{0}% \TestSub{1}{0}{1}% \TestSub{1}{2}{-1}% \TestSub{-1}{-1}{0}% \TestSub{2147483646}{-1}{2147483647}% \TestSub{-2147483647}{-2147483647}{0}% \TestSub{-4}{-1}{-3}% \TestSub{-1}{-4}{3}% \TestSub{-4}{1}{-5}% \TestSub{-1}{4}{-5}% \TestSub{4}{-1}{5}% \TestSub{1}{-4}{5}% \TestSub{-4}{-1}{-3}% \TestSub{-1}{-4}{3}% \TestSub{ -4 }{ -1 }{-3}% \TestSub{ -1 }{ -4 }{3}% \TestSub{ -4 }{ 1 }{-5}% \TestSub{ -1 }{ 4 }{-5}% \TestSub{ 4 }{ -1 }{5}% \TestSub{ 1 }{ -4 }{5}% \TestSub{ -4 }{ -1 }{-3}% \TestSub{ -1 }{ -4 }{3}% \TestSub{1000000000}{2}{999999998}% \TestSub{987654321}{111111111}{876543210}% %<*etex> \TestSub{100}{50+150}{-100}% \TestSub{2147483647}{-10+2147483647}{10}% % \end{qstest} \begin{qstest}{shl}{shl} \TestShl{0}{0}% \TestShl{1}{2}% \TestShl{5621}{11242}% \TestShl{1073741823}{2147483646}% \TestShl{-1}{-2}% \TestShl{-5621}{-11242}% \end{qstest} \begin{qstest}{shr}{shr} \TestShr{0}{0}% \TestShr{1}{0}% \TestShr{2}{1}% \TestShr{3}{1}% \TestShr{4}{2}% \TestShr{5}{2}% \TestShr{6}{3}% \TestShr{7}{3}% \TestShr{8}{4}% \TestShr{9}{4}% \TestShr{10}{5}% \TestShr{11}{5}% \TestShr{12}{6}% \TestShr{13}{6}% \TestShr{14}{7}% \TestShr{15}{7}% \TestShr{16}{8}% \TestShr{17}{8}% \TestShr{18}{9}% \TestShr{19}{9}% \TestShr{20}{10}% \TestShr{21}{10}% \TestShr{22}{11}% \TestShr{11241}{5620}% \TestShr{73054202}{36527101}% \TestShr{2147483646}{1073741823}% \TestShr{-1}{0}% \TestShr{-2}{-1}% \TestShr{-3}{-1}% \TestShr{-11241}{-5620}% \end{qstest} \begin{qstest}{mul}{mul} \TestMul{0}{0}{0}% \TestMul{1}{0}{0}% \TestMul{0}{1}{0}% \TestMul{1}{1}{1}% \TestMul{3}{1}{3}% \TestMul{1}{-3}{-3}% \TestMul{-4}{-5}{20}% \TestMul{3}{7}{21}% \TestMul{7}{3}{21}% \TestMul{3}{-7}{-21}% \TestMul{7}{-3}{-21}% \TestMul{-3}{7}{-21}% \TestMul{-7}{3}{-21}% \TestMul{-3}{-7}{21}% \TestMul{-7}{-3}{21}% \TestMul{12}{11}{132}% \TestMul{999}{333}{332667}% \TestMul{1000}{4321}{4321000}% \TestMul{12345}{173955}{2147474475}% \TestMul{1073741823}{2}{2147483646}% \TestMul{2}{1073741823}{2147483646}% \TestMul{-1073741823}{2}{-2147483646}% \TestMul{2}{-1073741823}{-2147483646}% %<*etex> \TestMul{2+3}{5+7}{60}% \TestMul{2147483647}{2147483647/2147483647}{2147483647}% % \end{qstest} \begin{qstest}{sqr}{sqr} \TestSqr{0}{0}% \TestSqr{1}{1}% \TestSqr{2}{4}% \TestSqr{3}{9}% \TestSqr{4}{16}% \TestSqr{9}{81}% \TestSqr{10}{100}% \TestSqr{46340}{2147395600}% \TestSqr{-1}{1}% \TestSqr{-2}{4}% \TestSqr{-46340}{2147395600}% \end{qstest} \begin{qstest}{pow}{pow} \TestPow{-2}{0}{1}% \TestPow{-1}{0}{1}% \TestPow{0}{0}{1}% \TestPow{1}{0}{1}% \TestPow{2}{0}{1}% \TestPow{3}{0}{1}% \TestPow{-2}{1}{-2}% \TestPow{-1}{1}{-1}% \TestPow{1}{1}{1}% \TestPow{2}{1}{2}% \TestPow{3}{1}{3}% \TestPow{-2}{2}{4}% \TestPow{-1}{2}{1}% \TestPow{0}{2}{0}% \TestPow{1}{2}{1}% \TestPow{2}{2}{4}% \TestPow{3}{2}{9}% \TestPow{0}{1}{0}% \TestPow{1}{-2}{1}% \TestPow{1}{-1}{1}% \TestPow{-1}{-2}{1}% \TestPow{-1}{-1}{-1}% \TestPow{-1}{3}{-1}% \TestPow{-1}{4}{1}% \TestPow{-2}{-1}{0}% \TestPow{-2}{-2}{0}% \TestPow{2}{3}{8}% \TestPow{2}{4}{16}% \TestPow{2}{5}{32}% \TestPow{2}{6}{64}% \TestPow{2}{7}{128}% \TestPow{2}{8}{256}% \TestPow{2}{9}{512}% \TestPow{2}{10}{1024}% \TestPow{-2}{3}{-8}% \TestPow{-2}{4}{16}% \TestPow{-2}{5}{-32}% \TestPow{-2}{6}{64}% \TestPow{-2}{7}{-128}% \TestPow{-2}{8}{256}% \TestPow{-2}{9}{-512}% \TestPow{-2}{10}{1024}% \TestPow{3}{3}{27}% \TestPow{3}{4}{81}% \TestPow{3}{5}{243}% \TestPow{-3}{3}{-27}% \TestPow{-3}{4}{81}% \TestPow{-3}{5}{-243}% \TestPow{2}{30}{1073741824}% \TestPow{-3}{19}{-1162261467}% \TestPow{5}{13}{1220703125}% \TestPow{-7}{11}{-1977326743}% \end{qstest} \begin{qstest}{div}{div} \TestDiv{1}{1}{1}% \TestDiv{2}{1}{2}% \TestDiv{-2}{1}{-2}% \TestDiv{2}{-1}{-2}% \TestDiv{-2}{-1}{2}% \TestDiv{15}{2}{7}% \TestDiv{-16}{2}{-8}% \TestDiv{1}{2}{0}% \TestDiv{1}{3}{0}% \TestDiv{2}{3}{0}% \TestDiv{-2}{3}{0}% \TestDiv{2}{-3}{0}% \TestDiv{-2}{-3}{0}% \TestDiv{13}{3}{4}% \TestDiv{-13}{-3}{4}% \TestDiv{-13}{3}{-4}% \TestDiv{-6}{5}{-1}% \TestDiv{-5}{5}{-1}% \TestDiv{-4}{5}{0}% \TestDiv{-3}{5}{0}% \TestDiv{-2}{5}{0}% \TestDiv{-1}{5}{0}% \TestDiv{0}{5}{0}% \TestDiv{1}{5}{0}% \TestDiv{2}{5}{0}% \TestDiv{3}{5}{0}% \TestDiv{4}{5}{0}% \TestDiv{5}{5}{1}% \TestDiv{6}{5}{1}% \TestDiv{-5}{4}{-1}% \TestDiv{-4}{4}{-1}% \TestDiv{-3}{4}{0}% \TestDiv{-2}{4}{0}% \TestDiv{-1}{4}{0}% \TestDiv{0}{4}{0}% \TestDiv{1}{4}{0}% \TestDiv{2}{4}{0}% \TestDiv{3}{4}{0}% \TestDiv{4}{4}{1}% \TestDiv{5}{4}{1}% \TestDiv{12345}{678}{18}% \TestDiv{32372}{5952}{5}% \TestDiv{284271294}{18162}{15651}% \TestDiv{217652429}{12561}{17327}% \TestDiv{462028434}{5439}{84947}% \TestDiv{2147483647}{1000}{2147483}% \TestDiv{2147483647}{-1000}{-2147483}% \TestDiv{-2147483647}{1000}{-2147483}% \TestDiv{-2147483647}{-1000}{2147483}% \end{qstest} \begin{qstest}{mod}{mod} \TestMod{-6}{5}{4}% \TestMod{-5}{5}{0}% \TestMod{-4}{5}{1}% \TestMod{-3}{5}{2}% \TestMod{-2}{5}{3}% \TestMod{-1}{5}{4}% \TestMod{0}{5}{0}% \TestMod{1}{5}{1}% \TestMod{2}{5}{2}% \TestMod{3}{5}{3}% \TestMod{4}{5}{4}% \TestMod{5}{5}{0}% \TestMod{6}{5}{1}% \TestMod{-5}{4}{3}% \TestMod{-4}{4}{0}% \TestMod{-3}{4}{1}% \TestMod{-2}{4}{2}% \TestMod{-1}{4}{3}% \TestMod{0}{4}{0}% \TestMod{1}{4}{1}% \TestMod{2}{4}{2}% \TestMod{3}{4}{3}% \TestMod{4}{4}{0}% \TestMod{5}{4}{1}% \TestMod{-6}{-5}{-1}% \TestMod{-5}{-5}{0}% \TestMod{-4}{-5}{-4}% \TestMod{-3}{-5}{-3}% \TestMod{-2}{-5}{-2}% \TestMod{-1}{-5}{-1}% \TestMod{0}{-5}{0}% \TestMod{1}{-5}{-4}% \TestMod{2}{-5}{-3}% \TestMod{3}{-5}{-2}% \TestMod{4}{-5}{-1}% \TestMod{5}{-5}{0}% \TestMod{6}{-5}{-4}% \TestMod{-5}{-4}{-1}% \TestMod{-4}{-4}{0}% \TestMod{-3}{-4}{-3}% \TestMod{-2}{-4}{-2}% \TestMod{-1}{-4}{-1}% \TestMod{0}{-4}{0}% \TestMod{1}{-4}{-3}% \TestMod{2}{-4}{-2}% \TestMod{3}{-4}{-1}% \TestMod{4}{-4}{0}% \TestMod{5}{-4}{-3}% \TestMod{2147483647}{1000}{647}% \TestMod{2147483647}{-1000}{-353}% \TestMod{-2147483647}{1000}{353}% \TestMod{-2147483647}{-1000}{-647}% \TestMod{ 0 }{ 4 }{0}% \TestMod{ 1 }{ 4 }{1}% \TestMod{ -1 }{ 4 }{3}% \TestMod{ 0 }{ -4 }{0}% \TestMod{ 1 }{ -4 }{-3}% \TestMod{ -1 }{ -4 }{-1}% %<*etex> \TestMod{1+2}{1+3}{3}% \TestMod{1-2}{1+3}{3}% \TestMod{1-2}{1-4}{-1}% \TestMod{1+2}{1-4}{0}% \TestMod{1+2}{1-5}{-1}% % \end{qstest} % %<*test2> \newcommand*{\TestError}[2]{% \begingroup \expandafter\def\csname IntCalcError:#1\endcsname{}% \Expect*{#2}{0}% \expandafter\def\csname IntCalcError:#1\endcsname{ERROR}% \Expect*{#2}{0ERROR }% \endgroup } \begin{qstest}{error}{error} \TestError{FacNegative}{\intcalcFac{-1}}% \TestError{FacNegative}{\intcalcFac{-2147483647}}% \TestError{FacOverflow}{\intcalcFac{13}}% \TestError{FacOverflow}{\intcalcFac{2147483647}}% \TestError{DivisionByZero}{\intcalcPow{0}{-1}}% \TestError{DivisionByZero}{\intcalcDiv{1}{0}}% \TestError{DivisionByZero}{\intcalcMod{1}{0}}% \TestError{DivisionByZero}{\IntCalcDiv1!0!}% \TestError{DivisionByZero}{\IntCalcMod1!0!}% \end{qstest} % %<*test2|test4> \begin{document} \end{document} % % \end{macrocode} % % \section{Installation} % % \subsection{Download} % % \paragraph{Package.} This package is available on % CTAN\footnote{\url{ftp://ftp.ctan.org/tex-archive/}}: % \begin{description} % \item[\CTAN{macros/latex/contrib/oberdiek/intcalc.dtx}] The source file. % \item[\CTAN{macros/latex/contrib/oberdiek/intcalc.pdf}] Documentation. % \end{description} % % % \paragraph{Bundle.} All the packages of the bundle `oberdiek' % are also available in a TDS compliant ZIP archive. There % the packages are already unpacked and the documentation files % are generated. The files and directories obey the TDS standard. % \begin{description} % \item[\CTAN{install/macros/latex/contrib/oberdiek.tds.zip}] % \end{description} % \emph{TDS} refers to the standard ``A Directory Structure % for \TeX\ Files'' (\CTAN{tds/tds.pdf}). Directories % with \xfile{texmf} in their name are usually organized this way. % % \subsection{Bundle installation} % % \paragraph{Unpacking.} Unpack the \xfile{oberdiek.tds.zip} in the % TDS tree (also known as \xfile{texmf} tree) of your choice. % Example (linux): % \begin{quote} % |unzip oberdiek.tds.zip -d ~/texmf| % \end{quote} % % \paragraph{Script installation.} % Check the directory \xfile{TDS:scripts/oberdiek/} for % scripts that need further installation steps. % Package \xpackage{attachfile2} comes with the Perl script % \xfile{pdfatfi.pl} that should be installed in such a way % that it can be called as \texttt{pdfatfi}. % Example (linux): % \begin{quote} % |chmod +x scripts/oberdiek/pdfatfi.pl|\\ % |cp scripts/oberdiek/pdfatfi.pl /usr/local/bin/| % \end{quote} % % \subsection{Package installation} % % \paragraph{Unpacking.} The \xfile{.dtx} file is a self-extracting % \docstrip\ archive. The files are extracted by running the % \xfile{.dtx} through \plainTeX: % \begin{quote} % \verb|tex intcalc.dtx| % \end{quote} % % \paragraph{TDS.} Now the different files must be moved into % the different directories in your installation TDS tree % (also known as \xfile{texmf} tree): % \begin{quote} % \def\t{^^A % \begin{tabular}{@{}>{\ttfamily}l@{ $\rightarrow$ }>{\ttfamily}l@{}} % intcalc.sty & tex/generic/oberdiek/intcalc.sty\\ % intcalc.pdf & doc/latex/oberdiek/intcalc.pdf\\ % test/intcalc-test1.tex & doc/latex/oberdiek/test/intcalc-test1.tex\\ % test/intcalc-test2.tex & doc/latex/oberdiek/test/intcalc-test2.tex\\ % test/intcalc-test3.tex & doc/latex/oberdiek/test/intcalc-test3.tex\\ % test/intcalc-test4.tex & doc/latex/oberdiek/test/intcalc-test4.tex\\ % intcalc.dtx & source/latex/oberdiek/intcalc.dtx\\ % \end{tabular}^^A % }^^A % \sbox0{\t}^^A % \ifdim\wd0>\linewidth % \begingroup % \advance\linewidth by\leftmargin % \advance\linewidth by\rightmargin % \edef\x{\endgroup % \def\noexpand\lw{\the\linewidth}^^A % }\x % \def\lwbox{^^A % \leavevmode % \hbox to \linewidth{^^A % \kern-\leftmargin\relax % \hss % \usebox0 % \hss % \kern-\rightmargin\relax % }^^A % }^^A % \ifdim\wd0>\lw % \sbox0{\small\t}^^A % \ifdim\wd0>\linewidth % \ifdim\wd0>\lw % \sbox0{\footnotesize\t}^^A % \ifdim\wd0>\linewidth % \ifdim\wd0>\lw % \sbox0{\scriptsize\t}^^A % \ifdim\wd0>\linewidth % \ifdim\wd0>\lw % \sbox0{\tiny\t}^^A % \ifdim\wd0>\linewidth % \lwbox % \else % \usebox0 % \fi % \else % \lwbox % \fi % \else % \usebox0 % \fi % \else % \lwbox % \fi % \else % \usebox0 % \fi % \else % \lwbox % \fi % \else % \usebox0 % \fi % \else % \lwbox % \fi % \else % \usebox0 % \fi % \end{quote} % If you have a \xfile{docstrip.cfg} that configures and enables \docstrip's % TDS installing feature, then some files can already be in the right % place, see the documentation of \docstrip. % % \subsection{Refresh file name databases} % % If your \TeX~distribution % (\teTeX, \mikTeX, \dots) relies on file name databases, you must refresh % these. For example, \teTeX\ users run \verb|texhash| or % \verb|mktexlsr|. % % \subsection{Some details for the interested} % % \paragraph{Attached source.} % % The PDF documentation on CTAN also includes the % \xfile{.dtx} source file. It can be extracted by % AcrobatReader 6 or higher. Another option is \textsf{pdftk}, % e.g. unpack the file into the current directory: % \begin{quote} % \verb|pdftk intcalc.pdf unpack_files output .| % \end{quote} % % \paragraph{Unpacking with \LaTeX.} % The \xfile{.dtx} chooses its action depending on the format: % \begin{description} % \item[\plainTeX:] Run \docstrip\ and extract the files. % \item[\LaTeX:] Generate the documentation. % \end{description} % If you insist on using \LaTeX\ for \docstrip\ (really, % \docstrip\ does not need \LaTeX), then inform the autodetect routine % about your intention: % \begin{quote} % \verb|latex \let\install=y\input{intcalc.dtx}| % \end{quote} % Do not forget to quote the argument according to the demands % of your shell. % % \paragraph{Generating the documentation.} % You can use both the \xfile{.dtx} or the \xfile{.drv} to generate % the documentation. The process can be configured by the % configuration file \xfile{ltxdoc.cfg}. For instance, put this % line into this file, if you want to have A4 as paper format: % \begin{quote} % \verb|\PassOptionsToClass{a4paper}{article}| % \end{quote} % An example follows how to generate the % documentation with pdf\LaTeX: % \begin{quote} %\begin{verbatim} %pdflatex intcalc.dtx %makeindex -s gind.ist intcalc.idx %pdflatex intcalc.dtx %makeindex -s gind.ist intcalc.idx %pdflatex intcalc.dtx %\end{verbatim} % \end{quote} % % \begin{History} % \begin{Version}{2007/09/09 v1.0} % \item % First version. % \end{Version} % \begin{Version}{2007/09/27 v1.1} % \item % \cs{intcalcNum} added. % \item % \cs{intcalcShl} and \cs{intcalcShr} allow negative numbers. % The sign is preserved. % \item % Reuse \cs{@gobble} instead of own macro \cs{IntCalc@Gobble}. % \item % Small fixes. % \item % Shorter internal prefix. % \item % Some programmer's interface. % \end{Version} % \end{History} % % \PrintIndex % % \Finale \endinput