| 1 |
\NeedsTeXFormat{LaTeX2e} |
| 2 |
\ProvidesPackage{fp-upn}[1996/10/21] |
| 3 |
|
| 4 |
%version information |
| 5 |
\def\FP@upnversion{2.1c} |
| 6 |
\message{% |
| 7 |
`UPN Fixed Point Calculator',% |
| 8 |
\space\space\space\space% |
| 9 |
Version \FP@upnversion% |
| 10 |
\space(C) Michael Mehlich% |
| 11 |
\space\space\space\space\space\space\space% |
| 12 |
\space\space\space\space\space\space% |
| 13 |
} |
| 14 |
|
| 15 |
%resolve dependencies |
| 16 |
\RequirePackage{fp} |
| 17 |
\RequirePackage{defpattern} |
| 18 |
|
| 19 |
%%%public area (macros which may be used)%%% |
| 20 |
\def\FPupn#1#2{\FP@@@upn{#1}{#2}} % #1 := eval(#2) |
| 21 |
|
| 22 |
%%%private fp-area (don't use these macros)%%% |
| 23 |
|
| 24 |
%routines for stack manipulation |
| 25 |
|
| 26 |
\edef\FP@stack{()} |
| 27 |
|
| 28 |
\defpattern\FP@@@putstack[(#2)#3]{\edef\FP@stack{(#3,#2)}} |
| 29 |
\defpattern\FP@@@putstack[()#2]{\edef\FP@stack{(#2)}} |
| 30 |
\def\FP@@putstack(#1)#2{\FP@@@putstack[(#1)#2]} |
| 31 |
\def\FP@putstack#1{\expandafter\FP@@putstack\FP@stack{#1}} |
| 32 |
|
| 33 |
\defpattern\FP@@@getstack[(#2)#3]{\edef\FP@stack{()}\edef#2{#3}} |
| 34 |
\defpattern\FP@@@getstack[(#2)#3,#4]{\edef\FP@stack{(#4)}\edef#2{#3}} |
| 35 |
\defpattern\FP@@@getstack[(#2)]{\FP@errmessage{UPN stack is empty}} |
| 36 |
\def\FP@@getstack(#1)#2{\FP@@@getstack[(#2)#1]} |
| 37 |
\def\FP@getstack#1{\expandafter\FP@@getstack\FP@stack{#1}} |
| 38 |
|
| 39 |
%local debug macro |
| 40 |
\def\FP@upn@debug#1{\FP@debug{upn: #1}} |
| 41 |
\def\FP@upnc@debug#1{\FP@upn@debug{calculating\space\string"#1\string"}} |
| 42 |
\def\FP@upnc@constant#1{\FP@upn@debug{constant\space\space\space\space\string"#1\string"}} |
| 43 |
\def\FP@upnc@result#1{\FP@upn@debug{result\space\space\space\space\space\space\string"#1\string"}} |
| 44 |
|
| 45 |
%call fp-functions with parameters from stack |
| 46 |
\def\FP@const#1{% |
| 47 |
\FP@upnc@constant{#1}% |
| 48 |
\expandafter\FP@putstack\csname FP#1\endcsname% |
| 49 |
} |
| 50 |
\def\FP@nullop#1{% |
| 51 |
\FP@upnc@debug{#1()}% |
| 52 |
\csname FP#1\endcsname\FP@valc% |
| 53 |
\FP@upnc@result{\FP@valc}% |
| 54 |
\FP@putstack\FP@valc% |
| 55 |
} |
| 56 |
\def\FP@monop#1{% |
| 57 |
\FP@getstack\FP@vala% |
| 58 |
\FP@upnc@debug{#1(\FP@vala)}% |
| 59 |
\csname FP#1\endcsname\FP@valc\FP@vala% |
| 60 |
\FP@upnc@result{\FP@valc}% |
| 61 |
\FP@putstack\FP@valc% |
| 62 |
} |
| 63 |
\def\FP@monopb#1{% |
| 64 |
\FP@getstack\FP@vala% |
| 65 |
\FP@upnc@debug{#1(\FP@vala)}% |
| 66 |
\csname FP#1\endcsname\FP@valc\FP@vald\FP@vala% |
| 67 |
\FP@upnc@result{\FP@valc}% |
| 68 |
\FP@putstack\FP@vald\FP@putstack\FP@valc% |
| 69 |
} |
| 70 |
\def\FP@binop#1{% |
| 71 |
\FP@getstack\FP@vala\FP@getstack\FP@valb% |
| 72 |
\FP@upnc@debug{\FP@vala\space #1 \FP@valb}% |
| 73 |
\csname FP#1\endcsname\FP@valc\FP@vala\FP@valb% |
| 74 |
\FP@upnc@result{\FP@valc}% |
| 75 |
\FP@putstack\FP@valc% |
| 76 |
} |
| 77 |
\def\FP@binopv#1{% |
| 78 |
\FP@getstack\FP@vala\FP@getstack\FP@valb% |
| 79 |
\FP@upnc@debug{\FP@valb\space #1 \FP@vala}% |
| 80 |
\csname FP#1\endcsname\FP@valc\FP@valb\FP@vala% |
| 81 |
\FP@upnc@result{\FP@valc}% |
| 82 |
\FP@putstack\FP@valc% |
| 83 |
} |
| 84 |
\def\FP@binopb#1{% |
| 85 |
\FP@getstack\FP@vala\FP@getstack\FP@valb% |
| 86 |
\FP@upnc@debug{#1(\FP@valb:\FP@vala)}% |
| 87 |
\csname FP#1\endcsname\FP@valc\FP@valb\FP@vala% |
| 88 |
\FP@upnc@result{\FP@valc}% |
| 89 |
\FP@putstack\FP@valc% |
| 90 |
} |
| 91 |
\def\FP@binopc#1{% |
| 92 |
\FP@getstack\FP@vala\FP@getstack\FP@valb% |
| 93 |
\FP@upnc@debug{\FP@valb-th #1 of \FP@vala}% |
| 94 |
\csname FP#1\endcsname\FP@valc\FP@vala\FP@valb% |
| 95 |
\FP@upnc@result{\FP@valc}% |
| 96 |
\FP@putstack\FP@valc% |
| 97 |
} |
| 98 |
\def\FP@binopd#1{% |
| 99 |
\FP@getstack\FP@vala\FP@getstack\FP@valb% |
| 100 |
\FP@upnc@debug{#1(\FP@valb,\FP@vala)}% |
| 101 |
\csname FP#1\endcsname\FP@valc\FP@vala\FP@valb% |
| 102 |
\FP@upnc@result{\FP@valc}% |
| 103 |
\FP@putstack\FP@valc% |
| 104 |
} |
| 105 |
|
| 106 |
%the calculation loop |
| 107 |
|
| 108 |
%put value to stack |
| 109 |
\def\FP@put#1\@upnend{% |
| 110 |
\FP@upnc@result{#1}% |
| 111 |
\FP@putstack{#1}% |
| 112 |
} |
| 113 |
|
| 114 |
\defpattern\FP@upn[#2#3]{% |
| 115 |
\ifx#2\@undefined% |
| 116 |
\FP@upnc@debug{#2#3}% |
| 117 |
\expandafter\expandafter\expandafter\FP@put\csname #2#3\endcsname\@upnend% |
| 118 |
\else\ifcat\noexpand#2\relax% |
| 119 |
\FP@upnc@debug{\string#2#3}% |
| 120 |
\expandafter\FP@put#2#3\@upnend% |
| 121 |
\else\ifcat#22\relax% |
| 122 |
\FP@upnc@debug{\string#2#3}% |
| 123 |
\expandafter\FP@put#2#3\@upnend% |
| 124 |
\else% |
| 125 |
\FP@upnc@debug{#2#3}% |
| 126 |
\expandafter\expandafter\expandafter\FP@put\csname #2#3\endcsname\@upnend% |
| 127 |
\fi\fi\fi% |
| 128 |
} |
| 129 |
|
| 130 |
%binary functions from fp-basic |
| 131 |
\defpattern\FP@upn[+]{\FP@binop{add}} |
| 132 |
\defpattern\FP@upn[add]{\FP@binopv{add}} |
| 133 |
\defpattern\FP@upn[-]{\FP@binop{sub}} |
| 134 |
\defpattern\FP@upn[sub]{\FP@binopv{sub}} |
| 135 |
\defpattern\FP@upn[*]{\FP@binop{mul}} |
| 136 |
\defpattern\FP@upn[mul]{\FP@binopv{mul}} |
| 137 |
\defpattern\FP@upn[/]{\FP@binop{div}} |
| 138 |
\defpattern\FP@upn[div]{\FP@binopv{div}} |
| 139 |
|
| 140 |
%unary functions from fp-basic |
| 141 |
\defpattern\FP@upn[abs]{\FP@monop{abs}} |
| 142 |
\defpattern\FP@upn[neg]{\FP@monop{neg}} |
| 143 |
\defpattern\FP@upn[sgn]{\FP@monop{sgn}} |
| 144 |
|
| 145 |
%binary functions from fp-addons |
| 146 |
\defpattern\FP@upn[min]{\FP@binopd{min}} |
| 147 |
\defpattern\FP@upn[max]{\FP@binopd{max}} |
| 148 |
|
| 149 |
%functions from fp-snap |
| 150 |
\defpattern\FP@upn[round]{\FP@binopb{round}} |
| 151 |
\defpattern\FP@upn[trunc]{\FP@binopb{trunc}} |
| 152 |
\defpattern\FP@upn[clip]{\FP@monop{clip}} |
| 153 |
|
| 154 |
%functions from fp-exp |
| 155 |
\defpattern\FP@upn[e]{\FP@const{e}} |
| 156 |
\defpattern\FP@upn[exp]{\FP@monop{exp}} |
| 157 |
\defpattern\FP@upn[ln]{\FP@monop{ln}} |
| 158 |
\defpattern\FP@upn[pow]{\FP@binopc{pow}} |
| 159 |
\defpattern\FP@upn[root]{\FP@binopc{root}} |
| 160 |
|
| 161 |
%functions from fp-random |
| 162 |
\defpattern\FP@upn[seed]{% |
| 163 |
\FP@getstack\FP@vala\FPseed=\FP@vala% |
| 164 |
} |
| 165 |
\defpattern\FP@upn[random]{\FP@nullop{random}} |
| 166 |
|
| 167 |
%functions from fp-trigo |
| 168 |
\defpattern\FP@upn[pi]{\FP@const{pi}} |
| 169 |
\defpattern\FP@upn[sin]{\FP@monop{sin}} |
| 170 |
\defpattern\FP@upn[cos]{\FP@monop{cos}} |
| 171 |
\defpattern\FP@upn[sincos]{\FP@monopb{sincos}} |
| 172 |
\defpattern\FP@upn[tan]{\FP@monop{tan}} |
| 173 |
\defpattern\FP@upn[cot]{\FP@monop{cot}} |
| 174 |
\defpattern\FP@upn[tancot]{\FP@monopb{tancot}} |
| 175 |
\defpattern\FP@upn[arcsin]{\FP@monop{arcsin}} |
| 176 |
\defpattern\FP@upn[arccos]{\FP@monop{arccos}} |
| 177 |
\defpattern\FP@upn[arcsincos]{\FP@monopb{arcsincos}} |
| 178 |
\defpattern\FP@upn[arctan]{\FP@monop{arctan}} |
| 179 |
\defpattern\FP@upn[arccot]{\FP@monop{arccot}} |
| 180 |
\defpattern\FP@upn[arctancot]{\FP@monopb{arctancot}} |
| 181 |
|
| 182 |
%stack manipulators |
| 183 |
\defpattern\FP@upn[pop]{% |
| 184 |
\FP@getstack\FP@vala% |
| 185 |
\FP@upn@debug{removing top number \string"\FP@vala\string"}% |
| 186 |
} |
| 187 |
\defpattern\FP@upn[swap]{% |
| 188 |
\FP@getstack\FP@vala\FP@getstack\FP@valb% |
| 189 |
\FP@upn@debug{exchanging top numbers \string"\FP@vala\string" and \string"\FP@valb\string"}% |
| 190 |
\FP@putstack\FP@vala\FP@putstack\FP@valb% |
| 191 |
} |
| 192 |
\defpattern\FP@upn[copy]{% |
| 193 |
\FP@getstack\FP@vala% |
| 194 |
\FP@upn@debug{duplicating top number \string"\FP@vala\string"}% |
| 195 |
\FP@putstack\FP@vala\FP@putstack\FP@vala% |
| 196 |
} |
| 197 |
|
| 198 |
%left-to-right loop |
| 199 |
\defpattern\FP@upn[#2 #3]{\FP@upn[#2]\FP@upn[#3]} |
| 200 |
\defpattern\FP@upn[#2 ]{\FP@upn[#2]} |
| 201 |
\defpattern\FP@upn[ #2]{\FP@upn[#2]} |
| 202 |
|
| 203 |
%get result from stacks with one value only |
| 204 |
\defpattern\FP@@upn@result[(#2)#3]{\edef#2{#3}} |
| 205 |
\defpattern\FP@@upn@result[(#2)]{\FP@errmessage{UPN stack is empty}\edef#2{0}} |
| 206 |
\defpattern\FP@@upn@result[(#2)#3,#4]{\FP@errmessage{Evaluation results in multiple values}\edef#2{#3}} |
| 207 |
\def\FP@upn@result(#1)#2{\FP@@upn@result[(#2)#1]} |
| 208 |
|
| 209 |
%save result of upn-expression to macro |
| 210 |
\def\FP@saveval[#1#2]{% |
| 211 |
\ifx#1\@undefined% |
| 212 |
\let#1\FP@tmp% |
| 213 |
\else\ifcat\noexpand#1\relax% |
| 214 |
\let#1\FP@tmp% |
| 215 |
\else% |
| 216 |
\expandafter\let\csname #1#2\endcsname\FP@tmp% |
| 217 |
\fi\fi% |
| 218 |
} |
| 219 |
|
| 220 |
%the main routine |
| 221 |
\def\FP@@upn#1#2{% |
| 222 |
\bgroup% |
| 223 |
\FP@upn@debug{+++ computing upn-expression \string"#2\string"}% |
| 224 |
\edef\FP@tmp{[#2]}% |
| 225 |
\expandafter\FP@upn\FP@tmp% |
| 226 |
\FP@upn@debug{+++ result of upn-expression is \string"\FP@stack\string"}% |
| 227 |
\expandafter\FP@upn@result\FP@stack\FP@tmp% |
| 228 |
\global\let\FP@tmp\FP@tmp% |
| 229 |
\egroup% |
| 230 |
\FP@saveval[#1]% |
| 231 |
} |
| 232 |
|
| 233 |
\def\FP@@@upn#1#2{% |
| 234 |
\FP@beginmessage{UPN}% |
| 235 |
\FP@@upn{#1}{#2}% |
| 236 |
\FP@endmessage{}% |
| 237 |
} |