Debian Codegraph @

texmf-dist/tex/generic/texdimens/texdimens.tex

raw
1 % This is file texdimens.tex, part of texdimens package, which
2 % is distributed under the LPPL 1.3c. Copyright (c) 2021 Jean-François Burnol
3 % 2021/11/17 v1.1
4 \edef\texdimensendinput{\endlinechar\the\endlinechar%
5 \catcode`\noexpand _=\the\catcode`\_%
6 \catcode`\noexpand @=\the\catcode`\@\relax\noexpand\endinput}%
7 \endlinechar13\relax%
8 % only for using \p@ (also \z@ now) of Plain. Check if \p@, \z@ exists?
9 \catcode`\_=11 \catcode`\@=11
10 % so tempted to do \input xintkernel.sty to have some utilities...
11 % not even a \@gobble in Plain...
12 \def\texdimenfirstofone#1{#1}%
13 \def\texdimengobtilminus#1-{}%
14 \def\texdimenzerominusfork #10-#2#3\krof {#2}%
15 %
16 % \texdimenuu, \texdimenuudown, \texdimenuuup
17 % ===========================================
18 %
19 % Mathematics
20 % -----------
21 %
22 % In the entire discussion here, "uu" stands for some core unit,
23 % or some unit corresponding to an internal dimension > 1pt.
24 %
25 % Main question at the origin of this file was:
26 % Is T sp attainable from unit "uu"?.
27 % If not, what is largest dimension < Tsp which is?
28 %
29 % Here we suppose T>0. TeX parsing of D uu is equivalent to:
30 %
31 % D uu --> N = round(D * 65536) --> T = trunc (N * phi)
32 %
33 % phi>1 is the conversion factor associated to "uu"
34 % psi=1/phi, psi<1. Define U(N, phi) = trunc (N * phi)
35 %
36 % U(N,phi) is thus the strictly increasing sequence,
37 % indexed by non-negative integers, of non-negative
38 % attainable dimensions. (in sp unit)
39 %
40 % T>0, then:
41 %
42 % U(N)<= T < U(N+1) iff N = ceil((T+1)psi) - 1
43 % U(M)< T <= U(M+1) iff M = ceil(T psi) - 1
44 %
45 % In other words:
46 %
47 % - the largest attainable dimension not exceeding T sp
48 % is obtained via the integer "Zd = ceil((T+1)psi) - 1 = N",
49 % (i.e. find D with Zd=round(65536 D) then "D uu" is "down"
50 % approximation)
51 %
52 % - the smallest attainable dimension at least equal to T sp
53 % is obtained from the integer "Zu = ceil(T psi) = M + 1"
54 %
55 % - the two "Z"'s are either equal (i.e. T is attained) or Zu=Zd+1.
56 %
57 % \texdimenUU macros use round((T+0.5)*psi)
58 % -----------------------------------------
59 %
60 % case1: M = N, i.e. Zd<Zu, i.e. T is not attainable:
61 % M=N=Zd < T psi < (T+1) psi <= N+1=Zu
62 %
63 % Then clearly R = round((T+0.5)psi) is either Zd or Zu.
64 % We will not know which one before computing trunc(R phi)
65 % and check if it is < T or > T.
66 %
67 % As will be explained later trunc(R phi) can be computed very
68 % easily by hijacking TeX's handling of dimensions, no \numexpr
69 % chains is needed.
70 %
71 % case2: M = N - 1, i.e. T = Zd = Zu is attained:
72 % T psi <= N < (T+1) psi, T = trunc(N phi)
73 %
74 % Let v=(T+0.5)psi. As v = T psi + 0.5 psi it is < N+0.5
75 % And as v = (T+1)psi - 0.5psi it is > N - 0.5.
76 % So R = round(v) = N.
77 %
78 % We thus have the initial observation which was at the core of this
79 % package initial release:
80 %
81 % - compute R = round((T+0.5) psi)
82 %
83 % - if T is attained, then T = trunc(R * phi)
84 %
85 % - if T is not attained then either { Zd = R and Zu = R+1 } or
86 % {Zd = R-1 and Zu = R}.
87 %
88 % How do we check if R = Zd or Zu? We need to evaluate trunc(R phi) and
89 % compare it with T. This trunc(R phi) can be computed the following way:
90 %
91 % - obtain D pt from \the\dimexpr R sp. Knuth's algorithm guarantees
92 % that R = round(D * 65536)
93 %
94 % - then D uu where uu is the unit with conversion factor phi is
95 % converted by TeX into "trunc(R phi) sp", i.e. trunc(R phi) =
96 % \number\dimexpr Duu\relax, where D pt = \the\dimexpr Rsp\relax.
97 %
98 % Conclusion:
99 %
100 % 1. the macro \texdimenuu does the one-liner R=round((T+0.5) psi)
101 % then \the\dimexpr Rsp\relax gives "Dpt", the "pt" is removed,
102 % we have a decimal D such that "Duu" does what one wants.
103 %
104 % 2. to get Zd (resp. Zu) one can use the D obtained in 1. and check
105 % if "D uu" is at most (or at least) the user input dimension.
106 %
107 % For units with conversion factor phi>2, a simplification is possible.
108 % In that case let X = round(T psi) (it has the advantage compared to
109 % R that we can apply the formula without checking the sign of T).
110 %
111 % Going back to our earlier analyis, now with psi < 0.5 (1uu>2pt)
112 %
113 % case1: T is not attainable
114 % M=N=Zd < T psi < (T+1) psi <= N+1=Zu
115 % As Zd < T psi < Zu, we have round(T psi) = Zd or Zu
116 %
117 % case2: T is attained, i.e. T psi <= N < (T+1) psi.
118 % As psi<0.5, and T psi + psi > N, we have T psi > N - 0.5.
119 % And T psi <= N so N = round(T psi).
120 %
121 % So, for psi < 0.5, the X=round(T psi) can play the same role as
122 % R=round((T+0.5)psi). If T is attained, we get the decimal D from this
123 % X and if T is not attained we know that X is either Zd or Zu.
124 %
125 % The computations of X and Y=trunc(X phi) can be done independently of
126 % sign of T. But the final test has to be changed to Y < T if T < 0 and
127 % then one must replace X by X+1. So we must filter out the sign of the
128 % input.
129 %
130 % Going back to the 1<phi<2 case, psi>0.5, then it would be slightly
131 % less costly to compute X = round(T psi) than R = round((T + 0.5) psi),
132 % but if we then realize that trunc(X phi) < T we do not yet know if
133 % trunc((X+1) phi) = T or is > T, i.e. we don't know if Zd =X or X+1,
134 % and we can not tell yet if T is attained or not.
135 %
136 % In contrast if we find out that trunc(R phi)<T, we then know for sure
137 % that Zd=R, Zu=R+1 and that T is not attained.
138 %
139 % Problems with \maxdimen in the obtention of Zu and Zd
140 % -----------------------------------------------------
141 %
142 % Obtaining R = round((T+0.5)psi) has no risk of overflow.
143 % But checking as described above which one of Zd or Zu (or both)
144 % is R goes via a test computation which will cause overflow
145 % if by bad luck R = Zu and Zu will give rise to a decimal D
146 % such that D uu > \maxdimen.
147 %
148 % For T=\maxdimen (or very close) this is what happens for the units
149 % "dd", "nc", and "in".
150 %
151 % Besides, it turns out that this test which is done to decide whether
152 % R=Zu or R=Zd, and on which the initial implementation of the macros
153 % "up" and "down" was done at 0.9 gamma release is a bit costly.
154 %
155 % At 1.0 release, all the "up" and "down" macros were re-implemented
156 % via a more stubborn usage of the ceil() based formulae for Zd and Zu.
157 % This made all usable even with \maxdimen input and besided, proved
158 % on average slightly faster.
159 %
160 % Overcoming the ceil() stumbling block for \texdimenUU{up,down}
161 % --------------------------------------------------------------
162 %
163 % I will in what follows refer to trunc(), floor() or ceil() only for
164 % positive arguments, obtained as ratios x/y or sometimes as a numexpr
165 % "scaling" operation" x*y/z which uses temporarily use doubled
166 % precision.
167 %
168 % As \numexpr's x/y is round(x/y), with rounding away from zero, we have
169 % access to floor(t) for t>=0 as round(t+0.5)-1 and for t>0 also as
170 % round(t-0.5). The former may cause overflow as it involves
171 % (2x+y)/(2y) but the latter (2x-y)/(2y) will not overflow if x comes
172 % from a dimension as 2x<2**31 then.
173 %
174 % ceil(t) is more complex as it is floor(t)+1 only for t not an integer.
175 % Let's explain how to overcome the challenge for Zd and the "in" unit,
176 % i.e. a conversion factor of 7227/100.
177 %
178 % We want Zd = ceil((T+1)*100/7227) - 1, with T assumed positive.
179 %
180 % Let T = k*7227 + r with 0<= r < 7227, 0<=k, and r>0 if k=0.
181 %
182 % (T+1)*100/7227 becomes 100*k + (r+1)*100/7227 and thus
183 %
184 % Zd = 100 * k + ceil(x) - 1
185 %
186 % with x = n*100/7227, and n = 1+r, so 0<n<=7227
187 %
188 % Here we have a nice situation 0 < x <= 100. Then:
189 %
190 % ceil(x) = 100 - floor(100 - x)
191 % = 100 - (round(100 - x + 0.5) - 1)
192 % = 101 - round(100 * (1 - n/7227) + 0.5)
193 % = 101 - round((200 * (7227 - n) + 7227)/14454)
194 %
195 % We can thus achieve the computation of Zd = ceil((T+1)*100/7227) - 1
196 % for T>0 without overflow in \numexpr this way:
197 %
198 % k = floor(T/7227) = round(T/7227 - 0.5)
199 % = round((2*T - 7227) / 14454) (T>0 used here)
200 %
201 % r = T - 7227 * k = T modulo 7227
202 %
203 % Zd = 100 * k + 100 - round( (201*7227 - 200*(r+1))/14454 )
204 %
205 % Everything here is computable within \numexpr and has absolutely no
206 % potential overflow problem at all. The same analysis can be done for
207 % Zu = ceil(T*100/7227) and for all core TeX units. See the comments
208 % below for all obtained formulae and some additional details.
209 %
210 {\catcode`p 12\catcode`t 12
211 \csname expandafter\endcsname\gdef\csname texdimenstrippt\endcsname#1pt{#1}}%
212 %
213 % pt
214 %
215 \def\texdimenpt#1{\expandafter\texdimenstrippt\the\dimexpr#1\relax}%
216 %
217 % bp 7227/7200 = 803/800
218 %
219 \def\texdimenbp#1{\expandafter\texdimenstrippt\the\dimexpr\numexpr(%
220 \expandafter\texdimen_bpnddd_signcheck
221 \the\numexpr2*\dimexpr#1\relax\relax)*400/803sp\relax}%
222 \def\texdimen_bpnddd_signcheck#1{\texdimengobtilminus#1-1+#1}%
223 %
224 % nd 685/642
225 %
226 \def\texdimennd#1{\expandafter\texdimenstrippt\the\dimexpr\numexpr(%
227 \expandafter\texdimen_bpnddd_signcheck
228 \the\numexpr2*\dimexpr#1\relax\relax)*321/685sp\relax}%
229 %
230 % dd 1238/1157
231 %
232 \def\texdimendd#1{\expandafter\texdimenstrippt\the\dimexpr\numexpr(%
233 \expandafter\texdimen_bpnddd_signcheck
234 \the\numexpr2*\dimexpr#1\relax\relax)*1157/2476sp\relax}%
235 %
236 % mm 7227/2540 phi now >2, use from here on the X = round(T psi) approach
237 %
238 \def\texdimenmm#1{\expandafter\texdimenstrippt\the\dimexpr(#1)*2540/7227\relax}%
239 %
240 % pc 12/1
241 %
242 \def\texdimenpc#1{\expandafter\texdimenstrippt\the\dimexpr(#1)/12\relax}%
243 %
244 % nc 1370/107
245 %
246 \def\texdimennc#1{\expandafter\texdimenstrippt\the\dimexpr(#1)*107/1370\relax}%
247 %
248 % cc 14856/1157
249 %
250 \def\texdimencc#1{\expandafter\texdimenstrippt\the\dimexpr(#1)*1157/14856\relax}%
251 %
252 % cm 7227/254
253 %
254 \def\texdimencm#1{\expandafter\texdimenstrippt\the\dimexpr(#1)*254/7227\relax}%
255 %
256 % in 7227/100
257 %
258 \def\texdimenin#1{\expandafter\texdimenstrippt\the\dimexpr(#1)*100/7227\relax}%
259 %
260 % "up and down macros"
261 % --------------------
262 %
263 % The notation <u/v> means u/v in numexpr, which does rounding
264 % away from zero. It is essential that the argument be >-0.5 else <x+1>
265 % not same as <x>+1. All formulae are overflow free.
266 %
267 % The comments are for T > 0.
268 %
269 % Roughly such an approach works for phi = a/b > 1, such that:
270 %
271 % a*(2b+1)<2**31 if a is odd, <2**32 if a is even
272 %
273 % This is true for all core units with quite some margin, the one with
274 % largest a*b being phi=7227/2540 for "mm".
275 %
276 % Note: for a unit such as "ex" or "em" where morally b=65536=2**16,
277 % this limits to a<=16383 if a is odd and to a<=32766 if a is even.
278 % Thus the general \texdimenwithunit{dim1}{dim2} (which for dim2<1pt
279 % computes basically an "up" value) can *not imitate fully* this scheme.
280 %
281 % The macros and formulas in the comments were obtained from a template
282 % (see file generateupdownmacros.py at the project repository),
283 % and we could actually combine them into a generic macro handling
284 % general a/b (assuming above bounds are verified).
285 % But for the the sake of efficiency, this is "rolled-out" here unit per unit.
286 %
287 \def\texdimenuudownup_zero#1;{\z@\relax}%
288 \def\texdimenuudownup_neg#1-{-#1}%
289 % bp 803/800
290 % T = 803 k + r
291 % Zd = 800 k + 800 - <(1284003 - 1600 r)/1606>
292 \def\texdimenbpdown#1{\expandafter\texdimenstrippt\the\dimexpr
293 \expandafter\texdimenbpdown_a\the\numexpr\dimexpr#1;%
294 }%
295 \def\texdimenbpdown_a#1{\texdimenzerominusfork
296 #1-\texdimenuudownup_zero
297 0#1\texdimenuudownup_neg
298 0-{}%
299 \krof \texdimenbpdown_b#1}%
300 \def\texdimenbpdown_b#1;{\expandafter\texdimenbpdown_c\the\numexpr(2*#1-803)/1606;#1;}%
301 \def\texdimenbpdown_c#1;#2;{\expandafter\texdimenbpdown_d\the\numexpr#2-803*#1;#1;}%
302 \def\texdimenbpdown_d#1;#2;{\numexpr800*#2+800-(1284003-1600*#1)/1606sp\relax}%
303 % Zu = 800 k + 800 + 1 - <(1285603 - 1600 r)/1606>
304 \def\texdimenbpup#1{\expandafter\texdimenstrippt\the\dimexpr
305 \expandafter\texdimenbpup_a\the\numexpr\dimexpr#1;%
306 }%
307 \def\texdimenbpup_a#1{\texdimenzerominusfork
308 #1-\texdimenuudownup_zero
309 0#1\texdimenuudownup_neg
310 0-{}%
311 \krof \texdimenbpup_b#1}%
312 \def\texdimenbpup_b#1;{\expandafter\texdimenbpup_c\the\numexpr(2*#1-803)/1606;#1;}%
313 \def\texdimenbpup_c#1;#2;{\expandafter\texdimenbpup_d\the\numexpr#2-803*#1;#1;}%
314 \def\texdimenbpup_d#1;#2;{\numexpr800*#2+801-(1285603-1600*#1)/1606sp\relax}%
315 % nd 685/642
316 % T = 685 k + r
317 % Zd = 642 k + 642 - <(878941 - 1284 r)/1370>
318 \def\texdimennddown#1{\expandafter\texdimenstrippt\the\dimexpr
319 \expandafter\texdimennddown_a\the\numexpr\dimexpr#1;%
320 }%
321 \def\texdimennddown_a#1{\texdimenzerominusfork
322 #1-\texdimenuudownup_zero
323 0#1\texdimenuudownup_neg
324 0-{}%
325 \krof \texdimennddown_b#1}%
326 \def\texdimennddown_b#1;{\expandafter\texdimennddown_c\the\numexpr(2*#1-685)/1370;#1;}%
327 \def\texdimennddown_c#1;#2;{\expandafter\texdimennddown_d\the\numexpr#2-685*#1;#1;}%
328 \def\texdimennddown_d#1;#2;{\numexpr642*#2+642-(878941-1284*#1)/1370sp\relax}%
329 % Zu = 642 k + 642 + 1 - <(880225 - 1284 r)/1370>
330 \def\texdimenndup#1{\expandafter\texdimenstrippt\the\dimexpr
331 \expandafter\texdimenndup_a\the\numexpr\dimexpr#1;%
332 }%
333 \def\texdimenndup_a#1{\texdimenzerominusfork
334 #1-\texdimenuudownup_zero
335 0#1\texdimenuudownup_neg
336 0-{}%
337 \krof \texdimenndup_b#1}%
338 \def\texdimenndup_b#1;{\expandafter\texdimenndup_c\the\numexpr(2*#1-685)/1370;#1;}%
339 \def\texdimenndup_c#1;#2;{\expandafter\texdimenndup_d\the\numexpr#2-685*#1;#1;}%
340 \def\texdimenndup_d#1;#2;{\numexpr642*#2+643-(880225-1284*#1)/1370sp\relax}%
341 % dd 1238/1157
342 % T = 1238 k + r
343 % Zd = 1157 k + 1157 - <(1431828 - 1157 r)/1238>
344 \def\texdimendddown#1{\expandafter\texdimenstrippt\the\dimexpr
345 \expandafter\texdimendddown_a\the\numexpr\dimexpr#1;%
346 }%
347 \def\texdimendddown_a#1{\texdimenzerominusfork
348 #1-\texdimenuudownup_zero
349 0#1\texdimenuudownup_neg
350 0-{}%
351 \krof \texdimendddown_b#1}%
352 \def\texdimendddown_b#1;{\expandafter\texdimendddown_c\the\numexpr(#1-619)/1238;#1;}%
353 \def\texdimendddown_c#1;#2;{\expandafter\texdimendddown_d\the\numexpr#2-1238*#1;#1;}%
354 \def\texdimendddown_d#1;#2;{\numexpr1157*#2+1157-(1431828-1157*#1)/1238sp\relax}%
355 % Zu = 1157 k + 1157 + 1 - <(1432985 - 1157 r)/1238>
356 \def\texdimenddup#1{\expandafter\texdimenstrippt\the\dimexpr
357 \expandafter\texdimenddup_a\the\numexpr\dimexpr#1;%
358 }%
359 \def\texdimenddup_a#1{\texdimenzerominusfork
360 #1-\texdimenuudownup_zero
361 0#1\texdimenuudownup_neg
362 0-{}%
363 \krof \texdimenddup_b#1}%
364 \def\texdimenddup_b#1;{\expandafter\texdimenddup_c\the\numexpr(#1-619)/1238;#1;}%
365 \def\texdimenddup_c#1;#2;{\expandafter\texdimenddup_d\the\numexpr#2-1238*#1;#1;}%
366 \def\texdimenddup_d#1;#2;{\numexpr1157*#2+1158-(1432985-1157*#1)/1238sp\relax}%
367 % mm 7227/2540
368 % T = 7227 k + r
369 % Zd = 2540 k + 2540 - <(36715307 - 5080 r)/14454>
370 \def\texdimenmmdown#1{\expandafter\texdimenstrippt\the\dimexpr
371 \expandafter\texdimenmmdown_a\the\numexpr\dimexpr#1;%
372 }%
373 \def\texdimenmmdown_a#1{\texdimenzerominusfork
374 #1-\texdimenuudownup_zero
375 0#1\texdimenuudownup_neg
376 0-{}%
377 \krof \texdimenmmdown_b#1}%
378 \def\texdimenmmdown_b#1;{\expandafter\texdimenmmdown_c\the\numexpr(2*#1-7227)/14454;#1;}%
379 \def\texdimenmmdown_c#1;#2;{\expandafter\texdimenmmdown_d\the\numexpr#2-7227*#1;#1;}%
380 \def\texdimenmmdown_d#1;#2;{\numexpr2540*#2+2540-(36715307-5080*#1)/14454sp\relax}%
381 % Zu = 2540 k + 2540 + 1 - <(36720387 - 5080 r)/14454>
382 \def\texdimenmmup#1{\expandafter\texdimenstrippt\the\dimexpr
383 \expandafter\texdimenmmup_a\the\numexpr\dimexpr#1;%
384 }%
385 \def\texdimenmmup_a#1{\texdimenzerominusfork
386 #1-\texdimenuudownup_zero
387 0#1\texdimenuudownup_neg
388 0-{}%
389 \krof \texdimenmmup_b#1}%
390 \def\texdimenmmup_b#1;{\expandafter\texdimenmmup_c\the\numexpr(2*#1-7227)/14454;#1;}%
391 \def\texdimenmmup_c#1;#2;{\expandafter\texdimenmmup_d\the\numexpr#2-7227*#1;#1;}%
392 \def\texdimenmmup_d#1;#2;{\numexpr2540*#2+2541-(36720387-5080*#1)/14454sp\relax}%
393 % pc 12/1
394 % T = 12 k + r
395 % Zd = 1 k + 1 - <(17 - 1 r)/12>
396 \def\texdimenpcdown#1{\expandafter\texdimenstrippt\the\dimexpr
397 \expandafter\texdimenpcdown_a\the\numexpr\dimexpr#1;%
398 }%
399 \def\texdimenpcdown_a#1{\texdimenzerominusfork
400 #1-\texdimenuudownup_zero
401 0#1\texdimenuudownup_neg
402 0-{}%
403 \krof \texdimenpcdown_b#1}%
404 \def\texdimenpcdown_b#1;{\expandafter\texdimenpcdown_c\the\numexpr(#1-6)/12;#1;}%
405 \def\texdimenpcdown_c#1;#2;{\expandafter\texdimenpcdown_d\the\numexpr#2-12*#1;#1;}%
406 \def\texdimenpcdown_d#1;#2;{\numexpr#2+1-(17-#1)/12sp\relax}%
407 % Zu = 1 k + 1 + 1 - <(18 - 1 r)/12>
408 \def\texdimenpcup#1{\expandafter\texdimenstrippt\the\dimexpr
409 \expandafter\texdimenpcup_a\the\numexpr\dimexpr#1;%
410 }%
411 \def\texdimenpcup_a#1{\texdimenzerominusfork
412 #1-\texdimenuudownup_zero
413 0#1\texdimenuudownup_neg
414 0-{}%
415 \krof \texdimenpcup_b#1}%
416 \def\texdimenpcup_b#1;{\expandafter\texdimenpcup_c\the\numexpr(#1-6)/12;#1;}%
417 \def\texdimenpcup_c#1;#2;{\expandafter\texdimenpcup_d\the\numexpr#2-12*#1;#1;}%
418 \def\texdimenpcup_d#1;#2;{\numexpr#2+2-(18-#1)/12sp\relax}%
419 % nc 1370/107
420 % T = 1370 k + r
421 % Zd = 107 k + 107 - <(147168 - 107 r)/1370>
422 \def\texdimenncdown#1{\expandafter\texdimenstrippt\the\dimexpr
423 \expandafter\texdimenncdown_a\the\numexpr\dimexpr#1;%
424 }%
425 \def\texdimenncdown_a#1{\texdimenzerominusfork
426 #1-\texdimenuudownup_zero
427 0#1\texdimenuudownup_neg
428 0-{}%
429 \krof \texdimenncdown_b#1}%
430 \def\texdimenncdown_b#1;{\expandafter\texdimenncdown_c\the\numexpr(#1-685)/1370;#1;}%
431 \def\texdimenncdown_c#1;#2;{\expandafter\texdimenncdown_d\the\numexpr#2-1370*#1;#1;}%
432 \def\texdimenncdown_d#1;#2;{\numexpr107*#2+107-(147168-107*#1)/1370sp\relax}%
433 % Zu = 107 k + 107 + 1 - <(147275 - 107 r)/1370>
434 \def\texdimenncup#1{\expandafter\texdimenstrippt\the\dimexpr
435 \expandafter\texdimenncup_a\the\numexpr\dimexpr#1;%
436 }%
437 \def\texdimenncup_a#1{\texdimenzerominusfork
438 #1-\texdimenuudownup_zero
439 0#1\texdimenuudownup_neg
440 0-{}%
441 \krof \texdimenncup_b#1}%
442 \def\texdimenncup_b#1;{\expandafter\texdimenncup_c\the\numexpr(#1-685)/1370;#1;}%
443 \def\texdimenncup_c#1;#2;{\expandafter\texdimenncup_d\the\numexpr#2-1370*#1;#1;}%
444 \def\texdimenncup_d#1;#2;{\numexpr107*#2+108-(147275-107*#1)/1370sp\relax}%
445 % cc 14856/1157
446 % T = 14856 k + r
447 % Zd = 1157 k + 1157 - <(17194663 - 1157 r)/14856>
448 \def\texdimenccdown#1{\expandafter\texdimenstrippt\the\dimexpr
449 \expandafter\texdimenccdown_a\the\numexpr\dimexpr#1;%
450 }%
451 \def\texdimenccdown_a#1{\texdimenzerominusfork
452 #1-\texdimenuudownup_zero
453 0#1\texdimenuudownup_neg
454 0-{}%
455 \krof \texdimenccdown_b#1}%
456 \def\texdimenccdown_b#1;{\expandafter\texdimenccdown_c\the\numexpr(#1-7428)/14856;#1;}%
457 \def\texdimenccdown_c#1;#2;{\expandafter\texdimenccdown_d\the\numexpr#2-14856*#1;#1;}%
458 \def\texdimenccdown_d#1;#2;{\numexpr1157*#2+1157-(17194663-1157*#1)/14856sp\relax}%
459 % Zu = 1157 k + 1157 + 1 - <(17195820 - 1157 r)/14856>
460 \def\texdimenccup#1{\expandafter\texdimenstrippt\the\dimexpr
461 \expandafter\texdimenccup_a\the\numexpr\dimexpr#1;%
462 }%
463 \def\texdimenccup_a#1{\texdimenzerominusfork
464 #1-\texdimenuudownup_zero
465 0#1\texdimenuudownup_neg
466 0-{}%
467 \krof \texdimenccup_b#1}%
468 \def\texdimenccup_b#1;{\expandafter\texdimenccup_c\the\numexpr(#1-7428)/14856;#1;}%
469 \def\texdimenccup_c#1;#2;{\expandafter\texdimenccup_d\the\numexpr#2-14856*#1;#1;}%
470 \def\texdimenccup_d#1;#2;{\numexpr1157*#2+1158-(17195820-1157*#1)/14856sp\relax}%
471 % cm 7227/254
472 % T = 7227 k + r
473 % Zd = 254 k + 254 - <(3678035 - 508 r)/14454>
474 \def\texdimencmdown#1{\expandafter\texdimenstrippt\the\dimexpr
475 \expandafter\texdimencmdown_a\the\numexpr\dimexpr#1;%
476 }%
477 \def\texdimencmdown_a#1{\texdimenzerominusfork
478 #1-\texdimenuudownup_zero
479 0#1\texdimenuudownup_neg
480 0-{}%
481 \krof \texdimencmdown_b#1}%
482 \def\texdimencmdown_b#1;{\expandafter\texdimencmdown_c\the\numexpr(2*#1-7227)/14454;#1;}%
483 \def\texdimencmdown_c#1;#2;{\expandafter\texdimencmdown_d\the\numexpr#2-7227*#1;#1;}%
484 \def\texdimencmdown_d#1;#2;{\numexpr254*#2+254-(3678035-508*#1)/14454sp\relax}%
485 % Zu = 254 k + 254 + 1 - <(3678543 - 508 r)/14454>
486 \def\texdimencmup#1{\expandafter\texdimenstrippt\the\dimexpr
487 \expandafter\texdimencmup_a\the\numexpr\dimexpr#1;%
488 }%
489 \def\texdimencmup_a#1{\texdimenzerominusfork
490 #1-\texdimenuudownup_zero
491 0#1\texdimenuudownup_neg
492 0-{}%
493 \krof \texdimencmup_b#1}%
494 \def\texdimencmup_b#1;{\expandafter\texdimencmup_c\the\numexpr(2*#1-7227)/14454;#1;}%
495 \def\texdimencmup_c#1;#2;{\expandafter\texdimencmup_d\the\numexpr#2-7227*#1;#1;}%
496 \def\texdimencmup_d#1;#2;{\numexpr254*#2+255-(3678543-508*#1)/14454sp\relax}%
497 % in 7227/100
498 % T = 7227 k + r
499 % Zd = 100 k + 100 - <(1452427 - 200 r)/14454>
500 \def\texdimenindown#1{\expandafter\texdimenstrippt\the\dimexpr
501 \expandafter\texdimenindown_a\the\numexpr\dimexpr#1;%
502 }%
503 \def\texdimenindown_a#1{\texdimenzerominusfork
504 #1-\texdimenuudownup_zero
505 0#1\texdimenuudownup_neg
506 0-{}%
507 \krof \texdimenindown_b#1}%
508 \def\texdimenindown_b#1;{\expandafter\texdimenindown_c\the\numexpr(2*#1-7227)/14454;#1;}%
509 \def\texdimenindown_c#1;#2;{\expandafter\texdimenindown_d\the\numexpr#2-7227*#1;#1;}%
510 \def\texdimenindown_d#1;#2;{\numexpr#200+100-(1452427-2*#100)/14454sp\relax}%
511 % Zu = 100 k + 100 + 1 - <(1452627 - 200 r)/14454>
512 \def\texdimeninup#1{\expandafter\texdimenstrippt\the\dimexpr
513 \expandafter\texdimeninup_a\the\numexpr\dimexpr#1;%
514 }%
515 \def\texdimeninup_a#1{\texdimenzerominusfork
516 #1-\texdimenuudownup_zero
517 0#1\texdimenuudownup_neg
518 0-{}%
519 \krof \texdimeninup_b#1}%
520 \def\texdimeninup_b#1;{\expandafter\texdimeninup_c\the\numexpr(2*#1-7227)/14454;#1;}%
521 \def\texdimeninup_c#1;#2;{\expandafter\texdimeninup_d\the\numexpr#2-7227*#1;#1;}%
522 \def\texdimeninup_d#1;#2;{\numexpr#200+101-(1452627-2*#100)/14454sp\relax}%
523 %
524 % "both in and cm"
525 % ================
526 %
527 % Mathematics
528 % -----------
529 %
530 % Let a and b be two non-negative integers such that U = floor(a 7227/100) =
531 % floor(b 7227/254). It can be proven that a=50k, b=127k for some integer k.
532 % The proof is left to reader. So U = floor(7227 k /2) for some k.
533 %
534 % Let's now find the largest such U <= T. So U = floor(k 7227/2)<= T which is
535 % equivalent (as k is integer) to k 7227/2 <= T + 1/2, i.e.
536 %
537 % kmax = floor((2T+1)/7227)
538 %
539 % If we used for x>0 the formula floor(x)=round(x-1/2)=<x-1/2> we would end
540 % up basically with some 4T hence overflow problems even in \numexpr.
541 % Here I used <.> to denote rounding in the sense of \numexpr. It is not
542 % 1-periodical due to how negative inputs are handled, but here x-1/2>-1/2.
543 %
544 % The following lemma holds: let T be a non-negative integer then
545 %
546 % floor((2T+1)/7227) = <(2T - 3612)/7227>
547 %
548 % So we can compute this k, hence get a=50k, b=127k, all within \numexpr and
549 % avoiding overflow.
550 %
551 % Implementation
552 % --------------
553 %
554 % Regarding the output in pt or sp, we seem to need floor(k 7227/2).
555 % The computation of floor(k 7227/2) as <(7227 k - 1)/2> would require to
556 % check if k==0 so we do it rather as <(7227 k + 1)/2> - 1. No overflow
557 % can arise as k = 297147 for \maxdimen, and then 7227 k = 2**31 - 2279 and
558 % there is ample room for 7227k+1 using \numexpr.
559 %
560 % But this step, as well as initial step to get kmax will require to separate
561 % handling of negative input from positive one.
562 %
563 % Alternative
564 % -----------
565 %
566 % For non-negative T we can compute U = ((T+1)/7227)*7227. If U <= T keep it,
567 % else if U > T, replace it by U - 3614. This is alternative road to the maximal
568 % floor(k 7227/2) at most equal to T.
569 %
570 \def\texdimenbothincm#1{\expandafter\texdimenstrippt\the\dimexpr
571 \expandafter\texdimenboth_a
572 \the\numexpr\dimexpr#1\relax\relax-3612)/7227)*127sp\relax}%
573 \def\texdimenbothcmin#1{\expandafter\texdimenstrippt\the\dimexpr
574 \expandafter\texdimenboth_a
575 \the\numexpr\dimexpr#1\relax\relax-3612)/7227)*50sp\relax}%
576 \def\texdimenboth_a#1{\texdimengobtilminus#1\texdimenboth_neg-\numexpr((2*#1}%
577 \def\texdimenboth_neg-\numexpr((2*-{-\numexpr((2*}%
578 %
579 \def\texdimenbothincmsp#1{\number
580 \expandafter\texdimenbothsp_a\the\numexpr\dimexpr#1\relax\relax
581 -3612)/7227)*7227+1)/2-1\relax}%
582 \def\texdimenbothincmpt#1{\expandafter\texdimenstrippt\the\dimexpr
583 \expandafter\texdimenbothsp_a\the\numexpr\dimexpr#1\relax\relax
584 -3612)/7227)*7227+1)/2-1sp\relax}%
585 \def\texdimenbothsp_a#1{\texdimengobtilminus#1\texdimenbothsp_neg-\numexpr(((2*#1}%
586 \def\texdimenbothsp_neg-\numexpr(((2*-{-\numexpr(((2*}%
587 %
588 \let\texdimenbothcminpt\texdimenbothincmpt
589 \let\texdimenbothcminsp\texdimenbothincmsp
590 %
591 % "both mm and bp"
592 % ================
593 %
594 % Mathematics and Algorithm
595 % -------------------------
596 %
597 % We start from a dimension expressed in sp unit, "T sp". Assume T positive.
598 % We know how to get largest "X sp <= T sp" which is exactly expressible
599 % in mm unit
600 % i.e. can be written X=trunc(a 7227/2540) for some non-negative integer a.
601 % We want to achieve X=trunc(b 803/800) for some b.
602 %
603 % Only the congruence of X modulo 803 matters for this.
604 % It turns out that the mod 803 impossible values are 267, 535, 802.
605 % As pointed out by Ruixi Zhang on the package repo issue #10,
606 % when a<--a+2540, X increases by 7227=9*803 hence the value
607 % modulo 803 does not change. Thus only "a modulo 2540" matters
608 % to check if X(a) is attainable with bp unit. Ruixi Zhang found by
609 % brute force that there are modulo 2540 nine excluded a-values
610 %
611 % Rather than checking if "a mod. 2540" avoids the 9 Ruixi Zhang values
612 % or if "X mod. 803" avoids 267, 535, 802, we will simply basically
613 % check if X sp = \texdimenbp{X sp}bp, as this approach is probably
614 % about the same cost or even less than computing "X mod. 803" and
615 % correspondingly branching.
616 %
617 % The key is that if "a" is bad, then "a-1" is automatically good as
618 % pointed out by R.Z. on #10, which can be seen without knowing the 9
619 % bad congruences, simply by noticing that a<--a-1 modifies X either to
620 % X-2 or X-3, so if X was bad certainly the new one is not.
621 %
622 % Once "a" has gotten its final value, we apply "\the\dimexpr a sp
623 % = D pt" trick to recover the D such that "D mm" gives rise to the found
624 % dimension. We go via this "Dmm" intermediary also to express the final
625 % result as "X sp", because anyhow the "X" we worked with and had in
626 % our token stream has to be recomputed if a<--a-1, so lets always
627 % recompute it from final "a", and this goes via "D mm" (but see
628 % the paragraph MEMO for alternative for this trunc(a 7227/2540) step).
629 %
630 % I will copy here the style I used for bothincm expansion triggering
631 % via an already positioned \dimexpr waiting to output final result.
632 \def\texdimenbothbpmm#1{\expandafter\texdimenstrippt\the\dimexpr
633 \expandafter\texdimenbothbpmm_fork\the\numexpr\dimexpr#1;}%
634 \def\texdimenbothbpmm_fork#1{\texdimenzerominusfork
635 #1-\texdimenbothbpmm_zero
636 0#1\texdimenbothbpmm_neg
637 0-\texdimenbothbpmm_a
638 \krof#1}%
639 % because this is *inside* a pre-positioned \dimexpr, we don't have
640 % to worry about zero output ending up as -0.0
641 \def\texdimenbothbpmm_neg-{-\texdimenbothbpmm_a}%
642 \def\texdimenbothbpmm_zero#1;{\z@\relax}%
643 % now, find X sp <= T sp maximal and expressible in mm unit
644 % it will be X=trunc(a 7227/2540), we first get a candidate for "a"
645 \def\texdimenbothbpmm_a#1;%
646 {\expandafter\texdimenbothbpmm_b\the\numexpr#1*2540/7227;#1;}%
647 % we get in a single line the X from this candidate, hijacking TeX's
648 % built-in *7227/2540... the "MEMO" above explains one could do this
649 % purely within \numexpr, working around its division rounds, and
650 % avoiding overflow, but I suspect this would be more costly.
651 \def\texdimenbothbpmm_b#1;{\expandafter\texdimenbothbpmm_c
652 \the\numexpr\dimexpr\expandafter\texdimenstrippt\the\dimexpr#1spmm;#1;}%
653 % now we have X;a;T;
654 \def\texdimenbothbpmm_c#1;#2;#3;{%
655 % If X>T, our candidate "a=#2" must be decreased by 1 and we go to _ca
656 % The original #3 is not needed anymore
657 \ifnum#1>#3 \expandafter\texdimenbothbpmm_ca\fi
658 % Else we decide whether it is "a" or "a-1" we must use. I preferred
659 % to induce a re-grabbing cost here, rather than have \texdimenbothbpmm_ca
660 % re-grab its arguments from \texdimenbothbpmm_d replacement text.
661 \texdimenbothbpmm_d#1;#2;%
662 }%
663 % Here, dynamically at the time of the concluding \dimexpr, we
664 % check if X sp is expressible in bp unit and then use "a" or "a-1"
665 % accordingly
666 \def\texdimenbothbpmm_d#1;#2;{#2sp%
667 \ifnum\dimexpr
668 \expandafter\texdimenstrippt\the\dimexpr\numexpr(2*#1+1)*400/803spbp=#1
669 \else-1sp\fi
670 % and a \relax to stop the concluding \dimexpr
671 \relax
672 }%
673 % Here we must decrease "a=#2" by 1, recompute X=#1, then loop
674 % back to \texdimenbothbpmm_d. Hesitation between forcing a
675 % re-grab or doing it in one step with the subtraction of 1 done twice
676 \def\texdimenbothbpmm_ca\texdimenbothbpmm_d#1;#2;%
677 {\expandafter\texdimenbothbpmm_cb\the\numexpr#2-1;}%
678 \def\texdimenbothbpmm_cb#1;{%
679 \expandafter\texdimenbothbpmm_d
680 \the\numexpr\dimexpr\expandafter\texdimenstrippt\the\dimexpr#1spmm;#1;%
681 }%
682 % done...
683 % now the lazy way for \texdimenbothmmbp
684 \def\texdimenbothmmbp#1{\expandafter\texdimenstrippt\the\dimexpr
685 \expandafter\texdimenbothmmbp_a\the\numexpr\dimexpr\texdimenbothbpmm{#1}mm;}%
686 % If zero at this stage, we will correctly get 0.0 in the end
687 \def\texdimenbothmmbp_a#1#2;{\numexpr(2*#1#2+\texdimengobtilminus#1-1)*400/803sp\relax}%
688 % \texdimenbothbpmmpt and its alias \texdimenbothmmbppt
689 \def\texdimenbothbpmmpt#1{\texdimenpt{\texdimenbothbpmm{#1}mm}}%
690 \let\texdimenbothmmbppt\texdimenbothbpmmpt
691 % \texdimenbothbpmmsp and its alias \texdimenbothmmbpsp
692 \def\texdimenbothbpmmsp#1{\the\numexpr\dimexpr\texdimenbothbpmm{#1}mm\relax\relax}%
693 \let\texdimenbothmmbpsp\texdimenbothbpmmsp
694 %
695 % \texdimenwithunit
696 % =================
697 %
698 % Mathematics
699 % -----------
700 %
701 % The ex and em units are handled by TeX as if multiplying by a
702 % conversion factor f/65536 (here f sp = 1ex resp. = 1em).
703 %
704 % In particular, for any decimal D, input "D em" is handled the exact
705 % same way as input "D\dimexpr 1em\relax"; this is not
706 % the case for the core units except for pt and pc (and sp), whose
707 % conversion factors are the sole ones with a power of 2 denominator
708 % (respectively 1, 1, and 65536). The further difference is that
709 % for the core units apart from sp, the conversion factor is >1.
710 %
711 % We assume for this discussion T is non-negative.
712 % If f/65536 > 1, the analysis is as above : some dimensions T sp
713 % are not attainable as D uu, but the formula
714 % N=round((2T+1)*32768/f)
715 % will give a suitable decimal D via \the\dimexpr N sp\relax.
716 % (if T=0, we get N=0 as 32768/f<0.5)
717 % This D will let TeX convert D uu into T sp, if the dimension
718 % is attainable else it will be a closest match
719 % either from above or below (not necessarily nearest overall).
720 %
721 % If f/65536=1, attention that above formula would give N=1 for
722 % T=0 (was bug #4).
723 %
724 % If f/65536<1, all dimensions Tsp are attainable as D uu. Indeed
725 % D uu is parsed by TeX via N=round(D*65536), then T=trunc(N*phi),
726 % with phi=f/65536. Starting from T we need to find an N such that
727 % T/phi <= N< (T+1)/phi.
728 %
729 % This is equivalent to ceil(T/phi)<= N < ceil((T+1)/phi)
730 %
731 % Now obsolete remark: let v=(T+0.5)/phi. As its
732 % distance to the extremities is 0.5/phi>0.5, (phi>1) its rounding M
733 % to an integer verifies automatically T/phi < M < (T+1)/phi, so
734 % is a valid candidate. This was used at 0.99 release.
735 % (it is funny that N=round((2T+1)*32768/f) works for all f>0
736 % *except* f=65536).
737 %
738 % The 1.0 release chooses to implement the ceil(T/phi) formula rather as
739 % it is closer to naive expectation "dim1/dim2" of a division.
740 %
741 % It is not obvious to compute this ceil(T/phi) without overflow.
742 %
743 % Implementation
744 % --------------
745 %
746 % \texdimenwithunit{dim1}{dim2}
747 %
748 % First done at 0.99, then refactored at 1.0:
749 % - to add support for dim2<0pt
750 % - to handle differently the dim2<1pt case and make the output
751 % closer to mathematical dim1/dim2
752 %
753 % To handle dim2<0pt, we simply simultaneously do
754 % dim1<-- (-dim1) and dim2<-- (-dim2).
755 %
756 % dim2=0pt is not intercepted and will cause division by zero low-level
757 % error. Code comments below were not adjusted and handle only
758 % dim2>0pt.
759 %
760 % We first get f from dim2 and branch according to whether f>65536,
761 % or f<=65536.
762 % We will also need to check the sign of T (dim1=T sp).
763 % f>65536: we compute round((2T+1)*32768/f)
764 % f=65536: merged with f<65536 branch (as it works and avoids checking for it)
765 % f<65536: 0.99 release used the round((2T+1)*32768/f) formula
766 % (it is funny that it works for all f except for f=65536)
767 %
768 % But the output then diverges noticeably from mathematical
769 % dim1/dim2 "=" T*65536/f, the more so the smaller the dim2.
770 % See issue #16 and also the discussion at #13.
771 %
772 % 1.0 release thus opted for the ceil(T*65536/f) formula, as it is the
773 % smallest allowable choice, hence the closest to naive dim1/dim2.
774 %
775 % To avoid arithmetic overflow issues we first do the euclidean
776 % division T = k f + r, 0<= r < f, 0<= k
777 %
778 % The final result in "sp" unit would be k*65536 + C with
779 % C = ceil(r * 65536/f).
780 %
781 % We don't do this k*65536 explicitly as it may overflow and is
782 % anyhow unneeded: the output will be the integer k concatenated with
783 % the decimal E given by TeX from \the\dimexpr C sp, i.e. such that
784 % E pt = C sp, with C = ceil(r*65536/f).
785 %
786 % As r is at most f-1, r*65536/f is at most 65536-65536/f, and as
787 % 65536>=f (we use this branch also for f=65536), C<=65535. Hence
788 % E is never 1.0 but always "0.<some digits>"
789 %
790 % To compute the Euclidean quotient k in \numexpr we use there
791 % <(2T-f)/(2f)> i.e. round((2T-f)/2f) = trunc(T/f)
792 % as we are careful to never have T=0 in-there...
793 %
794 % Computing C = ceil(r * 65536/f) in \numexpr is the delicate
795 % part, as r can be as large as f-1 hence 65535 and 65535*65536 would
796 % overflow. Let's try anyhow to see how to compute ceil() with round():
797 %
798 % C = 65536 - floor(65536 * (1 - r/f))
799 % = 65536 - round(65536*(f-r)/f - 0.5) (as r<f so no "round(-0.5)=-1")
800 % = 65536 - <(2*65536*(f-r) - f)/(2f)>
801 %
802 % Here the problem is with small r, and large f, and naive implementation
803 % of this formula can overflow...
804 % Let's thus retreat to eTeX scaling operation <r*65536/f> as it
805 % operates with temporary double precision.
806 %
807 % R=round(r*65536/f)=<r*65536/f> is either C-1 or C
808 % Let x = mathematical exact r*65536/f:
809 % - if R < x, C=R+1.
810 % - if R >= x, C=R.
811 %
812 % C=ceil(r*65536/f) is the smallest integer such that
813 % trunc(C*f/65536)>=r, or more precisely (as f<=65536) the
814 % smallest integer with trunc(C*f/65536)=r. So trunc(R*f/65536)
815 % will be either r (then R=C), or r-1, then R=C-1.
816 %
817 % Method from release 1.0: let's TeX compute P=trunc(R*f/65536) itself!
818 % Via P sp = E <f sp> where E is a decimal such that E pt = R sp.
819 % So
820 % - if P>=r (it is then equal to r in fact) then C=R
821 % - if P<r (it is then equal to r-1), then C=R+1.
822 %
823 % New method: overflow-free pure \numexpr way to get the sign of R-x.
824 %
825 % Write R=4*S+t, with say S=<R/4>=round(R/4), so t=-2,-1,0,+1.
826 %
827 % Then R*f-65536*r = 4*(S*f-16384*r)+t*f
828 %
829 % We know that R<=C<65536, so <R/4> <= 16384 and 16384*f
830 % is at worst 2**(14+16)=\maxdimen+1 but we will be in \numexpr,
831 % so no overflow!
832 % And r<f<=65536 so also 16384*r can not overflow.
833 % As |R - r*65536/f|<= 0.5, then |R*f-65536*r|<= f/2, so
834 % 4*|S*f-16384*r| <= 2.5*f is very far from overflow risk
835 %
836 % T>0, 0<f<=65536
837 % k = <(2*T-f)/(2*f)>
838 % r = T - k*f
839 % R=<r*65536/f>
840 % S=<R/4>
841 % t=R-4*S
842 %
843 % IF: 4*(S*f-16384*r)+t*f < 0 THEN C=R+1 ELSE C=R.
844 %
845 % Ept=\the\dimexpr Csp, E=0.d...d
846 %
847 % End expansion with the contatenation k.d...d
848 %
849 \def\texdimenwithunit#1#2{\expandafter\texdimenwithunit_i
850 % no premultiplication of dim1 by 2 as was done for technical
851 % reasons when dim2<1pt branch used round((2T+1)*32768/f)
852 \the\numexpr\dimexpr#2\expandafter;\the\numexpr\dimexpr#1;%
853 }%
854 \def\texdimenwithunit_i#1{%
855 \texdimengobtilminus#1\texdimenwithunit_switchsigns-%
856 \texdimenwithunit_j#1%
857 }%
858 \def\texdimenwithunit_switchsigns-\texdimenwithunit_j-#1;#2%
859 {%
860 % due to \texdimenwithunit_Bneg we can not simply prefix dim1
861 % with -, as -0 is bad there. So let's check also if #2 is 0
862 \texdimenzerominusfork
863 #2-\texdimenwithunit_Bzero % also used in \texdimenwithunit_B
864 0#2\texdimenwithunit_j % abusive shortcut
865 0-{\texdimenwithunit_ic#2}%
866 \krof
867 #1;%
868 }%
869 \def\texdimenwithunit_ic#1#2;{\texdimenwithunit_j#2;-#1}%
870 \def\texdimenwithunit_j#1;#2{%
871 \ifnum#1>\p@\texdimenwithunit_A\fi
872 \texdimenwithunit_B#2#1;%
873 }%
874 % unit>1pt, handle this as for bp.
875 % Attention it would be wrong for unit=1pt!
876 \def\texdimenwithunit_A\fi\texdimenwithunit_B#1#2;#3;{\fi
877 \expandafter\texdimenstrippt
878 \the\dimexpr\numexpr(2*#1#3+\texdimengobtilminus#1-1)*32768/#2sp\relax
879 % - fine if dim1>0, <0, or =0
880 % - with *\p@ better but an early doubled dim2 would complicate 1pt
881 % test and not sure if doing \p@/(2*#2) here advantageous
882 }%
883 % unit<=1pt.
884 % if dim1<0, simply negate result for dim1>0 as it can not possibly be 0.0
885 % Indeed T*65536/f will be at least 1 so its ceil also (in fact ceil
886 % will even be at least 2 if f<65536).
887 % The dim1=0 case must get filtered out due to way of calculating the
888 % "ceil" in \numexpr
889 \def\texdimenwithunit_B#1{\texdimenzerominusfork
890 #1-\texdimenwithunit_Bzero
891 0#1\texdimenwithunit_Bneg
892 0-\texdimenwithunit_Ba
893 \krof#1}%
894 \def\texdimenwithunit_Bzero#1;#2;{0.0}%
895 \def\texdimenwithunit_Ba#1#2;#3;{%
896 % no overflow possible from 2*#1#3 in \numexpr
897 \expanded{\expandafter\texdimenwithunit_Bb
898 \the\numexpr(2*#1#3-#2)/(2*#2);#1#3;#2;}%
899 }%
900 % I could have inserted \expanded\bgroup in \texdimenwithunit_B
901 % but then needed to modify _Bzero (used also by \texdimenwithunit_switchsigns)
902 % so easiest is to simply defined Bneg explicitly here rather than
903 % insisting on deriving it from _Ba
904 \def\texdimenwithunit_Bneg-#1;#2;{%
905 \expanded{-\expandafter\texdimenwithunit_Bb
906 \the\numexpr(2*#2-#1)/(2*#1);#2;#1;}%
907 }%
908 % now k;T;f;. Get the remainder r=T-k*f, and abandon k in the token stream.
909 % the earlier \expanded maintains f-expandability
910 \def\texdimenwithunit_Bb#1;#2;#3;{%
911 #1\expandafter\texdimenwithunit_Bc\the\numexpr#2-#1*#3;#3;%
912 }%
913 % now r;f;. Get R=<r*65536/f>
914 \def\texdimenwithunit_Bc#1;#2;{%
915 \expandafter\texdimenwithunit_Bd\the\numexpr #1*\p@/#2;#1;#2;%
916 }%
917 % R;r;f; Is 4*(S*f-16384*r)+t*f < 0 ? with S=<R/4>, t=R-4S
918 \def\texdimenwithunit_Bd#1;#2;#3;{%
919 \expandafter\texdimenwithunitstripzeroandpt
920 \the\dimexpr\numexpr#1%
921 \ifnum\numexpr 4*((#1/4)*#3-16384*#2)<\numexpr(4*(#1/4)-#1)*#3\relax
922 +1\fi sp\relax
923 }%
924 {\catcode`P12\catcode`T12\lowercase{\gdef\texdimenwithunitstripzeroandpt0#1PT}{#1}}%
925 \texdimensendinput