Debian Codegraph @

texmf-dist/metapost/base/marith.mp

raw
1 % $Id: marith.mp,v 1.2 2004/09/19 21:47:10 karl Exp $
2 % Public domain.
3
4 % Macros for dealing with very large and very small numbers in `Mlog form'.
5
6 % A number x in Mlog form represents mexp(x) if x is an even multiple of
7 % epsilon and -mexp(x) if x is an odd multiple of epsilon, where epsilon=1/65536
8 % is the basic unit for MetaPost's fixpoint numbers. Such numbers can represent
9 % values large as 3.8877e+55 or as small as 1.604e-28. (Anything less than that
10 % is treated as zero.)
11
12 % Mlog_str <string> convert a string like "6.02e23" to Mlog form
13 % Mexp_str <M_numeric> convert from Mlog form to a string like "6.02e23"
14 % Meform(q) find (x,y) such that q is the Mlog form of x*10^y
15 % Mlog <numeric> convert a number to Mlog form
16 % Mexp <M_numeric> convert from Mlog form into ordinary numeric form
17 % Mabs <M_numeric> absolute value
18 % Mlog_Str <string or numeric> convert to Mlog form; unknown results in unknown
19 % <M_numeric> Madd <M_numeric> add
20 % <M_numeric> Msub <M_numeric> subtract
21 % <M_numeric> Mmul <M_numeric> multiply
22 % <M_numeric> Mdiv <M_numeric> divide
23 % <M_numeric> Mleq <M_numeric> the boolean operator <=
24 % Mzero constant that represents zero
25 % Mten constant that represents 10.0
26
27 % All other externally visible names start with `M' and end with `_'.
28
29
30
31 warningcheck := 0; % Need warningcheck:=0 anytime these macros are in use
32
33 if unknown Mzero: % If we have not already seen this file
34
35 newinternal Mzero;
36 Mzero := -16384; % Anything at least this small is treated as zero
37
38
39 input string.mp
40
41
42
43 % Ideal value of mlog(10) is 589.4617838 or 38630967.46/65536. To get an even
44 % numerator, we round to 38630968/65536 or 589.4617920.
45 newinternal Mten;
46 Mten := 589.46179;
47
48 % Convert x*10^y to Mlog form, assuming x is already in Mlog form.
49 primarydef x M_e_ y = (x+Mten*y) enddef;
50
51 def Mgobble_ text t = enddef;
52 pair M_iv_; M_iv_=(0,4);
53
54
55 % String s is a number in scientific notatation with no leading spaces.
56 % Convert it to Mlog form.
57 vardef Mlog_str primary s =
58 save k, t, e, r;
59 string t;
60 if substring(0,1) of s="-":
61 r=-epsilon; t=substring (1,infinity) of s;
62 else:
63 r=0; t=s;
64 fi
65 let e = Mgobble_;
66 if begingroup scantokens substring M_iv_ of t endgroup >= 1000:
67 k = cspan(t,isdigit);
68 t := substring M_iv_ of t & "." & substring(4,k) of t &
69 substring (k if substring(k,k+1) of t=".": +1 fi, infinity) of t;
70 r := r + (k-4)*Mten;
71 fi
72 let e = M_e_;
73 r + Mlog scantokens t
74 enddef;
75
76
77 % Convert x from Mlog form into a pair whose xpart gives a mantissa and whose
78 % ypart gives a power of ten. The constant 1768.38985 is slightly more than
79 % 3Mten as is required to ensure that 3Mten-epsilon<=q+e*Mten<4Mten-epsilon.
80 % This forces the xpart of the result to be between 1000 and 10000.
81 vardef Meform(expr q) =
82 if q<=Mzero: (0,0)
83 else:
84 save e; e=floor((q-1768.38985)/Mten);
85 (Mexp(q-e*Mten), e)
86 fi
87 enddef;
88
89 % Perform the inverse of Mlog_str, converting from Mlog form to a string.
90 vardef Mexp_str primary x =
91 save p;
92 pair p; p=Meform(x);
93 decimal xpart p
94 if ypart p<>0: & "e" & decimal ypart p fi
95 enddef;
96
97
98 vardef Mabs primary x = x*.5*2 enddef;
99
100 % Convert a number to Mlog form
101 vardef Mlog primary x =
102 if x>0: Mabs mlog x
103 elseif x<0: epsilon + Mabs mlog(-x)
104 else: Mzero
105 fi
106 enddef;
107
108
109 % Convert a number from Mlog form
110 vardef Mexp primary x =
111 if x=Mabs x: mexp x
112 else: -mexp x
113 fi
114 enddef;
115
116
117 primarydef a Mmul b =
118 if (a<=Mzero) or (b<=Mzero): Mzero
119 else: (a+b)
120 fi
121 enddef;
122
123
124 primarydef a Mdiv b =
125 if a<=Mzero: Mzero
126 else: (a-b)
127 fi
128 enddef;
129
130
131 % 256ln(1579)=123556596.0003/65536=1885.3240356445
132 secondarydef a Madd b =
133 if a>=b: (Mlog(1579 + Mexp(b Mmul (1885.32404-a))) + a-1885.32404)
134 else: b Madd a
135 fi
136 enddef;
137
138
139 secondarydef a Msub b = a Madd (b-epsilon) enddef;
140
141
142 tertiarydef a Mleq b =
143 (if a=Mabs a:
144 if b=Mabs b: a<=b else: false fi
145 elseif b=Mabs b: true else: b<=a fi
146 )
147 enddef;
148
149
150
151 vardef Mlog_Str primary x =
152 if unknown x: whatever
153 elseif string x: Mlog_str x
154 else: Mlog x
155 fi
156 enddef;
157
158
159 fi % end the if..fi that encloses this file