| 1 |
% $Id: format.mp,v 1.2 2004/09/19 21:47:10 karl Exp $ |
| 2 |
% Public domain. |
| 3 |
|
| 4 |
% Macros for generating typeset pictures of computed numbers |
| 5 |
|
| 6 |
% format(f,x) typeset generalized number x using format string f |
| 7 |
% Mformat(f,x) like format, but x is in Mlog form (see marith.mp) |
| 8 |
% init_numbers(s,m,x,sn,e) choose typeset style given sample sign, mantissa,... |
| 9 |
% roundd(x,d) round numeric x to d places right of decimal point |
| 10 |
% Fe_base what precedes the exponent for typeset powers of 10 |
| 11 |
% Fe_plus plus sign if any for typesetting positive exponents |
| 12 |
% Ten_to[] powers of ten for indices 0,1,2,3,4 |
| 13 |
|
| 14 |
% Other than the above-documented user interface, all externally visible names |
| 15 |
% start with F and end with _. |
| 16 |
|
| 17 |
|
| 18 |
if unknown Fmfont_: % If this file has not been read yet |
| 19 |
begingroup interim warningcheck:=0; % Allow big numbers in token lists |
| 20 |
|
| 21 |
input string |
| 22 |
|
| 23 |
|
| 24 |
%%%%%%%%%%%%%%%%%%%%%%% Choosing the Layout %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 25 |
|
| 26 |
picture Fmneg_, Femarker_, Feneg_, Fe_base, Fe_plus; |
| 27 |
string Fmfont_, Fefont_; |
| 28 |
numeric Fmscale_, Fescale_, Feraise_; |
| 29 |
|
| 30 |
|
| 31 |
% Argument s is a leading minus sign; m is a 1-digit mantissa; |
| 32 |
% x is whatever follows the mantissa; sn is a leading minus for the exponent; |
| 33 |
% and e is a 1-digit exponent. Numbers in scientific notation are constructed |
| 34 |
% by placing these peices side-by-side; decimal numbers use only m and/or s. |
| 35 |
% To get exponenets with leading plus signs, assign to Fe_plus after calling |
| 36 |
% init_numbers. To do something special with a unit mantissa followed by x, |
| 37 |
% assign to Fe_base after calling init_numbers. |
| 38 |
vardef init_numbers(expr s, m, x, sn, e) = |
| 39 |
Fmneg_ := s; |
| 40 |
for p within m: |
| 41 |
Fmfont_ := fontpart p; |
| 42 |
Fmscale_ := xxpart p; |
| 43 |
exitif true; |
| 44 |
endfor |
| 45 |
Femarker_ := x; |
| 46 |
Feneg_ := sn; |
| 47 |
for p within e: |
| 48 |
Fefont_ := fontpart p; |
| 49 |
Fescale_ := xxpart p; |
| 50 |
Feraise_ := ypart llcorner p; |
| 51 |
exitif true; |
| 52 |
endfor |
| 53 |
Fe_base := Fline_up_("1" infont Fmfont_ scaled Fmscale_, Femarker_); |
| 54 |
Fe_plus := nullpicture; |
| 55 |
enddef; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
%%%%%%%%%%%%%%%%%%%%%%% Low-Level Typesetting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 60 |
|
| 61 |
vardef Fmant_(expr x) = |
| 62 |
(decimal abs x infont Fmfont_ scaled Fmscale_) |
| 63 |
enddef; |
| 64 |
|
| 65 |
vardef Fexp_(expr x) = |
| 66 |
(decimal x infont Fefont_ scaled Fescale_ shifted (0,Feraise_)) |
| 67 |
enddef; |
| 68 |
|
| 69 |
vardef Fline_up_(text t_) = |
| 70 |
save p_, c_; |
| 71 |
picture p_; p_=nullpicture; |
| 72 |
pair c_; c_=(0,0); |
| 73 |
for q_=t_: |
| 74 |
addto p_ also q_ if string q_: infont defaultfont scaled defaultscale fi |
| 75 |
shifted c_; |
| 76 |
c_ := (xpart lrcorner p_, 0); |
| 77 |
endfor |
| 78 |
p_ |
| 79 |
enddef; |
| 80 |
|
| 81 |
vardef Fdec_o_(expr x) = |
| 82 |
if x<0: Fline_up_(Fmneg_, Fmant_(x)) |
| 83 |
else: Fmant_(x) |
| 84 |
fi |
| 85 |
enddef; |
| 86 |
|
| 87 |
vardef Fsci_o_(expr x, e) = |
| 88 |
Fline_up_(if x<0: Fmneg_,fi |
| 89 |
if abs x =1: Fe_base else: Fmant_(x), Femarker_ fi, |
| 90 |
if e<0: Feneg_ else: Fe_plus fi, |
| 91 |
Fexp_(abs e)) |
| 92 |
enddef; |
| 93 |
|
| 94 |
|
| 95 |
% Assume troffmode=1 implies troff mode. This internal is present |
| 96 |
% since version 0.99, and should be set in the change file. |
| 97 |
% As a default, fall back to the previous 'prologues=1' test. |
| 98 |
% The mpx file mechanism requires separate input files here. |
| 99 |
if troffmode=1: input troffnum |
| 100 |
else: |
| 101 |
if prologues=1: input troffnum |
| 102 |
else: input texnum |
| 103 |
fi |
| 104 |
fi |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
%%%%%%%%%%%%%%%%%%%%%%% Scaling and Rounding %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 109 |
|
| 110 |
% Find a pair p where x=xpart p*10**ypart p and either p=(0,0) or xpart p is |
| 111 |
% between 1000 and 9999.99999. This is the `exponent form' of x. |
| 112 |
vardef Feform_(expr x) = |
| 113 |
interim warningcheck:=0; |
| 114 |
if string x: |
| 115 |
if unknown Mzero: scantokens "input marith"; fi |
| 116 |
Meform(Mlog_str x) |
| 117 |
else: |
| 118 |
save b, e; |
| 119 |
b=x; e=0; |
| 120 |
if abs b>=10000: (b/10, 1) |
| 121 |
elseif b=0: origin |
| 122 |
else: forever: |
| 123 |
exitif abs b>=1000; |
| 124 |
b:=b*10; e:=e-1; |
| 125 |
endfor |
| 126 |
(b, e) |
| 127 |
fi |
| 128 |
fi |
| 129 |
enddef; |
| 130 |
|
| 131 |
% The marith.mp macros include a similar macro Meform that converts from |
| 132 |
% `Mlog form' to exponent form. |
| 133 |
|
| 134 |
|
| 135 |
% In case rounding has made the xpart of an exponent form number too large, |
| 136 |
% fix it. |
| 137 |
vardef Feadj_(expr x, y) = |
| 138 |
if abs x>=10000: (x/10, y+1) else: (x,y) fi |
| 139 |
enddef; |
| 140 |
|
| 141 |
|
| 142 |
% Round x to d places right of the decimal point. When d<0, round to the |
| 143 |
% nearest multiple of 10 to the -d. |
| 144 |
vardef roundd(expr x, d) = |
| 145 |
if abs d >4: |
| 146 |
if d>0: x else: 0 fi |
| 147 |
elseif d>0: |
| 148 |
save i; i=floor x; |
| 149 |
i + round(Ten_to[d]*(x-i))/Ten_to[d] |
| 150 |
else: |
| 151 |
round(x/Ten_to[-d])*Ten_to[-d] |
| 152 |
fi |
| 153 |
enddef; |
| 154 |
|
| 155 |
Ten_to0=1; Ten_to1=10; Ten_to2=100; Ten_to3=1000; Ten_to4=10000; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
% Round an exponent form number p to k significant figures. |
| 160 |
primarydef p Fprec_ k = |
| 161 |
Feadj_ (roundd(xpart p,k-4), ypart p) enddef; |
| 162 |
|
| 163 |
% Round an exponent form number p to k digits right of the decimal point. |
| 164 |
primarydef p Fdigs_ k = |
| 165 |
Feadj_ (roundd(xpart p,k+ypart p), ypart p) enddef; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
%%%%%%%%%%%%%%%%%%%%%%% High-Level Routines %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 170 |
|
| 171 |
% The following operators convert z from exponent form and produce typeset |
| 172 |
% output: Formsci_ generates scientific notation; Formdec_ generates decimal |
| 173 |
% notation; and Formgen_ generates whatever is likely to be most compact. |
| 174 |
|
| 175 |
vardef Formsci_(expr z) = Fsci_o_(xpart z/1000, ypart z +3) enddef; |
| 176 |
|
| 177 |
vardef Formdec_(expr z) = |
| 178 |
if ypart z>0: Formsci_(z) |
| 179 |
else: |
| 180 |
Fdec_o_(xpart z |
| 181 |
if ypart z>=-4: /Ten_to[-ypart z] |
| 182 |
else: for i=ypart z upto -5: /(10) endfor/10000 fi) |
| 183 |
fi |
| 184 |
enddef; |
| 185 |
|
| 186 |
vardef Formgen_(expr q) = |
| 187 |
clearxy; (x,y)=q; |
| 188 |
if x=0: Formdec_ |
| 189 |
elseif y>=-6: Formdec_ |
| 190 |
else: Formsci_ |
| 191 |
fi (q) |
| 192 |
enddef; |
| 193 |
|
| 194 |
|
| 195 |
def Fset_item_(expr s) = |
| 196 |
if s<>"": s infont defaultfont scaled defaultscale, fi |
| 197 |
enddef; |
| 198 |
|
| 199 |
|
| 200 |
% For each format letter, the table below tells how to round and typeset |
| 201 |
% a quantity z in exponent form. |
| 202 |
string fmt_[]; |
| 203 |
fmt_[ASCII "e"] = "Formsci_(z Fprec_ p)"; % scientific, p significant figures |
| 204 |
fmt_[ASCII "f"] = "Formdec_(z Fdigs_ p)"; % decimal, p digits right of the point |
| 205 |
fmt_[ASCII "g"] = "Formgen_(z Fprec_ p)"; % decimal or scientific, p sig. figs. |
| 206 |
fmt_[ASCII "G"] = "Formgen_(z Fdigs_ p)"; % decimal or scientific, p digits |
| 207 |
|
| 208 |
|
| 209 |
% The format and Mformat macros take a format string f and generate typeset |
| 210 |
% output for a numeric quantity x. String f should contain a `%' followed by |
| 211 |
% an optional number and one of the format letters defined above. The number |
| 212 |
% should be an integer giving the precision (default 3). |
| 213 |
|
| 214 |
vardef dofmt_@#(expr f, x) = |
| 215 |
if f="": nullpicture |
| 216 |
else: |
| 217 |
interim warningcheck:=0; |
| 218 |
save k, l, s, p, z; |
| 219 |
pair z; z=@#(x); |
| 220 |
k = 1 + cspan(f, "%"<>); |
| 221 |
l-k = cspan(substring(k,infinity)of f, isdigit); |
| 222 |
p = if l>k: scantokens substring(k,l)of f else: 3 fi; |
| 223 |
string s; s=fmt_[ASCII substring (l,l+1) of f]; |
| 224 |
if unknown s: |
| 225 |
if k<=length f: errmessage("No valid format letter found in "&f); fi |
| 226 |
s = "nullpicture"; |
| 227 |
fi |
| 228 |
Fline_up_( |
| 229 |
Fset_item_(substring (0,k-1) of f) |
| 230 |
scantokens s, |
| 231 |
Fset_item_(substring (l+1,infinity) of f) |
| 232 |
nullpicture) |
| 233 |
fi |
| 234 |
enddef; |
| 235 |
|
| 236 |
vardef format(expr f, x) = dofmt_.Feform_(f,x) enddef; |
| 237 |
vardef Mformat(expr f, x) = dofmt_.Meform(f,x) enddef; |
| 238 |
|
| 239 |
|
| 240 |
endgroup; % restore warningcheck to previous value |
| 241 |
fi % end the if..fi that encloses this file |