÷ƒ’À;è 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�ŒÌtro�s3ductionŽŸç�‘IóKñ`y cmr10²Arithmetic–’in“T�‘þU>Ÿ'EŽ‘ãxX“is“done“using“lo•¸ãw-lev“el–’op�Gerations“suc�¸ãh“as“ó"ßêŸ'EŽ‘ãxX.Ž¡�‘XThe–­Ÿin x“expressions“can“b�Ge“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“ob�Gey“the“normal“scoping“rules.)‘ÎéInŽ¡�‘Istandard–ë§L�Ÿýó5�‘üff±AŽŽ‘͉²T�‘þU>Ÿ'EŽ‘ãxX,‘;the“argumenš¸ãts“to“these“commands“m˜ust“b�Ge“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š¸ãyp�Ges.‘–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“b�Gelo˜w.Ž¡�‘XIn–e•the“follo•¸ãwing,‘i¥w“e–e•shall“use“standard“T�‘þU>Ÿ'EŽ‘ãxX“terminology‘ÿ*ª.‘¢‡The“corresp�GondenceŽ¡�‘Ib�Get•¸ã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“corresp�GondŽ¡�‘Ito–tvT�‘þU>Ÿ'EŽ‘ãxX's“counš¸ãt“registers;‘they“hold“quan˜tities“of“t˜yp�Ge“¸h²n˜um˜b�Ger¸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š¸ãyp�Ges“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“yp�Ge;Ž¡����‘X¸ŽŽŽ‘b²m•¸ãultiplication›ˆýb“y˜a˜Îr–ÿ}'e“al‘‘†²n•¸ãum“b�Ger˜(i.e.,‘±Üa˜n“um“b•Ger˜with˜a˜fractional˜part)˜can˜b“eަ�‘bp�Gerformed–4Çon“a“register“of“anš¸ãy“t˜yp�Ge,‘;Jbut“the“stretc˜h“and“shrink“comp�Gonen˜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“op�Gerators“(the“tok˜ens“`Í+²',‘àw`Í-²',ަ�‘I`Í*²',‘Àûand–›ä`Í/²'“with“their“usual“meaning)“using“the“familiar“in x“notation;‘Ùµparen�¸ãthesesަ�‘Imaš¸ãy–bÔb�Ge“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–Þ{b�Ge“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“sp�Gecialަ�‘Isynš¸ãtax;–UUsee“b�Gelo˜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“do�Gesn't“allo˜w“un˜t˜yp�Ged“constan˜ts“to“b�Ge“assigned“to“aަ�‘Idimen‘UUregister.ަ�‘XThe–ÂÕÍcalc“²pacš¸ãk‘ÿqÇage“also“allo˜ws“m˜ultiplication“and“division“b˜y“real“n˜um˜b�Gers.ަ�‘IHo•¸ãw“ev“er,‘”îa–ˆ6sp�Gecial“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“b�Geަ�‘Iused–’Úfor“mš¸ãultiplication/division.‘*UThe“ rst“form“has“the“ob˜vious“meaning,‘â:andަ�‘Ithe–Ùsecond“form“denotes“the“n•¸ãum“b�Ger–Ù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“comp�Gonen˜ts“of“glue“when“glue“is“m˜ultipliedޤ �‘Ibš¸ãy–UUa“real“n˜um˜b�Ger.‘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“b�Ge“used“to“enhance“readabilit˜y‘ÿ*ª.)Ž¡�‘XWhen–‘`T�‘þU>Ÿ'EŽ‘ãxX“p�Gerforms“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“comp�Gosite“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“b�Ger¸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“b�Ger¸iŽŽ¦��‘Xh²m�¸ãultiply–UUor“divide¸iŽ���’µcŽŽŽ’»1Ù!–UUÍ*�Ÿÿ±12Ž‘#�¸j“Í/�Ÿÿ±12ŽŽ¦��‘X¸h²real‘UUn•¸ãum“b�Ger¸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“pro�Gduct“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˜b�Ger¸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“op�Gerations“in“a“mac�¸ãhine-Ž¡�‘Ico�Gde–¹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“do�Ges“the“follo˜wingŽ¡�‘I[In•¸ãv‘ÿqÇariance›Prop�Gert“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“co�Gde“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“b�Ge“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“lo�Gcal“assignmen�¸ãt“to“the“register“on“the“lhs.Ž¡����‘X¸ŽŽŽ‘b²\��ŸÎ:´,‘þ¢_·!±[´C�}‚±]ŽŽ‘<<²"–ªïmeans“\sa•¸ãv“e–ªïthe“co�Gde“µC‘b ²unš¸ãtil“the“curren˜t“group“(scop�Ge)“ends,‘ÀUthenŽ© �‘bexecute–UUit."‘qÇThis“corresp�Gonds“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“co�Gde“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“clobb�Geredަ�‘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˜b�Ge˜simpler˜if˜all˜terms˜are˜treatedަ�‘Ithe–UUsame“w•¸ãa“y‘ÿ*ª.ަ�‘XThe–Z_co�Gde“sequence“\¸f›ª¨²[–þUX[µT�Ÿÿ±2Ž‘|s²]“]˜¸g–Ï~µB‘Oï¸ “µB‘¼­²+‘<<µA²"–Z_can“b�Ge“translated“inš¸ãto“the“follo˜wingަ�‘Iequiv›ÿqÇalen�¸ãt–«Âco�Gde“sequence:‘¢\¸f��ŸÎ:´,‘þ¢_·!±[´B•W=· ´B“±+´A±]ŽŽ‘0‘'²[–þUX[µT�Ÿÿ±2Ž‘|s²]“]‘ª¨¸g²".‘uThis–«Âobserv˜ation“turns“out“toަ�‘Ib�Ge–‚û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“sp�Geci 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��ŸÎ:´,‘þ¢_·!±[´B�W=· ´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��ŸÎ:´,‘þ¢_·!±[´B�W=· ´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“prop�Gert�¸ãy“is“attained.ޤ �‘XBy–w"insp�Gection“of“this“translation“sc•¸ãheme,‘•w“e–w"see“that“wš¸ãe“ha˜v˜e“to“generate“theŽ¡�‘Ifollo�¸ãwing‘UUco�Gde:ŽŸ{û����‘X¸ŽŽŽ‘b²w•¸ãe›C^m“ust˜generate˜\¸f��ŸÎ:´,‘þ¢_·!±[´B�W=· ´A±]ŽŽ–"`ã¸f��ŸÎ:´,‘þ¢_·!±[´B�W=· ´A±]ŽŽ“²"˜at˜the˜left˜b�Gorder˜of˜an˜expressionŽ¡�‘b(i.e.,‘cfor–`Yeacš¸ãh“left“paren˜thesis“and“the“implicit“left“paren˜thesis“at“the“b�Gegin-Ž¡�‘bning–UUof“the“whole“expression);Ž©=þ����‘X¸ŽŽŽ‘b²w•¸ãe›à¾m“ust˜generate˜\¸gµA–¯q¸(“µB›€q¸gµA“¸(“µB˜²"–à¾at“the“righ�¸ãt“b�Gorder“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��ŸÎ:´,‘þ¢_·!±[´B�W=· ´A±]ŽŽ‘"`ã²";ަ����‘X¸ŽŽŽ‘b²when–—±wš¸ãe“see“(exp�Gect)“a“n˜umeric“quan˜tit˜y‘ÿ*ª,‘èHw˜e“insert“the“assignmen˜t“co�GdeŽ¡�‘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“b�Ger¸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˜yp�Ge“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˜yp�Ge“as“the“whole“expression,‘©Ÿand“allŽ¡�‘I¸h²nš¸ãumeric¸i²s–UUin“an“¸h²in˜teger“factor¸i“²are“¸h²n˜um˜b�Ger¸i²s.Ž¡�‘XW‘ÿ*ªe›¥ha•¸ãv“e˜to˜ensure˜that˜µA˜²and˜µB‘%r²alw“a“ys˜ha“v“e˜an˜appropriate˜t“yp�Ge˜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“exp�Gected,‘w˜e“m˜ust“c˜hange“µA“²and“µBŽ¡�‘I²to–ÆBrefer“to“inš¸ãteger“t˜yp�Ge“registers.‘BW‘ÿ*ªe“can“accomplish“this“b˜y“including“instructionsŽ¡�‘Ito–acš¸ãhange“the“t˜yp�Ge“of“µA“²and“µB‘„Ò²to“in˜teger“t˜yp�Ge“as“part“of“the“replacemen˜t“co�GdeŽ¡�‘Ifor–ë`¸²;‘m5if“wš¸ãe“app�Gend“suc˜h“instructions“to“the“replacemen˜t“cošGde“describ˜ed“ab˜o•¸ãv“e,Ž¡�‘Iwš¸ãe–Çalso“ensure“that“the“t˜yp�Ge-c˜hange“is“lo�Gcal“(pro˜vided“that“the“t˜yp�Ge-c˜hangingŽ¡�‘Iinstructions–4¾only“ha•¸ãv“e–4¾lo�Gcal“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“op�Gen-group“and“close-groupŽ¡�‘Ic•¸ãharacters.‘’uThis›µ�a“v“oids˜problems˜with˜spacing˜in˜math˜(as˜p�Goin“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“b�Ge“ev‘ÿqÇaluated.Ž¡�‘XThe–”Ðthird“argumen�¸ãt“(the“lv‘ÿqÇalue)“will“b�Ge“used“as“a“pre x“to“a“register“thatŽ¡�‘Iconš¸ãtains–UUthe“v‘ÿqÇalue“of“the“sp�Geci ed“expression“(the“fourth“argumen˜t).Ž¡�‘XIn–<=general,‘tuan“lv‘ÿqÇalue“is“anš¸ãything“that“ma˜y“b�Ge“follo˜w˜ed“b˜y“a“v‘ÿqÇariable“of“the“appro-Ž¡�‘Ipriate–€mt�¸ãyp�Ge.‘ó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˜co�Gde˜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“p�Goinš¸ã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“b�Geen“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,‘¿corresp�Gonding“toŽ¡�‘Ithe–UUtš¸ãyp�Ges“¸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›UUlo�Gcally–ÿ*ª.‘qÇW“e˜m�¸ãust˜allo�Gcate˜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–UUop�Gerator,“Í\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“b�Ge“aŽ¡�‘I¸h²nš¸ãumeric¸i–ûN²of“some“sort;‘Qthis“allo˜ws“unary“`Í+²'“and“unary“`Í-²'“to“b�Ge“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��ŸÎ:´,‘þ¢_·!±[´B�W=· ´A±]ŽŽ–"`ã¸f��ŸÎ:´,‘þ¢_·!±[´B�W=· ´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“b�Geen“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.‘ÕDep�Gending–‡¯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“b�Ge“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“do�Gcumenš¸ãts“that“do“not“use“the“extra“expressiv˜e“p�Go˜w˜er“pro˜videdŽ¡�‘Ibš¸ãy–UUthe“Ícalc“²pac˜k‘ÿqÇage“only“su er“a“minim˜um“slo˜wdo˜wn“in“pro�Gcessing“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“co�Gde,“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˜op�Gerators,–Íô`Í*²'˜and˜`Í/²',“ma•¸ãy˜b�Ge˜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“op�Gerator“alw•¸ãa“ys›]ha“v“e˜in“teger˜t“yp�Ge;‘,°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“op�Geration“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“co�Gde“for“a“righš¸ãt“paren˜thesis“(whic˜h“w˜as“deriv˜ed“toޤ �‘Ib�Ge‘�(\¸gµA–ºË¸(“µB›€q¸gµA“¸(“µB˜²"–�(in“Section“4).‘õAAfter“this“co�Gde,‘Ìthe“con�¸ãtrol“is“returnedŽ¡�‘Ito–UîÍ\calc@post@scan“²in“order“to“lo�Gok“for“another“righš¸ãt“paren˜thesis“or“a“binaryŽ¡�‘Iop�Gerator.Ž©��‘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²,‘êãdep�GendingŽ¡�‘Ion–^wthe“preceding“cš¸ãharacter.‘�-Those“macros“b�Goth“forw˜ard“the“con˜trol“to“the“macroŽ¡�‘IÍ\calc@ratio@evaluate²,‘ô¾whicš¸ãh–¡©p�Gerforms“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“b�Ge“mš¸ãultiplied“b˜y“the“ratio“as“so�Gon“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“`opp�Gosite'Ž¡�‘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“temp�Gorary“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“pro�Gcedure“isŽ¡�‘Ithis:‘‹ßFirst,‘e¤the›bat•¸ãw“o˜expressions˜are˜ev‘ÿqÇaluated˜and˜co�Gerced˜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“temp�Gorary“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Ž¡�‘Ip�Goin�¸ã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˜p�Goin“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“b�Ge“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“p�Gerform“the“actual“m�¸ãultiplication.‘êIt“isŽ¡�‘Iimp�Gortanš¸ã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“b�Ge“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“op�GeratorŽ¡�‘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˜b�Ger.ަ��‘E�±111Ž‘Sª®È\def\calc@multiply@by@real#1{\calc@Bdimen–¹–#1\calc@B“\calc@B\calc@Bdimen}Ž¡�‘I²(Note–(Othat“this“co�Gde“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Ž¡�‘Isupp�Gort–¾‚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“b�Ge“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“ab�Go•¸ã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ËRep�s3orting‘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�¸ãyp�Gesetting“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ù–(ßßßßßßß