• No results found

Index of /CTAN/macros/latex/contrib/intcalc

N/A
N/A
Protected

Academic year: 2022

Share "Index of /CTAN/macros/latex/contrib/intcalc"

Copied!
48
0
0

Full text

(1)

The intcalc package

Heiko Oberdiek

2019/12/15 v1.3

Abstract

This package provides expandable arithmetic operations with integers.

Contents

1 Documentation 2

1.1 Introduction. . . 2

1.2 Conditions. . . 2

1.2.1 Preconditions . . . 2

1.2.2 Postconditions . . . 3

1.3 Error handling . . . 3

1.4 Operations . . . 3

1.4.1 Num . . . 3

1.4.2 Inv,Abs, Sgn . . . 4

1.4.3 Min,Max,Cmp . . . 4

1.4.4 Inc,Dec,Add, Sub . . . 5

1.4.5 Shl,Shr . . . 5

1.4.6 Mul,Sqr,Fac,Pow . . . 6

1.4.7 Div, Mod . . . 6

1.5 Interface for programmer . . . 7

2 Implementation 8 2.1 Reload check and package identification . . . 8

2.2 Catcodes . . . 9

2.3 Macros independent ofε-TEX . . . 10

2.3.1 Abs,Sgn . . . 10

2.3.2 Min,Max,Cmp . . . 10

2.3.3 Fac. . . 11

2.4 Implementation based on ε-TEX. . . 11

2.4.1 Num . . . 11

2.4.2 Inv,Abs, Sgn . . . 12

2.4.3 Min,Max,Cmp . . . 12

2.4.4 Inc,Dec . . . 12

2.4.5 Add,Sub . . . 13

2.4.6 Shl,Shr . . . 13

2.4.7 Mul,Sqr,Fac . . . 14

2.4.8 Pow . . . 14

2.4.9 Div, Mod . . . 15

2.5 Implementation withoutε-TEX . . . 18

Please report any issues athttps://github.com/ho-tex/intcalc/issues

(2)

2.5.1 Num . . . 18

2.5.2 Inv,Abs, Sgn . . . 18

2.5.3 Min,Max,Cmp . . . 19

2.5.4 Inc,Dec . . . 19

2.5.5 Add,Sub . . . 21

2.5.6 Shl,Shr . . . 29

2.5.7 \InCa@Tim . . . 31

2.5.8 Mul . . . 34

2.5.9 Sqr,Fac . . . 36

2.5.10 Pow . . . 36

2.5.11 Div . . . 38

2.5.12 Mod . . . 42

2.5.13 Help macros. . . 44

3 Installation 44 3.1 Download . . . 44

3.2 Bundle installation . . . 44

3.3 Package installation . . . 44

3.4 Refresh file name databases . . . 45

3.5 Some details for the interested . . . 45

4 History 45 [2007/09/09 v1.0] . . . 45

[2007/09/27 v1.1] . . . 46

[2016/05/16 v1.2] . . . 46

[2019/12/15 v1.3] . . . 46

5 Index 46

1 Documentation

1.1 Introduction

Package 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 pa- rameter and return the integer result. The macro name is a three letter operation name prefixed by the package name, e.g. \intcalcAdd{10}{43} returns53.

The macros are fully expandable, exactly two expansion steps generate the result. Therefore the operations may be used nearly everywhere in TEX, even inside\number,\csname, file names, or other expandable contexts.

The package contains two implementations of the operations. Ifε-TEX is de- tected then the macros are implemented using its features (\numexpr). Otherwise the slower implementation withoutε-TEX’s help is choosen.

1.2 Conditions

1.2.1 Preconditions

• Arguments can be anything that TEX interprets as “number”. Examples:

plain numbers, count or length register, macros that expands to a number.

(3)

• 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.

• The argument may be expressions that \numexpr understands if ε-TEX is available.

• The resulting number must fit in the allowed range.

1.2.2 Postconditions

Additional properties of the macros apart from calculating a correct result (of course,):

• The macros are fully expandable. Thus they can be used inside \edef,

\csname, after\number, for example.

• Furthermore exactly two expansion steps calculate the result.

• 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\numbergenerates. And the tokens (minus sign, digits) have catcode 12 (other).

• 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ε-TEX expres- sions. This strategy avoids multiple evaluations of such arguments.

1.3 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 un- defined control sequence is called and causes a TeX error message, example:

\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.

1.4 Operations

Some definition equations below use the function Int that converts a real number to an integer. The number is truncated that means rounding to zero:

Int(x) :=

(bxc ifx≥0 dxe otherwise

(4)

1.4.1 Num

\intcalcNum{hxi}

Macro\intcalcNumconverts its argument to a normalized integer number without unnecessary leading zeros or signs. The result matches the regular expression:

0|-?[1-9][0-9]*

1.4.2 Inv,Abs,Sgn

\intcalcInv{hxi}

Macro\intcalcInvswitches the sign.

Inv(x) :=−x

\intcalcAbs{hxi}

Macro\intcalcAbsreturns the absolute value of integerhxi.

Abs(x) :=|x|

\intcalcSgn{hxi}

Macro\intcalcSgnencodes the sign ofhxias number.

Sgn(x) :=





−1 ifx <0 0 ifx= 0 1 ifx >0

These return values can easily be distinguished by\ifcase:

\ifcase\intcalcSgn{<x>}

$x=0$

\or

$x>0$

\else

$x<0$

\fi

1.4.3 Min,Max, Cmp

\intcalcMin{hxi} {hyi}

Macro\intcalcMinreturns the smaller of the two integers.

Min(x, y) :=

(x ifx < y y otherwise

(5)

\intcalcMax{hxi} {hyi}

Macro\intcalcMaxreturns the larger of the two integers.

Max(x, y) :=

(x ifx > y y otherwise

\intcalcCmp{hxi} {hyi}

Macro\intcalcCmpencodes the comparison result as number:

Cmp(x, y) :=





−1 ifx < y 0 ifx=y 1 ifx > y

These values can be distinguished by\ifcase:

\ifcase\intcalcCmp{<x>}{<y>}

$x=y$

\or

$x>y$

\else

$x<y$

\fi

1.4.4 Inc,Dec, Add, Sub

\intcalcInc{hxi}

Macro\intcalcIncincrementshxiby one.

Inc(x) :=x+ 1

\intcalcDec{hxi}

Macro\intcalcDecdecrementshxiby one.

Dec(x) :=x−1

\intcalcAdd{hxi} {hyi}

Macro\intcalcAddadds the two numbers.

Add(x, y) :=x+y

\intcalcSub{hxi} {hyi}

Macro\intcalcSubcalculates the difference.

Sub(x, y) :=x−y

(6)

1.4.5 Shl, Shr

\intcalcShl{hxi}

Macro \intcalcShl implements shifting to the left that means the number is multiplied by two. Overflow is possible. The sign is preserved.

Shl(x) :=x∗2

\intcalcShr{hxi}

Macro \intcalcShr implements shifting to the right. That is equivalent to an integer division by two. The sign is preserved.

Shr(x) := Int(x/2) 1.4.6 Mul,Sqr, Fac,Pow

\intcalcMul{hxi} {hyi}

Macro\intcalcMulcalculates the product ofhxiandhyi.

Mul(x, y) :=x∗y

\intcalcSqr{hxi}

Macro\intcalcSqrreturns the square product.

Sqr(x) :=x2

\intcalcFac{hxi}

Macro\intcalcFac returns the factorial of hxi. Negative numbers are not per- mitted.

Fac(x) :=x! forx≥0 (0! = 1)

\intcalcPow{hxi} {hyi}

Macro \intcalcPow calculates the value of hxi to the power of hyi. The error

“division by zero” is thrown ifhxiis zero andhyiis negative. permitted:

Pow(x, y) := Int(xy) forx6= 0 ory≥0 (00= 1)

(7)

1.4.7 Div, Mod

\intcalcDiv{hxi} {hyi}

Macro\intcalcDivperforms an integer division. Argumenthyimust not be zero.

Div(x, y) := Int(x/y) fory6= 0

\intcalcMod{hxi} {hyi}

Macro\intcalcMod gets the remainder of the integer division. The sign follows the divisorhyi. Argument hyimust not be zero.

Mod(x, y) :=x%y fory6= 0 The result ranges:

−|y|<Mod(x, y)≤0 fory <0 0≤Mod(x, y)< y fory≥0

1.5 Interface for programmer

If the programmer can ensure some more properties about the arguments of the operations, then the following macros are a little more efficient.

In general numbers must obey the following constraints:

• Plain number: digit tokens only, no command tokens.

• Non-negative. Signs are forbidden.

• Arguments and the result must fit in range0..2147483647.

• Delimited by exclamation mark. Curly braces around the number are not allowed and will break the code.

\IntCalcInchnumberi!

Incrementation, range: 0..2147483646.

\IntCalcDechnumberi!

Decrementation, range: 1..2147483647.

\IntCalcAddhnumber Ai!hnumber Bi! Addition,A≥B.

\IntCalcSubhnumber Ai!hnumber Bi! Subtraction,A≥B.

\IntCalcShlhnumberi!

Left shift (multiplication with two), range: 0..1073741823.

(8)

\IntCalcShrhnumberi!

Right shift (integer division by two).

\IntCalcMulhnumber Ai!hnumber Bi! Multiplication,A≥B.

\IntCalcDivhnumber Ai!hnumber Bi! Division operation.

\IntCalcModhnumber Ai!hnumber Bi! Modulo operation.

2 Implementation

1h*packagei

2.1 Reload check and package identification

Reload check, especially if the package is not used with LATEX.

2\begingroup\catcode61\catcode48\catcode32=10\relax%

3 \catcode13=5 % ^^M

4 \endlinechar=13 %

5 \catcode35=6 % #

6 \catcode39=12 % ’

7 \catcode44=12 % ,

8 \catcode45=12 % -

9 \catcode46=12 % .

10 \catcode58=12 % :

11 \catcode64=11 % @

12 \catcode123=1 % {

13 \catcode125=2 % }

14 \expandafter\let\expandafter\x\csname [email protected]\endcsname

15 \ifx\x\relax % plain-TeX, first loading

16 \else

17 \def\empty{}%

18 \ifx\x\empty % LaTeX, first loading,

19 % variable is initialized, but \ProvidesPackage not yet seen

20 \else

21 \expandafter\ifx\csname PackageInfo\endcsname\relax

22 \def\x#1#2{%

23 \immediate\write-1{Package #1 Info: #2.}%

24 }%

25 \else

26 \def\x#1#2{\PackageInfo{#1}{#2, stopped}}%

27 \fi

28 \x{intcalc}{The package is already loaded}%

29 \aftergroup\endinput

30 \fi

31 \fi

32\endgroup%

(9)

Package identification:

33\begingroup\catcode61\catcode48\catcode32=10\relax%

34 \catcode13=5 % ^^M

35 \endlinechar=13 %

36 \catcode35=6 % #

37 \catcode39=12 % ’

38 \catcode40=12 % (

39 \catcode41=12 % )

40 \catcode44=12 % ,

41 \catcode45=12 % -

42 \catcode46=12 % .

43 \catcode47=12 % /

44 \catcode58=12 % :

45 \catcode64=11 % @

46 \catcode91=12 % [

47 \catcode93=12 % ]

48 \catcode123=1 % {

49 \catcode125=2 % }

50 \expandafter\ifx\csname ProvidesPackage\endcsname\relax

51 \def\x#1#2#3[#4]{\endgroup

52 \immediate\write-1{Package: #3 #4}%

53 \xdef#1{#4}%

54 }%

55 \else

56 \def\x#1#2[#3]{\endgroup

57 #2[{#3}]%

58 \ifx#1\@undefined

59 \xdef#1{#3}%

60 \fi

61 \ifx#1\relax

62 \xdef#1{#3}%

63 \fi

64 }%

65 \fi

66\expandafter\x\csname [email protected]\endcsname

67\ProvidesPackage{intcalc}%

68 [2019/12/15 v1.3 Expandable calculations with integers (HO)]%

2.2 Catcodes

69\begingroup\catcode61\catcode48\catcode32=10\relax%

70 \catcode13=5 % ^^M

71 \endlinechar=13 %

72 \catcode123=1 % {

73 \catcode125=2 % }

74 \catcode64=11 % @

75 \def\x{\endgroup

76 \expandafter\edef\csname InCa@AtEnd\endcsname{%

77 \endlinechar=\the\endlinechar\relax

78 \catcode13=\the\catcode13\relax

79 \catcode32=\the\catcode32\relax

80 \catcode35=\the\catcode35\relax

81 \catcode61=\the\catcode61\relax

82 \catcode64=\the\catcode64\relax

83 \catcode123=\the\catcode123\relax

84 \catcode125=\the\catcode125\relax

85 }%

86 }%

(10)

87\x\catcode61\catcode48\catcode32=10\relax%

88\catcode13=5 % ^^M

89\endlinechar=13 %

90\catcode35=6 % #

91\catcode64=11 % @

92\catcode123=1 % {

93\catcode125=2 % }

94\def\TMP@EnsureCode#1#2{%

95 \edef\InCa@AtEnd{%

96 \InCa@AtEnd

97 \catcode#1=\the\catcode#1\relax

98 }%

99 \catcode#1=#2\relax

100}

101\TMP@EnsureCode{33}{12}% !

102\TMP@EnsureCode{40}{12}% (

103\TMP@EnsureCode{41}{12}% )

104\TMP@EnsureCode{42}{12}% *

105\TMP@EnsureCode{43}{12}% +

106\TMP@EnsureCode{45}{12}% -

107\TMP@EnsureCode{47}{12}% /

108\TMP@EnsureCode{58}{11}% : (letter!)

109\TMP@EnsureCode{60}{12}% <

110\TMP@EnsureCode{62}{12}% >

111\TMP@EnsureCode{63}{14}% ? (comment!)

112\edef\InCa@AtEnd{\InCa@AtEnd\noexpand\endinput}

113\begingroup\expandafter\expandafter\expandafter\endgroup

114\expandafter\ifx\csname InCa@TestMode\endcsname\relax

115\else

116 \catcode63=9 % ? (ignore)

117\fi

118? \let\InCa@@TestMode\InCa@TestMode

2.3 Macros independent of ε -TEX

2.3.1 Abs,Sgn

\InCa@Abs

119\def\InCa@Abs#1#2!{%

120 \ifx#1-%

121 #2%

122 \else

123 #1#2%

124 \fi

125}

\InCa@Sgn

126\def\InCa@Sgn#1#2!{%

127 \ifx#1-%

128 -1%

129 \else

130 \ifx#10%

131 0%

132 \else

133 1%

134 \fi

135 \fi

136}

(11)

2.3.2 Min,Max, Cmp

\InCa@Min

137\def\InCa@Min#1!#2!{%

138 \ifnum#1<#2 %

139 #1%

140 \else

141 #2%

142 \fi

143}

\InCa@Max

144\def\InCa@Max#1!#2!{%

145 \ifnum#1>#2 %

146 #1%

147 \else

148 #2%

149 \fi

150}

\InCa@Cmp

151\def\InCa@Cmp#1!#2!{%

152 \ifnum#1=#2 %

153 0%

154 \else

155 \ifnum#1<#2 %

156 -%

157 \fi

158 1%

159 \fi

160} 2.3.3 Fac

\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.

161\def\InCa@Fac#1!{%

162 \ifcase#1 1% 0!

163 \or 1% 1!

164 \or 2% 2!

165 \or 6% 3!

166 \or 24% 4!

167 \or 120% 5!

168 \or 720% 6!

169 \or 5040% 7!

170 \or 40320% 8!

171 \or 362880% 9!

172 \or 3628800% 10!

173 \or 39916800% 11!

174 \or 479001600% 12!

175 \else

176 \ifnum#1<\z@

177 0\IntCalcError:FacNegative%

178 \else

179 0\IntCalcError:FacOverflow%

180 \fi

181 \fi

182}

(12)

2.4 Implementation based on ε -TEX

Only\numexpris used from ε-TEX.

183\begingroup\expandafter\expandafter\expandafter\endgroup

184\expandafter\ifx\csname numexpr\endcsname\relax

185\else 2.4.1 Num

\intcalcNum

186 \def\intcalcNum#1{%

187 \the\numexpr#1\relax

188 }%

2.4.2 Inv,Abs,Sgn

\intcalcInv

189 \def\intcalcInv#1{%

190 \number-\intcalcNum{#1} %

191 }%

\intcalcAbs

192 \def\intcalcAbs#1{%

193 \number\expandafter\InCa@Abs\the\numexpr#1! %

194 }%

\intcalcSgn

195 \def\intcalcSgn#1{%

196 \number\expandafter\InCa@Sgn\the\numexpr#1! %

197 }%

2.4.3 Min,Max, Cmp

\intcalcMin

198 \def\intcalcMin#1#2{%

199 \number\expandafter\InCa@Min

200 \the\numexpr#1\expandafter!%

201 \the\numexpr#2! %

202 }%

\intcalcMax

203 \def\intcalcMax#1#2{%

204 \number\expandafter\InCa@Max

205 \the\numexpr#1\expandafter!%

206 \the\numexpr#2! %

207 }%

\intcalcCmp

208 \def\intcalcCmp#1#2{%

209 \number\expandafter\InCa@Cmp

210 \the\numexpr#1\expandafter!\the\numexpr#2! %

211 }%

(13)

2.4.4 Inc,Dec

\intcalcInc

212 \def\intcalcInc#1{%

213 \the\numexpr#1+1\relax

214 }%

\intcalcDec

215 \def\intcalcDec#1{%

216 \the\numexpr#1-1\relax

217 }%

\IntCalcInc

218 \def\IntCalcInc#1!{%

219 \the\numexpr#1+1\relax

220 }%

\IntCalcDec

221 \def\IntCalcDec#1!{%

222 \the\numexpr#1-1\relax

223 }%

2.4.5 Add,Sub

\intcalcAdd

224 \def\intcalcAdd#1#2{%

225 \the\numexpr#1+(#2)\relax

226 }%

\intcalcSub

227 \def\intcalcSub#1#2{%

228 \the\numexpr#1-(#2)\relax

229 }%

\IntCalcAdd

230 \def\IntCalcAdd#1!#2!{%

231 \the\numexpr#1+#2\relax

232 }%

\IntCalcSub

233 \def\IntCalcSub#1!#2!{%

234 \the\numexpr#1-#2\relax

235 }%

2.4.6 Shl, Shr

\intcalcShl

236 \def\intcalcShl#1{%

237 \the\numexpr(#1)*2\relax

238 }%

\intcalcShr

239 \def\intcalcShr#1{%

240 \number\expandafter\InCa@Shr\the\numexpr#1! %

241 }%

(14)

\IntCalcShl

242 \def\IntCalcShl#1!{%

243 \the\numexpr#1*2\relax

244 }%

\IntCalcShr

245 \def\IntCalcShr#1!{%

246 \the\numexpr\ifodd#1 (#1-1)\else#1\fi/2\relax

247 }%

\InCa@Shr

248 \def\InCa@Shr#1#2!{%

249 \ifx#1-%

250 -\InCa@Shr#2!%

251 \else

252 \ifodd#1#2 %

253 \the\numexpr(#1#2-1)/2\relax

254 \else

255 \the\numexpr#1#2/2\relax

256 \fi

257 \fi

258 }%

2.4.7 Mul,Sqr, Fac

\intcalcMul

259 \def\intcalcMul#1#2{%

260 \the\numexpr(#1)*(#2)\relax

261 }%

\IntCalcMul

262 \def\IntCalcMul#1!#2!{%

263 \the\numexpr#1*#2\relax

264 }%

\intcalcSqr

265 \def\intcalcSqr#1{%

266 \number\expandafter\InCa@Sqr\the\numexpr#1! %

267 }%

\InCa@Sqr

268 \def\InCa@Sqr#1!{%

269 \the\numexpr#1*#1\relax

270 }%

\intcalcFac

271 \def\intcalcFac#1{%

272 \number\expandafter\InCa@Fac\the\numexpr#1! %

273 }%

2.4.8 Pow

\intcalcPow

274 \def\intcalcPow#1#2{%

275 \number\expandafter\InCa@Pow

276 \the\numexpr#1\expandafter!%

277 \the\numexpr#2! %

278 }%

(15)

\InCa@Pow

279 \def\InCa@Pow#1#2!#3#4!{%

280 \ifcase#3#4 % power = 0

281 1%

282 \or % power = 1

283 #1#2%

284 \or % power = 2

285 \the\numexpr#1#2*#1#2\relax

286 \else

287 \ifcase#1#2 % basis = 0, power <> 0

288 0%

289 \ifx#3-% power < 0

290 0\IntCalcError:DivisionByZero%

291 \fi

292 \or

293 1% basis = 1

294 \else

295 \ifnum#1#2=\m@ne % basis = -1

296 \ifodd#3#4 %

297 -%

298 \fi

299 1%

300 \else % |basis| > 1

301 \ifx#3-% power < 0

302 0%

303 \else % power > 2

304 \InCa@PowRec#1#2!#3#4!1!%

305 \fi

306 \fi

307 \fi

308 \fi

309 }%

\InCa@PowRec 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 }

310 \def\InCa@PowRec#1!#2!#3!{%

311 \ifnum#2=\@ne

312 \the\numexpr#1*#3\relax

313 \else

314 \ifodd#2 %

315 \expandafter\InCa@PowRec

316 \the\numexpr#1*#1\expandafter!%

317 \the\numexpr(#2-1)/2\expandafter!%

318 \the\numexpr#1*#3\expandafter\expandafter\expandafter!%

319 \else

320 \expandafter\InCa@PowRec

(16)

321 \the\numexpr#1*#1\expandafter!%

322 \the\numexpr(#2-1)/2\expandafter!%

323 \number#3\expandafter\expandafter\expandafter!%

324 \fi

325 \fi

326 }%

2.4.9 Div, Mod

TEX’s\dividetruncates,ε-TEX’s\numexprrounds the result of a division. The rounding method is called “Symmetric Arithmetic Rounding” or “Round-Half-Up”

(“Kaufm¨annisches Runden” in German):

1 = 3 divide 2 = 1.5 = numexpr 3/2 = 2 -1 = -3 divide 2 = -1.5 = numexpr -3/2 = -2

Macro \intcalcDiv follows TEX and truncates. The calculation is done by the following formula:

Div(X, Y) = (X−(Y −1)/2)/Y forX, Y >0 (1) The operator ‘/’ is\numexpr’s division.

\intcalcDiv

327 \def\intcalcDiv#1#2{%

328 \number\expandafter\InCa@Div

329 \the\numexpr#1\expandafter!%

330 \the\numexpr#2! %

331 }%

\InCa@Div

332 \def\InCa@Div#1!#2!{%

333 \ifcase#2 %

334 0\IntCalcError:DivisionByZero%

335 \else

336 \ifcase#1 %

337 0%

338 \else

339 \expandafter\InCa@@Div

340 \romannumeral 0%

341 \ifnum#1<\z@

342 \expandafter-\number-#1%

343 \else

344 \expandafter+\number#1%

345 \fi

346 \expandafter!%

347 \romannumeral 0%

348 \ifnum#2<\z@

349 \expandafter-\number-#2%

350 \else

351 \expandafter+\number#2%

352 \fi

353 !%

354 \fi

355 \fi

356 }%

\IntCalcDiv

(17)

357 \def\InCa@Temp#1{%

358 \def\IntCalcDiv##1!##2!{%

359 \number

360 \ifcase##2 %

361 0\IntCalcError:DivisionByZero%

362 \else

363 \ifcase##1 %

364 0%

365 \else

366 \the\numexpr(##1-(##2-1)/2)/##2\relax

367 \fi

368 \fi

369 #1%

370 }%

371 }%

372 \InCa@Temp{ }%

\InCa@@Div

373 \def\InCa@@Div#1#2!#3#4!{%

374 #1#3%

375 \the\numexpr(#2-(#4-1)/2)/#4\relax

376 }%

\intcalcMod

377 \def\intcalcMod#1#2{%

378 \number\expandafter\InCa@Mod

379 \the\numexpr#1\expandafter!%

380 \the\numexpr#2! %

381 }%

\InCa@Mod

382 \def\InCa@Mod#1!#2!{%

383 \ifcase#2 %

384 0\IntCalcError:DivisionByZero%

385 \else

386 \ifcase#1 %

387 0%

388 \else

389 \expandafter\InCa@@Mod

390 \romannumeral 0%

391 \ifnum#1<\z@

392 \expandafter-\number-#1%

393 \else

394 \expandafter+\number#1%

395 \fi

396 \expandafter!%

397 \romannumeral 0%

398 \ifnum#2<\z@

399 \expandafter-\number-#2%

400 \else

401 \expandafter+\number#2%

402 \fi

403 !%

404 \fi

405 \fi

406 }%

\IntCalcMod

(18)

407 \def\InCa@Temp#1{%

408 \def\IntCalcMod##1!##2!{%

409 \number

410 \ifcase##2 %

411 0\IntCalcError:DivisionByZero%

412 \else

413 \ifcase##1 %

414 0%

415 \else

416 \the\numexpr##1-(##1-(##2-1)/2)/##2*##2\relax

417 \fi

418 \fi

419 #1%

420 }%

421 }%

422 \InCa@Temp{ }%

\InCa@@Mod

423 \def\InCa@@Mod#1#2!#3#4!{%

424 \if#3+%

425 \if#1+%

426 \the\numexpr#2-\InCa@@Div+#2!+#4!*#4\relax

427 \else

428 \expandafter\InCa@ModX

429 \the\numexpr-#2+\InCa@@Div+#2!+#4!*#4!#4!%

430 \fi

431 \else

432 -%

433 \if#1+%

434 \expandafter\InCa@ModX

435 \the\numexpr-#2+\InCa@@Div+#2!+#4!*#4!#4!%

436 \else

437 \the\numexpr#2-\InCa@@Div+#2!+#4!*#4\relax

438 \fi

439 \fi

440 }%

\InCa@ModX

441 \def\InCa@ModX#1!#2!{%

442 \ifcase#1 %

443 0%

444 \else

445 \the\numexpr#1+#2\relax

446 \fi

447 }%

448 \expandafter\InCa@AtEnd

449\fi%

2.5 Implementation without ε -TEX

2.5.1 Num

\intcalcNum

450\def\intcalcNum#1{%

451 \number\expandafter\InCa@FirstOfOne\number#1! %

452}

(19)

2.5.2 Inv,Abs,Sgn

\intcalcInv

453\def\intcalcInv#1{%

454 \number\expandafter\InCa@FirstOfOne\number-#1! %

455}

\InCa@FirstOfOne

456\def\InCa@FirstOfOne#1!{#1}

\intcalcAbs

457\def\intcalcAbs#1{%

458 \number\expandafter\InCa@Abs\number#1! %

459}

\intcalcSgn

460\def\intcalcSgn#1{%

461 \number\expandafter\InCa@Sgn\number#1! %

462}

2.5.3 Min,Max, Cmp

\intcalcMin

463\def\intcalcMin#1#2{%

464 \number\expandafter\InCa@Min

465 \number\number#1\expandafter!\number#2! %

466}

\intcalcMax

467\def\intcalcMax#1#2{%

468 \number\expandafter\InCa@Max

469 \number\number#1\expandafter!\number#2! %

470}

\intcalcCmp

471\def\intcalcCmp#1#2{%

472 \number\expandafter\InCa@Cmp

473 \number\number#1\expandafter!\number#2! %

474}%

2.5.4 Inc,Dec

\intcalcInc

475\def\intcalcInc#1{%

476 \number\expandafter\InCa@IncSwitch\number#1! %

477}

\InCa@IncSwitch

478\def\InCa@IncSwitch#1#2!{%

479 \ifx#1-%

480 -%

481 \csname InCa@Empty%

482 \InCa@Dec#2!%

483 \else

484 \csname InCa@Empty%

485 \InCa@Inc#1#2!%

486 \fi

487}

(20)

\intcalcDec

488\def\intcalcDec#1{%

489 \number\expandafter\InCa@DecSwitch\number#1! %

490}

\InCa@DecSwitch

491\def\InCa@DecSwitch#1#2!{%

492 \ifx#1-%

493 -%

494 \csname InCa@Empty%

495 \expandafter\InCa@Inc#2!%

496 \else

497 \ifx#10%

498 -1%

499 \else

500 \csname InCa@Empty%

501 \InCa@Dec#1#2!%

502 \fi

503 \fi

504}

\IntCalcInc

505\def\IntCalcInc#1!{%

506 \number\csname InCa@Empty\InCa@Inc#1! %

507}

\IntCalcDec

508\def\IntCalcDec#1!{%

509 \number\csname InCa@Empty\InCa@Dec#1! %

510}

\InCa@Inc

511\def\InCa@Inc#1#2{%

512 \ifx#2!%

513 \csname InCa@IncDigit#1\endcsname1%

514 \else

515 \csname InCa@IncDigit#1%

516 \expandafter\InCa@Inc\expandafter#2%

517 \fi

518}

\InCa@IncDigit[0-8]

519\def\InCa@Temp#1#2{%

520 \expandafter\def\csname InCa@IncDigit#1\endcsname##1{%

521 \endcsname

522 0%

523 \ifcase##1 %

524 #1%

525 \else

526 #2%

527 \fi

528 }%

529}

530\InCa@Temp 01

531\InCa@Temp 12

532\InCa@Temp 23

533\InCa@Temp 34

(21)

534\InCa@Temp 45

535\InCa@Temp 56

536\InCa@Temp 67

537\InCa@Temp 78

538\InCa@Temp 89

\InCa@IncDigit9

539\expandafter\def\csname InCa@IncDigit9\endcsname#1{%

540 \expandafter\endcsname

541 \ifcase#1 %

542 09%

543 \else

544 10%

545 \fi

546}

\InCa@Dec

547\def\InCa@Dec#1#2{%

548 \ifx#2!%

549 \csname InCa@DecDigit#1\endcsname1%

550 \else

551 \csname InCa@DecDigit#1%

552 \expandafter\InCa@Dec\expandafter#2%

553 \fi

554}

\InCa@DecDigit[1-9]

555\def\InCa@Temp#1#2{%

556 \expandafter\def\csname InCa@DecDigit#1\endcsname##1{%

557 \endcsname

558 0%

559 \ifcase##1 %

560 #1%

561 \else

562 #2%

563 \fi

564 }%

565}

566\InCa@Temp 98

567\InCa@Temp 87

568\InCa@Temp 76

569\InCa@Temp 65

570\InCa@Temp 54

571\InCa@Temp 43

572\InCa@Temp 32

573\InCa@Temp 21

574\InCa@Temp 10

\InCa@DecDigit0

575\expandafter\def\csname InCa@DecDigit0\endcsname#1{%

576 \expandafter\endcsname

577 \ifcase#1 %

578 00%

579 \else

580 19%

581 \fi

582}

(22)

2.5.5 Add,Sub

\intcalcAdd

583\def\intcalcAdd#1#2{%

584 \number

585 \expandafter\InCa@AddSwitch

586 \number\number#1\expandafter!%

587 \number#2! %

588}

\intcalcSub

589\def\intcalcSub#1#2{%

590 \number

591 \expandafter\InCa@AddSwitch

592 \number\number#1\expandafter!%

593 \number-\number#2! %

594}

\InCa@AddSwitch Decision table for \InCa@AddSwitch. The sign of negative numbers can be re- moved by a simple\@gobbleinstead of the more expensive\number-.

x <0 y <0 x < y − Add(−x,−y)

else Add(−y,−x)

else −x > y − Sub(−x, y) else + Sub(y,−x) else y <0 x >−y + Sub(x,−y) else − Sub(−y, x) else x > y + Add(x, y)

else Add(y, x)

595\def\InCa@AddSwitch#1!#2!{%

596 \ifnum#1<\z@

597 \ifnum#2<\z@

598 -%

599 \ifnum#1<#2 %

600 \expandafter\InCa@Add\number-#1\expandafter!%

601 \@gobble#2!%

602 \else

603 \expandafter\InCa@Add\number-#2\expandafter!%

604 \@gobble#1!%

605 \fi

606 \else

607 \ifnum-#1>#2 %

608 -%

609 \expandafter\InCa@Sub\@gobble#1!#2!%

610 \else

611 \expandafter\InCa@Sub\number#2\expandafter!%

612 \@gobble#1!%

613 \fi

614 \fi

615 \else

616 \ifnum#2<\z@

617 \ifnum#1>-#2 %

618 \expandafter\InCa@Sub\number#1\expandafter!%

619 \@gobble#2!%

620 \else

621 -%

(23)

622 \expandafter\InCa@Sub\@gobble#2!#1!%

623 \fi

624 \else

625 \ifnum#1>#2 %

626 \InCa@Add#1!#2!%

627 \else

628 \InCa@Add#2!#1!%

629 \fi

630 \fi

631 \fi

632}

\IntCalcAdd

633\def\IntCalcAdd#1!#2!{%

634 \number\InCa@Add#1!#2! %

635}

\IntCalcSub

636\def\IntCalcSub#1!#2!{%

637 \number\InCa@Sub#1!#2! %

638}

\InCa@Space

639\begingroup

640 \def\x#1{\endgroup

641 \let\InCa@Space= #1%

642 }%

643\x{ }

\InCa@Add

644\def\InCa@Add#1!#2!{%

645 \ifcase#2 %

646 #1%

647 \else

648 \InCa@@Add#1!#2!000000000\InCa@Space

649 \fi

650}

\InCa@Sub

651\def\InCa@Sub#1!#2!{%

652 \ifnum#1=#2 %

653 0%

654 \else

655 \InCa@@Sub#1!#2!000000000\InCa@Space

656 \fi

657}

\InCa@@Add

658\def\InCa@@Add#1!#2#3!{%

659 \ifx\InCa@Empty#3\InCa@Empty

660 \@ReturnAfterElseFi{%

661 \InCa@@@Add!!#1!#2%

662 }%

663 \else

664 \@ReturnAfterFi{%

665 \InCa@@Add#1!#3!#2%

666 }%

667 \fi

668}

(24)

\InCa@@Sub

669\def\InCa@@Sub#1!#2#3!{%

670 \ifx\InCa@Empty#3\InCa@Empty

671 \@ReturnAfterElseFi{%

672 \InCa@@@Sub!!#1!#2%

673 }%

674 \else

675 \@ReturnAfterFi{%

676 \InCa@@Sub#1!#3!#2%

677 }%

678 \fi

679}

\InCa@@@Add

680\def\InCa@@@Add#1!#2!#3#4!#5{%

681 \ifx\InCa@Empty#4\InCa@Empty

682 \csname InCa@Empty%

683 \@ReturnAfterElseFi{%

684 \InCa@ProcessAdd#1#3!#5#2%

685 }%

686 \else

687 \@ReturnAfterFi{%

688 \InCa@@@Add#1#3!#5#2!#4!%

689 }%

690 \fi

691}

\InCa@@@Sub

692\def\InCa@@@Sub#1!#2!#3#4!#5{%

693 \ifx\InCa@Empty#4\InCa@Empty

694 \csname @gobble%

695 \@ReturnAfterElseFi{%

696 \InCa@ProcessSub#1#3!#5#2%

697 }%

698 \else

699 \@ReturnAfterFi{%

700 \InCa@@@Sub#1#3!#5#2!#4!%

701 }%

702 \fi

703}

\InCa@ProcessAdd

704\def\InCa@ProcessAdd#1#2!#3#4{%

705 \ifx\InCa@Empty#2\InCa@Empty

706 \csname InCa@AddDigit#1\endcsname#3%

707 \romannumeral0#4%

708 \else

709 \csname InCa@AddDigit#1\csname InCa@DigitCarry#3%

710 \@ReturnAfterFi{%

711 \InCa@ProcessAdd#2!#4%

712 }%

713 \fi

714}

\InCa@ProcessSub

715\def\InCa@ProcessSub#1#2!#3#4{%

716 \ifx\InCa@Empty#2\InCa@Empty

717 \csname InCa@SubDigit#1\endcsname#3%

(25)

718 \romannumeral0#4%

719 \else

720 \csname InCa@SubDigit#1\csname InCa@DigitCarry#3%

721 \@ReturnAfterFi{%

722 \InCa@ProcessSub#2!#4%

723 }%

724 \fi

725}

\InCa@DigitCarry[0-9]

726\def\InCa@Temp#1#2{%

727 \expandafter\def\csname InCa@DigitCarry#1\endcsname##1{%

728 \ifcase##1 %

729 \endcsname#1%

730 \else

731 \endcsname#2%

732 \fi

733 }%

734}

735\InCa@Temp 01

736\InCa@Temp 12

737\InCa@Temp 23

738\InCa@Temp 34

739\InCa@Temp 45

740\InCa@Temp 56

741\InCa@Temp 67

742\InCa@Temp 78

743\InCa@Temp 89

744\InCa@Temp 9{{10}}

\InCa@AddDigit0

745\expandafter\def\csname InCa@AddDigit0\endcsname#1{%

746 \ifnum#1>9 %

747 \endcsname10%

748 \else

749 \endcsname0#1%

750 \fi

751}

\InCa@AddDigit[1-9]

752\def\InCa@Temp#1#2#3{%

753 \expandafter\def\csname InCa@AddDigit#1\endcsname##1{%

754 \ifnum##1>#2 %

755 \endcsname 1%

756 \else

757 \endcsname 0%

758 \fi

759 \ifcase##1 #1% 0

760 #3%

761 \else #1% 10

762 \fi

763 }%

764}

765\InCa@Temp 18{%

766 \or 2% 1

767 \or 3% 2

768 \or 4% 3

769 \or 5% 4

(26)

770 \or 6% 5

771 \or 7% 6

772 \or 8% 7

773 \or 9% 8

774 \or 0% 9

775}%

776\InCa@Temp 27{%

777 \or 3% 1

778 \or 4% 2

779 \or 5% 3

780 \or 6% 4

781 \or 7% 5

782 \or 8% 6

783 \or 9% 7

784 \or 0% 8

785 \or 1% 9

786}%

787\InCa@Temp 36{%

788 \or 4% 1

789 \or 5% 2

790 \or 6% 3

791 \or 7% 4

792 \or 8% 5

793 \or 9% 6

794 \or 0% 7

795 \or 1% 8

796 \or 2% 9

797}%

798\InCa@Temp 45{%

799 \or 5% 1

800 \or 6% 2

801 \or 7% 3

802 \or 8% 4

803 \or 9% 5

804 \or 0% 6

805 \or 1% 7

806 \or 2% 8

807 \or 3% 9

808}%

809\InCa@Temp 54{%

810 \or 6% 1

811 \or 7% 2

812 \or 8% 3

813 \or 9% 4

814 \or 0% 5

815 \or 1% 6

816 \or 2% 7

817 \or 3% 8

818 \or 4% 9

819}%

820\InCa@Temp 63{%

821 \or 7% 1

822 \or 8% 2

823 \or 9% 3

824 \or 0% 4

825 \or 1% 5

826 \or 2% 6

827 \or 3% 7

(27)

828 \or 4% 8

829 \or 5% 9

830}%

831\InCa@Temp 72{%

832 \or 8% 1

833 \or 9% 2

834 \or 0% 3

835 \or 1% 4

836 \or 2% 5

837 \or 3% 6

838 \or 4% 7

839 \or 5% 8

840 \or 6% 9

841}%

842\InCa@Temp 81{%

843 \or 9% 1

844 \or 0% 2

845 \or 1% 3

846 \or 2% 4

847 \or 3% 5

848 \or 4% 6

849 \or 5% 7

850 \or 6% 8

851 \or 7% 9

852}%

853\InCa@Temp 90{%

854 \or 0% 1

855 \or 1% 2

856 \or 2% 3

857 \or 3% 4

858 \or 4% 5

859 \or 5% 6

860 \or 6% 7

861 \or 7% 8

862 \or 8% 9

863}%

\InCa@SubDigit[0-9]

864\def\InCa@Temp#1#2{%

865 \expandafter\def\csname InCa@SubDigit#1\endcsname##1{%

866 \ifnum##1>#1 %

867 \endcsname 1%

868 \else

869 \endcsname 0%

870 \fi

871 \ifcase##1 #1% 0

872 #2%

873 \else #1% 10

874 \fi

875 }%

876}

877\InCa@Temp 0{%

878 \or 9% 1

879 \or 8% 2

880 \or 7% 3

881 \or 6% 4

882 \or 5% 5

883 \or 4% 6

(28)

884 \or 3% 7

885 \or 2% 8

886 \or 1% 9

887}

888\InCa@Temp 1{%

889 \or 0% 1

890 \or 9% 2

891 \or 8% 3

892 \or 7% 4

893 \or 6% 5

894 \or 5% 6

895 \or 4% 7

896 \or 3% 8

897 \or 2% 9

898}

899\InCa@Temp 2{%

900 \or 1% 1

901 \or 0% 2

902 \or 9% 3

903 \or 8% 4

904 \or 7% 5

905 \or 6% 6

906 \or 5% 7

907 \or 4% 8

908 \or 3% 9

909}

910\InCa@Temp 3{%

911 \or 2% 1

912 \or 1% 2

913 \or 0% 3

914 \or 9% 4

915 \or 8% 5

916 \or 7% 6

917 \or 6% 7

918 \or 5% 8

919 \or 4% 9

920}

921\InCa@Temp 4{%

922 \or 3% 1

923 \or 2% 2

924 \or 1% 3

925 \or 0% 4

926 \or 9% 5

927 \or 8% 6

928 \or 7% 7

929 \or 6% 8

930 \or 5% 9

931}

932\InCa@Temp 5{%

933 \or 4% 1

934 \or 3% 2

935 \or 2% 3

936 \or 1% 4

937 \or 0% 5

938 \or 9% 6

939 \or 8% 7

940 \or 7% 8

941 \or 6% 9

(29)

942}

943\InCa@Temp 6{%

944 \or 5% 1

945 \or 4% 2

946 \or 3% 3

947 \or 2% 4

948 \or 1% 5

949 \or 0% 6

950 \or 9% 7

951 \or 8% 8

952 \or 7% 9

953}

954\InCa@Temp 7{%

955 \or 6% 1

956 \or 5% 2

957 \or 4% 3

958 \or 3% 4

959 \or 2% 5

960 \or 1% 6

961 \or 0% 7

962 \or 9% 8

963 \or 8% 9

964}

965\InCa@Temp 8{%

966 \or 7% 1

967 \or 6% 2

968 \or 5% 3

969 \or 4% 4

970 \or 3% 5

971 \or 2% 6

972 \or 1% 7

973 \or 0% 8

974 \or 9% 9

975}

976\InCa@Temp 9{%

977 \or 8% 1

978 \or 7% 2

979 \or 6% 3

980 \or 5% 4

981 \or 4% 5

982 \or 3% 6

983 \or 2% 7

984 \or 1% 8

985 \or 0% 9

986}

2.5.6 Shl, Shr

\intcalcShl

987\def\intcalcShl#1{%

988 \number\expandafter\InCa@ShlSwitch\number#1! %

989}

\InCa@ShlSwitch

990\def\InCa@ShlSwitch#1#2!{%

991 \ifx#1-%

992 -\csname InCa@Empty%

993 \InCa@Shl#2!%

(30)

994 \else

995 \csname InCa@Empty%

996 \InCa@Shl#1#2!%

997 \fi

998}

\IntCalcShl

999\def\IntCalcShl#1!{%

1000 \number

1001 \csname InCa@Empty%

1002 \InCa@Shl#1! %

1003}

\IntCal@ShlDigit

1004\def\InCa@Shl#1#2{%

1005 \ifx#2!%

1006 \csname InCa@ShlDigit#1\endcsname0%

1007 \else

1008 \csname InCa@ShlDigit#1%

1009 \@ReturnAfterFi{%

1010 \InCa@Shl#2%

1011 }%

1012 \fi

1013}

\InCa@ShlDigit0

1014\expandafter\def\csname InCa@ShlDigit0\endcsname{%

1015 \endcsname0%

1016}

\InCa@ShlDigit[1-9]

1017\def\InCa@Temp#1#2#3#4#5{%

1018 \expandafter\def\csname InCa@ShlDigit#1\endcsname##1{%

1019 \expandafter\endcsname

1020 \ifcase##1 %

1021 #2#3%

1022 \else

1023 #4#5%

1024 \fi

1025 }%

1026}

1027\InCa@Temp 10203

1028\InCa@Temp 20405

1029\InCa@Temp 30607

1030\InCa@Temp 40809

1031\InCa@Temp 51011

1032\InCa@Temp 61213

1033\InCa@Temp 71415

1034\InCa@Temp 81617

1035\InCa@Temp 91819

\intcalcShr

1036\def\intcalcShr#1{%

1037 \number\expandafter\InCa@ShrSwitch\number#1! %

1038}

\InCa@ShrSwitch

(31)

1039\def\InCa@ShrSwitch#1#2!{%

1040 \ifx#1-%

1041 -\InCa@Shr#2!%

1042 \else

1043 \InCa@Shr#1#2!%

1044 \fi

1045}

\IntCalcShr

1046\def\IntCalcShr#1!{%

1047 \number\InCa@Shr#1! %

1048}

\InCa@Shr

1049\def\InCa@Shr#1#2{%

1050 \InCa@ShrDigit#1!%

1051 \ifx#2!%

1052 \else

1053 \@ReturnAfterFi{%

1054 \ifodd#1 %

1055 \@ReturnAfterElseFi{%

1056 \InCa@Shr{1#2}%

1057 }%

1058 \else

1059 \expandafter\InCa@Shr\expandafter#2%

1060 \fi

1061 }%

1062 \fi

1063}

1064\def\InCa@ShrDigit#1!{%

1065 \ifcase#1 0% 0

1066 \or 0% 1

1067 \or 1% 2

1068 \or 1% 3

1069 \or 2% 4

1070 \or 2% 5

1071 \or 3% 6

1072 \or 3% 7

1073 \or 4% 8

1074 \or 4% 9

1075 \or 5% 10

1076 \or 5% 11

1077 \or 6% 12

1078 \or 6% 13

1079 \or 7% 14

1080 \or 7% 15

1081 \or 8% 16

1082 \or 8% 17

1083 \or 9% 18

1084 \or 9% 19

1085 \fi

1086}

2.5.7 \InCa@Tim

\InCa@Tim Macro\InCa@Timimplements “Numbertimes digit”.

1087\def\InCa@Temp#1{%

(32)

1088 \def\InCa@Tim##1##2{%

1089 \number

1090 \ifcase##2 % 0

1091 0%

1092 \or % 1

1093 ##1%

1094 \else % 2-9

1095 \csname InCa@Empty%

1096 \InCa@ProcessTim##2##1!%

1097 \fi

1098 #1%

1099 }%

1100}

1101\InCa@Temp{ }

\InCa@ProcessTim

1102\def\InCa@ProcessTim#1#2#3{%

1103 \ifx#3!%

1104 \csname InCa@TimDigit#2\endcsname#10%

1105 \else

1106 \csname InCa@TimDigit#2\csname InCa@Param#1%

1107 \@ReturnAfterFi{%

1108 \InCa@ProcessTim#1#3%

1109 }%

1110 \fi

1111}

\InCa@Param[0-9]

1112\def\InCa@Temp#1{%

1113 \expandafter\def\csname InCa@Param#1\endcsname{%

1114 \endcsname#1%

1115 }%

1116}

1117\InCa@Temp 0%

1118\InCa@Temp 1%

1119\InCa@Temp 2%

1120\InCa@Temp 3%

1121\InCa@Temp 4%

1122\InCa@Temp 5%

1123\InCa@Temp 6%

1124\InCa@Temp 7%

1125\InCa@Temp 8%

1126\InCa@Temp 9%

\InCa@TimDigit0

1127\expandafter\def\csname InCa@TimDigit0\endcsname#1#2{%

1128 \endcsname

1129 0#2%

1130}

\InCa@TimDigit1

1131\expandafter\def\csname InCa@TimDigit1\endcsname#1#2{%

1132 \ifcase#2 %

1133 \endcsname 0#1%

1134 \else

1135 \csname InCa@AddDigit#1\endcsname #2%

1136 \fi

1137}

(33)

\InCa@TimDigit[2-9]

1138\def\InCa@Temp#1#2{%

1139 \expandafter\def\csname InCa@TimDigit#1\endcsname##1{%

1140 \expandafter\InCa@TimDigitCarry

1141 \number

1142 \ifcase##1 0% 0

1143 #2%

1144 \fi

1145 !%

1146 }%

1147}

1148\InCa@Temp 2{%

1149 \or 2% 1

1150 \or 4% 2

1151 \or 6% 3

1152 \or 8% 4

1153 \or 10% 5

1154 \or 12% 6

1155 \or 14% 7

1156 \or 16% 8

1157 \or 18% 9

1158}

1159\InCa@Temp 3{%

1160 \or 3% 1

1161 \or 6% 2

1162 \or 9% 3

1163 \or 12% 4

1164 \or 15% 5

1165 \or 18% 6

1166 \or 21% 7

1167 \or 24% 8

1168 \or 27% 9

1169}

1170\InCa@Temp 4{%

1171 \or 4% 1

1172 \or 8% 2

1173 \or 12% 3

1174 \or 16% 4

1175 \or 20% 5

1176 \or 24% 6

1177 \or 28% 7

1178 \or 32% 8

1179 \or 36% 9

1180}

1181\InCa@Temp 5{%

1182 \or 5% 1

1183 \or 10% 2

1184 \or 15% 3

1185 \or 20% 4

1186 \or 25% 5

1187 \or 30% 6

1188 \or 35% 7

1189 \or 40% 8

1190 \or 45% 9

1191}

1192\InCa@Temp 6{%

1193 \or 6% 1

1194 \or 12% 2

(34)

1195 \or 18% 3

1196 \or 24% 4

1197 \or 30% 5

1198 \or 36% 6

1199 \or 42% 7

1200 \or 48% 8

1201 \or 54% 9

1202}

1203\InCa@Temp 7{%

1204 \or 7% 1

1205 \or 14% 2

1206 \or 21% 3

1207 \or 28% 4

1208 \or 35% 5

1209 \or 42% 6

1210 \or 49% 7

1211 \or 56% 8

1212 \or 63% 9

1213}

1214\InCa@Temp 8{%

1215 \or 8% 1

1216 \or 16% 2

1217 \or 24% 3

1218 \or 32% 4

1219 \or 40% 5

1220 \or 48% 6

1221 \or 56% 7

1222 \or 64% 8

1223 \or 72% 9

1224}

1225\InCa@Temp 9{%

1226 \or 9% 1

1227 \or 18% 2

1228 \or 27% 3

1229 \or 36% 4

1230 \or 45% 5

1231 \or 54% 6

1232 \or 63% 7

1233 \or 72% 8

1234 \or 81% 9

1235}

\InCa@TimDigitCarry

1236\def\InCa@TimDigitCarry#1!{%

1237 \ifnum#1<10 %

1238 \csname InCa@AddDigit#1\expandafter\endcsname

1239 \else

1240 \@ReturnAfterFi{%

1241 \InCa@@TimDigitCarry#1!%

1242 }%

1243 \fi

1244}

\InCa@@TimDigitCarry

1245\def\InCa@@TimDigitCarry#1#2!#3{%

1246 \csname InCa@DigitCarry#1%

1247 \csname InCa@AddDigit#2\endcsname #3%

1248}

(35)

2.5.8 Mul

\intcalcMul

1249\def\intcalcMul#1#2{%

1250 \number

1251 \expandafter\InCa@MulSwitch

1252 \number\number#1\expandafter!%

1253 \number#2! %

1254}

\InCa@MulSwitch Decision table for\InCa@MulSwitch.

x <0 y <0 x < y + Mul(−x,−y)

else Mul(−y,−x)

else −x > y − Mul(−x, y)

else Mul(y,−x)

else y <0 x >−y − Mul(x,−y)

else Mul(−y, x)

else x > y + Mul(x, y)

else Mul(y, x)

1255\def\InCa@MulSwitch#1!#2!{%

1256 \ifnum#1<\z@

1257 \ifnum#2<\z@

1258 \ifnum#1<#2 %

1259 \expandafter\InCa@Mul\number-#1\expandafter!%

1260 \@gobble#2!%

1261 \else

1262 \expandafter\InCa@Mul\number-#2\expandafter!%

1263 \@gobble#1!%

1264 \fi

1265 \else

1266 -%

1267 \ifnum-#1>#2 %

1268 \expandafter\InCa@Mul\@gobble#1!#2!%

1269 \else

1270 \expandafter\InCa@Mul\number#2\expandafter!%

1271 \@gobble#1!%

1272 \fi

1273 \fi

1274 \else

1275 \ifnum#2<\z@

1276 -%

1277 \ifnum#1>-#2 %

1278 \expandafter\InCa@Mul\number#1\expandafter!%

1279 \@gobble#2!%

1280 \else

1281 \expandafter\InCa@Mul\@gobble#2!#1!%

1282 \fi

1283 \else

1284 \ifnum#1>#2 %

1285 \InCa@Mul#1!#2!%

1286 \else

1287 \InCa@Mul#2!#1!%

1288 \fi

1289 \fi

1290 \fi

1291}

(36)

\IntCalcMul

1292\def\IntCalcMul#1!#2!{%

1293 \number\InCa@Mul#1!#2! %

1294}

\InCa@Mul

1295\def\InCa@Mul#1!#2!{%

1296 \ifcase#2 %

1297 0%

1298 \or

1299 #1%

1300 \or

1301 \csname InCa@Empty%

1302 \expandafter\InCa@Shl#1!%

1303 \else

1304 \ifnum#2<10 %

1305 \InCa@Tim{#1}#2%

1306 \else

1307 \InCa@ProcessMul!#2!#1!%

1308 \fi

1309 \fi

1310}

\InCa@Mul

1311\def\InCa@ProcessMul#1!#2#3!#4!{%

1312 \ifx\InCa@Empty#3\InCa@Empty

1313 \expandafter\InCa@Add\number

1314 #10\expandafter\expandafter\expandafter!%

1315 \InCa@Tim{#4}#2!%

1316 \else

1317 \ifx\InCa@Empty#1\InCa@Empty

1318 \expandafter\expandafter\expandafter\InCa@ProcessMul

1319 \InCa@Tim{#4}#2!%

1320 #3!#4!%

1321 \else

1322 \expandafter\InCa@ProcessMul\number

1323 \expandafter\InCa@Add\number%

1324 #10\expandafter\expandafter\expandafter!%

1325 \InCa@Tim{#4}#2!!%

1326 #3!#4!%

1327 \fi

1328 \fi

1329}

2.5.9 Sqr, Fac

\intcalcSqr

1330\def\intcalcSqr#1{%

1331 \number\expandafter\InCa@Sqr\number#1! %

1332}

\InCa@Sqr

1333\def\InCa@Sqr#1#2!{%

1334 \ifx#1-%

1335 \InCa@Mul#2!#2!%

1336 \else

1337 \InCa@Mul#1#2!#1#2!%

(37)

1338 \fi

1339}

\intcalcFac

1340\def\intcalcFac#1{%

1341 \number\expandafter\InCa@Fac\number#1! %

1342}

2.5.10 Pow

\intcalcPow

1343\def\intcalcPow#1#2{%

1344 \number\expandafter\InCa@Pow

1345 \number\number#1\expandafter!%

1346 \number#2! %

1347}

\InCa@Pow

1348\def\InCa@Pow#1#2!#3#4!{%

1349 \ifcase#3#4 % power = 0

1350 1%

1351 \or % power = 1

1352 #1#2%

1353 \or % power = 2

1354 \ifx#1-%

1355 \InCa@Mul#2!#2!%

1356 \else

1357 \InCa@Mul#1#2!#1#2!%

1358 \fi

1359 \else

1360 \ifcase#1#2 % basis = 0, power <> 0

1361 0%

1362 \ifx#3-% power < 0

1363 0\IntCalcError:DivisionByZero%

1364 \fi

1365 \or

1366 1% basis = 1

1367 \else

1368 \ifnum#1#2=\m@ne % basis = -1

1369 \ifodd#3#4 %

1370 -%

1371 \fi

1372 1%

1373 \else % |basis| > 1

1374 \ifx#3-% power < 0

1375 0%

1376 \else % power > 2

1377 \ifx#1-% basis < 0

1378 \ifodd#3#4 %

1379 -%

1380 \fi

1381 \InCa@PowRec#2!#3#4!1!%

1382 \else

1383 \InCa@PowRec#1#2!#3#4!1!%

1384 \fi

1385 \fi

1386 \fi

1387 \fi

(38)

1388 \fi

1389}

\InCa@PowRec 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 }

1390\def\InCa@PowRec#1!#2!#3!{%

1391 \ifnum#2=\@ne

1392 \ifnum#1>#3 %

1393 \InCa@Mul#1!#3!%

1394 \else

1395 \InCa@Mul#3!#1!%

1396 \fi

1397 \else

1398 \expandafter\InCa@PowRec

1399 \number\InCa@Mul#1!#1!\expandafter!%

1400 \number\intcalcShr{#2}\expandafter!%

1401 \number

1402 \ifodd#2 %

1403 \ifnum#1>#3 %

1404 \InCa@Mul#1!#3!%

1405 \else

1406 \InCa@Mul#3!#1!%

1407 \fi

1408 \else

1409 #3%

1410 \fi

1411 \expandafter!%

1412 \fi

1413} 2.5.11 Div

\intcalcDiv

1414\def\intcalcDiv#1#2{%

1415 \number\expandafter\InCa@Div

1416 \number\number#1\expandafter!%

1417 \number#2! %

1418}

\InCa@Div

1419\def\InCa@Div#1!#2!{%

1420 \ifcase#2 %

1421 0\IntCalcError:DivisionByZero%

1422 \else

1423 \ifcase#1 %

1424 0%

References

Related documents

Package attachfile2 and the Perl script communicate via an auxiliary file with file extension .atfi.. The script is used between two L A TEX runs and updates the auxiliary file,

• Since chemsym makes _ and ^ active, these characters cannot be used in labels when using the chemsym package, nor in file names loaded in L A TEX runs loading the chemsym

The total ABC contribution to Australian screen drama, combined with approximately $125 million in external funding, delivered up to $244 million in production value to

This is based on the revenue of economic activities in 14 marine industry sub-sectors, including water-based transport, domestic and international tourism, marinas

The bundle provides source code environments that also print the output and defines quite a lot of macros for formatting of control sequence names, package names, package options and

Sessional Com m ittee on the Environm ent 79.. A strong research and development effort, particularly into the integration of control methods, is essential to the

If you want some text typeset with the duerer fonts for a short text you can use one of the commands. \

Any import declarations that appear in the specification will also appear in the source file for the parser class, allowing various names from that package to be

If set to a terminal which produces TEX output, such as latex, tex, epslatex, or pstricks, the file processed by gnuplot will be included with the \include command, else

If pdfTEX 1.30 or greater is used in PDF mode then the graphics file names may contain spaces except for MPS files.. Therefore option space is only enabled by default, if the

The hep-text package extends L A TEX lists using the enumitem package and provides some text macros... The package can be loaded by

Index: The main part of this package deals with index links in order to support the kind of index that package doc provides.... Thumbnails: Package thumbpdf may additionally be

Finally, the \printindex command is used in your L A TEX document to indicate where the file foo.idx should be inserted, i.e., where the index should appear in your document.. The

• Additional High Conservation Value Vegetation (AHCVV) means areas of vegetation which were found during ground-truthing which would otherwise meet the definition of Existing

Benzene (ppb) change in annual max 1-hour (MDA1) ground level concentrations from Scenario 2 due to future industry (S3-S2) for a subset of the CAMx 1.33 km domain centred over

5.15 At the time of Mr C’s requests for access to the NDIS, the NDIA did not have any policy or guideline dealing specifically with incarcerated individuals and access to the NDIS.

existence. In making such an estimate, the Government Printer was requested to take as understood that each author body would have its report printed by the

disadvantage have special resonance for the Australian Aboriginal community, where the construct, the best interests of the child, has been applied and has resulted in an

Le lieu et le titre sont mis en premier dans la seconde colonne, chacun dans la bonne fonte, et en les faisant suivre d’un point. Ensuite, on ajoute, si elle existe,

M AGNETICS formats present a difficulty in that compsoc and transmag journal (but not compsoc conference) papers place the abstract and index terms sections in single column format

The package consists of a preprocessor that converts an indented list tree format into L A TEX input (plain TEX doesn’t seem to work), and some macro files which lay out the trees

\nodeconnect takes the same arguments as in tree-dvips, with a final optional [ ] argument taking PSTricks graphics parameters:.. (4) \nodeconnect[from loc]{from node}[to

The Swedish school authorities have drawn attention to this work and designated the school ‘the best school in Sweden working for equal value 2008’. Student empowerment, child’s