÷ƒ’À;è TeX output 1996.03.05:1426‹ÿÿÿÿ ª ýs o ýÞ‘V›ÉóDÓítG®G®cmr17¹The–7tó߆µTG® cmtt12ºcalc“¹pacŒqk‘ÿäage:‘¬In x“notation“arithmetic“inŽŸ’æIvLŸüdú‘úKGóX«Q cmr12»AŽŽ‘¹”¹T‘ý[™Ÿ¸-EŽ‘;gXŸùº¯ó!",š cmsy10¿ŽŽŽŸ’ O»Kresten–ê¨Krab“ThorupŽŽŽŽ’"¬F‘ÿVrank‘ê¨JensenŽŽŽŽŽŸ37’Ý 1995/04/10ŽŸ.’æ!Kót ‰: cmbx9ÇAbstractŽŸ¬Ñ‘oßüóo´‹Ç cmr9ÆThe–W÷óߤN cmtt9Ècalc“Æpacš¾9k‘ÿ|rage“reimplemen˜ts“the“LŸýó5‘ü«‡ó¹Aa¨cmr6ÁAŽŽ‘© ÆT‘þuAŸïÿEŽ‘˜ŸX“commands“È\setcounterÆ,Ž¤ ‘bÈ\addtocounterÆ,–$ØÈ\setlengthÆ,“and–!½È\addtolengthÆ.‘A¬Instead“of“a“simple“v‘ÿ|ralue,Ž¡‘bthese–Tcommands“no¾9w“accept“an“in x“notation“expression.Ž©!Ä‘Ió!ÂÖN ff cmbx12Ì1Ž‘aLËInŒÌtros3ductionŽŸç‘IóKñ`y cmr10²Arithmetic–’in“T‘þU>Ÿ'EŽ‘ãxX“is“done“using“lo•¸ãw-lev“el–’opGerations“suc¸ãh“as“ó"ßêŸ'EŽ‘ãxX.Ž¡‘XThe–­Ÿin x“expressions“can“bGe“used“in“argumenš¸ãts“to“macros“(the“Ícalc“²pac˜k‘ÿqÇageŽ¡‘IdošGesn't–UUemplo¸ãy“category“co˜de“cš¸ãhanges“to“ac˜hiev˜e“its“goals).Ž¦‘IÌ2Ž‘aLËInformal‘ffdescriptionŽŸç‘I²Standard–õdLŸýó5‘üff±AŽŽ‘͉²T‘þU>Ÿ'EŽ‘ãxX“proš¸ãvides“the“follo˜wing“set“of“commands“to“manipulate“coun˜tersŽ¡‘Iand–UUlengths“[2Ž‘,“pages“194“and“216].Ž©‘IÍ\setcounter{ó#ý': cmti10Îctr‘Í}{Înum‘ÄcÍ}–'¦²sets“the“v›ÿqÇalue“of“the“coun¸ãter“Îctr‘;3²equal“to“(the“v˜alue“of‘Ç)Ž¡‘bÎnum².‘qÇ(F‘ÿ*ªragile)Ž¦‘IÍ\addtocounter{Îctr‘Í}{Înum‘ÄcÍ}–²incremenš¸ãts“the“v‘ÿqÇalue“of“the“coun˜ter“Îctr‘$Ž²b˜y“(the“v‘ÿqÇalueŽ¡‘bof‘Ç)‘UUÎnum².‘qÇ(F‘ÿ*ªragile)Ž¦‘IÍ\setlength{Îcmd‘‰Í}{Îlen‘ÄcÍ}–NÞ²sets“the“v‘ÿqÇalue“of“the“length“command“Îcmd‘Wg²equal“to“(theŽ¡‘bv‘ÿqÇalue–UUof‘Ç)“Îlen².‘qÇ(Robust)Ž¦‘IÍ\addtolength{Îcmd‘‰Í}{Îlen‘ÄcÍ}–L²sets“the“v‘ÿqÇalue“of“the“length“command“Îcmd‘ Õ²equal“to“itsŽ¡‘bcurren¸ãt–UUv›ÿqÇalue“plus“(the“v˜alue“of‘Ç)“Îlen².‘qÇ(Robust)Ž‘IŸæf‰ffÿrŸ @‘ ÷ˆŸý-:óq¡% cmsy6ÅŽŽ‘LÜó|{Ycmr8ÀW›ÿJªe–ßthank“F˜rank“MittelbacšÃŽh“for“his“v‡aluable“commen˜ts“and“suggestions“whic˜h“ha˜v˜e“greatlyŽŸ €impro•ÃŽv“ed–ÕXthis“pacÃŽk‡age.ŽŽŸ’ø²1ŽŽŒ‹* ª ýs o ý´‘I²(The–ÅÍ\setcounter“²and“Í\addtocounter“²commands“ha•¸ãv“e–Åglobal“e ect,‘Q!while“theŽ¤ ‘IÍ\setlength–t`²and“Í\addtolength“²commands“obGey“the“normal“scoping“rules.)‘ÎéInŽ¡‘Istandard–ë§LŸýó5‘üff±AŽŽ‘͉²T‘þU>Ÿ'EŽ‘ãxX,‘;the“argumenš¸ãts“to“these“commands“m˜ust“bGe“simple“v‘ÿqÇalues.‘4¼TheŽ¡‘IÍcalc–E²pac¸ãk‘ÿqÇage“extends“these“commands“to“accept“in x“notation“expressions,‘Ide-Ž¡‘Inoting–¶Ôv‘ÿqÇalues“of“appropriate“tš¸ãypGes.‘–DUsing“the“Ícalc“²pac˜k‘ÿqÇage,‘Ï4Înum‘{7²is“replaced“b˜yŽ¡‘Ió !",š cmsy10¸h²inš¸ãteger–xjexpression¸i²,‘/and“Îlen‘<Ͳis“replaced“b˜y“¸h²glue“expression¸i².‘ÛThe“formal“syn˜taxŽ¡‘Iof–UU¸h²inš¸ãteger“expression¸i“²and“¸h²glue“expression¸i“²is“giv˜en“bGelo˜w.Ž¡‘XIn–e•the“follo•¸ãwing,‘i¥w“e–e•shall“use“standard“T‘þU>Ÿ'EŽ‘ãxX“terminology‘ÿ*ª.‘¢‡The“correspGondenceŽ¡‘IbGet•¸ãw“een–·»T‘þU>Ÿ'EŽ›ãxX“and“LŸýó5‘üff±AŽŽ‘͉²T‘þU>Ÿ'EŽ˜X“terminology“is“as“follo¸ãws:‘6“LŸýó5‘üff±AŽŽ‘͉²T‘þU>Ÿ'EŽ˜X“coun¸ãters“correspGondŽ¡‘Ito–tvT‘þU>Ÿ'EŽ‘ãxX's“counš¸ãt“registers;‘they“hold“quan˜tities“of“t˜ypGe“¸h²n˜um˜bGer¸i².‘Ï+LŸýó5‘üff±AŽŽ‘͉²T‘þU>Ÿ'EŽ‘ãxX“lengthŽ¡‘Icommands–šcorrespšGond“to“T‘þU>Ÿ'EŽ‘ãxX's“dimen“(for“rigid“lengths)“and“skip“(for“rubb˜erŽ¡‘Ilengths)–UUregisters;“they“hold“quanš¸ãtities“of“t˜ypšGes“¸h²dimen¸i“²and“¸h²glue¸i²,“resp˜ectiv¸ãely‘ÿ*ª.Ž¡‘XT‘þU>Ÿ'EŽ‘ãxX–¬™givš¸ães“us“primitiv˜e“opšGerations“to“p˜erform“arithmetic“on“registers“as“follo¸ãws:Ž¤‘X¸ŽŽŽ‘b²addition–UUand“subtraction“on“all“tš¸ãypGes“of“quan˜tities“without“restrictions;Ž¡‘X¸ŽŽŽ‘b²mš¸ãultiplication–1and“division“b˜y“an“Îinte‘ÿ}'ger‘’¾²can“bšGe“p˜erformed“on“a“register“ofŽ© ‘ban•¸ãy‘UUt“ypGe;Ž¡‘X¸ŽŽŽ‘b²m•¸ãultiplication›ˆýb“y˜a˜Îr–ÿ}'e“al‘‘†²n•¸ãum“bGer˜(i.e.,‘±Üa˜n“um“b•Ger˜with˜a˜fractional˜part)˜can˜b“eŽ¦‘bpGerformed–4Çon“a“register“of“anš¸ãy“t˜ypGe,‘;Jbut“the“stretc˜h“and“shrink“compGonen˜tsŽ¦‘bof–UUa“glue“quan•¸ãtit“y–UUare“discarded.Ž¡‘IThe–béÍcalc“²pacš¸ãk‘ÿqÇage“uses“these“T‘þU>Ÿ'EŽ‘ãxX“primitiv˜es“but“pro˜vides“a“more“user-friendlyŽ¦‘Inotation–UUfor“expressing“the“arithmetic.Ž¦‘XAn–m=expression“is“formed“of“nš¸ãumerical“quan˜titites“(suc˜h“as“explicit“constan˜ts“andŽ¦‘ILŸýó5‘üff±AŽŽ‘͉²T‘þU>Ÿ'EŽ‘ãxX–Ä£counš¸ãters“and“length“commands)“and“binary“opGerators“(the“tok˜ens“`Í+²',‘àw`Í-²',Ž¦‘I`Í*²',‘Àûand–›ä`Í/²'“with“their“usual“meaning)“using“the“familiar“in x“notation;‘Ùµparen¸ãthesesŽ¦‘Imaš¸ãy–bÔbGe“used“to“o˜v˜erride“the“usual“precedences“(that“m˜ultiplication/division“ha˜v˜eŽ¦‘Ihigher–UUprecedence“than“addition/subtraction).Ž¦‘XExpressions–žÄm¸ãust“bšGe“prop˜erly“t¸ãyp˜ed.‘4ìThis“means,–ÃHe.g.,“that–žÄa“dimen“expressionŽ¦‘Imš¸ãust–Þ{bGe“a“sum“of“dimen“terms:‘6Zi.e.,‘öAy˜ou“cannot“sa˜y“`Í2cm+4²'“but“`Í2cm+4pt²'“is“v‘ÿqÇalid.Ž¦‘XIn–f®a“dimen“term,‘kthe“dimension“part“m¸ãust“come“ rst;‘oZthe“same“holds“for“glueŽ¦‘Iterms.‘5 Also,‘Ñmš¸ãultiplication–Ÿ and“division“b˜y“non-in˜teger“quan˜tities“require“a“spGecialŽ¦‘Isynš¸ãtax;–UUsee“bGelo˜w.Ž¦‘XEv‘ÿqÇaluation–¬—of“subšGexpressions“at“the“same“lev¸ãel“of“precedence“pro˜ceeds“from“leftŽ¦‘Ito–' righš¸ãt.‘b`Consider“a“dimen“term“suc˜h“as“\Í4cm*3*4²".‘b`First,‘0^the“v‘ÿqÇalue“of“the“factorŽ¦‘IÍ4cm–<`²is“assigned“to“a“dimen“register,‘v"then“this“register“is“mš¸ãultiplied“b˜y“3“(usingŽ¦‘IÍ\multiply²),–UWand,“ nally‘ÿ*ª,“the“register“is“mš¸ãultiplied“b˜y“4“(again“using“Í\multiply²).Ž¦‘IThis–Œ5also“explains“wh¸ãy“the“dimension“part“(i.e.,‘´othe“part“with“the“unit“designation)Ž¦‘Imš¸ãust–f¶come“ rst;‘ofT‘þU>Ÿ'EŽ‘ãxX“simply“doGesn't“allo˜w“un˜t˜ypGed“constan˜ts“to“bGe“assigned“to“aŽ¦‘Idimen‘UUregister.Ž¦‘XThe–ÂÕÍcalc“²pacš¸ãk‘ÿqÇage“also“allo˜ws“m˜ultiplication“and“division“b˜y“real“n˜um˜bGers.Ž¦‘IHo•¸ãw“ev“er,‘”îa–ˆ6spGecial“synš¸ãtax“is“required:‘׉y˜ou“m˜ust“use“Í\real{¸h²decimal“constan˜t¸iÍ}Ÿü^ÿ±1ŽŽŽ¦‘I²or–pZÍ\ratio{¸h²dimen“expression¸iÍ}{¸h²dimen“expression¸iÍ}“²to“denote“a“real“v‘ÿqÇalue“to“bGeŽ¦‘Iused–’Úfor“mš¸ãultiplication/division.‘*UThe“ rst“form“has“the“ob˜vious“meaning,‘â:andŽ¦‘Ithe–Ùsecond“form“denotes“the“n•¸ãum“bGer–Ùobtained“b¸ãy“dividing“the“v‘ÿqÇalue“of“the“ rstŽ¦‘Iexpression–UUb¸ãy“the“v‘ÿqÇalue“of“the“second“expression.Ž‘IŸX-‰ffÿrŸ J=‘ "5Ÿý-:Á1ŽŽŽ‘LÜÀActually‘ÿJª,›#xinstead–à¤of“ó¾KÈ cmsy8ÄhÀdecimal“constanÃŽtÄiÀ,˜the“more“general“ÄhÀoptional“signsÄihÀfactorÄi“Àcan“bŸ'EŽ‘ãxX–ˆâdiscards“the“stretcš¸ãh“and“shrink“compGonen˜ts“of“glue“when“glue“is“m˜ultipliedŽ¤ ‘Ibš¸ãy–UUa“real“n˜um˜bGer.‘qÇSo,“for“example,Ž©‘[æXÈ\setlength{\parskip}{3pt–¹–plus“3pt“*“\real{1.5}}ŽŸ‘I²will–ô±set“the“paragraph“separation“to“4.5pt“with“no“stretcš¸ãh“or“shrink.‘Q(Inciden˜tally‘ÿ*ª,Ž¡‘Inote–UUhoš¸ãw“spaces“can“bGe“used“to“enhance“readabilit˜y‘ÿ*ª.)Ž¡‘XWhen–‘`T‘þU>Ÿ'EŽ‘ãxX“pGerforms“arithmetic“on“in•¸ãtegers,‘ can“y–‘`fractional“part“of“the“resultsŽ¡‘Iare–UUdiscarded.‘qÇF‘ÿ*ªor“example,Ž¦‘[æXÈ\setcounter{x}{7/2}Ž¤ ‘[æX\setcounter{y}{3*\real{1.6}}Ž¡‘[æX\setcounter{z}{3*\real{1.7}}ŽŸ‘I²will–ø³assign“the“v›ÿqÇalue“3“to“the“coun¸ãter“Íx²,‘!‹the“v˜alue“4“to“Íy²,‘!‹and“the“v˜alue“5“to“Íz².Ž¤ ‘IThis–Îtruncation“also“applies“to“Îinterme‘ÿ}'diate‘Ã)²results“in“the“sequen¸ãtial“computationŽ¡‘Iof–UUa“compGosite“expression;“thš¸ãus,“the“follo˜wing“commandŽ¦‘[æXÈ\setcounter{x}{3–¹–*“\real{1.6}“*“\real{1.7}}ŽŸ‘I²will–UUassign“6“to“Íx².Ž¡‘XAs–±¦an“example“of“the“use“of“Í\ratio²,‘Ⱥconsider“the“problem“of“scaling“a“ gureŽ¡‘Ito–ɼošGccup¸ãy“the“full“width“(i.e.,‘æÕÍ\textwidth²)“of“the“b˜o˜dy“of“a“page.‘ÎûAssume“thatŽ¡‘Ithe–Ÿ'EŽ‘ãxXb•Go“ok–‘à[1Ž‘].‘'gW‘ÿ*ªe“use“Îtyp‘ÿ}'e‘R;²as“a“meta-v¸ãeriable,‘¡standing“forŽ¡‘I`in¸ãteger',–UU`dimen',“and“`glue'.Ÿü^ÿ±2ŽŽŽ©‘X¸hÎtyp‘ÿ}'e‘°²expression¸iŽ’¨Ä›ŽŽŽ’®á!‘UUhÎtyp‘ÿ}'e‘°²term¸iŽŽ¡‘vj‘UUhÎtyp‘ÿ}'e‘°²expression¸iŽ‘MoFh²plus–UUor“min¸ãus¸iŽ’„¤hÎtyp‘ÿ}'e‘°²term¸iŽŽ¦‘XhÎtyp‘ÿ}'e‘°²term¸iŽ’}|ŽŽŽ’–™ò!‘UUhÎtyp‘ÿ}'e‘°²factor¸iŽŽ¡‘vj‘UUhÎtyp‘ÿ}'e‘°²term¸iŽ‘5('h²m¸ãultiply–UUor“divide¸iŽ’‹’àh²in¸ãteger‘UUfactor¸iŽŽ¡‘vj‘UUhÎtyp‘ÿ}'e‘°²term¸iŽ‘5('h²m¸ãultiply–UUor“divide¸iŽ’‹’àh²real‘UUn•¸ãum“bGer¸iŽŽ¦‘XhÎtyp‘ÿ}'e‘°²factor¸iŽ’•6aŽŽŽ’›R×!‘UUhÎtyp‘ÿ}'e‘À[¸iŽ‘#è$j‘UUÍ(Ÿÿ±12Ž‘xæ¸hÎtyp‘ÿ}'e‘°²expression¸iŽ‘R’×Í)Ÿÿ±12ŽŽ¦‘X¸h²in¸ãteger¸iŽ’€ÜvŽŽŽ’†øì!‘UUh²n•¸ãum“bGer¸iŽŽ¦‘Xh²m¸ãultiply–UUor“divide¸iŽ’µcŽŽŽ’»1Ù!–UUÍ*Ÿÿ±12Ž‘#¸j“Í/Ÿÿ±12ŽŽ¦‘X¸h²real‘UUn•¸ãum“bGer¸iŽ’›ŽŽŽ’¡}!–UUÍ\ratio{¸h²dimen“expression¸iŽ›R£™Í}{¸h²dimen“expression¸iŽ˜Í}Ž¡‘v¸j–UUÍ\real{¸h²decimal“constan¸ãt¸iŽ‘QqÒÍ}Ž‘IŸæf‰ffÿrŸ J=‘ "5Ÿý-:Á2ŽŽŽ‘LÜÀThis–ÕXvšÃŽersion“of“the“ó$ßCÊscmtt8Ïcalc“Àpac˜k‡age“došIt“is“trivial“to“add“subtraction“andŽ¡‘Idivision.Ž¡‘XAn–‚vexpression“ó  b> cmmi10µE‘²is“a“sum“of“terms:‘̵TŸÿ±1Ž‘~²+‘Ÿ¸–ª¨“Ž‘­å²+‘ŸµTŸÿó 0e—rcmmi7´nŽ‘q~²;‘a“term“is“a“proGduct“ofŽ¡‘Ifactors:‘9cµFŸÿ±1Ž‘ÓƸ‘WS–ª¨“Ž‘YM‘WSµFŸÿ´mŽ‘˜›²;‘ %a–äfactor“is“either“a“simple“nš¸ãumeric“quan˜tit˜y“µf‘ø²(lik˜e“¸h²n˜um˜bGer¸iŽ¡‘I²as–UUdescribšGed“in“the“T‘þU>Ÿ'EŽ‘ãxXb˜o˜ok),“or“a“paren¸ãthesized“expression“(µE‘“Ÿü^ÿó O!â…cmsy7·0Ž‘aƲ).Ž¡‘XSince–[Ëthe“T‘þU>Ÿ'EŽ‘ãxX“engine“can“only“execute“arithmetic“opGerations“in“a“mac¸ãhine-Ž¡‘IcoGde–¹likš¸ãe“manner,‘Òw˜e“ha˜v˜e“to“ nd“a“w˜a˜y“to“translate“the“in x“notation“in˜to“thisŽ¡‘I`instruction‘UUset'.Ž¡‘XOur–ë‹goal“is“to“design“a“translation“sc¸ãheme“that“translates“µX‘´m²(an“expression,Ž¡‘Ia–ïterm,‘sor“a“factor)“inš¸ãto“a“sequence“of“T‘þU>Ÿ'EŽ‘ãxX“instructions“that“doGes“the“follo˜wingŽ¡‘I[In•¸ãv‘ÿqÇariance›PropGert“y]:‘éWcorrectly˜ev‘ÿqÇaluates˜µX‘Èâ²,‘@lea“v“es˜the˜result˜in˜a˜global˜regis-Ž¡‘Iter–ÜõA“²(using“a“global“assignmen¸ãt),‘ôáand“došGes“not“p˜erform“global“assignmen¸ãts“to“theŽ¡‘Iscratcš¸ãh–register“µB‘€q²;‘-,moreo˜v˜er,‘%#the“coGde“sequence“m˜ust“bšGe“balanced“with“resp˜ect“toŽ¡‘IT‘þU>Ÿ'EŽ‘ãxX–UUgroups.‘qÇW‘ÿ*ªe“shall“denote“the“cošGde“sequence“corresp˜onding“to“µX‘7²b¸ãy“[–þUX[µX‘Èâ²]“].Ž¡‘XIn–UUthe“replacemen¸ãt“cošGde“sp˜eci ed“b˜eloš¸ãw,“w˜e“use“the“follo˜wing“con˜v˜en˜tions:ŽŸ)Ï‘X¸ŽŽŽ‘bµA–Ýc²and“µB‘]Ô²denote“registers;‘_all“assignmen¸ãts“to“µA“²will“bGe“global,‘õ`and“all“assign-Ž¡‘bmen¸ãts–UUto“µB‘ÕƲwill“bšGe“lo˜cal.Ž¤”è‘X¸ŽŽŽ‘b²\¸(²"–UUmeans“global“assignmen¸ãt“to“the“register“on“the“lhs.Ž¡‘X¸ŽŽŽ‘b²\¸ ²"–UUmeans“loGcal“assignmen¸ãt“to“the“register“on“the“lhs.Ž¡‘X¸ŽŽŽ‘b²\ŸÎ:´,‘þ¢_·!±[´C}‚±]ŽŽ‘<<²"–ªïmeans“\sa•¸ãv“e–ªïthe“coGde“µC‘b ²unš¸ãtil“the“curren˜t“group“(scopGe)“ends,‘ÀUthenŽ© ‘bexecute–UUit."‘qÇThis“correspGonds“to“the“T‘þU>Ÿ'EŽ‘ãxX-primitiv¸ãe“Í\aftergroup².Ž¡‘X¸ŽŽŽ‘b²\¸f²"–UUdenotes“the“start“of“a“new“group,“and“\¸g²"“denotes“the“end“of“a“group.ŽŸ)ΑXLet–\†us“consider“an“expression“µTŸÿ±1Ž–ù²+šGFµTŸÿ±2Ž“²+˜¸–ª¨“Ž‘ 93²+˜µTŸÿ´nŽ‘q~².‘ØAssuming–\†that“[–þUX[µTŸÿ´kŽ‘ë²]“]‘\†(1–Ǹ“µk‘¯¸“µn²)Ž¦‘Iattains–3nthe“stated“goal,‘:6the“follo¸ãwing“coGde“clearly“attains“the“stated“goal“for“theirŽ¦‘Isum:Ž¤ôB‘fÝ:[‘þUX[µTŸÿ±1Ž–µS²+›8àµTŸÿ±2Ž“²+˜¸–ª¨“Ž‘g²+˜µTŸÿ´nŽ‘q~²]‘þUX]ŽŽ’Èì=‘þUX¸)ŽŽ’ãzf›ª¨²[–þUX[µTŸÿ±1Ž‘|s²]“]˜¸g–ǵB‘G‰¸ “µA‘ ¸f˜²[–þUX[µTŸÿ±2Ž‘|s²]“]˜¸g–ǵB‘G‰¸ “µB‘¹Q²+‘8àµAŽŽŽŸ’÷|:–ª¨:“:Ž’]̸f›ª¨²[–þUX[µTŸÿ´nŽ‘q~²]“]˜¸g–ǵB‘G‰¸ “µB‘¹Q²+‘8àµA‘ A“¸(“µBŽŽŽ¡‘I²Note–}Ëthe“extra“levš¸ãel“of“grouping“enclosing“eac˜h“of“[–þUX[µTŸÿ±1Ž›|s²]“],‘‡é[“[µTŸÿ±2Ž˜²]“],‘‡é.–ªª.“.“,›}Ë[–þUX[µTŸÿ´nŽ‘q~²]“].‘ë*This˜willŽ¦‘Iensure–øthat“register“µB‘€q²,› Åused“to“compute“the“sum“of“the“terms,˜is“not“clobbGeredŽ¦‘Ibš¸ãy–Ethe“in˜termediate“computations“of“the“individual“terms.‘@ùActually‘ÿ*ª,‘€ÿthe“groupŽ¦‘Ienclosing›¿x[–þUX[µTŸÿ±1Ž‘|s²]“]˜is˜unnecessary‘ÿ*ª,‘Ýrbut˜it˜turns˜out˜to˜bGe˜simpler˜if˜all˜terms˜are˜treatedŽ¦‘Ithe–UUsame“w•¸ãa“y‘ÿ*ª.Ž¦‘XThe–Z_coGde“sequence“\¸f›ª¨²[–þUX[µTŸÿ±2Ž‘|s²]“]˜¸g–Ï~µB‘Oï¸ “µB‘¼­²+‘<<µA²"–Z_can“bGe“translated“inš¸ãto“the“follo˜wingŽ¦‘Iequiv›ÿqÇalen¸ãt–«ÂcoGde“sequence:‘¢\¸fŸÎ:´,‘þ¢_·!±[´B•W=· ´B“±+´A±]ŽŽ‘0‘'²[–þUX[µTŸÿ±2Ž‘|s²]“]‘ª¨¸g²".‘uThis–«Âobserv˜ation“turns“out“toŽ¦‘IbGe–‚ûthe“kš¸ãey“to“the“implemen˜tation:‘ÍThe“\ŸÎ:´,‘þ¢_·!±[´B•W=· ´B“±+´A±]ŽŽ‘.æ²"“is“generated“Îb–ÿ}'efor“e‘CVµTŸÿ±2Ž‘ÿn²isŽ¦‘Itranslated,–UUat“the“same“time“as“the“`+'“opšGerator“b˜et•¸ãw“een–UUµTŸÿ±1Ž›ÑȲand“µTŸÿ±2Ž˜²is“seen.Ž¦‘XNoš¸ãw,–UUthe“spGeci cation“of“the“translation“sc˜heme“is“straigh˜tforw˜ard:Ž¡’´|²[–þUX[µf‘²]“]ŽŽ’Ì<¼=‘þUX¸)ŽŽ’æY3µA–Ǹ(“µfŽŽŽ¤Êt’§êí²[–þUX[(µE‘“ŸûÞÿ·0Ž‘aƲ)]“]ŽŽ’Ì<¼=‘þUX¸)ŽŽ’æY3²[–þUX[µE‘“ŸûÞÿ·0Ž‘aƲ]“]ŽŽŽ¡‘n™‚[‘þUX[µFŸÿ±1Ž–µS¸›8àµFŸÿ±2Ž“¸˜–ª¨“Ž‘g˜µFŸÿ´mŽ‘˜›²]‘þUX]ŽŽ’Ì<¼=‘þUX¸)ŽŽ’æY3fŸÎ:´,‘þ¢_·!±[´BW=· ´A±]ŽŽ‘$ ‹²[–þUX[µFŸÿ±1Ž›|s²]“]‘ª¨¸g‘ fŸÎ:´,‘þ¢_·!±[´B•W=· ´B“·´A±]ŽŽ‘.†{²[“[µFŸÿ±2Ž˜²]“]‘ª¨¸gŽŽŽŽŸ’ø²4ŽŽŒ‹*Ä ª ýs o ý´’úY5µ:–ª¨:“:Ž’®…¸fŸÎ:´,‘þ¢_·!±[´B•W=· ´B“·´A±]ŽŽ‘.†{²[–þUX[µFŸÿ´mŽ‘˜›²]“]‘ª¨¸g‘ µA–Ǹ(“µBŽŽŽŸžÿ‘j-ó²[‘þUX[µTŸÿ±1Ž–µS²+›8àµTŸÿ±2Ž“²+˜¸–ª¨“Ž‘g²+˜µTŸÿ´nŽ‘q~²]‘þUX]ŽŽ’Ì<¼=‘þUX¸)ŽŽ’æY3fŸÎ:´,‘þ¢_·!±[´BW=· ´A±]ŽŽ‘$ ‹²[–þUX[µTŸÿ±1Ž›|s²]“]‘ª¨¸g‘ fŸÎ:´,‘þ¢_·!±[´B•W=· ´B“±+´A±]ŽŽ‘0‘'²[“[µTŸÿ±2Ž˜²]“]‘ª¨¸gŽŽŽŸ’úY5µ:–ª¨:“:Ž’®…¸fŸÎ:´,‘þ¢_·!±[´B•W=· ´B“±+´A±]ŽŽ‘0‘'²[–þUX[µTŸÿ´nŽ‘q~²]“]‘ª¨¸g‘ µA–Ǹ(“µBŽŽŽŸù‘I²By–UUstructural“induction,“it“is“easily“seen“that“the“stated“propGert¸ãy“is“attained.Ž¤ ‘XBy–w"inspGection“of“this“translation“sc•¸ãheme,‘•w“e–w"see“that“wš¸ãe“ha˜v˜e“to“generate“theŽ¡‘Ifollo¸ãwing‘UUcoGde:ŽŸ{û‘X¸ŽŽŽ‘b²w•¸ãe›C^m“ust˜generate˜\¸fŸÎ:´,‘þ¢_·!±[´BW=· ´A±]ŽŽ–"`ã¸fŸÎ:´,‘þ¢_·!±[´BW=· ´A±]ŽŽ“²"˜at˜the˜left˜bGorder˜of˜an˜expressionŽ¡‘b(i.e.,‘cfor–`Yeacš¸ãh“left“paren˜thesis“and“the“implicit“left“paren˜thesis“at“the“bGegin-Ž¡‘bning–UUof“the“whole“expression);Ž©=þ‘X¸ŽŽŽ‘b²w•¸ãe›à¾m“ust˜generate˜\¸gµA–¯q¸(“µB›€q¸gµA“¸(“µB˜²"–à¾at“the“righ¸ãt“bGorder“of“an“expressionŽ¡‘b(i.e.,‘•,eac•¸ãh›ˆgrigh“t˜paren“thesis˜and˜the˜implicit˜righ“t˜paren“thesis˜at˜the˜end˜ofŽ¡‘bthe–UUfull“expression);Ž¦‘X¸ŽŽŽ‘b²`Í*²'–UUis“replaced“b¸ãy“\¸gfŸÎ:´,‘þ¢_·!±[´B•W=· ´B“·´A±]ŽŽ‘,ÛÓ²";Ž¦‘X¸ŽŽŽ‘b²`Í+²'–UUis“replaced“b¸ãy“\¸gµA–Ǹ(“µB‘€q¸gfŸÎ:´,‘þ¢_·!±[´B•W=· ´B“±+´A±]ŽŽ‘.æ¸fŸÎ:´,‘þ¢_·!±[´BW=· ´A±]ŽŽ‘"`ã²";Ž¦‘X¸ŽŽŽ‘b²when–—±wš¸ãe“see“(expGect)“a“n˜umeric“quan˜tit˜y‘ÿ*ª,‘èHw˜e“insert“the“assignmen˜t“coGdeŽ¡‘b\µA‘Ǹ(²"–UUin“fronš¸ãt“of“the“quan˜tit˜y“and“let“T‘þU>Ÿ'EŽ‘ãxX“parse“it.ŽŸ ¾<‘IÌ5Ž‘aLËImplemenŒÌtationŽŸç‘I²F‘ÿ*ªor–UUbrevit¸ãy“de neŽ¤=ý‘X¸h²n¸ãumeric¸iŽ’…ÜwŽŽŽ’‹øí!‘UUh²n•¸ãum“bGer¸iŽ‘/‡"j‘UUh²dimen¸iŽ‘)wj‘UUh²glue¸iŽ‘ 8çj‘UUh²m¸ãuglue¸iŽŽ¡‘X²So–Àfar“wš¸ãe“ha˜v˜e“ignored“the“question“of“ho˜w“to“determine“the“t˜ypGe“of“registerŽ¤ ‘Ito–USbšGe“used“in“the“co˜de.‘qÂHo•¸ãw“ev“er,‘•Sit–USis“easy“to“see“that“(1)“`¸²'“alw•¸ãa“ys‘USinitiatesŽ¡‘Ian–^þ¸h²inš¸ãteger“factor¸i²,‘¡h(2)“all“¸h²n˜umeric¸i²s“in“an“expression,‘¡hexcept“those“whic˜h“areŽ¡‘Ipart–˜Ãof“an“¸h²inš¸ãteger“factor¸i²,‘©Ÿare“of“the“same“t˜ypGe“as“the“whole“expression,‘©Ÿand“allŽ¡‘I¸h²nš¸ãumeric¸i²s–UUin“an“¸h²in˜teger“factor¸i“²are“¸h²n˜um˜bGer¸i²s.Ž¡‘XW‘ÿ*ªe›¥ha•¸ãv“e˜to˜ensure˜that˜µA˜²and˜µB‘%r²alw“a“ys˜ha“v“e˜an˜appropriate˜t“ypGe˜for˜theŽ¡‘I¸h²nš¸ãumeric¸i²s–°>they“manipulate.‘‚‚W‘ÿ*ªe“can“ac˜hiev˜e“this“b˜y“ha˜ving“an“instance“of“µAŽ¡‘I²and–ÊϵB‘K@²for“eacš¸ãh“t˜ypšGe.‘C›Initially‘ÿ*ª,‘愵A“²and“µB‘K@²refer“to“registers“of“the“prop˜er“t¸ãyp˜e“for“theŽ¡‘Iwhole– ¿expression.‘YêWhen“an“¸h²inš¸ãteger“factor¸i“²is“expGected,‘w˜e“m˜ust“c˜hange“µA“²and“µBŽ¡‘I²to–ÆBrefer“to“inš¸ãteger“t˜ypGe“registers.‘BW‘ÿ*ªe“can“accomplish“this“b˜y“including“instructionsŽ¡‘Ito–acš¸ãhange“the“t˜ypGe“of“µA“²and“µB‘„Ò²to“in˜teger“t˜ypGe“as“part“of“the“replacemen˜t“coGdeŽ¡‘Ifor–ë`¸²;‘m5if“wš¸ãe“appGend“suc˜h“instructions“to“the“replacemen˜t“cošGde“describ˜ed“ab˜o•¸ãv“e,Ž¡‘Iwš¸ãe–Çalso“ensure“that“the“t˜ypGe-c˜hange“is“loGcal“(pro˜vided“that“the“t˜ypGe-c˜hangingŽ¡‘Iinstructions–4¾only“ha•¸ãv“e–4¾loGcal“e ect).‘fêHo•¸ãw“ev“er,‘;Cnote–4¾that“the“instance“of“µA“²referredŽ¡‘Ito–UUin“ŸÎ:´,‘þ¢_·!±[´B•W=· ´B“·´A±]ŽŽ‘3†}²is“the“in¸ãteger“instance“of“µA².Ž¡‘XW‘ÿ*ªe–shall“use“Í\begingroup“²and“Í\endgroup“²for“the“opGen-group“and“close-groupŽ¡‘Ic•¸ãharacters.‘’uThis›µa“v“oids˜problems˜with˜spacing˜in˜math˜(as˜pGoin“ted˜out˜to˜us˜b“yŽ¡‘IF‘ÿ*ªrank‘UUMittelbac¸ãh).ŽŸª®‘Ió%ÂÖN  cmbx12Ð5.1Ž‘gÀGetting‘€startedŽŸuT‘I²No•¸ãw›Å>w“e˜ha“v“e˜enough˜insigh“t˜to˜do˜the˜actual˜implemen“tation˜in˜T‘þU>Ÿ'EŽ‘ãxX.‘ÁFirst,‘á8w“eŽ¡‘Iannounce–UUthe“macro“pac¸ãk‘ÿqÇage.ŽŽŸ’ø5ŽŽŒ‹=R ª ýs o ý´‘Lüs±1Ž‘Sª®ó©±Ê cmsy9Êhó&ÒoÇw cmss9Ñpack¾9ageŽ‘#g ÊiŽ¤ ‘Lüs±2Ž‘Sª®È\NeedsTeXFormat{LaTeX2e}Ž¡‘Lüs±3Ž‘Sª®È\ProvidesPackage{calc}[\filedate\space\fileversion]Ž¡‘Lüs±4Ž‘Sª®È\typeout{Package:–¹–`calc'“\fileversion\space“<\filedate>“(KKT“and“FJ)}ŽŸs^‘IÐ5.2Ž‘gÀAssignmen t‘€macrosŽŸuT‘àƲÈ\calc@assign@genericŽŽŽ‘I²The–KÍ\calc@assign@generic“²macro“takš¸ães“four“argumen˜ts:‘lë(1“and“2)“the“registersŽ¤ ‘Ito–$=bšGe“used“for“global“and“lo˜cal“manipulations,‘Wöresp˜ectiv¸ãely;‘‹±(3)“the“lv‘ÿqÇalue“part;Ž¡‘I(4)–UUthe“expression“to“bGe“ev‘ÿqÇaluated.Ž¡‘XThe–”Ðthird“argumen¸ãt“(the“lv‘ÿqÇalue)“will“bGe“used“as“a“pre x“to“a“register“thatŽ¡‘Iconš¸ãtains–UUthe“v‘ÿqÇalue“of“the“spGeci ed“expression“(the“fourth“argumen˜t).Ž¡‘XIn–<=general,‘tuan“lv‘ÿqÇalue“is“anš¸ãything“that“ma˜y“bGe“follo˜w˜ed“b˜y“a“v‘ÿqÇariable“of“the“appro-Ž¡‘Ipriate–€mt¸ãypGe.‘óAs“an“example,‘‹3Í\linepenalty“²and“Í\global\advance\linepenaltyŽ¡‘I²ma¸ãy–UUbšGoth“b˜e“follo•¸ãw“ed›UUb“y˜an˜¸h²in“teger˜v‘ÿqÇariable¸i².Ž¡‘XThe–‘0macros“describšGed“b˜eloš¸ãw“refer“to“the“registers“b˜y“the“names“Í\calc@A‘‘ ²andŽ¡‘IÍ\calc@B²;–UUthis“is“accomplished“bš¸ãy“Í\let²-assignmen˜ts.Ž¡‘XAs›8bdisco•¸ãv“ered˜in˜Section˜4,‘q$w“e˜ha“v“e˜to˜generate˜coGde˜as˜if˜the˜expression˜isŽ¡‘Iparen¸ãthesized.‘,ŒAs–“—describšGed“b˜elo¸ãw,‘£'Í\calc@open“²is“the“macro“that“replaces“a“leftŽ¡‘Iparen•¸ãtheseis›„b“y˜its˜corresp•Gonding˜T‘þU>Ÿ'EŽ‘ãxX˜co“de˜sequence.‘U×When˜the˜scanning˜pro“cessŽ¡‘Isees–8cthe“exclamation“pGoinš¸ãt,‘q'it“generates“an“Í\endgroup“²and“stops.‘òAs“w˜e“recallŽ¡‘Ifrom–LõSection“4,‘ŠÝthe“correct“expansion“of“a“righš¸ãt“paren˜thesis“is“\¸gµA–c̸(“µB‘€q¸gµA“¸(Ž¡‘IµB‘€q²".‘‘¾The– §remaining“tok¸ãens“of“this“expansion“are“inserted“explicitly‘ÿ*ª,‘7üexcept“thatŽ¡‘Ithe–‚hlast“assignmenš¸ãt“has“bGeen“replaced“b˜y“the“lv‘ÿqÇalue“part“(i.e.,‘ͬargumen˜t“Í#3“²ofŽ¡‘IÍ\calc@assign@generic²)›UUfollo•¸ãw“ed˜b“y˜Í\calc@B².ŽŸF‘Lüs±5Ž‘Sª®È\def\calc@assign@generic#1#2#3#4{\let\calc@A#1\let\calc@B#2%Ž¤ ‘Lüs±6Ž‘f‘È\expandafter\calc@open\expandafter(#4!%Ž¡‘Lüs±7Ž‘f‘È\global\calc@A\calc@B\endgroup#3\calc@B}ŽŸ½„‘I²(The–Š«Í\expandafter“²tokš¸ãens“allo˜w“the“user“to“use“expressions“stored“in“macros“asŽ¤ ‘Iargumenš¸ãts–UUin“assignmen˜t“commands.)Ž©F‘ê9ÞÈ\calc@assign@countŽŽ¡‘ê9Þ\calc@assign@dimenŽŽŽŽ¡¡‘îót\calc@assign@skipŽŽŽŽŽ‘I²W‘ÿ*ªe–©éneed“three“instances“of“the“Í\calc@assign@generic“²macro,‘¿correspGonding“toŽ¡‘Ithe–UUtš¸ãypGes“¸h²in˜teger¸i²,“¸h²dimen¸i²,“and“¸h²glue¸i².ŽŸ ^‘Lüs±8Ž‘Sª®È\def\calc@assign@count{\calc@assign@generic\calc@Acount\calc@Bcount}Ž¤ ‘Lüs±9Ž‘Sª®È\def\calc@assign@dimen{\calc@assign@generic\calc@Adimen\calc@Bdimen}Ž¡‘I±10Ž‘Sª®È\def\calc@assign@skip{\calc@assign@generic\calc@Askip\calc@Bskip}Ž¦‘I²These–Þâmacros“eacš¸ãh“refer“to“t˜w˜o“registers,‘Eone“to“bšGe“used“globally“and“one“to“b˜eŽ© ‘Iused›UUloGcally–ÿ*ª.‘qÇW“e˜m¸ãust˜alloGcate˜these˜registers.ŽŸ ^‘I±11Ž‘Sª®È\newcount\calc@Acount‘,Â\newcount\calc@BcountŽ¡‘I±12Ž‘Sª®È\newdimen\calc@Adimen‘,Â\newdimen\calc@BdimenŽ¡‘I±13Ž‘Sª®È\newskip\calc@Askip‘Ÿî\newskip\calc@BskipŽŸs^‘IÐ5.3Ž‘gÀThe–€LŸýAl‘û#×ó-2Ç@Écmbx8ØAŽŽ~/ÐT‘ý¿áŸªªEŽ‘ìSX“in terfaceŽŸuT‘I²As–')promised,‘0ewš¸ãe“rede ne“the“follo˜wing“standard“LŸýó5‘üff±AŽŽ‘͉²T‘þU>Ÿ'EŽ‘ãxX“commands:‘Z±Í\setcounter²,Ž¦‘IÍ\addtocounter²,–UUÍ\setlength²,“and“Í\addtolength².ŽŸ ^‘I±14Ž‘Sª®È\def\setcounter#1#2{\@ifundefined{c@#1}{\@nocounterr{#1}}%Ž¡‘I±15Ž‘a×pÈ{\calc@assign@count{\global\csname‘¹–c@#1\endcsname}{#2}}}Ž¡‘I±16Ž‘Sª®È\def\addtocounter#1#2{\@ifundefined{c@#1}{\@nocounterr{#1}}%Ž¡‘I±17Ž‘a×pÈ{\calc@assign@count{\global\advance\csname‘¹–c@#1\endcsname}{#2}}}ŽŽŸ’ø²6ŽŽŒ‹M# ª ýs o ý´‘I±18Ž‘Sª®È\DeclareRobustCommand\setlength{\calc@assign@skip}Ž© ‘I±19Ž‘Sª®È\DeclareRobustCommand\addtolength[1]{\calc@assign@skip{\advance#1}}ŽŸ—3‘I²(Í\setlength–UU²and“Í\addtolength“²are“robust“according“to“[2Ž‘].)ŽŸÑ‘IÐ5.4Ž‘gÀThe‘€scannerŽŸuT‘I²W‘ÿ*ªe–VVev‘ÿqÇaluate“expressions“bš¸ãy“explicit“scanning“of“c˜haracters.‘tËW‘ÿ*ªe“do“not“rely“onŽ¤ ‘Iactiv•¸ãe›UUc“haracters˜for˜this.Ž¡‘XThe–‰scanner“consists“of“t•¸ãw“o–‰parts,‘ÖÍ\calc@pre@scan“²and“Í\calc@post@scan²;Ž¡‘IÍ\calc@pre@scan––²consumes“left“paren¸ãtheses,‘¦Land“Í\calc@post@scan“²consumes“bi-Ž¡‘Inary–UUopGerator,“Í\real²,“Í\ratio²,“and“righš¸ãt“paren˜thesis“tok˜ens.ŽŸ—3‘ý 6È\calc@pre@scanŽŽŽ‘IÍ\calc@pre@scan– Y²reads“the“initial“part“(unš¸ãtil“some“¸h²n˜umeric¸i“²is“seen)“of“expres-Ž¡‘Isions;‘­Áonly–åleft“parenš¸ãtheses“are“allo˜w˜ed“here,‘I¯ev˜erything“else“is“tak˜en“to“bGe“aŽ¡‘I¸h²nš¸ãumeric¸i–ûN²of“some“sort;‘Qthis“allo˜ws“unary“`Í+²'“and“unary“`Í-²'“to“bGe“treated“correctly‘ÿ*ª.ŽŸ Ë™‘I±20Ž‘Sª®È\def\calc@pre@scan#1{%Ž¦‘I±21Ž‘a×pÈ\ifx(#1%Ž¦‘I±22Ž‘t½ÈÈ\let\calc@next\calc@openŽ¦‘I±23Ž‘a×pÈ\elseŽ¦‘I±24Ž‘t½ÈÈ\let\calc@next\calc@numericŽ¦‘I±25Ž‘a×pÈ\fiŽ¦‘I±26Ž‘a×pÈ\calc@next#1}ŽŸ—3‘IÍ\calc@open–)i²is“used“when“there“is“a“left“parenš¸ãthesis“righ˜t“ahead.‘c#This“paren˜thesisŽ¡‘Iis–K2replaced“b¸ãy“T‘þU>Ÿ'EŽ‘ãxX“cošGde“corresp˜onding“to“the“co˜de“sequence“\¸fŸÎ:´,‘þ¢_·!±[´BW=· ´A±]ŽŽ–"`ã¸fŸÎ:´,‘þ¢_·!±[´BW=· ´A±]ŽŽ“²"Ž¡‘Ideriv¸ãed–UUin“Section“4.‘qÇFinally‘ÿ*ª,“Í\calc@pre@scan“²is“called“again.ŽŸ Ë™‘I±27Ž‘Sª®È\def\calc@open({\begingroup\aftergroup\calc@initBŽ¦‘I±28Ž‘a×pÈ\begingroup\aftergroup\calc@initBŽ¦‘I±29Ž‘a×pÈ\calc@pre@scan}Ž¦‘I±30Ž‘Sª®È\def\calc@initB{\calc@B\calc@A}ŽŸË™‘IÍ\calc@numeric–6Ųassigns“the“folloš¸ãwing“v‘ÿqÇalue“to“Í\calc@A‘6½²and“then“transfers“con˜trolŽ¡‘Ito‘UUÍ\calc@post@scan².ŽŸ Ë™‘I±31Ž‘Sª®È\def\calc@numeric{\afterassignment\calc@post@scan‘¹–\global\calc@A}ŽŸbÌ‘øf \calc@post@scanŽŽŽ‘I²The–•Vmacro“Í\calc@post@scan“²is“called“righš¸ãt“after“a“v‘ÿqÇalue“has“bGeen“read.‘1ËA˜t“thisŽ¡‘IpšGoin¸ãt,‘‡¥a–T9binary“op˜erator,‘‡¥a“sequence“of“righš¸ãt“paren˜theses,‘‡¥and“the“end-of-expressionŽ¡‘Imark–‡¯(`Í!²')“is“allo•¸ãw“ed.‘ÕDepGending–‡¯on“our“ ndings,‘ÔEw¸ãe“call“a“suitable“macro“toŽ¡‘Igenerate–UTthe“correspšGonding“T‘þU>Ÿ'EŽ‘ãxX“co˜de“(except“when“w¸ãe“detect“the“end-of-expressionŽ¡‘Imarkš¸ãer:‘q[then–T}scanning“ends,‘T©and“con˜trol“is“returned“to“Í\calc@assign@generic²).Ž¡‘XThis–macro“maš¸ãy“bGe“optimized“b˜y“selecting“a“di eren˜t“order“of“Í\ifx²-tests.‘[0TheŽ¡‘Itest–xáfor“`Í!²'‘Ük(end-of-expression)“is“placed“ rst“as“it“will“alw•¸ãa“ys–xábšGe“p˜erformed:‘¸ßthisŽ¡‘Iis–í´the“only“test“to“bšGe“p˜erformed“if“the“expression“consists“of“a“single“¸h²n¸ãumeric¸i².Ž¡‘IThis– ”ensures“that“doGcumenš¸ãts“that“do“not“use“the“extra“expressiv˜e“pGo˜w˜er“pro˜videdŽ¡‘Ibš¸ãy–UUthe“Ícalc“²pac˜k‘ÿqÇage“only“su er“a“minim˜um“slo˜wdo˜wn“in“proGcessing“time.ŽŸbÌ‘I±32Ž‘Sª®È\def\calc@post@scan#1{%Ž¦‘I±33Ž‘a×pÈ\ifx#1!\let\calc@next\endgroup‘¹–\elseŽ¦‘I±34Ž‘f‘È\ifx#1+\let\calc@next\calc@add‘¹–\elseŽ¦‘I±35Ž‘kJœÈ\ifx#1-\let\calc@next\calc@subtract‘¹–\elseŽ¦‘I±36Ž‘p2È\ifx#1*\let\calc@next\calc@multiplyx‘¹–\elseŽ¦‘I±37Ž‘t½ÈÈ\ifx#1/\let\calc@next\calc@dividex‘¹–\elseŽ¦‘I±38Ž‘yw^È\ifx#1)\let\calc@next\calc@close–¹–\else“\calc@error#1%ŽŽŸ’ø²7ŽŽŒ‹\¤ ª ýs o ý´‘I±39Ž‘yw^È\fiŽ¤ ‘I±40Ž‘t½ÈÈ\fiŽ¡‘I±41Ž‘p2È\fiŽ¡‘I±42Ž‘kJœÈ\fiŽ¡‘I±43Ž‘f‘È\fiŽ¡‘I±44Ž‘a×pÈ\fiŽ¡‘I±45Ž‘a×pÈ\calc@next}ŽŸ‘X²The–9xreplacemen¸ãt“cošGde“for“the“binary“op˜erators“`Í+²'“and“`Í-²'“follo¸ãw“a“commonŽ¤ ‘Ipattern;‘_“the–±~only“di erence“is“the“tokš¸ãen“that“is“stored“a˜w˜a˜y“b˜y“Í\aftergroup².Ž¡‘IAfter–UUthis“replacemenš¸ãt“coGde,“con˜trol“is“transferred“to“Í\calc@pre@scan².Ž©‘I±46Ž‘Sª®È\def\calc@add{\calc@generic@add\calc@addAtoB}Ž¤ ‘I±47Ž‘Sª®È\def\calc@subtract{\calc@generic@add\calc@subtractAfromB}Ž¡‘I±48Ž‘Sª®È\def\calc@generic@add#1{\endgroup\global\calc@A\calc@B\endgroupŽ¡‘I±49Ž‘a×pÈ\begingroup\aftergroup#1\begingroup\aftergroup\calc@initBŽ¡‘I±50Ž‘a×pÈ\calc@pre@scan}Ž¡‘I±51Ž‘Sª®È\def\calc@addAtoB{\advance\calc@B\calc@A}Ž¡‘I±52Ž‘Sª®È\def\calc@subtractAfromB{\advance\calc@B-\calc@A}ŽŸ‘X²The›‚¡m•¸ãultiplicativ“e˜opGerators,–Íô`Í*²'˜and˜`Í/²',“ma•¸ãy˜bGe˜follo“w“ed˜b“y˜a˜Í\real˜²or˜aŽ¤ ‘IÍ\ratio–{B²tokš¸ãen.‘ãŽThose“con˜trol“sequences“are“not“de ned“(at“least“not“b˜y“the“ÍcalcŽ¡‘I²pac¸ãk‘ÿqÇage).Ž¦‘I±53Ž‘Sª®È\def\calc@multiplyx#1{\def\calc@tmp{#1}%Ž¤ ‘I±54Ž‘a×pÈ\ifx\calc@tmp\calc@ratio@x–¹–\let\calc@next\calc@ratio@multiply“\elseŽ¡‘I±55Ž‘p2È\ifx\calc@tmp\calc@real@x–¹–\let\calc@next\calc@real@multiply“\elseŽ¡‘I±56Ž‘~0ôÈ\let\calc@next\calc@multiplyŽ¡‘I±57Ž‘p2È\fiŽ¡‘I±58Ž‘a×pÈ\fiŽ¡‘I±59Ž‘a×pÈ\calc@next#1}Ž¡‘I±60Ž‘Sª®È\def\calc@dividex#1{\def\calc@tmp{#1}%Ž¡‘I±61Ž‘a×pÈ\ifx\calc@tmp\calc@ratio@x–¹–\let\calc@next\calc@ratio@divide“\elseŽ¡‘I±62Ž‘p2È\ifx\calc@tmp\calc@real@x–¹–\let\calc@next\calc@real@divide“\elseŽ¡‘I±63Ž‘~0ôÈ\let\calc@next\calc@divideŽ¡‘I±64Ž‘p2È\fiŽ¡‘I±65Ž‘a×pÈ\fiŽ¡‘I±66Ž‘a×pÈ\calc@next#1}Ž¡‘I±67Ž‘Sª®È\def\calc@ratio@x{\ratio}Ž¡‘I±68Ž‘Sª®È\def\calc@real@x{\real}ŽŸ‘I²The–Spbinary“opšGerators“`Í*²'“and“`Í/²'“also“insert“co˜de“as“determined“ab˜o•¸ãv“e.‘q%Moreo“v“er,Ž¤ ‘Ithe–ú¯meaning“of“Í\calc@A›ú˜²and“Í\calc@B˜²is“cš¸ãhanged“as“factors“follo˜wing“a“m˜ultiplica-Ž¡‘Ition–]and“division“opGerator“alw•¸ãa“ys›]ha“v“e˜in“teger˜t“ypGe;‘,°the˜original˜meaning˜of˜theseŽ¡‘Imacros–UUwill“bšGe“restored“when“the“factor“has“b˜een“read“and“ev‘ÿqÇaluated.Ž¦‘I±69Ž‘Sª®È\def\calc@multiply{\calc@generic@multiply\calc@multiplyBbyA}Ž¤ ‘I±70Ž‘Sª®È\def\calc@divide{\calc@generic@multiply\calc@divideBbyA}Ž¡‘I±71Ž‘Sª®È\def\calc@generic@multiply#1{\endgroup\begingroupŽ¡‘I±72Ž‘a×pÈ\let\calc@A\calc@Acount‘¹–\let\calc@B\calc@BcountŽ¡‘I±73Ž‘a×pÈ\aftergroup#1\calc@pre@scan}Ž¡‘I±74Ž‘Sª®È\def\calc@multiplyBbyA{\multiply\calc@B\calc@Acount}Ž¡‘I±75Ž‘Sª®È\def\calc@divideBbyA{\divide\calc@B\calc@Acount}ŽŸ‘I²Since–ƒŠthe“v‘ÿqÇalue“to“use“in“the“m¸ãultiplication/division“opGeration“is“stored“in“theŽ¤ ‘IÍ\calc@Acount–ò$²register,‘ûthe“Í\calc@multiplyBbyA›ò ²and“Í\calc@divideBbyA˜²macrosŽ¡‘Iuse–UUthis“register.ŽŽŸ’ø8ŽŽŒ‹ j¼ ª ýs o ý´‘XÍ\calc@close–÷x²generates“coGde“for“a“righš¸ãt“paren˜thesis“(whic˜h“w˜as“deriv˜ed“toŽ¤ ‘IbGe‘(\¸gµA–ºË¸(“µB›€q¸gµA“¸(“µB˜²"–(in“Section“4).‘õAAfter“this“coGde,‘Ìthe“con¸ãtrol“is“returnedŽ¡‘Ito–UîÍ\calc@post@scan“²in“order“to“loGok“for“another“righš¸ãt“paren˜thesis“or“a“binaryŽ¡‘IopGerator.Ž©‘I±76Ž‘Sª®È\def\calc@closeŽ¤ ‘I±77Ž‘a×pÈ{\endgroup\global\calc@A\calc@BŽ¡‘I±78Ž‘f‘È\endgroup\global\calc@A\calc@BŽ¡‘I±79Ž‘f‘È\calc@post@scan}ŽŸþ6‘IÐ5.5Ž‘gÀCalculating–€a“ratioŽŸuT‘I²When–ºÔÍ\calc@post@scan“²encounš¸ãters“a“Í\ratio“²con˜trol“sequence,‘Ô3it“hands“con˜trolŽ¤ ‘Ito–ÐFone“of“the“macros“Í\calc@ratio@multiply“²or“Í\calc@ratio@divide²,‘êãdepGendingŽ¡‘Ion–^wthe“preceding“cš¸ãharacter.‘-Those“macros“bGoth“forw˜ard“the“con˜trol“to“the“macroŽ¡‘IÍ\calc@ratio@evaluate²,‘ô¾whicš¸ãh–¡©pGerforms“t˜w˜o“steps:‘ o(1)“it“calculates“the“ratio,Ž¡‘Iwhicš¸ãh–ËËis“sa˜v˜ed“in“the“global“macro“tok˜en“Í\calc@the@ratio²;‘ù¤(2)“it“mak˜es“sure“thatŽ¡‘Ithe–b!v‘ÿqÇalue“of“Í\calc@B‘b²will“bGe“mš¸ãultiplied“b˜y“the“ratio“as“soGon“as“the“curren˜t“groupŽ¡‘Iends.Ž¡‘XThe–jfolloš¸ãwing“macros“call“Í\calc@ratio@evaluate“²whic˜h“m˜ultiplies“Í\calc@BŽ¡‘I²bš¸ãy–g the“ratio,‘kwbut“Í\calc@ratio@divide“² ips“the“argumen˜ts“so“that“the“`oppGosite'Ž¡‘Ifraction–UUis“actually“ev‘ÿqÇaluated.Ž¦‘I±80Ž‘Sª®È\def\calc@ratio@multiply\ratio{\calc@ratio@evaluate}ŽŸ ‘I±81Ž‘Sª®È\def\calc@ratio@divide\ratio#1#2{\calc@ratio@evaluate{#2}{#1}}ŽŸ‘I²W›ÿ*ªe–V¿shall“need“t•¸ãw“o–V¿registers“for“tempGorary“usage“in“the“calculations.‘vW˜e“can“sa•¸ãv“eŽ¡‘Ione–UUregister“since“w¸ãe“can“reuse“Í\calc@Bcount².Ž¦‘I±82Ž‘Sª®È\let\calc@numerator=\calc@BcountŽŸ ‘I±83Ž‘Sª®È\newcount\calc@denominatorŽŸ‘I²Here–›is“the“macro“that“handles“the“actual“ev‘ÿqÇaluation“of“ratios.‘BÐThe“proGcedure“isŽ¡‘Ithis:‘‹ßFirst,‘e¤the›bat•¸ãw“o˜expressions˜are˜ev‘ÿqÇaluated˜and˜coGerced˜to˜in“tegers.‘˜ìThe˜wholeŽ¡‘IprošGcedure–9ýis“enclosed“in“a“group“to“b˜e“able“to“use“the“registers“Í\calc@numeratorŽ¡‘I²and–UUÍ\calc@denominator“²for“tempGorary“manipulations.Ž¦‘I±84Ž‘Sª®È\def\calc@ratio@evaluate#1#2{%Ž¤ ‘I±85Ž‘a×pÈ\endgroup\begingroupŽ¡‘I±86Ž‘p2È\calc@assign@dimen\calc@numerator{#1}%Ž¡‘I±87Ž‘p2È\calc@assign@dimen\calc@denominator{#2}%ŽŸ‘I²Here–Ÿìwš¸ãe“calculate“the“ratio.‘5OFirst,‘Ä4w˜e“c˜hec˜k“for“negativ˜e“n˜umerator“and/or“denom-Ž¤ ‘Iinator;‘Ãßnote–Ÿthat“T‘þU>Ÿ'EŽ‘ãxX“inš¸ãterprets“t˜w˜o“min˜us“signs“the“same“as“a“plus“sign.‘NÜThen,Ž¡‘Iwš¸ãe–upcalculate“the“in˜teger“part.‘ÒThe“min˜us“sign(s),‘}wthe“in˜teger“part,‘}wand“a“decimalŽ¡‘IpGoin¸ãt,–UUform“the“initial“expansion“of“the“Í\calc@the@ratio“²macro.Ž¦‘I±88Ž‘p2È\gdef\calc@the@ratio{}%Ž¤ ‘I±89Ž‘p2È\ifnum\calc@numerator<0‘¹–\calc@numerator-\calc@numeratorŽ¡‘I±90Ž‘~0ôÈ\gdef\calc@the@ratio{-}%Ž¡‘I±91Ž‘p2È\fiŽ¡‘I±92Ž‘p2È\ifnum\calc@denominator<0‘¹–\calc@denominator-\calc@denominatorŽ¡‘I±93Ž‘~0ôÈ\xdef\calc@the@ratio{\calc@the@ratio-}%Ž¡‘I±94Ž‘p2È\fiŽ¡‘I±95Ž‘p2È\calc@Acount\calc@numeratorŽ¡‘I±96Ž‘p2È\divide\calc@Acount\calc@denominatorŽ¡‘I±97Ž‘p2È\xdef\calc@the@ratio{\calc@the@ratio\number\calc@Acount.}%ŽŽŸ’ø²9ŽŽŒ‹ vÍ ª ýs o ý´‘I²No•¸ãw›%¡w“e˜generate˜the˜digits˜after˜the˜decimal˜pGoin“t,‘Y´one˜at˜a˜time.‘â¬When˜T‘þU>Ÿ'EŽ‘ãxXŽ¤ ‘Iscans–¿‚these“digits“(in“the“actual“m¸ãultiplication“opšGeration),‘Ú it“forms“a“ xed-p˜oin¸ãtŽ¡‘In•¸ãum“bšGer–„owith“16“bits“for“the“fractional“part.‘ÿW‘ÿ*ªe“hop˜e“that“six“digits“is“sucien¸ãt,Ž¡‘Ievš¸ãen–UUthough“the“last“digit“ma˜y“not“bGe“rounded“correctly‘ÿ*ª.Ž©‘I±98Ž‘p2È\calc@next@digit–¹–\calc@next@digit“\calc@next@digitŽ¤ ‘I±99Ž‘p2È\calc@next@digit–¹–\calc@next@digit“\calc@next@digitŽ¡‘E±100Ž‘a×pÈ\endgroupŽŸ‘I²No•¸ãw›K×w“e˜ha“v“e˜the˜ratio˜represen“ted˜(as˜the˜expansion˜of˜the˜global˜macroŽ¤ ‘IÍ\calc@the@ratio²)–zèin“the“synš¸ãtax“¸h²decimal“constan˜t¸i“²[1Ž‘,‘ÄLpage“270].‘âThis“is“fedŽ¡‘Ito–}mÍ\calc@multiply@by@real“²that“will“pGerform“the“actual“m¸ãultiplication.‘êIt“isŽ¡‘IimpGortanš¸ãt–÷°that“the“m˜ultiplication“tak˜es“place“at“the“correct“grouping“lev˜el“so“thatŽ¡‘Ithe–.Æcorrect“instance“of“the“µB‘¯7²register“will“bGe“used.‘díAlso“note“that“w¸ãe“do“not“needŽ¡‘Ithe–UUÍ\aftergroup“²mec¸ãhanism“in“this“case.Ž¦‘E±101Ž‘a×pÈ\calc@multiply@by@real\calc@the@ratioŽ¤ ‘E±102Ž‘a×pÈ\begingroupŽ¡‘E±103Ž‘a×pÈ\calc@post@scan}ŽŸ‘I²The–åÍ\begingroup“²inserted“bšGefore“the“Í\calc@post@scan“²will“b˜e“matcš¸ãhed“b˜y“theŽ¤ ‘IÍ\endgroup–m@²generated“as“part“of“the“replacemenš¸ãt“of“a“subsequen˜t“binary“opGeratorŽ¡‘Ior–UUrighš¸ãt“paren˜thesis.Ž¦‘E±104Ž‘Sª®È\def\calc@next@digit{%Ž¤ ‘E±105Ž‘p2È\multiply\calc@Acount\calc@denominatorŽ¡‘E±106Ž‘p2È\advance\calc@numerator‘¹–-\calc@AcountŽ¡‘E±107Ž‘p2È\multiply\calc@numerator‘¹–10Ž¡‘E±108Ž‘p2È\calc@Acount\calc@numeratorŽ¡‘E±109Ž‘p2È\divide\calc@Acount\calc@denominatorŽ¡‘E±110Ž‘p2È\xdef\calc@the@ratio{\calc@the@ratio\number\calc@Acount}}Ž¤‘I²In–rthe“follo¸ãwing“cošGde,‘<¹it“is“imp˜ortanš¸ãt“that“w˜e“ rst“assign“the“result“to“a“dimenŽŸ ‘Iregister.‘qÇOtherwise,–UUT‘þU>Ÿ'EŽ‘ãxX“wš¸ãon't“allo˜w“us“to“m˜ultiply“with“a“real“n˜um˜bGer.Ž¦‘E±111Ž‘Sª®È\def\calc@multiply@by@real#1{\calc@Bdimen–¹–#1\calc@B“\calc@B\calc@Bdimen}Ž¡‘I²(Note–(Othat“this“coGde“wš¸ãouldn't“w˜ork“if“Í\calc@B‘(²w˜ere“a“m˜uglue“register.‘ê´This“isŽ¤ ‘Ithe–ZÌreal“reason“whš¸ãy“the“Ícalc“²pac˜k‘ÿqÇage“došGesn't“supp˜ort“m¸ãuglue“expressions.‘‚+T‘ÿ*ªoŽ¡‘IsuppGort–¾‚mš¸ãuglue“expressions“in“full,‘Íthe“Í\calc@multiply@by@real“²macro“m˜ustŽ¡‘Iuse–²ea“mš¸ãuglue“register“instead“of“Í\calc@Bdimen“²when“Í\calc@B‘²;²is“a“m˜uglue“register;Ž¡‘Iotherwise,‘Šýa–€Bdimen“register“should“bšGe“used.‘òSince“in¸ãteger“expressions“can“app˜earŽ¡‘Ias–Cœpart“of“a“mš¸ãuglue“expression,‘.it“w˜ould“bGe“necessary“to“determine“the“correctŽ¡‘Iregister–UUto“use“eacš¸ãh“time“a“m˜ultiplication“is“made.)ŽŸþ6‘IÐ5.6Ž‘gÀMultiplication–€bš y“real“n˜um˜b`ersŽŸuT‘I²This–==is“similar“to“the“Í\calc@ratio@evaluate“²macro“abGo•¸ãv“e,‘Bexcept–==that“it“is“con-Ž¡‘Isiderably–UUsimpli ed“since“w¸ãe“don't“need“to“calculate“the“factor“explicitly‘ÿ*ª.Ž¦‘E±112Ž‘Sª®È\def\calc@real@multiply\real#1{\endgroupŽ¤ ‘E±113Ž‘a×pÈ\calc@multiply@by@real{#1}\begingroupŽ¡‘E±114Ž‘a×pÈ\calc@post@scan}Ž¡‘E±115Ž‘Sª®È\def\calc@real@divide\real#1{\calc@ratio@evaluate{1pt}{#1pt}}ŽŽŸ’õÿ²10ŽŽŒ‹ „ ª ýs o ý´‘IÌ6Ž‘aLËReps3orting‘fferrorsŽ©ç‘I²If–5Í\calc@post@scan“²reads“a“c¸ãharacter“that“is“not“one“of“`Í+²',–;`Í-²',“`Í*²',“`Í/²',“or‘5`Í)²',“anŽ¤ ‘Ierror–Üûhas“ošGccurred,‘þäand“this“is“rep˜orted“to“the“user.‘¹Violations“in“the“syn¸ãtax“ofŽ¡‘I¸h²n¸ãumeric¸i²s–UUwill“bšGe“detected“and“rep˜orted“b¸ãy“T‘þU>Ÿ'EŽ‘ãxX.ŽŸ‘E±116Ž‘Sª®È\def\calc@error#1{%Ž¤ ‘E±117Ž‘a×pÈ\errhelp{Calc–¹–error:“I“expected“to“see“one“of:“+“-“*“/“)}%Ž¡‘E±118Ž‘a×pÈ\errmessage{Invalid–¹–character“`#1'“in“arithmetic“expression}}Ž¡‘E±119Ž‘Sª®Êhó5ùž" cmmi9É=Ñpack¾9ageŽ‘#g ÊiŽŸ!Ä‘IÌReferencesŽ¦‘I²[1]ŽŽ‘XŽ;ó.Œ-ø cmcsc10ÙD.–ôE.“Knuth².›b ÎThe‘ü1T‘þKß'EŽ‘Í-Xb–ÿ}'o“ok‘Ãú²(Computers–°m&“T¸ãypGesetting“V‘ÿ*ªolume“A).˜Addison-Ž¤ ‘XŽ;W–ÿ*ªesley“,–UUReading,“Massac•¸ãh“usetts,‘UU1986.ŽŸ‘I[2]ŽŽ‘XŽ;ÙL.‘´WLampor‘ÿUTt².‘ÄaÎLŸý雑üQíó/t}\Êcmti7ÚAŽŽ‘ÙÎT‘þKß'EŽ‘Í-X,‘ÓPA‘¢ìDo–ÿ}'cument›£)Pr“ep“ar“ation˜System.‘Äa²Addison-W–ÿ*ªesley“,‘ƒýRead-Ž¡‘XŽ;ing,›UUMassac•¸ãh“usetts,˜Second˜edition˜1994/1985.ŽŽŸ’õÿ11ŽŽŒø‘‡ƒ’À;説 ó/t}\Êcmti7ó.Œ-ø cmcsc10ó-2Ç@Écmbx8ó&ÒoÇw cmss9ó%ÂÖN  cmbx12ó$ßCÊscmtt8ó#ý': cmti10ó"ßê cmmi10ó 0e—rcmmi7óKñ`y cmr10óÙ“ Rcmr7ù–(ßßßßßßß