Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3fp-basics.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3fp-basics.dtx
4 %
5 % Copyright (C) 2011-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3fp-basics} module\\
35 % Floating point arithmetic^^A
36 % }
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 % \date{Released 2026-05-15}
46 %
47 % \maketitle
48 %
49 % \begin{documentation}
50 %
51 % \end{documentation}
52 %
53 % \begin{implementation}
54 %
55 % \section{\pkg{l3fp-basics} implementation}
56 %
57 % \begin{macrocode}
58 %<*code>
59 % \end{macrocode}
60 %
61 % \begin{macrocode}
62 %<@@=fp>
63 % \end{macrocode}
64 %
65 % The \pkg{l3fp-basics} module implements addition, subtraction,
66 % multiplication, and division of two floating points, and the absolute
67 % value and sign-changing operations on one floating point.
68 % All operations implemented in this module yield the outcome of
69 % rounding the infinitely precise result of the operation to the
70 % nearest floating point.
71 %
72 % Some algorithms used below end up being quite similar to some
73 % described in \enquote{What Every Computer Scientist Should Know About
74 % Floating Point Arithmetic}, by David Goldberg, which can be found at
75 % \texttt{http://cr.yp.to/2005-590/goldberg.pdf}.
76 %
77 % \begin{macro}[EXP]
78 % {
79 % \@@_parse_word_abs:N ,
80 % \@@_parse_word_logb:N ,
81 % \@@_parse_word_sign:N ,
82 % \@@_parse_word_sqrt:N ,
83 % }
84 % Unary functions.
85 % \begin{macrocode}
86 \cs_new:Npn \@@_parse_word_abs:N
87 { \@@_parse_unary_function:NNN \@@_set_sign_o:w 0 }
88 \cs_new:Npn \@@_parse_word_logb:N
89 { \@@_parse_unary_function:NNN \@@_logb_o:w ? }
90 \cs_new:Npn \@@_parse_word_sign:N
91 { \@@_parse_unary_function:NNN \@@_sign_o:w ? }
92 \cs_new:Npn \@@_parse_word_sqrt:N
93 { \@@_parse_unary_function:NNN \@@_sqrt_o:w ? }
94 % \end{macrocode}
95 % \end{macro}
96 %
97 % \subsection{Addition and subtraction}
98 %
99 % We define here two functions, \cs{@@_-_o:ww} and \cs{@@_+_o:ww}, which
100 % perform the subtraction and addition of their two floating point
101 % operands, and expand the tokens following the result once.
102 %
103 % A more obscure function, \cs{@@_add_big_i_o:wNww}, is used in
104 % \pkg{l3fp-expo}.
105 %
106 % The logic goes as follows:
107 % \begin{itemize}
108 % \item \cs{@@_-_o:ww} calls \cs{@@_+_o:ww} to do the work, with the
109 % sign of the second operand flipped;
110 % \item \cs{@@_+_o:ww} dispatches depending on the type of floating
111 % point, calling specialized auxiliaries;
112 % \item in all cases except summing two normal floating point numbers,
113 % we return one or the other operands depending on the signs, or
114 % detect an invalid operation in the case of $\infty - \infty$;
115 % \item for normal floating point numbers, compare the signs;
116 % \item to add two floating point numbers of the same sign or of
117 % opposite signs, shift the significand of the smaller one to match the
118 % bigger one, perform the addition or subtraction of significands,
119 % check for a carry, round, and pack using the
120 % \cs[no-index]{@@_basics_pack_\ldots{}} functions.
121 % \end{itemize}
122 % The trickiest part is to round correctly when adding or subtracting
123 % normal floating point numbers.
124 %
125 % \subsubsection{Sign, exponent, and special numbers}
126 %
127 % \begin{macro}[EXP]{\@@_-_o:ww}
128 % The \cs{@@_+_o:ww} auxiliary has a hook: it takes one argument
129 % between the first \cs{s_@@} and \cs{@@_chk:w}, which is applied to
130 % the sign of the second operand. Positioning the hook there means
131 % that \cs{@@_+_o:ww} can still perform the sanity check that it was
132 % followed by \cs{s_@@}.
133 % \begin{macrocode}
134 \cs_new:cpe { @@_-_o:ww } \s_@@
135 {
136 \exp_not:c { @@_+_o:ww }
137 \exp_not:n { \s_@@ \@@_neg_sign:N }
138 }
139 % \end{macrocode}
140 % \end{macro}
141 %
142 % \begin{macro}[EXP]{\@@_+_o:ww}
143 % This function is either called directly with an empty |#1| to
144 % compute an addition, or it is called by \cs{@@_-_o:ww} with
145 % \cs{@@_neg_sign:N} as |#1| to compute a subtraction, in which case
146 % the second operand's sign should be changed. If the
147 % \meta{types} |#2| and |#4| are the same, dispatch to case |#2| ($0$,
148 % $1$, $2$, or $3$), where we call specialized functions: thanks to
149 % \cs{int_value:w}, those receive the tweaked \meta{sign_2}
150 % (expansion of |#1#5|) as an argument. If the \meta{types} are
151 % distinct, the result is simply the floating point number with the
152 % highest \meta{type}. Since case $3$ (used for two \texttt{nan})
153 % also picks the first operand, we can also use it when \meta{type_1}
154 % is greater than \meta{type_2}. Also note that we don't need to
155 % worry about \meta{sign_2} in that case since the second operand is
156 % discarded.
157 % \begin{macrocode}
158 \cs_new:cpn { @@_+_o:ww }
159 \s_@@ #1 \@@_chk:w #2 #3 \@@_sep: \s_@@ \@@_chk:w #4 #5
160 {
161 \if_case:w
162 \if_meaning:w #2 #4
163 #2
164 \else:
165 \if_int_compare:w #2 > #4 \exp_stop_f:
166 3
167 \else:
168 4
169 \fi:
170 \fi:
171 \exp_stop_f:
172 \exp_after:wN \@@_add_zeros_o:Nww \int_value:w
173 \or: \exp_after:wN \@@_add_normal_o:Nww \int_value:w
174 \or: \exp_after:wN \@@_add_inf_o:Nww \int_value:w
175 \or: \@@_case_return_i_o:ww
176 \else: \exp_after:wN \@@_add_return_ii_o:Nww \int_value:w
177 \fi:
178 #1 #5
179 \s_@@ \@@_chk:w #2 #3 \@@_sep:
180 \s_@@ \@@_chk:w #4 #5
181 }
182 % \end{macrocode}
183 % \end{macro}
184 %
185 % \begin{macro}[EXP]{\@@_add_return_ii_o:Nww}
186 % Ignore the first operand, and return the second, but using the sign
187 % |#1| rather than |#4|. As usual, expand after the floating point.
188 % \begin{macrocode}
189 \cs_new:Npn \@@_add_return_ii_o:Nww #1 #2 \@@_sep: \s_@@ \@@_chk:w #3 #4
190 { \@@_exp_after_o:w \s_@@ \@@_chk:w #3 #1 }
191 % \end{macrocode}
192 % \end{macro}
193 %
194 % \begin{macro}[EXP]{\@@_add_zeros_o:Nww}
195 % Adding two zeros yields \cs{c_zero_fp}, except if both zeros were
196 % $-0$.
197 % \begin{macrocode}
198 \cs_new:Npn \@@_add_zeros_o:Nww #1 \s_@@ \@@_chk:w 0 #2
199 {
200 \if_int_compare:w #2 #1 = 20 \exp_stop_f:
201 \exp_after:wN \@@_add_return_ii_o:Nww
202 \else:
203 \@@_case_return_i_o:ww
204 \fi:
205 #1
206 \s_@@ \@@_chk:w 0 #2
207 }
208 % \end{macrocode}
209 % \end{macro}
210 %
211 % \begin{macro}[EXP]{\@@_add_inf_o:Nww}
212 % If both infinities have the same sign, just return that infinity,
213 % otherwise, it is an invalid operation. We find out if that invalid
214 % operation is an addition or a subtraction by testing whether the
215 % tweaked \meta{sign_2} (|#1|) and the \meta{sign_2} (|#4|) are
216 % identical.
217 % \begin{macrocode}
218 \cs_new:Npn \@@_add_inf_o:Nww
219 #1 \s_@@ \@@_chk:w 2 #2 #3\@@_sep: \s_@@ \@@_chk:w 2 #4
220 {
221 \if_meaning:w #1 #2
222 \@@_case_return_i_o:ww
223 \else:
224 \@@_case_use:nw
225 {
226 \exp_last_unbraced:Nf \@@_invalid_operation_o:Nww
227 { \token_if_eq_meaning:NNTF #1 #4 + - }
228 }
229 \fi:
230 \s_@@ \@@_chk:w 2 #2 #3\@@_sep:
231 \s_@@ \@@_chk:w 2 #4
232 }
233 % \end{macrocode}
234 % \end{macro}
235 %
236 % \begin{macro}[EXP]{\@@_add_normal_o:Nww}
237 % \begin{quote}
238 % \cs{@@_add_normal_o:Nww} \meta{sign_2}
239 % \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_1}
240 % \meta{exp_1} \meta{body_1} \cs{@@_sep:}
241 % \cs{s_@@} \cs{@@_chk:w} |1| \meta{initial sign_2}
242 % \meta{exp_2} \meta{body_2} \cs{@@_sep:}
243 % \end{quote}
244 % We now have two normal numbers to add, and we have to check signs
245 % and exponents more carefully before performing the addition.
246 % \begin{macrocode}
247 \cs_new:Npn \@@_add_normal_o:Nww #1 \s_@@ \@@_chk:w 1 #2
248 {
249 \if_meaning:w #1#2
250 \exp_after:wN \@@_add_npos_o:NnwNnw
251 \else:
252 \exp_after:wN \@@_sub_npos_o:NnwNnw
253 \fi:
254 #2
255 }
256 % \end{macrocode}
257 % \end{macro}
258 %
259 % \subsubsection{Absolute addition}
260 %
261 % In this subsection, we perform the addition of two positive normal
262 % numbers.
263 %
264 % \begin{macro}[EXP]{\@@_add_npos_o:NnwNnw}
265 % \begin{quote}
266 % \cs{@@_add_npos_o:NnwNnw} \meta{sign_1} \meta{exp_1} \meta{body_1}
267 % \cs{@@_sep:} \cs{s_@@} \cs{@@_chk:w} |1| \meta{initial sign_2}
268 % \meta{exp_2} \meta{body_2} \cs{@@_sep:}
269 % \end{quote}
270 % Since we are doing an addition, the final sign is \meta{sign_1}.
271 % Start an \cs{@@_int_eval:w}, responsible for computing the exponent:
272 % the result, and the \meta{final sign} are then given to
273 % \cs{@@_sanitize:Nw} which checks for overflow. The exponent is
274 % computed as the largest exponent |#2| or |#5|, incremented if there
275 % is a carry. To add the significands, we decimate the smaller number by
276 % the difference between the exponents. This is done by
277 % \cs{@@_add_big_i:wNww} or \cs{@@_add_big_ii:wNww}. We need to bring
278 % the final sign with us in the midst of the calculation to round
279 % properly at the end.
280 % \begin{macrocode}
281 \cs_new:Npn \@@_add_npos_o:NnwNnw #1#2#3 \@@_sep: \s_@@ \@@_chk:w 1 #4 #5
282 {
283 \exp_after:wN \@@_sanitize:Nw
284 \exp_after:wN #1
285 \int_value:w \@@_int_eval:w
286 \if_int_compare:w #2 > #5 \exp_stop_f:
287 #2
288 \exp_after:wN \@@_add_big_i_o:wNww \int_value:w -
289 \else:
290 #5
291 \exp_after:wN \@@_add_big_ii_o:wNww \int_value:w
292 \fi:
293 \@@_int_eval:w #5 - #2 \@@_sep: #1 #3\@@_sep:
294 }
295 % \end{macrocode}
296 % \end{macro}
297 %
298 % \begin{macro}[rEXP]{\@@_add_big_i_o:wNww}
299 % \begin{macro}[rEXP]{\@@_add_big_ii_o:wNww}
300 % \begin{quote}
301 % \cs{@@_add_big_i_o:wNww} \meta{shift} \cs{@@_sep:} \meta{final sign}
302 % \meta{body_1} \cs{@@_sep:} \meta{body_2} \cs{@@_sep:}
303 % \end{quote}
304 % Used in \pkg{l3fp-expo}.
305 % Shift the significand of the small number, then add with
306 % \cs{@@_add_significand_o:NnnwnnnnN}.
307 % \begin{macrocode}
308 \cs_new:Npn \@@_add_big_i_o:wNww #1\@@_sep: #2 #3\@@_sep: #4\@@_sep:
309 {
310 \@@_decimate:nNnnnn {#1}
311 \@@_add_significand_o:NnnwnnnnN
312 #4
313 #3
314 #2
315 }
316 \cs_new:Npn \@@_add_big_ii_o:wNww #1\@@_sep: #2 #3\@@_sep: #4\@@_sep:
317 {
318 \@@_decimate:nNnnnn {#1}
319 \@@_add_significand_o:NnnwnnnnN
320 #3
321 #4
322 #2
323 }
324 % \end{macrocode}
325 % \end{macro}
326 % \end{macro}
327 %
328 % \begin{macro}[rEXP]{\@@_add_significand_o:NnnwnnnnN}
329 % \begin{macro}[rEXP]
330 % {\@@_add_significand_pack:NNNNNNN, \@@_add_significand_test_o:N}
331 % \begin{quote}\raggedright
332 % \cs{@@_add_significand_o:NnnwnnnnN}
333 % \meta{rounding digit}
334 % \Arg{Y'_1} \Arg{Y'_2} \meta{extra-digits} \cs{@@_sep:}
335 % \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4}
336 % \meta{final sign}
337 % \end{quote}
338 % To round properly, we must know at which digit the rounding
339 % should occur. This requires to know whether the addition
340 % produces an overall carry or not. Thus, we do the computation
341 % now and check for a carry, then go back and do the rounding.
342 % The rounding may cause a carry in very rare cases such as
343 % $0.99\cdots 95 \to 1.00\cdots 0$, but this situation always
344 % give an exact power of $10$, for which it is easy to correct
345 % the result at the end.
346 % \begin{macrocode}
347 \cs_new:Npn \@@_add_significand_o:NnnwnnnnN #1 #2#3 #4\@@_sep: #5#6#7#8
348 {
349 \exp_after:wN \@@_add_significand_test_o:N
350 \int_value:w \@@_int_eval:w 1#5#6 + #2
351 \exp_after:wN \@@_add_significand_pack:NNNNNNN
352 \int_value:w \@@_int_eval:w 1#7#8 + #3 \@@_sep: #1
353 }
354 \cs_new:Npn \@@_add_significand_pack:NNNNNNN #1 #2#3#4#5#6#7
355 {
356 \if_meaning:w 2 #1
357 + 1
358 \fi:
359 \@@_sep: #2 #3 #4 #5 #6 #7 \@@_sep:
360 }
361 \cs_new:Npn \@@_add_significand_test_o:N #1
362 {
363 \if_meaning:w 2 #1
364 \exp_after:wN \@@_add_significand_carry_o:wwwNN
365 \else:
366 \exp_after:wN \@@_add_significand_no_carry_o:wwwNN
367 \fi:
368 }
369 % \end{macrocode}
370 % \end{macro}
371 % \end{macro}
372 %
373 % \begin{macro}[rEXP]{\@@_add_significand_no_carry_o:wwwNN}
374 % \begin{quote}
375 % \cs{@@_add_significand_no_carry_o:wwwNN}
376 % \meta{8d} \cs{@@_sep:} \meta{6d} \cs{@@_sep:} \meta{2d} \cs{@@_sep:}
377 % \meta{rounding digit} \meta{sign}
378 % \end{quote}
379 % If there's no carry, grab all the digits again and round. The
380 % packing function \cs{@@_basics_pack_high:NNNNNw} takes care of the
381 % case where rounding brings a carry.
382 % \begin{macrocode}
383 \cs_new:Npn \@@_add_significand_no_carry_o:wwwNN
384 #1\@@_sep: #2\@@_sep: #3#4 \@@_sep: #5#6
385 {
386 \exp_after:wN \@@_basics_pack_high:NNNNNw
387 \int_value:w \@@_int_eval:w 1 #1
388 \exp_after:wN \@@_basics_pack_low:NNNNNw
389 \int_value:w \@@_int_eval:w 1 #2 #3#4
390 + \@@_round:NNN #6 #4 #5
391 \exp_after:wN \@@_sep:
392 }
393 % \end{macrocode}
394 % \end{macro}
395 %
396 % \begin{macro}[rEXP]{\@@_add_significand_carry_o:wwwNN}
397 % \begin{quote}
398 % \cs{@@_add_significand_carry_o:wwwNN}
399 % \meta{8d} \cs{@@_sep:} \meta{6d} \cs{@@_sep:} \meta{2d} \cs{@@_sep:}
400 % \meta{rounding digit} \meta{sign}
401 % \end{quote}
402 % The case where there is a carry is very similar. Rounding can even
403 % raise the first digit from $1$ to $2$, but we don't care.
404 % \begin{macrocode}
405 \cs_new:Npn \@@_add_significand_carry_o:wwwNN
406 #1\@@_sep: #2\@@_sep: #3#4\@@_sep: #5#6
407 {
408 + 1
409 \exp_after:wN \@@_basics_pack_weird_high:NNNNNNNNw
410 \int_value:w \@@_int_eval:w 1 1 #1
411 \exp_after:wN \@@_basics_pack_weird_low:NNNNw
412 \int_value:w \@@_int_eval:w 1 #2#3 +
413 \exp_after:wN \@@_round:NNN
414 \exp_after:wN #6
415 \exp_after:wN #3
416 \int_value:w \@@_round_digit:Nw #4 #5 \@@_sep:
417 \exp_after:wN \@@_sep:
418 }
419 % \end{macrocode}
420 % \end{macro}
421 %
422 % \subsubsection{Absolute subtraction}
423 %
424 % \begin{macro}[EXP]{\@@_sub_npos_o:NnwNnw}
425 % \begin{macro}[EXP]{\@@_sub_eq_o:Nnwnw, \@@_sub_npos_ii_o:Nnwnw}
426 % \begin{quote}
427 % \cs{@@_sub_npos_o:NnwNnw}
428 % \meta{sign_1} \meta{exp_1} \meta{body_1} \cs{@@_sep:}
429 % \cs{s_@@} \cs{@@_chk:w} |1|
430 % \meta{initial sign_2} \meta{exp_2} \meta{body_2} \cs{@@_sep:}
431 % \end{quote}
432 % Rounding properly in some modes requires to know what the sign of
433 % the result will be. Thus, we start by comparing the exponents and
434 % significands. If the numbers coincide, return zero. If the second
435 % number is larger, swap the numbers and call
436 % \cs{@@_sub_npos_i_o:Nnwnw} with the opposite of \meta{sign_1}.
437 % \begin{macrocode}
438 \cs_new:Npn \@@_sub_npos_o:NnwNnw
439 #1#2#3\@@_sep: \s_@@ \@@_chk:w 1 #4#5#6\@@_sep:
440 {
441 \if_case:w
442 \@@_compare_npos:nwnw {#2} #3\@@_sep: {#5} #6\@@_sep: \exp_stop_f:
443 \exp_after:wN \@@_sub_eq_o:Nnwnw
444 \or:
445 \exp_after:wN \@@_sub_npos_i_o:Nnwnw
446 \else:
447 \exp_after:wN \@@_sub_npos_ii_o:Nnwnw
448 \fi:
449 #1 {#2} #3\@@_sep: {#5} #6\@@_sep:
450 }
451 \cs_new:Npn \@@_sub_eq_o:Nnwnw #1#2\@@_sep: #3\@@_sep:
452 { \exp_after:wN \c_zero_fp }
453 \cs_new:Npn \@@_sub_npos_ii_o:Nnwnw #1 #2\@@_sep: #3\@@_sep:
454 {
455 \exp_after:wN \@@_sub_npos_i_o:Nnwnw
456 \int_value:w \@@_neg_sign:N #1
457 #3\@@_sep: #2\@@_sep:
458 }
459 % \end{macrocode}
460 % \end{macro}
461 % \end{macro}
462 %
463 % \begin{macro}[EXP]{\@@_sub_npos_i_o:Nnwnw}
464 % After the computation is done, \cs{@@_sanitize:Nw} checks for
465 % overflow/underflow. It expects the \meta{final sign} and the
466 % \meta{exponent} (delimited by \cs{@@_sep:}). Start an integer expression
467 % for the exponent, which starts with the exponent of the largest number,
468 % and may be decreased if the two numbers are very close. If the two
469 % numbers have the same exponent, call the \texttt{near} auxiliary.
470 % Otherwise, decimate $y$, then call the \texttt{far} auxiliary to
471 % evaluate the difference between the two significands. Note that we
472 % decimate by $1$ less than one could expect.
473 % \begin{macrocode}
474 \cs_new:Npn \@@_sub_npos_i_o:Nnwnw #1 #2#3\@@_sep: #4#5\@@_sep:
475 {
476 \exp_after:wN \@@_sanitize:Nw
477 \exp_after:wN #1
478 \int_value:w \@@_int_eval:w
479 #2
480 \if_int_compare:w #2 = #4 \exp_stop_f:
481 \exp_after:wN \@@_sub_back_near_o:nnnnnnnnN
482 \else:
483 \exp_after:wN \@@_decimate:nNnnnn \exp_after:wN
484 { \int_value:w \@@_int_eval:w #2 - #4 - 1 \exp_after:wN }
485 \exp_after:wN \@@_sub_back_far_o:NnnwnnnnN
486 \fi:
487 #5
488 #3
489 #1
490 }
491 % \end{macrocode}
492 % \end{macro}
493 %
494 % \begin{macro}[rEXP]{\@@_sub_back_near_o:nnnnnnnnN}
495 % \begin{macro}[rEXP]
496 % {\@@_sub_back_near_pack:NNNNNNw, \@@_sub_back_near_after:wNNNNw}
497 % \begin{quote}
498 % \cs{@@_sub_back_near_o:nnnnnnnnN}
499 % \Arg{Y_1} \Arg{Y_2} \Arg{Y_3} \Arg{Y_4}
500 % \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4}
501 % \meta{final sign}
502 % \end{quote}
503 % In this case, the subtraction is exact, so we discard the
504 % \meta{final sign} |#9|. The very large shifts of $10^{9}$ and
505 % $1.1\cdot10^{9}$ are unnecessary here, but allow the auxiliaries to
506 % be reused later. Each integer expression produces a $10$ digit
507 % result. If the resulting $16$ digits start with a $0$, then we need
508 % to shift the group, padding with trailing zeros.
509 % \begin{macrocode}
510 \cs_new:Npn \@@_sub_back_near_o:nnnnnnnnN #1#2#3#4 #5#6#7#8 #9
511 {
512 \exp_after:wN \@@_sub_back_near_after:wNNNNw
513 \int_value:w \@@_int_eval:w 10#5#6 - #1#2 - 11
514 \exp_after:wN \@@_sub_back_near_pack:NNNNNNw
515 \int_value:w \@@_int_eval:w 11#7#8 - #3#4 \exp_after:wN \@@_sep:
516 }
517 \cs_new:Npn \@@_sub_back_near_pack:NNNNNNw #1#2#3#4#5#6#7 \@@_sep:
518 { + #1#2 \@@_sep: {#3#4#5#6} {#7} \@@_sep: }
519 \cs_new:Npn \@@_sub_back_near_after:wNNNNw 10 #1#2#3#4 #5 \@@_sep:
520 {
521 \if_meaning:w 0 #1
522 \exp_after:wN \@@_sub_back_shift:wnnnn
523 \fi:
524 \@@_sep: {#1#2#3#4} {#5}
525 }
526 % \end{macrocode}
527 % \end{macro}
528 % \end{macro}
529 %
530 % \begin{macro}[rEXP]{\@@_sub_back_shift:wnnnn}
531 % \begin{macro}[rEXP]
532 % {
533 % \@@_sub_back_shift_ii:ww,
534 % \@@_sub_back_shift_iii:NNNNNNNNw,
535 % \@@_sub_back_shift_iv:nnnnw
536 % }
537 % \begin{quote}
538 % \cs{@@_sub_back_shift:wnnnn} \cs{@@_sep:}
539 % \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} \cs{@@_sep:}
540 % \end{quote}
541 % This function is called with $\meta{Z_1}\leq 999$. Act with
542 % \tn{number} to trim leading zeros from \meta{Z_1} \meta{Z_2} (we
543 % don't do all four blocks at once, since non-zero blocks would then
544 % overflow \TeX{}'s integers). If the first two blocks are zero, the
545 % auxiliary receives an empty |#1| and trims |#2#30| from leading
546 % zeros, yielding a total shift between $7$ and~$16$ to the exponent.
547 % Otherwise we get the shift from |#1| alone, yielding a result
548 % between $1$ and~$6$. Once the exponent is taken care of, trim
549 % leading zeros from |#1#2#3| (when |#1| is empty, the space before
550 % |#2#3| is ignored), get four blocks of $4$~digits and finally clean
551 % up. Trailing zeros are added so that digits can be grabbed safely.
552 % \begin{macrocode}
553 \cs_new:Npn \@@_sub_back_shift:wnnnn \@@_sep: #1#2
554 {
555 \exp_after:wN \@@_sub_back_shift_ii:ww
556 \int_value:w #1 #2 0 \@@_sep:
557 }
558 \cs_new:Npn \@@_sub_back_shift_ii:ww #1 0 \@@_sep: #2#3 \@@_sep:
559 {
560 \if_meaning:w @ #1 @
561 - 7
562 - \exp_after:wN \use_i:nnn
563 \exp_after:wN \@@_sub_back_shift_iii:NNNNNNNNw
564 \int_value:w #2#3 0 ~ 123456789\@@_sep:
565 \else:
566 - \@@_sub_back_shift_iii:NNNNNNNNw #1 123456789\@@_sep:
567 \fi:
568 \exp_after:wN \@@_pack_twice_four:wNNNNNNNN
569 \exp_after:wN \@@_pack_twice_four:wNNNNNNNN
570 \exp_after:wN \@@_sub_back_shift_iv:nnnnw
571 \exp_after:wN \@@_sep:
572 \int_value:w
573 #1 ~ #2#3 0 ~ 0000 0000 0000 000 \@@_sep:
574 }
575 \cs_new:Npn \@@_sub_back_shift_iii:NNNNNNNNw #1#2#3#4#5#6#7#8#9\@@_sep: {#8}
576 \cs_new:Npn \@@_sub_back_shift_iv:nnnnw #1 \@@_sep: #2 \@@_sep:
577 { \@@_sep: #1 \@@_sep: }
578 % \end{macrocode}
579 % \end{macro}
580 % \end{macro}
581 %
582 % \begin{macro}[rEXP]{\@@_sub_back_far_o:NnnwnnnnN}
583 % \begin{quote}\raggedright
584 % \cs{@@_sub_back_far_o:NnnwnnnnN}
585 % \meta{rounding} \Arg{Y'_1} \Arg{Y'_2} \meta{extra-digits} \cs{@@_sep:}
586 % \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4}
587 % \meta{final sign}
588 % \end{quote}
589 % If the difference is greater than $10^{\meta{expo_x}}$, call the
590 % \texttt{very_far} auxiliary. If the result is less than
591 % $10^{\meta{expo_x}}$, call the \texttt{not_far} auxiliary. If it is
592 % too close a call to know yet, namely if $1 \meta{Y'_1} \meta{Y'_2} =
593 % \meta{X_1} \meta{X_2} \meta{X_3} \meta{X_4} 0$, then call the
594 % \texttt{quite_far} auxiliary. We use the odd combination of space
595 % and \cs{@@_sep:} delimiters to allow the \texttt{not_far} auxiliary to
596 % grab each piece individually, the \texttt{very_far} auxiliary to use
597 % \cs{@@_pack_eight:wNNNNNNNN}, and the \texttt{quite_far} to ignore
598 % the significands easily (using the \cs{@@_sep:} delimiter).
599 % \begin{macrocode}
600 \cs_new:Npn \@@_sub_back_far_o:NnnwnnnnN #1 #2#3 #4\@@_sep: #5#6#7#8
601 {
602 \if_case:w
603 \if_int_compare:w 1 #2 = #5#6 \use_i:nnnn #7 \exp_stop_f:
604 \if_int_compare:w #3 = \use_none:n #7#8 0 \exp_stop_f:
605 0
606 \else:
607 \if_int_compare:w #3 > \use_none:n #7#8 0 - \fi: 1
608 \fi:
609 \else:
610 \if_int_compare:w 1 #2 > #5#6 \use_i:nnnn #7 - \fi: 1
611 \fi:
612 \exp_stop_f:
613 \exp_after:wN \@@_sub_back_quite_far_o:wwNN
614 \or: \exp_after:wN \@@_sub_back_very_far_o:wwwwNN
615 \else: \exp_after:wN \@@_sub_back_not_far_o:wwwwNN
616 \fi:
617 #2 ~ #3 \@@_sep: #5 #6 ~ #7 #8 \@@_sep: #1
618 }
619 % \end{macrocode}
620 % \end{macro}
621 %
622 % \begin{macro}[EXP]{\@@_sub_back_quite_far_o:wwNN}
623 % \begin{macro}[EXP]{\@@_sub_back_quite_far_ii:NN}
624 % The easiest case is when $x-y$ is extremely close to a power of
625 % $10$, namely the first digit of $x$ is $1$, and all others vanish
626 % when subtracting $y$. Then the \meta{rounding} |#3| and the
627 % \meta{final sign} |#4| control whether we get $1$ or $0.9999 9999
628 % 9999 9999$. In the usual round-to-nearest mode, we get $1$
629 % whenever the \meta{rounding} digit is less than or equal to $5$
630 % (remember that the \meta{rounding} digit is only equal to $5$ if
631 % there was no further non-zero digit).
632 % \begin{macrocode}
633 \cs_new:Npn \@@_sub_back_quite_far_o:wwNN #1\@@_sep: #2\@@_sep: #3#4
634 {
635 \exp_after:wN \@@_sub_back_quite_far_ii:NN
636 \exp_after:wN #3
637 \exp_after:wN #4
638 }
639 \cs_new:Npn \@@_sub_back_quite_far_ii:NN #1#2
640 {
641 \if_case:w \@@_round_neg:NNN #2 0 #1
642 \exp_after:wN \use_i:nn
643 \else:
644 \exp_after:wN \use_ii:nn
645 \fi:
646 { \@@_sep: {1000} {0000} {0000} {0000} \@@_sep: }
647 { - 1 \@@_sep: {9999} {9999} {9999} {9999} \@@_sep: }
648 }
649 % \end{macrocode}
650 % \end{macro}
651 % \end{macro}
652 %
653 % \begin{macro}[rEXP]{\@@_sub_back_not_far_o:wwwwNN}
654 % In the present case, $x$ and $y$ have different exponents, but
655 % $y$~is large enough that $x-y$ has a smaller exponent than~$x$.
656 % Decrement the exponent (with |-1|). Then proceed in a way
657 % similar to the \texttt{near} auxiliaries seen earlier, but
658 % multiplying $x$ by~$10$ (|#30| and |#40| below), and with the added
659 % quirk that the \meta{rounding} digit has to be taken into account.
660 % Namely, we may have to decrease the result by one unit if
661 % \cs{@@_round_neg:NNN} returns~$1$. This function expects the
662 % \meta{final sign}~|#6|, the last digit of |1100000000+#40-#2|, and
663 % the \meta{rounding} digit. Instead of redoing the computation for
664 % the second argument, we note that \cs{@@_round_neg:NNN} only cares
665 % about its parity, which is identical to that of the last digit
666 % of~|#2|.
667 % \begin{macrocode}
668 \cs_new:Npn \@@_sub_back_not_far_o:wwwwNN #1 ~ #2\@@_sep: #3 ~ #4\@@_sep: #5#6
669 {
670 - 1
671 \exp_after:wN \@@_sub_back_near_after:wNNNNw
672 \int_value:w \@@_int_eval:w 1#30 - #1 - 11
673 \exp_after:wN \@@_sub_back_near_pack:NNNNNNw
674 \int_value:w \@@_int_eval:w 11 0000 0000 + #40 - #2
675 - \exp_after:wN \@@_round_neg:NNN
676 \exp_after:wN #6
677 \use_none:nnnnnnn #2 #5
678 \exp_after:wN \@@_sep:
679 }
680 % \end{macrocode}
681 % \end{macro}
682 %
683 % \begin{macro}[EXP]{\@@_sub_back_very_far_o:wwwwNN}
684 % \begin{macro}[EXP]{\@@_sub_back_very_far_ii_o:nnNwwNN}
685 % The case where $x-y$ and $x$ have the same exponent is a bit more
686 % tricky, mostly because it cannot reuse the same auxiliaries. Shift
687 % the $y$~significand by adding a leading~$0$. Then the logic is similar
688 % to the \texttt{not_far} functions above. Rounding is a bit more
689 % complicated: we have two \meta{rounding} digits |#3| and |#6| (from
690 % the decimation, and from the new shift) to take into account, and
691 % getting the parity of the main result requires a computation. The
692 % first \cs{int_value:w} triggers the second one because the number
693 % is unfinished; we can thus not use $0$ in place of $2$ there.
694 % \begin{macrocode}
695 \cs_new:Npn \@@_sub_back_very_far_o:wwwwNN #1#2#3#4#5#6#7
696 {
697 \@@_pack_eight:wNNNNNNNN
698 \@@_sub_back_very_far_ii_o:nnNwwNN
699 { 0 #1#2#3 #4#5#6#7 }
700 \@@_sep:
701 }
702 \cs_new:Npn \@@_sub_back_very_far_ii_o:nnNwwNN
703 #1#2 \@@_sep: #3 \@@_sep: #4 ~ #5\@@_sep: #6#7
704 {
705 \exp_after:wN \@@_basics_pack_high:NNNNNw
706 \int_value:w \@@_int_eval:w 1#4 - #1 - 1
707 \exp_after:wN \@@_basics_pack_low:NNNNNw
708 \int_value:w \@@_int_eval:w 2#5 - #2
709 - \exp_after:wN \@@_round_neg:NNN
710 \exp_after:wN #7
711 \int_value:w
712 \if_int_odd:w \@@_int_eval:w #5 - #2 \@@_int_eval_end:
713 1 \else: 2 \fi:
714 \int_value:w \@@_round_digit:Nw #3 #6 \@@_sep:
715 \exp_after:wN \@@_sep:
716 }
717 % \end{macrocode}
718 % \end{macro}
719 % \end{macro}
720 %
721 % \subsection{Multiplication}
722 %
723 % \subsubsection{Signs, and special numbers}
724 %
725 % \begin{macro}[EXP]{\@@_*_o:ww}
726 % We go through an auxiliary, which is common with \cs{@@_/_o:ww}.
727 % The first argument is the operation, used for the invalid operation
728 % exception. The second is inserted in a formula to dispatch cases
729 % slightly differently between multiplication and division. The third
730 % is the operation for normal floating points. The fourth is there
731 % for extra cases needed in \cs{@@_/_o:ww}.
732 % \begin{macrocode}
733 \cs_new:cpn { @@_*_o:ww }
734 {
735 \@@_mul_cases_o:NnNnww
736 *
737 { - 2 + }
738 \@@_mul_npos_o:Nww
739 { }
740 }
741 % \end{macrocode}
742 % \end{macro}
743 %
744 % \begin{macro}[EXP]{\@@_mul_cases_o:nNnnww}
745 % Split into $10$ cases ($12$ for division).
746 % If both numbers are normal, go to case $0$
747 % (same sign) or case $1$ (opposite signs): in both cases, call
748 % \cs{@@_mul_npos_o:Nww} to do the work. If the first operand is
749 % \texttt{nan}, go to case $2$, in which the second operand is
750 % discarded; if the second operand is \texttt{nan}, go to case $3$, in
751 % which the first operand is discarded (note the weird interaction
752 % with the final test on signs). Then we separate the case where the
753 % first number is normal and the second is zero: this goes to cases
754 % $4$ and $5$ for multiplication, $10$ and $11$ for division.
755 % Otherwise, we do a computation which
756 % dispatches the products $0\times 0 = 0\times 1 = 1\times 0 = 0$ to
757 % case $4$ or $5$ depending on the combined sign, the products
758 % $0\times\infty$ and $\infty\times0$ to case $6$ or $7$ (invalid
759 % operation), and the products $1\times\infty = \infty\times1 =
760 % \infty\times\infty = \infty$ to cases $8$ and $9$. Note that the
761 % code for these two cases (which return $\pm\infty$) is inserted as
762 % argument |#4|, because it differs in the case of divisions.
763 % \begin{macrocode}
764 \cs_new:Npn \@@_mul_cases_o:NnNnww
765 #1#2#3#4 \s_@@ \@@_chk:w #5#6#7\@@_sep: \s_@@ \@@_chk:w #8#9
766 {
767 \if_case:w \@@_int_eval:w
768 \if_int_compare:w #5 #8 = 11 ~
769 1
770 \else:
771 \if_meaning:w 3 #8
772 3
773 \else:
774 \if_meaning:w 3 #5
775 2
776 \else:
777 \if_int_compare:w #5 #8 = 10 ~
778 9 #2 - 2
779 \else:
780 (#5 #2 #8) / 2 * 2 + 7
781 \fi:
782 \fi:
783 \fi:
784 \fi:
785 \if_meaning:w #6 #9 - 1 \fi:
786 \@@_int_eval_end:
787 \@@_case_use:nw { #3 0 }
788 \or: \@@_case_use:nw { #3 2 }
789 \or: \@@_case_return_i_o:ww
790 \or: \@@_case_return_ii_o:ww
791 \or: \@@_case_return_o:Nww \c_zero_fp
792 \or: \@@_case_return_o:Nww \c_minus_zero_fp
793 \or: \@@_case_use:nw { \@@_invalid_operation_o:Nww #1 }
794 \or: \@@_case_use:nw { \@@_invalid_operation_o:Nww #1 }
795 \or: \@@_case_return_o:Nww \c_inf_fp
796 \or: \@@_case_return_o:Nww \c_minus_inf_fp
797 #4
798 \fi:
799 \s_@@ \@@_chk:w #5 #6 #7\@@_sep:
800 \s_@@ \@@_chk:w #8 #9
801 }
802 % \end{macrocode}
803 % \end{macro}
804 %
805 % \subsubsection{Absolute multiplication}
806 %
807 % In this subsection, we perform the multiplication
808 % of two positive normal numbers.
809 %
810 % \begin{macro}[EXP]{\@@_mul_npos_o:Nww}
811 % \begin{quote}
812 % \cs{@@_mul_npos_o:Nww} \meta{final sign}
813 % \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_1} \Arg{exp_1} \meta{body_1} \cs{@@_sep:}
814 % \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_2} \Arg{exp_2} \meta{body_2} \cs{@@_sep:}
815 % \end{quote}
816 % After the computation, \cs{@@_sanitize:Nw} checks for overflow or
817 % underflow. As we did for addition, \cs{@@_int_eval:w} computes the
818 % exponent, catching any shift coming from the computation in the
819 % significand. The \meta{final sign} is needed to do the rounding
820 % properly in the significand computation. We setup the post-expansion
821 % here, triggered by \cs{@@_mul_significand_o:nnnnNnnnn}.
822 %
823 % This is also used in \pkg{l3fp-convert}.
824 % \begin{macrocode}
825 \cs_new:Npn \@@_mul_npos_o:Nww
826 #1 \s_@@ \@@_chk:w #2 #3 #4 #5 \@@_sep:
827 \s_@@ \@@_chk:w #6 #7 #8 #9 \@@_sep:
828 {
829 \exp_after:wN \@@_sanitize:Nw
830 \exp_after:wN #1
831 \int_value:w \@@_int_eval:w
832 #4 + #8
833 \@@_mul_significand_o:nnnnNnnnn #5 #1 #9
834 }
835 % \end{macrocode}
836 % \end{macro}
837 %
838 % \begin{macro}[rEXP]{\@@_mul_significand_o:nnnnNnnnn}
839 % \begin{macro}[EXP]
840 % {\@@_mul_significand_drop:NNNNNw, \@@_mul_significand_keep:NNNNNw}
841 % \begin{quote}
842 % \cs{@@_mul_significand_o:nnnnNnnnn}
843 % \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4} \meta{sign}
844 % \Arg{Y_1} \Arg{Y_2} \Arg{Y_3} \Arg{Y_4}
845 % \end{quote}
846 % Note the three \cs{@@_sep:}s at the end of the definition. One is for
847 % the last \cs{@@_mul_significand_drop:NNNNNw}; one is for
848 % \cs{@@_round_digit:Nw} later on; and one, preceded by
849 % \cs{exp_after:wN}, which is correctly expanded (within an
850 % \cs{@@_int_eval:w}), is used by \cs{@@_basics_pack_low:NNNNNw}.
851 %
852 % The product of two $16$ digit integers has $31$ or $32$ digits,
853 % but it is impossible to know which one before computing. The place
854 % where we round depends on that number of digits, and may depend
855 % on all digits until the last in some rare cases. The approach is
856 % thus to compute the $5$ first blocks of $4$ digits (the first one
857 % is between $100$ and $9999$ inclusive), and a compact version of
858 % the remaining $3$ blocks. Afterwards, the number of digits is
859 % known, and we can do the rounding within yet another set of
860 % \cs{@@_int_eval:w}.
861 % \begin{macrocode}
862 \cs_new:Npn \@@_mul_significand_o:nnnnNnnnn #1#2#3#4 #5 #6#7#8#9
863 {
864 \exp_after:wN \@@_mul_significand_test_f:NNN
865 \exp_after:wN #5
866 \int_value:w \@@_int_eval:w 99990000 + #1*#6 +
867 \exp_after:wN \@@_mul_significand_keep:NNNNNw
868 \int_value:w \@@_int_eval:w 99990000 + #1*#7 + #2*#6 +
869 \exp_after:wN \@@_mul_significand_keep:NNNNNw
870 \int_value:w \@@_int_eval:w 99990000 + #1*#8 + #2*#7 + #3*#6 +
871 \exp_after:wN \@@_mul_significand_drop:NNNNNw
872 \int_value:w \@@_int_eval:w 99990000 + #1*#9 + #2*#8 +
873 #3*#7 + #4*#6 +
874 \exp_after:wN \@@_mul_significand_drop:NNNNNw
875 \int_value:w \@@_int_eval:w 99990000 + #2*#9 + #3*#8 +
876 #4*#7 +
877 \exp_after:wN \@@_mul_significand_drop:NNNNNw
878 \int_value:w \@@_int_eval:w 99990000 + #3*#9 + #4*#8 +
879 \exp_after:wN \@@_mul_significand_drop:NNNNNw
880 \int_value:w \@@_int_eval:w 100000000 + #4*#9 \@@_sep:
881 \@@_sep: \exp_after:wN \@@_sep:
882 }
883 \cs_new:Npn \@@_mul_significand_drop:NNNNNw #1#2#3#4#5 #6\@@_sep:
884 { #1#2#3#4#5 \@@_sep: + #6 }
885 \cs_new:Npn \@@_mul_significand_keep:NNNNNw #1#2#3#4#5 #6\@@_sep:
886 { #1#2#3#4#5 \@@_sep: #6 \@@_sep: }
887 % \end{macrocode}
888 % \end{macro}
889 % \end{macro}
890 %
891 % \begin{macro}[rEXP]{\@@_mul_significand_test_f:NNN}
892 % \begin{quote}
893 % \cs{@@_mul_significand_test_f:NNN} \meta{sign} |1|
894 % \meta{digits 1--8} \cs{@@_sep:} \meta{digits 9--12} \cs{@@_sep:} \meta{digits 13--16} \cs{@@_sep:}
895 % |+| \meta{digits 17--20} |+| \meta{digits 21--24}
896 % |+| \meta{digits 25--28} |+| \meta{digits 29--32} \cs{@@_sep:}
897 % \cs{exp_after:wN} \cs{@@_sep:}
898 % \end{quote}
899 % If the \meta{digit 1} is non-zero, then for rounding we only care
900 % about the digits $16$ and $17$, and whether further digits are zero
901 % or not (check for exact ties). On the other hand, if \meta{digit 1}
902 % is zero, we care about digits $17$ and $18$, and whether further
903 % digits are zero.
904 % \begin{macrocode}
905 \cs_new:Npn \@@_mul_significand_test_f:NNN #1 #2 #3
906 {
907 \if_meaning:w 0 #3
908 \exp_after:wN \@@_mul_significand_small_f:NNwwwN
909 \else:
910 \exp_after:wN \@@_mul_significand_large_f:NwwNNNN
911 \fi:
912 #1 #3
913 }
914 % \end{macrocode}
915 % \end{macro}
916 %
917 % \begin{macro}[EXP]{\@@_mul_significand_large_f:NwwNNNN}
918 % In this branch, \meta{digit 1} is non-zero. The result is thus
919 % \meta{digits 1--16}, plus some rounding which depends on the digits
920 % $16$, $17$, and whether all subsequent digits are zero or not.
921 % Here, \cs{@@_round_digit:Nw} takes digits $17$ and further (as an
922 % integer expression), and replaces it by a \meta{rounding digit},
923 % suitable for \cs{@@_round:NNN}.
924 % \begin{macrocode}
925 \cs_new:Npn \@@_mul_significand_large_f:NwwNNNN
926 #1 #2\@@_sep: #3\@@_sep: #4#5#6#7\@@_sep: +
927 {
928 \exp_after:wN \@@_basics_pack_high:NNNNNw
929 \int_value:w \@@_int_eval:w 1#2
930 \exp_after:wN \@@_basics_pack_low:NNNNNw
931 \int_value:w \@@_int_eval:w 1#3#4#5#6#7
932 + \exp_after:wN \@@_round:NNN
933 \exp_after:wN #1
934 \exp_after:wN #7
935 \int_value:w \@@_round_digit:Nw
936 }
937 % \end{macrocode}
938 % \end{macro}
939 %
940 % \begin{macro}[rEXP]{\@@_mul_significand_small_f:NNwwwN}
941 % In this branch, \meta{digit 1} is zero. Our result is thus
942 % \meta{digits 2--17}, plus some rounding which depends on the digits
943 % $17$, $18$, and whether all subsequent digits are zero or not.
944 % The $8$ digits |1#3| are followed, after expansion of the
945 % \texttt{small_pack} auxiliary, by the next digit, to form a $9$
946 % digit number.
947 % \begin{macrocode}
948 \cs_new:Npn \@@_mul_significand_small_f:NNwwwN
949 #1 #2#3\@@_sep: #4#5\@@_sep: #6\@@_sep: + #7
950 {
951 - 1
952 \exp_after:wN \@@_basics_pack_high:NNNNNw
953 \int_value:w \@@_int_eval:w 1#3#4
954 \exp_after:wN \@@_basics_pack_low:NNNNNw
955 \int_value:w \@@_int_eval:w 1#5#6#7
956 + \exp_after:wN \@@_round:NNN
957 \exp_after:wN #1
958 \exp_after:wN #7
959 \int_value:w \@@_round_digit:Nw
960 }
961 % \end{macrocode}
962 % \end{macro}
963 %
964 % \subsection{Division}
965 %
966 % \subsubsection{Signs, and special numbers}
967 %
968 % Time is now ripe to tackle the hardest of the four elementary
969 % operations: division.
970 %
971 % \begin{macro}[EXP]{\@@_/_o:ww}
972 % Filtering special floating point is very similar to what we did for
973 % multiplications, with a few variations. Invalid operation
974 % exceptions display |/| rather than |*|. In the formula for
975 % dispatch, we replace |- 2 +| by |-|. The case of normal
976 % numbers is treated using \cs{@@_div_npos_o:Nww} rather than
977 % \cs{@@_mul_npos_o:Nww}. There are two additional cases: if the
978 % first operand is normal and the second is a zero, then the division
979 % by zero exception is raised: cases $10$ and $11$ of the
980 % \cs{if_case:w} construction in \cs{@@_mul_cases_o:NnNnww} are
981 % provided as the fourth argument here.
982 % \begin{macrocode}
983 \cs_new:cpn { @@_/_o:ww }
984 {
985 \@@_mul_cases_o:NnNnww
986 /
987 { - }
988 \@@_div_npos_o:Nww
989 {
990 \or:
991 \@@_case_use:nw
992 { \@@_division_by_zero_o:NNww \c_inf_fp / }
993 \or:
994 \@@_case_use:nw
995 { \@@_division_by_zero_o:NNww \c_minus_inf_fp / }
996 }
997 }
998 % \end{macrocode}
999 % \end{macro}
1000 %
1001 % \begin{macro}[EXP]{\@@_div_npos_o:Nww}
1002 % \begin{quote}
1003 % \cs{@@_div_npos_o:Nww} \meta{final sign}
1004 % \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_A} \Arg{exp A}
1005 % \Arg{A_1} \Arg{A_2} \Arg{A_3} \Arg{A_4} \cs{@@_sep:}
1006 % \cs{s_@@} \cs{@@_chk:w} |1| \meta{sign_Z} \Arg{exp Z}
1007 % \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} \cs{@@_sep:}
1008 % \end{quote}
1009 % We want to compute $A/Z$. As for multiplication,
1010 % \cs{@@_sanitize:Nw} checks for overflow or underflow; we provide it
1011 % with the \meta{final sign}, and an integer expression in which we
1012 % compute the exponent. We set up the arguments of
1013 % \cs{@@_div_significand_i_o:wnnw}, namely an integer \meta{y} obtained
1014 % by adding $1$ to the first $5$ digits of $Z$ (explanation given soon
1015 % below), then the four \Arg{A_{i}}, then the four \Arg{Z_{i}}, a
1016 % \cs{@@_sep:}, and the \meta{final sign}, used for rounding at the end.
1017 % \begin{macrocode}
1018 \cs_new:Npn \@@_div_npos_o:Nww
1019 #1 \s_@@ \@@_chk:w 1 #2 #3 #4 \@@_sep:
1020 \s_@@ \@@_chk:w 1 #5 #6 #7#8#9\@@_sep:
1021 {
1022 \exp_after:wN \@@_sanitize:Nw
1023 \exp_after:wN #1
1024 \int_value:w \@@_int_eval:w
1025 #3 - #6
1026 \exp_after:wN \@@_div_significand_i_o:wnnw
1027 \int_value:w \@@_int_eval:w #7 \use_i:nnnn #8 + 1 \@@_sep:
1028 #4
1029 {#7}{#8}#9 \@@_sep:
1030 #1
1031 }
1032 % \end{macrocode}
1033 % \end{macro}
1034 %
1035 % \subsubsection{Work plan}
1036 %
1037 % In this subsection, we explain how to avoid overflowing \TeX{}'s
1038 % integers when performing the division of two positive normal numbers.
1039 %
1040 % We are given two numbers, $A=0.A_{1}A_{2}A_{3}A_{4}$ and
1041 % $Z=0.Z_{1}Z_{2}Z_{3}Z_{4}$, in blocks of $4$ digits, and we know that
1042 % the first digits of $A_{1}$ and of $Z_{1}$ are non-zero. To compute
1043 % $A/Z$, we proceed as follows.
1044 % \begin{itemize}
1045 % \item Find an integer $Q_{A} \simeq 10^{4} A / Z$.
1046 % \item Replace $A$ by $B = 10^{4} A - Q_{A} Z$.
1047 % \item Find an integer $Q_{B} \simeq 10^{4} B / Z$.
1048 % \item Replace $B$ by $C = 10^{4} B - Q_{B} Z$.
1049 % \item Find an integer $Q_{C} \simeq 10^{4} C / Z$.
1050 % \item Replace $C$ by $D = 10^{4} C - Q_{C} Z$.
1051 % \item Find an integer $Q_{D} \simeq 10^{4} D / Z$.
1052 % \item Consider $E = 10^{4} D - Q_{D} Z$, and ensure
1053 % correct rounding.
1054 % \end{itemize}
1055 % The result is then $Q = 10^{-4} Q_{A} + 10^{-8} Q_{B} + 10^{-12} Q_{C}
1056 % + 10^{-16} Q_{D} + \text{rounding}$. Since the $Q_{i}$ are integers,
1057 % $B$, $C$, $D$, and~$E$ are all exact multiples of $10^{-16}$, in other
1058 % words, computing with $16$ digits after the decimal separator yields
1059 % exact results. The problem is the risk of overflow: in general $B$, $C$,
1060 % $D$, and $E$ may be greater than $1$.
1061 %
1062 % Unfortunately, things are not as easy as they seem. In particular, we
1063 % want all intermediate steps to be positive, since negative results
1064 % would require extra calculations at the end. This requires that
1065 % $Q_{A} \leq 10^{4} A / Z$ etc. A reasonable attempt would be
1066 % to define $Q_{A}$ as
1067 % \begin{equation*}
1068 % \cs{int_eval:n} \left\{
1069 % \frac{ A_{1} A_{2} }{ Z_{1} + 1 } - 1 \right\}
1070 % \leq 10^{4} \frac{A}{Z}
1071 % \end{equation*}
1072 % Subtracting $1$ at the end takes care of the fact that \eTeX{}'s
1073 % \cs{@@_int_eval:w} rounds divisions instead of truncating (really,
1074 % $1/2$ would be sufficient, but we work with integers). We add $1$ to
1075 % $Z_{1}$ because $Z_{1} \leq 10^{4}Z < Z_{1}+1$ and we need $Q_{A}$ to
1076 % be an underestimate. However, we are now underestimating $Q_{A}$ too
1077 % much: it can be wrong by up to $100$, for instance when $Z = 0.1$ and
1078 % $A \simeq 1$. Then $B$ could take values up to $10$ (maybe more), and
1079 % a few steps down the line, we would run into arithmetic overflow,
1080 % since \TeX{} can only handle integers less than roughly $2\cdot
1081 % 10^{9}$.
1082 %
1083 % A better formula is to take
1084 % \begin{equation*}
1085 % Q_{A} = \cs{int_eval:n} \left\{
1086 % \frac{ 10 \cdot A_{1} A_{2} }
1087 % { \left\lfloor 10^{-3} \cdot Z_{1} Z_{2} \right\rfloor + 1 }
1088 % - 1 \right\}.
1089 % \end{equation*}
1090 % This is always less than $10^{9} A / (10^{5} Z)$, as we wanted. In
1091 % words, we take the $5$ first digits of $Z$ into account, and the $8$
1092 % first digits of $A$, using $0$ as a $9$-th digit rather than the true
1093 % digit for efficiency reasons. We shall prove that using this formula
1094 % to define all the $Q_{i}$ avoids any overflow. For convenience, let
1095 % us denote
1096 % \begin{equation*}
1097 % y = \left\lfloor 10^{-3} \cdot Z_{1} Z_{2} \right\rfloor + 1,
1098 % \end{equation*}
1099 % so that, taking into account the fact that \eTeX{} rounds ties away
1100 % from zero,
1101 % \begin{align*}
1102 % Q_{A}
1103 % &= \left\lfloor \frac{A_{1}A_{2}0}{y} - \frac{1}{2} \right\rfloor
1104 % \\
1105 % &>\frac{A_{1}A_{2}0}{y} - \frac{3}{2}.
1106 % \end{align*}
1107 % Note that $10^{4}<y\leq 10^{5}$, and $999 \leq Q_{A} \leq 99989$.
1108 % Also note that this formula does not cause an overflow as long as $A <
1109 % (2^{31}-1) / 10^{9} \simeq 2.147\cdots$, since the numerator involves an
1110 % integer slightly smaller than $10^{9} A$.
1111 %
1112 % Let us bound $B$:
1113 % \begin{align*}
1114 % 10^{5} B
1115 % &=
1116 % A_{1}A_{2}0 + 10 \cdot 0.A_{3}A_{4}
1117 % - 10 \cdot Z_{1}.Z_{2}Z_{3}Z_{4} \cdot Q_{A}
1118 % \\
1119 % &<
1120 % A_{1}A_{2}0
1121 % \cdot \left( 1 - 10 \cdot \frac{Z_{1}.Z_{2}Z_{3}Z_{4}}{y} \right)
1122 % + \frac{3}{2} \cdot 10 \cdot Z_{1}.Z_{2}Z_{3}Z_{4} + 10
1123 % \\
1124 % &\leq
1125 % \frac{A_{1}A_{2}0 \cdot (y - 10 \cdot Z_{1}.Z_{2}Z_{3}Z_{4})}{y}
1126 % + \frac{3}{2} y + 10
1127 % \\
1128 % &\leq
1129 % \frac{A_{1}A_{2}0\cdot 1}{y} + \frac{3}{2} y + 10
1130 % \leq
1131 % \frac{10^{9} A}{y} + 1.6\cdot y.
1132 % \end{align*}
1133 % At the last step, we hide $10$ into the second term for later
1134 % convenience. The same reasoning yields
1135 % \begin{align*}
1136 % 10^{5} B &< 10^{9} A/y + 1.6 y, \\
1137 % 10^{5} C &< 10^{9} B/y + 1.6 y, \\
1138 % 10^{5} D &< 10^{9} C/y + 1.6 y, \\
1139 % 10^{5} E &< 10^{9} D/y + 1.6 y. \\
1140 % \end{align*}
1141 % The goal is now to prove that none of $B$, $C$, $D$, and $E$ can go
1142 % beyond $(2^{31}-1) / 10^{9} = 2.147\cdots$.
1143 %
1144 % Combining the various inequalities together with $A<1$, we get
1145 % \begin{align*}
1146 % 10^{5} B &< 10^{9}/y + 1.6 y, \\
1147 % 10^{5} C &< 10^{13}/y^{2} + 1.6 (y + 10^{4}), \\
1148 % 10^{5} D &< 10^{17}/y^{3} + 1.6 (y + 10^{4} + 10^{8}/y), \\
1149 % 10^{5} E &< 10^{21}/y^{4} + 1.6 (y + 10^{4} + 10^{8}/y + 10^{12}/y^{2}). \\
1150 % \end{align*}
1151 % All of those bounds are convex functions of $y$ (since every power of
1152 % $y$ involved is convex, and the coefficients are positive), and thus
1153 % maximal at one of the end-points of the allowed range $10^{4} < y \leq
1154 % 10^{5}$. Thus,
1155 % \begin{align*}
1156 % 10^{5} B &< \mathrm{max} ( 1.16\cdot 10^{5}, 1.7 \cdot 10^{5}), \\
1157 % 10^{5} C &< \mathrm{max} ( 1.32\cdot 10^{5}, 1.77 \cdot 10^{5}), \\
1158 % 10^{5} D &< \mathrm{max} ( 1.48\cdot 10^{5}, 1.777 \cdot 10^{5}), \\
1159 % 10^{5} E &< \mathrm{max} ( 1.64\cdot 10^{5}, 1.7777 \cdot 10^{5}). \\
1160 % \end{align*}
1161 % All of those bounds are less than $2.147\cdot 10^{5}$, and we are thus
1162 % within \TeX{}'s bounds in all cases!
1163 %
1164 % We later need to have a bound on the $Q_{i}$. Their definitions
1165 % imply that $Q_{A} < 10^{9} A/y - 1/2 < 10^{5} A$ and similarly for the
1166 % other $Q_{i}$. Thus, all of them are less than $177770$.
1167 %
1168 % The last step is to ensure correct rounding. We have
1169 % \begin{equation*}
1170 % A/Z = \sum_{i=1}^{4} \left(10^{-4i} Q_{i}\right) + 10^{-16} E/Z
1171 % \end{equation*}
1172 % exactly. Furthermore, we know that the result is in $[0.1,10)$,
1173 % hence will be rounded to a multiple of $10^{-16}$ or of $10^{-15}$, so
1174 % we only need to know the integer part of $E/Z$, and a
1175 % \enquote{rounding} digit encoding the rest. Equivalently, we need to
1176 % find the integer part of $2E/Z$, and determine whether it was an
1177 % exact integer or not (this serves to detect ties). Since
1178 % \begin{equation*}
1179 % \frac{2E}{Z} = 2\frac{10^{5} E}{10^{5} Z}
1180 % \leq 2\frac{10^{5} E}{10^{4}} < 36,
1181 % \end{equation*}
1182 % this integer part is between $0$ and $35$ inclusive. We let \eTeX{}
1183 % round
1184 % \begin{equation*}
1185 % P = \cs{int_eval:n} \left\{
1186 % \frac{2\cdot E_{1}E_{2}}{Z_{1}Z_{2}} \right\},
1187 % \end{equation*}
1188 % which differs from $2E/Z$ by at most
1189 % \begin{equation*}
1190 % \frac{1}{2}
1191 % + 2 \left\lvert \frac{E}{Z} - \frac{E}{10^{-8} Z_{1}Z_{2}}\right\rvert
1192 % + 2 \left\lvert \frac{10^{8} E - E_{1}E_{2}}{Z_{1}Z_{2}}\right\rvert
1193 % < 1,
1194 % \end{equation*}
1195 % ($1/2$ comes from \eTeX{}'s rounding) because each absolute value is
1196 % less than $10^{-7}$. Thus $P$ is either the correct integer part, or
1197 % is off by $1$; furthermore, if $2 E / Z$ is an integer, $P = 2 E / Z$.
1198 % We will check the sign of $2 E - P Z$. If it is negative, then $E / Z
1199 % \in \big((P - 1) / 2, P / 2\big)$. If it is zero, then $E / Z = P /
1200 % 2$. If it is positive, then $E / Z \in \big(P / 2, (P - 1) / 2\big)$.
1201 % In each case, we know how to round to an integer, depending on the
1202 % parity of $P$, and the rounding mode.
1203 %
1204 % \subsubsection{Implementing the significand division}
1205 %
1206 % \begin{macro}[rEXP]{\@@_div_significand_i_o:wnnw}
1207 % \begin{quote}
1208 % \cs{@@_div_significand_i_o:wnnw} \meta{y} \cs{@@_sep:}
1209 % \Arg{A_1} \Arg{A_2} \Arg{A_3} \Arg{A_4}
1210 % \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} \cs{@@_sep:} \meta{sign}
1211 % \end{quote}
1212 % Compute $10^{6} + Q_{A}$ (a $7$~digit number thanks to the shift),
1213 % unbrace \meta{A_1} and \meta{A_2}, and prepare the
1214 % \meta{continuation} arguments for $4$ consecutive calls to
1215 % \cs{@@_div_significand_calc:wwnnnnnnn}. Each of these calls needs
1216 % \meta{y} (|#1|), and it turns out that we need post-expansion there,
1217 % hence the \cs{int_value:w}. Here, |#4| is six brace groups, which
1218 % give the six first |n|-type arguments of the \texttt{calc} function.
1219 % \begin{macrocode}
1220 \cs_new:Npn \@@_div_significand_i_o:wnnw #1 \@@_sep: #2#3 #4 \@@_sep:
1221 {
1222 \exp_after:wN \@@_div_significand_test_o:w
1223 \int_value:w \@@_int_eval:w
1224 \exp_after:wN \@@_div_significand_calc:wwnnnnnnn
1225 \int_value:w \@@_int_eval:w 999999 + #2 #3 0 / #1 \@@_sep:
1226 #2 #3 \@@_sep:
1227 #4
1228 { \exp_after:wN \@@_div_significand_ii:wwn \int_value:w #1 }
1229 { \exp_after:wN \@@_div_significand_ii:wwn \int_value:w #1 }
1230 { \exp_after:wN \@@_div_significand_ii:wwn \int_value:w #1 }
1231 { \exp_after:wN \@@_div_significand_iii:wwnnnnn \int_value:w #1 }
1232 }
1233 % \end{macrocode}
1234 % \end{macro}
1235 %
1236 % \begin{macro}[rEXP]{\@@_div_significand_calc:wwnnnnnnn}
1237 % \begin{macro}[rEXP]
1238 % {
1239 % \@@_div_significand_calc_i:wwnnnnnnn,
1240 % \@@_div_significand_calc_ii:wwnnnnnnn,
1241 % }
1242 % \begin{quote}
1243 % \cs{@@_div_significand_calc:wwnnnnnnn} \meta{$10^{6}+{}$Q_{A}} \cs{@@_sep:}
1244 % \meta{A_1} \meta{A_2} \cs{@@_sep:} \Arg{A_3} \Arg{A_4}
1245 % \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4}
1246 % \Arg{continuation}
1247 % \end{quote}
1248 % expands to
1249 % \begin{quote}
1250 % \meta{$10^{6}+{}$Q_{A}} \meta{continuation} \cs{@@_sep:}
1251 % \meta{B_1} \meta{B_2} \cs{@@_sep:} \Arg{B_3} \Arg{B_4}
1252 % \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4}
1253 % \end{quote}
1254 % where $B = 10^{4} A - Q_{A} \cdot Z$. This function is also used to
1255 % compute $C$, $D$, $E$ (with the input shifted accordingly), and is
1256 % used in \pkg{l3fp-expo}.
1257 %
1258 % We know that $0<Q_{A}<1.8\cdot 10^{5}$, so the product of $Q_{A}$
1259 % with each $Z_{i}$ is within \TeX{}'s bounds. However, it is a
1260 % little bit too large for our purposes: we would not be able to use
1261 % the usual trick of adding a large power of $10$ to ensure that the
1262 % number of digits is fixed.
1263 %
1264 % The bound on $Q_{A}$, implies that $10^{6}+Q_{A}$ starts with the
1265 % digit $1$, followed by $0$ or $1$. We test, and call different
1266 % auxiliaries for the two cases. An earlier implementation did the
1267 % tests within the computation, but since we added a
1268 % \meta{continuation}, this is not possible because the macro has $9$
1269 % parameters.
1270 %
1271 % The result we want is then (the overall power of $10$ is arbitrary):
1272 % \begin{align*}
1273 % &10^{-4} ( \#2 - \#1 \cdot \#5 - 10 \cdot \meta{i} \cdot \#5\#6 )
1274 % + 10^{-8} ( \#3 - \#1 \cdot \#6 - 10 \cdot \meta{i} \cdot \#7 ) \\
1275 % &+ 10^{-12}( \#4 - \#1 \cdot \#7 - 10 \cdot \meta{i} \cdot \#8 )
1276 % + 10^{-16}( - \#1 \cdot \#8 ),
1277 % \end{align*}
1278 % where \meta{i} stands for the $10^{5}$ digit of $Q_{A}$, which is
1279 % $0$ or~$1$, and $\#1$, $\#2$, \emph{etc.\@} are the parameters of
1280 % either auxiliary. The factors of $10$ come from the fact that
1281 % $Q_{A} = 10\cdot 10^{4} \cdot \meta{i} + \#1$. As usual, to combine
1282 % all the terms, we need to choose some shifts which must ensure that
1283 % the number of digits of the second, third, and fourth terms are each
1284 % fixed. Here, the positive contributions are at most $10^{8}$ and
1285 % the negative contributions can go up to $10^{9}$. Indeed, for the
1286 % auxiliary with $\meta{i}=1$, |#1| is at most $80000$, leading to
1287 % contributions of at worse $-8\cdot 10^{8}4$, while the other
1288 % negative term is very small $<10^{6}$ (except in the first
1289 % expression, where we don't care about the number of digits); for the
1290 % auxiliary with $\meta{i}=0$, |#1| can go up to $99999$, but there is
1291 % no other negative term. Hence, a good choice is $2\cdot 10^{9}$,
1292 % which produces totals in the range $[10^{9}, 2.1\cdot 10^{9}]$. We
1293 % are flirting with \TeX{}'s limits once more.
1294 % \begin{macrocode}
1295 \cs_new:Npn \@@_div_significand_calc:wwnnnnnnn 1#1
1296 {
1297 \if_meaning:w 1 #1
1298 \exp_after:wN \@@_div_significand_calc_i:wwnnnnnnn
1299 \else:
1300 \exp_after:wN \@@_div_significand_calc_ii:wwnnnnnnn
1301 \fi:
1302 }
1303 \cs_new:Npn \@@_div_significand_calc_i:wwnnnnnnn
1304 #1\@@_sep: #2\@@_sep:#3#4 #5#6#7#8 #9
1305 {
1306 1 1 #1
1307 #9 \exp_after:wN \@@_sep:
1308 \int_value:w \@@_int_eval:w \c_@@_Bigg_leading_shift_int
1309 + #2 - #1 * #5 - #5#60
1310 \exp_after:wN \@@_pack_Bigg:NNNNNNw
1311 \int_value:w \@@_int_eval:w \c_@@_Bigg_middle_shift_int
1312 + #3 - #1 * #6 - #70
1313 \exp_after:wN \@@_pack_Bigg:NNNNNNw
1314 \int_value:w \@@_int_eval:w \c_@@_Bigg_middle_shift_int
1315 + #4 - #1 * #7 - #80
1316 \exp_after:wN \@@_pack_Bigg:NNNNNNw
1317 \int_value:w \@@_int_eval:w \c_@@_Bigg_trailing_shift_int
1318 - #1 * #8 \@@_sep:
1319 {#5}{#6}{#7}{#8}
1320 }
1321 \cs_new:Npn \@@_div_significand_calc_ii:wwnnnnnnn
1322 #1\@@_sep: #2\@@_sep:#3#4 #5#6#7#8 #9
1323 {
1324 1 0 #1
1325 #9 \exp_after:wN \@@_sep:
1326 \int_value:w \@@_int_eval:w \c_@@_Bigg_leading_shift_int
1327 + #2 - #1 * #5
1328 \exp_after:wN \@@_pack_Bigg:NNNNNNw
1329 \int_value:w \@@_int_eval:w \c_@@_Bigg_middle_shift_int
1330 + #3 - #1 * #6
1331 \exp_after:wN \@@_pack_Bigg:NNNNNNw
1332 \int_value:w \@@_int_eval:w \c_@@_Bigg_middle_shift_int
1333 + #4 - #1 * #7
1334 \exp_after:wN \@@_pack_Bigg:NNNNNNw
1335 \int_value:w \@@_int_eval:w \c_@@_Bigg_trailing_shift_int
1336 - #1 * #8 \@@_sep:
1337 {#5}{#6}{#7}{#8}
1338 }
1339 % \end{macrocode}
1340 % \end{macro}
1341 % \end{macro}
1342 %
1343 % \begin{macro}[EXP]{\@@_div_significand_ii:wwn}
1344 % \begin{quote}
1345 % \cs{@@_div_significand_ii:wwn} \meta{y} \cs{@@_sep:}
1346 % \meta{B_1} \cs{@@_sep:} \Arg{B_2} \Arg{B_3} \Arg{B_4}
1347 % \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4}
1348 % \meta{continuations} \meta{sign}
1349 % \end{quote}
1350 % Compute $Q_{B}$ by evaluating $\meta{B_1}\meta{B_2}0 / y - 1$. The
1351 % result is output to the left, in an \cs{@@_int_eval:w} which we
1352 % start now. Once that is evaluated (and the other $Q_{i}$ also,
1353 % since later expansions are triggered by this one), a packing
1354 % auxiliary takes care of placing the digits of $Q_{B}$ in an
1355 % appropriate way for the final addition to obtain $Q$. This
1356 % auxiliary is also used to compute $Q_{C}$ and $Q_{D}$ with the
1357 % inputs $C$ and $D$ instead of $B$.
1358 % \begin{macrocode}
1359 \cs_new:Npn \@@_div_significand_ii:wwn #1\@@_sep: #2\@@_sep:#3
1360 {
1361 \exp_after:wN \@@_div_significand_pack:NNN
1362 \int_value:w \@@_int_eval:w
1363 \exp_after:wN \@@_div_significand_calc:wwnnnnnnn
1364 \int_value:w \@@_int_eval:w
1365 999999 + #2 #3 0 / #1 \@@_sep: #2 #3 \@@_sep:
1366 }
1367 % \end{macrocode}
1368 % \end{macro}
1369 %
1370 % \begin{macro}[rEXP]{\@@_div_significand_iii:wwnnnnn}
1371 % \begin{quote}
1372 % \cs{@@_div_significand_iii:wwnnnnn} \meta{y} \cs{@@_sep:}
1373 % \meta{E_1} \cs{@@_sep:} \Arg{E_2} \Arg{E_3} \Arg{E_4}
1374 % \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} \meta{sign}
1375 % \end{quote}
1376 % We compute $P \simeq 2E/Z$ by rounding $2 E_{1} E_{2}/Z_{1}Z_{2}$.
1377 % Note the first $0$, which multiplies $Q_{D}$ by $10$: we later
1378 % add (roughly) $5\cdot P$, which amounts to adding $P/2 \simeq E/Z$
1379 % to $Q_{D}$, the appropriate correction from a hypothetical $Q_{E}$.
1380 % \begin{macrocode}
1381 \cs_new:Npn \@@_div_significand_iii:wwnnnnn #1\@@_sep: #2\@@_sep:#3#4#5 #6#7
1382 {
1383 0
1384 \exp_after:wN \@@_div_significand_iv:wwnnnnnnn
1385 \int_value:w \@@_int_eval:w ( 2 * #2 #3) / #6 #7 \@@_sep: % <- P
1386 #2 \@@_sep: {#3} {#4} {#5}
1387 {#6} {#7}
1388 }
1389 % \end{macrocode}
1390 % \end{macro}
1391 %
1392 % \begin{macro}[rEXP]
1393 % {
1394 % \@@_div_significand_iv:wwnnnnnnn,
1395 % \@@_div_significand_v:NNw,
1396 % \@@_div_significand_vi:Nw
1397 % }
1398 % \begin{quote}
1399 % \cs{@@_div_significand_iv:wwnnnnnnn} \meta{P} \cs{@@_sep:}
1400 % \meta{E_1} \cs{@@_sep:} \Arg{E_2} \Arg{E_3} \Arg{E_4}
1401 % \Arg{Z_1} \Arg{Z_2} \Arg{Z_3} \Arg{Z_4} \meta{sign}
1402 % \end{quote}
1403 % This adds to the current expression ($10^{7} + 10\cdot Q_{D}$) a
1404 % contribution of $5 \cdot P + \operatorname{sign}(T)$ with $T = 2 E -
1405 % P Z$. This amounts to adding $P / 2$ to $Q_{D}$, with an extra
1406 % \meta{rounding} digit. This \meta{rounding} digit is $0$ or $5$ if
1407 % $T$ does not contribute, i.e., if $0 = T = 2 E - P Z$, in
1408 % other words if $10^{16} A / Z$ is an integer or half-integer.
1409 % Otherwise it is in the appropriate range, $[1,4]$ or $[6,9]$. This
1410 % is precise enough for rounding purposes (in any mode).
1411 %
1412 % It seems an overkill to compute $T$ exactly as I do here, but I see
1413 % no faster way right now.
1414 %
1415 % Once more, we need to be careful and show that the calculation
1416 % $\#1\cdot\#6\#7$ below does not cause an overflow: naively, $P$ can
1417 % be up to $35$, and $\#6\#7$ up to $10^{8}$, but both cannot happen
1418 % simultaneously. To show that things are fine, we split in two
1419 % (non-disjoint) cases.
1420 % \begin{itemize}
1421 % \item For $P < 10$, the product obeys $P\cdot\#6\#7 < 10^{8} \cdot P
1422 % < 10^{9} $.
1423 % \item For large $P\geq 3$, the rounding error on $P$, which is at
1424 % most $1$, is less than a factor of $2$, hence $P\leq 4E/Z$. Also,
1425 % $\#6\#7 \leq 10^{8} \cdot Z$, hence $P\cdot \#6\#7 \leq 4E\cdot
1426 % 10^{8} < 10^{9}$.
1427 % \end{itemize}
1428 % Both inequalities could be made tighter if needed.
1429 %
1430 % Note however that $P\cdot \#8\#9$ may overflow, since the two
1431 % factors are now independent, and the result may reach $3.5\cdot
1432 % 10^{9}$. Thus we compute the two lower levels separately. The rest
1433 % is standard, except that we use |+| as a separator (ending integer
1434 % expressions explicitly). $T$ is negative if the first character is
1435 % |-|, it is positive if the first character is neither |0| nor |-|.
1436 % It is also positive if the first character is |0| and second
1437 % argument of \cs{@@_div_significand_vi:Nw}, a sum of several terms, is
1438 % also zero. Otherwise, there was an exact agreement: $T = 0$.
1439 % \begin{macrocode}
1440 \cs_new:Npn \@@_div_significand_iv:wwnnnnnnn
1441 #1\@@_sep: #2\@@_sep:#3#4#5 #6#7#8#9
1442 {
1443 + 5 * #1
1444 \exp_after:wN \@@_div_significand_vi:Nw
1445 \int_value:w \@@_int_eval:w -50 + 2*#2#3 - #1*#6#7 +
1446 \exp_after:wN \@@_div_significand_v:NN
1447 \int_value:w \@@_int_eval:w 499950 + 2*#4 - #1*#8 +
1448 \exp_after:wN \@@_div_significand_v:NN
1449 \int_value:w \@@_int_eval:w 500000 + 2*#5 - #1*#9 \@@_sep:
1450 }
1451 \cs_new:Npn \@@_div_significand_v:NN #1#2 { #1#2 \@@_int_eval_end: + }
1452 \cs_new:Npn \@@_div_significand_vi:Nw #1#2\@@_sep:
1453 {
1454 \if_meaning:w 0 #1
1455 \if_int_compare:w \@@_int_eval:w #2 > 0 + 1 \fi:
1456 \else:
1457 \if_meaning:w - #1 - \else: + \fi: 1
1458 \fi:
1459 \@@_sep:
1460 }
1461 % \end{macrocode}
1462 % \end{macro}
1463 %
1464 % \begin{macro}[EXP]{\@@_div_significand_pack:NNN}
1465 % At this stage, we are in the following situation: \TeX{} is in the
1466 % process of expanding several integer expressions, thus functions at
1467 % the bottom expand before those above.
1468 % \begin{quote}
1469 % \cs{@@_div_significand_test_o:w} $10^{6} + Q_{A}$
1470 % \cs{@@_div_significand_pack:NNN} $10^{6} + Q_{B}$
1471 % \cs{@@_div_significand_pack:NNN} $10^{6} + Q_{C}$
1472 % \cs{@@_div_significand_pack:NNN}
1473 % $10^{7} + 10\cdot Q_{D} + 5 \cdot P + \varepsilon$ \cs{@@_sep:} \meta{sign}
1474 % \end{quote}
1475 % Here, $\varepsilon = \operatorname{sign}(T)$ is $0$ in case $2E=PZ$,
1476 % $1$ in case $2E>PZ$, which means that $P$ was the correct value, but
1477 % not with an exact quotient, and $-1$ if $2E<PZ$, i.e., $P$
1478 % was an overestimate. The packing function we define now does
1479 % nothing special: it removes the $10^{6}$ and carries two digits (for
1480 % the $10^{5}$'s and the $10^{4}$'s).
1481 % \begin{macrocode}
1482 \cs_new:Npn \@@_div_significand_pack:NNN 1 #1 #2 { + #1 #2 \@@_sep: }
1483 % \end{macrocode}
1484 % \end{macro}
1485 %
1486 % \begin{macro}[rEXP]{\@@_div_significand_test_o:w}
1487 % \begin{quote}
1488 % \cs{@@_div_significand_test_o:w} |1| |0| \meta{5d} \cs{@@_sep:}
1489 % ~~\meta{4d} \cs{@@_sep:} \meta{4d} \cs{@@_sep:} \meta{5d} \cs{@@_sep:} \meta{sign}
1490 % \end{quote}
1491 % The reason we know that the first two digits are |1| and |0| is that
1492 % the final result is known to be between $0.1$ (inclusive) and $10$,
1493 % hence $\widetilde{Q_{A}}$ (the tilde denoting the contribution from
1494 % the other $Q_{i}$) is at most $99999$, and $10^{6}+\widetilde{Q_{A}}
1495 % = 10\cdots$.
1496 %
1497 % It is now time to round. This depends on how many digits the final
1498 % result will have.
1499 % \begin{macrocode}
1500 \cs_new:Npn \@@_div_significand_test_o:w 10 #1
1501 {
1502 \if_meaning:w 0 #1
1503 \exp_after:wN \@@_div_significand_small_o:wwwNNNNwN
1504 \else:
1505 \exp_after:wN \@@_div_significand_large_o:wwwNNNNwN
1506 \fi:
1507 #1
1508 }
1509 % \end{macrocode}
1510 % \end{macro}
1511 %
1512 % \begin{macro}[EXP]{\@@_div_significand_small_o:wwwNNNNwN}
1513 % \begin{quote}
1514 % \cs{@@_div_significand_small_o:wwwNNNNwN} |0| \meta{4d} \cs{@@_sep:}
1515 % ~~\meta{4d} \cs{@@_sep:} \meta{4d} \cs{@@_sep:} \meta{5d} \cs{@@_sep:} \meta{final sign}
1516 % \end{quote}
1517 % Standard use of the functions \cs{@@_basics_pack_low:NNNNNw} and
1518 % \cs{@@_basics_pack_high:NNNNNw}. We finally get to use the
1519 % \meta{final sign} which has been sitting there for a while.
1520 % \begin{macrocode}
1521 \cs_new:Npn \@@_div_significand_small_o:wwwNNNNwN
1522 0 #1\@@_sep: #2\@@_sep: #3\@@_sep: #4#5#6#7#8\@@_sep: #9
1523 {
1524 \exp_after:wN \@@_basics_pack_high:NNNNNw
1525 \int_value:w \@@_int_eval:w 1 #1#2
1526 \exp_after:wN \@@_basics_pack_low:NNNNNw
1527 \int_value:w \@@_int_eval:w 1 #3#4#5#6#7
1528 + \@@_round:NNN #9 #7 #8
1529 \exp_after:wN \@@_sep:
1530 }
1531 % \end{macrocode}
1532 % \end{macro}
1533 %
1534 % \begin{macro}[rEXP]{\@@_div_significand_large_o:wwwNNNNwN}
1535 % \begin{quote}
1536 % \cs{@@_div_significand_large_o:wwwNNNNwN} \meta{5d} \cs{@@_sep:}
1537 % ~~\meta{4d} \cs{@@_sep:} \meta{4d} \cs{@@_sep:} \meta{5d} \cs{@@_sep:} \meta{sign}
1538 % \end{quote}
1539 % We know that the final result cannot reach $10$, hence |1#1#2|,
1540 % together with contributions from the level below, cannot reach
1541 % $2\cdot 10^{9}$. For rounding, we build the \meta{rounding digit}
1542 % from the last two of our $18$ digits.
1543 % \begin{macrocode}
1544 \cs_new:Npn \@@_div_significand_large_o:wwwNNNNwN
1545 #1\@@_sep: #2\@@_sep: #3\@@_sep: #4#5#6#7#8\@@_sep: #9
1546 {
1547 + 1
1548 \exp_after:wN \@@_basics_pack_weird_high:NNNNNNNNw
1549 \int_value:w \@@_int_eval:w 1 #1 #2
1550 \exp_after:wN \@@_basics_pack_weird_low:NNNNw
1551 \int_value:w \@@_int_eval:w 1 #3 #4 #5 #6 +
1552 \exp_after:wN \@@_round:NNN
1553 \exp_after:wN #9
1554 \exp_after:wN #6
1555 \int_value:w \@@_round_digit:Nw #7 #8 \@@_sep:
1556 \exp_after:wN \@@_sep:
1557 }
1558 % \end{macrocode}
1559 % \end{macro}
1560 %
1561 % \subsection{Square root}
1562 %
1563 % \begin{macro}[EXP]{\@@_sqrt_o:w}
1564 % Zeros are unchanged: $\sqrt{-0} = -0$ and $\sqrt{+0} = +0$.
1565 % Negative numbers (other than $-0$) have no real square root.
1566 % Positive infinity, and \texttt{nan}, are unchanged. Finally, for
1567 % normal positive numbers, there is some work to do.
1568 % \begin{macrocode}
1569 \cs_new:Npn \@@_sqrt_o:w #1 \s_@@ \@@_chk:w #2#3#4\@@_sep: @
1570 {
1571 \if_meaning:w 0 #2 \@@_case_return_same_o:w \fi:
1572 \if_meaning:w 2 #3
1573 \@@_case_use:nw { \@@_invalid_operation_o:nw { sqrt } }
1574 \fi:
1575 \if_meaning:w 1 #2 \else: \@@_case_return_same_o:w \fi:
1576 \@@_sqrt_npos_o:w
1577 \s_@@ \@@_chk:w #2 #3 #4\@@_sep:
1578 }
1579 % \end{macrocode}
1580 % \end{macro}
1581 %
1582 % \begin{macro}[EXP]{\@@_sqrt_npos_o:w}
1583 % \begin{macro}[rEXP]
1584 % {\@@_sqrt_npos_auxi_o:wwnnN, \@@_sqrt_npos_auxii_o:wNNNNNNNN}
1585 % Prepare \cs{@@_sanitize:Nw} to receive the final sign~|0| (the
1586 % result is always positive) and the exponent, equal to half of the
1587 % exponent~|#1| of the argument. If the exponent~|#1| is even, find a
1588 % first approximation of the square root of the significand $10^{8}
1589 % a_1 + a_2 = 10^{8} |#2#3| + |#4#5|$ through Newton's method,
1590 % starting at $x = 57234133 \simeq 10^{7.75}$. Otherwise, first shift
1591 % the significand of the argument by one digit, getting
1592 % $a_1'\in[10^{6}, 10^{7})$ instead of $[10^{7}, 10^{8})$, then use
1593 % Newton's method starting at $17782794 \simeq 10^{7.25}$.
1594 % \begin{macrocode}
1595 \cs_new:Npn \@@_sqrt_npos_o:w \s_@@ \@@_chk:w 1 0 #1#2#3#4#5\@@_sep:
1596 {
1597 \exp_after:wN \@@_sanitize:Nw
1598 \exp_after:wN 0
1599 \int_value:w \@@_int_eval:w
1600 \if_int_odd:w #1 \exp_stop_f:
1601 \exp_after:wN \@@_sqrt_npos_auxi_o:wwnnN
1602 \fi:
1603 #1 / 2
1604 \@@_sqrt_Newton_o:wwn 56234133\@@_sep: 0\@@_sep: {#2#3} {#4#5} 0
1605 }
1606 \cs_new:Npn \@@_sqrt_npos_auxi_o:wwnnN #1 / 2 #2\@@_sep: 0\@@_sep: #3#4#5
1607 {
1608 ( #1 + 1 ) / 2
1609 \@@_pack_eight:wNNNNNNNN
1610 \@@_sqrt_npos_auxii_o:wNNNNNNNN
1611 \@@_sep:
1612 0 #3 #4
1613 }
1614 \cs_new:Npn \@@_sqrt_npos_auxii_o:wNNNNNNNN #1\@@_sep: #2#3#4#5#6#7#8#9
1615 { \@@_sqrt_Newton_o:wwn 17782794\@@_sep: 0\@@_sep: {#1} {#2#3#4#5#6#7#8#9} }
1616 % \end{macrocode}
1617 % \end{macro}
1618 % \end{macro}
1619 %
1620 % \begin{macro}[rEXP]{\@@_sqrt_Newton_o:wwn}
1621 % Newton's method maps $x\mapsto\bigl[(x + [10^{8} a_1 / x])/2\bigr]$
1622 % in each iteration, where $[b/c]$ denotes \eTeX{}'s division. This
1623 % division rounds the real number $b/c$ to the closest integer,
1624 % rounding ties away from zero, hence when $c$~is even,
1625 % $b/c - 1/2 + 1/c \leq [b/c] \leq b/c + 1/2$
1626 % and when $c$~is odd,
1627 % $b/c - 1/2 + 1/(2c) \leq [b/c] \leq b/c + 1/2 - 1/(2c)$.
1628 % For all~$c$, $b/c - 1/2 + 1/(2c) \leq [b/c] \leq b/c + 1/2$.
1629 %
1630 % Let us prove that the method converges when implemented with \eTeX{}
1631 % integer division, for any $10^{6} \leq a_1 < 10^{8}$ and starting
1632 % value $10^{6} \leq x < 10^{8}$. Using the inequalities above and
1633 % the arithmetic--geometric inequality $(x+t)/2 \geq \sqrt{xt}$ for $t
1634 % = 10^{8} a_1 / x$, we find
1635 % \[
1636 % x'
1637 % = \left[\frac{x + [10^{8} a_1 / x]}{2}\right]
1638 % \geq \frac{x + 10^{8} a_1 / x - 1/2 + 1/(2x)}{2}
1639 % \geq \sqrt{10^{8} a_1} - \frac{1}{4} + \frac{1}{4x} \,.
1640 % \]
1641 % After any step of iteration, we thus have $\delta = x - \sqrt{10^{8}
1642 % a_1} \geq -0.25 + 0.25 \cdot 10^{-8}$. The new difference
1643 % $\delta' = x' - \sqrt{10^{8} a_1}$ after one step is bounded above
1644 % as
1645 % \[
1646 % x' - \sqrt{10^{8} a_1}
1647 % \leq \frac{x + 10^{8} a_1 / x + 1/2}{2} + \frac{1}{2}
1648 % - \sqrt{10^{8} a_1}
1649 % \leq \frac{\delta}{2} \frac{\delta}{\sqrt{10^{8} a_1} + \delta}
1650 % + \frac{3}{4} \,.
1651 % \]
1652 % For $\delta > 3/2$, this last expression is
1653 % $\leq\delta/2+3/4<\delta$, hence $\delta$~decreases at each step:
1654 % since all~$x$ are integers, $\delta$~must reach a value
1655 % $-1/4<\delta\leq 3/2$. In this range of values, we get $\delta'
1656 % \leq \frac{3}{4} \frac{3}{2\sqrt{10^{8} a_1}} + \frac{3}{4} \leq
1657 % 0.75 + 1.125 \cdot 10^{-7}$. We deduce that the difference $\delta
1658 % = x - \sqrt{10^{8} a_1}$ eventually reaches a value in the interval
1659 % $[-0.25 + 0.25\cdot 10^{-8}, 0.75 + 11.25 \cdot 10^{-8}]$, whose
1660 % width is $1 + 11 \cdot 10^{-8}$. The corresponding interval for~$x$
1661 % may contain two integers, hence $x$~might oscillate between those
1662 % two values.
1663 %
1664 % However, the fact that $x\mapsto x-1$ and $x-1 \mapsto x$ puts
1665 % stronger constraints, which are not compatible: the first implies
1666 % \[
1667 % x + [10^{8} a_1 / x] \leq 2x - 2
1668 % \]
1669 % hence $10^{8} a_1 / x \leq x - 3/2$, while the second implies
1670 % \[
1671 % x - 1 + [10^{8} a_1 / (x - 1)] \geq 2x - 1
1672 % \]
1673 % hence $10^{8} a_1 / (x - 1) \geq x - 1/2$. Combining the two
1674 % inequalities yields $x^2 - 3x/2 \geq 10^{8} a_1 \geq x - 3x/2 +
1675 % 1/2$, which cannot hold. Therefore, the iteration always converges
1676 % to a single integer~$x$. To stop the iteration when two consecutive
1677 % results are equal, the function \cs{@@_sqrt_Newton_o:wwn} receives
1678 % the newly computed result as~|#1|, the previous result as~|#2|, and
1679 % $a_1$ as~|#3|. Note that \eTeX{} combines the computation of a
1680 % multiplication and a following division, thus avoiding overflow in
1681 % |#3 * 100000000 / #1|. In any case, the result is within $[10^{7},
1682 % 10^{8}]$.
1683 % \begin{macrocode}
1684 \cs_new:Npn \@@_sqrt_Newton_o:wwn #1\@@_sep: #2\@@_sep: #3
1685 {
1686 \if_int_compare:w #1 = #2 \exp_stop_f:
1687 \exp_after:wN \@@_sqrt_auxi_o:NNNNwnnN
1688 \int_value:w \@@_int_eval:w 9999 9999 +
1689 \exp_after:wN \@@_use_none_until_s:w
1690 \fi:
1691 \exp_after:wN \@@_sqrt_Newton_o:wwn
1692 \int_value:w \@@_int_eval:w (#1 + #3 * 1 0000 0000 / #1) / 2 \@@_sep:
1693 #1\@@_sep: {#3}
1694 }
1695 % \end{macrocode}
1696 % \end{macro}
1697 %
1698 % \begin{macro}[rEXP]{\@@_sqrt_auxi_o:NNNNwnnN}
1699 % This function is followed by $10^{8}+x-1$, which has~$9$ digits
1700 % starting with~$1$, then \cs{@@_sep:} \Arg{a_1} \Arg{a_2} \meta{a'}. Here,
1701 % $x \simeq \sqrt{10^{8} a_1}$ and we want to estimate the square root of
1702 % $a = 10^{-8} a_1 + 10^{-16} a_2 + 10^{-17} a'$. We set up an
1703 % initial underestimate
1704 % \[
1705 % y = (x - 1) 10^{-8} + 0.2499998875 \cdot 10^{-8} \lesssim \sqrt{a}\,.
1706 % \]
1707 % From the inequalities shown earlier, we know that $y \leq
1708 % \sqrt{10^{-8} a_1} \leq \sqrt{a}$ and that $\sqrt{10^{-8} a_1} \leq
1709 % y + 10^{-8} + 11\cdot 10^{-16}$ hence (using $0.1\leq y\leq
1710 % \sqrt{a}\leq 1$)
1711 % \[
1712 % a - y^2 \leq 10^{-8} a_1 + 10^{-8} - y^2
1713 % \leq (y + 10^{-8} + 11\cdot 10^{-16})^2 - y^2 + 10^{-8}
1714 % < 3.2 \cdot 10^{-8} \,,
1715 % \]
1716 % and $\sqrt{a} - y = (a - y^2)/(\sqrt{a} + y) \leq 16 \cdot 10^{-8}$.
1717 % Next, \cs{@@_sqrt_auxii_o:NnnnnnnnN} is called several times to
1718 % get closer and closer underestimates of~$\sqrt{a}$. By
1719 % construction, the underestimates~$y$ are always increasing, $a - y^2
1720 % < 3.2 \cdot 10^{-8}$ for all. Also, $y<1$.
1721 % \begin{macrocode}
1722 \cs_new:Npn \@@_sqrt_auxi_o:NNNNwnnN 1 #1#2#3#4#5\@@_sep:
1723 {
1724 \@@_sqrt_auxii_o:NnnnnnnnN
1725 \@@_sqrt_auxiii_o:wnnnnnnnn
1726 {#1#2#3#4} {#5} {2499} {9988} {7500}
1727 }
1728 % \end{macrocode}
1729 % \end{macro}
1730 %
1731 % \begin{macro}[rEXP]{\@@_sqrt_auxii_o:NnnnnnnnN}
1732 % This receives a continuation function~|#1|, then five blocks of~$4$
1733 % digits for~$y$, then two $8$-digit blocks and a single digit
1734 % for~$a$. A common estimate of $\sqrt{a} - y = (a - y^2) / (\sqrt{a}
1735 % + y)$ is $(a - y^2)/(2y)$, which leads to alternating overestimates
1736 % and underestimates. We tweak this, to only work with underestimates
1737 % (no need then to worry about signs in the computation). Each step
1738 % finds the largest integer $j\leq 6$ such that $10^{4j}(a-y^2) <
1739 % 2\cdot 10^{8}$, then computes the integer (with \eTeX{}'s rounding
1740 % division)
1741 % \[
1742 % 10^{4j} z =
1743 % \Bigl[\bigl(\lfloor 10^{4j}(a-y^2)\rfloor - 257\bigr)
1744 % \cdot (0.5\cdot 10^{8})
1745 % \Bigm/ \lfloor 10^{8} y + 1\rfloor\Bigr] \,.
1746 % \]
1747 % The choice of~$j$ ensures that $10^{4j} z < 2\cdot 10^{8} \cdot
1748 % 0.5\cdot 10^{8} / 10^{7} = 10^{9}$, thus $10^{9} + 10^{4j} z$ has
1749 % exactly $10$~digits, does not overflow \TeX{}'s integer range, and
1750 % starts with~$1$. Incidentally, since all $a - y^2 \leq 3.2\cdot
1751 % 10^{-8}$, we know that $j\geq 3$.
1752 %
1753 % Let us show that $z$ is an underestimate of $\sqrt{a} - y$. On the
1754 % one hand, $\sqrt{a} - y \leq 16\cdot 10^{-8}$ because this holds for
1755 % the initial~$y$ and values of~$y$ can only increase. On the other
1756 % hand, the choice of~$j$ implies that $\sqrt{a} - y \leq
1757 % 5(\sqrt{a}+y)(\sqrt{a}-y) = 5(a - y^2) < 10^{9-4j}$. For $j=3$, the
1758 % first bound is better, while for larger~$j$, the second bound is
1759 % better. For all $j\in[3,6]$, we find $\sqrt{a}-y < 16\cdot
1760 % 10^{-2j}$. From this, we deduce that
1761 % \[
1762 % 10^{4j} (\sqrt{a}-y)
1763 % = \frac{10^{4j}\bigl(a-y^2-(\sqrt{a}-y)^2\bigr)}{2y}
1764 % \geq \frac{\bigl\lfloor 10^{4j}(a-y^2)\bigr\rfloor-257}
1765 % {2\cdot 10^{-8} \lfloor 10^{8}y+1\rfloor}
1766 % + \frac{1}{2}
1767 % \]
1768 % where we have replaced the bound $10^{4j}(16\cdot 10^{-2j}) = 256$
1769 % by~$257$ and extracted the corresponding term $1/\bigl(2\cdot
1770 % 10^{-8} \lfloor 10^{8}y+1\rfloor\bigr) \geq 1/2$. Given that
1771 % \eTeX{}'s integer division obeys $[b/c] \leq b/c + 1/2$, we deduce
1772 % that $10^{4j} z \leq 10^{4j} (\sqrt{a}-y)$, hence $y+z\leq\sqrt{a}$
1773 % is an underestimate of~$\sqrt{a}$, as claimed. One implementation
1774 % detail: because the computation involves |-#4*#4| |-| |2*#3*#5| |-|
1775 % |2*#2*#6| which may be as low as $-5\cdot 10^{8}$, we need to use
1776 % the \texttt{pack_big} functions, and the \texttt{big} shifts.
1777 % \begin{macrocode}
1778 \cs_new:Npn \@@_sqrt_auxii_o:NnnnnnnnN #1 #2#3#4#5#6 #7#8#9
1779 {
1780 \exp_after:wN #1
1781 \int_value:w \@@_int_eval:w \c_@@_big_leading_shift_int
1782 + #7 - #2 * #2
1783 \exp_after:wN \@@_pack_big:NNNNNNw
1784 \int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int
1785 - 2 * #2 * #3
1786 \exp_after:wN \@@_pack_big:NNNNNNw
1787 \int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int
1788 + #8 - #3 * #3 - 2 * #2 * #4
1789 \exp_after:wN \@@_pack_big:NNNNNNw
1790 \int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int
1791 - 2 * #3 * #4 - 2 * #2 * #5
1792 \exp_after:wN \@@_pack_big:NNNNNNw
1793 \int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int
1794 + #9 000 0000 - #4 * #4 - 2 * #3 * #5 - 2 * #2 * #6
1795 \exp_after:wN \@@_pack_big:NNNNNNw
1796 \int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int
1797 - 2 * #4 * #5 - 2 * #3 * #6
1798 \exp_after:wN \@@_pack_big:NNNNNNw
1799 \int_value:w \@@_int_eval:w \c_@@_big_middle_shift_int
1800 - #5 * #5 - 2 * #4 * #6
1801 \exp_after:wN \@@_pack_big:NNNNNNw
1802 \int_value:w \@@_int_eval:w
1803 \c_@@_big_middle_shift_int
1804 - 2 * #5 * #6
1805 \exp_after:wN \@@_pack_big:NNNNNNw
1806 \int_value:w \@@_int_eval:w
1807 \c_@@_big_trailing_shift_int
1808 - #6 * #6 \@@_sep:
1809 % (
1810 - 257 ) * 5000 0000 / (#2#3 + 1) + 10 0000 0000 \@@_sep:
1811 {#2}{#3}{#4}{#5}{#6} {#7}{#8}#9
1812 }
1813 % \end{macrocode}
1814 % \end{macro}
1815 %
1816 % \begin{macro}[rEXP]
1817 % {
1818 % \@@_sqrt_auxiii_o:wnnnnnnnn,
1819 % \@@_sqrt_auxiv_o:NNNNNw,
1820 % \@@_sqrt_auxv_o:NNNNNw,
1821 % \@@_sqrt_auxvi_o:NNNNNw,
1822 % \@@_sqrt_auxvii_o:NNNNNw
1823 % }
1824 % We receive here the difference $a-y^2=d=\sum_i d_i \cdot 10^{-4i}$,
1825 % as \meta{d_2} \cs{@@_sep:} \Arg{d_3} \ldots{} \Arg{d_{10}}, where each
1826 % block has~$4$ digits, except \meta{d_2}. This function finds the largest
1827 % $j\leq 6$ such that $10^{4j}(a-y^2) < 2\cdot 10^{8}$, then leaves an
1828 % open parenthesis and the integer
1829 % $\bigl\lfloor 10^{4j}(a-y^2)\bigr\rfloor$ in an integer
1830 % expression. The closing parenthesis is provided by the caller
1831 % \cs{@@_sqrt_auxii_o:NnnnnnnnN}, which completes the expression
1832 % \[
1833 % 10^{4j} z =
1834 % \Bigl[\bigl(\lfloor 10^{4j}(a-y^2)\rfloor - 257\bigr)
1835 % \cdot (0.5\cdot 10^{8})
1836 % \Bigm/ \lfloor 10^{8} y + 1\rfloor\Bigr]
1837 % \]
1838 % for an estimate of $10^{4j} (\sqrt{a} - y)$. If $d_2\geq 2$, $j=3$
1839 % and the \texttt{auxiv} auxiliary receives $10^{12} z$. If $d_2\leq
1840 % 1$ but $10^{4} d_2 + d_3 \geq 2$, $j=4$ and the \texttt{auxv}
1841 % auxiliary is called, and receives $10^{16} z$, and so on. In all
1842 % those cases, the \texttt{auxviii} auxiliary is set up to add~$z$
1843 % to~$y$, then go back to the \texttt{auxii} step with continuation
1844 % \texttt{auxiii} (the function we are currently describing). The
1845 % maximum value of $j$ is~$6$, regardless of whether $10^{12} d_2 +
1846 % 10^{8} d_3 + 10^{4} d_4 + d_5 \geq 1$. In this last case, we detect
1847 % when $10^{24} z < 10^{7}$, which essentially means $\sqrt{a} - y
1848 % \lesssim 10^{-17}$: once this threshold is reached, there is enough
1849 % information to find the correctly rounded~$\sqrt{a}$ with only one
1850 % more call to \cs{@@_sqrt_auxii_o:NnnnnnnnN}. Note that the
1851 % iteration cannot be stuck before reaching $j=6$, because for $j<6$,
1852 % one has $2\cdot 10^{8}\leq 10^{4(j+1)}(a-y^2)$, hence
1853 % \[
1854 % 10^{4j} z
1855 % \geq \frac{(20000-257)(0.5\cdot 10^{8})}{\lfloor 10^{8} y + 1\rfloor}
1856 % \geq (20000-257)\cdot 0.5 > 0 \,.
1857 % \]
1858 % \begin{macrocode}
1859 \cs_new:Npn \@@_sqrt_auxiii_o:wnnnnnnnn
1860 #1\@@_sep: #2#3#4#5#6#7#8#9
1861 {
1862 \if_int_compare:w #1 > \c_one_int
1863 \exp_after:wN \@@_sqrt_auxiv_o:NNNNNw
1864 \int_value:w \@@_int_eval:w (#1#2 %)
1865 \else:
1866 \if_int_compare:w #1#2 > \c_one_int
1867 \exp_after:wN \@@_sqrt_auxv_o:NNNNNw
1868 \int_value:w \@@_int_eval:w (#1#2#3 %)
1869 \else:
1870 \if_int_compare:w #1#2#3 > \c_one_int
1871 \exp_after:wN \@@_sqrt_auxvi_o:NNNNNw
1872 \int_value:w \@@_int_eval:w (#1#2#3#4 %)
1873 \else:
1874 \exp_after:wN \@@_sqrt_auxvii_o:NNNNNw
1875 \int_value:w \@@_int_eval:w (#1#2#3#4#5 %)
1876 \fi:
1877 \fi:
1878 \fi:
1879 }
1880 \cs_new:Npn \@@_sqrt_auxiv_o:NNNNNw 1#1#2#3#4#5#6\@@_sep:
1881 { \@@_sqrt_auxviii_o:nnnnnnn {#1#2#3#4#5#6} {00000000} }
1882 \cs_new:Npn \@@_sqrt_auxv_o:NNNNNw 1#1#2#3#4#5#6\@@_sep:
1883 { \@@_sqrt_auxviii_o:nnnnnnn {000#1#2#3#4#5} {#60000} }
1884 \cs_new:Npn \@@_sqrt_auxvi_o:NNNNNw 1#1#2#3#4#5#6\@@_sep:
1885 { \@@_sqrt_auxviii_o:nnnnnnn {0000000#1} {#2#3#4#5#6} }
1886 \cs_new:Npn \@@_sqrt_auxvii_o:NNNNNw 1#1#2#3#4#5#6\@@_sep:
1887 {
1888 \if_int_compare:w #1#2 = \c_zero_int
1889 \exp_after:wN \@@_sqrt_auxx_o:Nnnnnnnn
1890 \fi:
1891 \@@_sqrt_auxviii_o:nnnnnnn {00000000} {000#1#2#3#4#5}
1892 }
1893 % \end{macrocode}
1894 % \end{macro}
1895 %
1896 % \begin{macro}[rEXP]
1897 % {\@@_sqrt_auxviii_o:nnnnnnn, \@@_sqrt_auxix_o:wnwnw}
1898 % Simply add the two $8$-digit blocks of~$z$, aligned to the last four
1899 % of the five $4$-digit blocks of~$y$, then call the \texttt{auxii}
1900 % auxiliary to evaluate $y'^{2} = (y+z)^{2}$.
1901 % \begin{macrocode}
1902 \cs_new:Npn \@@_sqrt_auxviii_o:nnnnnnn #1#2 #3#4#5#6#7
1903 {
1904 \exp_after:wN \@@_sqrt_auxix_o:wnwnw
1905 \int_value:w \@@_int_eval:w #3
1906 \exp_after:wN \@@_basics_pack_low:NNNNNw
1907 \int_value:w \@@_int_eval:w #1 + 1#4#5
1908 \exp_after:wN \@@_basics_pack_low:NNNNNw
1909 \int_value:w \@@_int_eval:w #2 + 1#6#7 \@@_sep:
1910 }
1911 \cs_new:Npn \@@_sqrt_auxix_o:wnwnw #1\@@_sep: #2#3\@@_sep: #4#5\@@_sep:
1912 {
1913 \@@_sqrt_auxii_o:NnnnnnnnN
1914 \@@_sqrt_auxiii_o:wnnnnnnnn {#1}{#2}{#3}{#4}{#5}
1915 }
1916 % \end{macrocode}
1917 % \end{macro}
1918 %
1919 % \begin{macro}[rEXP]
1920 % {\@@_sqrt_auxx_o:Nnnnnnnn, \@@_sqrt_auxxi_o:wwnnN}
1921 % At this stage, $j=6$ and $10^{24} z < 10^{7}$, hence
1922 % \[
1923 % 10^{7} + 1/2 > 10^{24} z + 1/2 \geq
1924 % \bigl(10^{24}(a-y^2) - 258\bigr) \cdot (0.5\cdot 10^{8})
1925 % \Bigm/ (10^{8} y + 1) \,,
1926 % \]
1927 % then $10^{24}(a-y^2) - 258 < 2 (10^{7} + 1/2) (y + 10^{-8})$, and
1928 % \[
1929 % 10^{24}(a-y^2)
1930 % < (10^{7} + 1290.5) (1 + 10^{-8}/y) (2y)
1931 % < (10^{7} + 1290.5) (1 + 10^{-7}) (y + \sqrt{a}) \,,
1932 % \]
1933 % which finally implies $0\leq\sqrt{a}-y < 0.2\cdot 10^{-16}$. In
1934 % particular, $y$~is an underestimate of~$\sqrt{a}$ and $y+0.5\cdot
1935 % 10^{-16}$ is a (strict) overestimate. There is at exactly one
1936 % multiple $m$~of $0.5\cdot 10^{-16}$ in the interval $[y, y+0.5\cdot
1937 % 10^{-16})$. If $m^2>a$, then the square root is inexact and is
1938 % obtained by rounding $m-\epsilon$ to a multiple of $10^{-16}$ (the
1939 % precise shift $0<\epsilon<0.5\cdot 10^{-16}$ is irrelevant for
1940 % rounding). If $m^2=a$ then the square root is exactly~$m$, and
1941 % there is no rounding. If $m^2<a$ then we round $m+\epsilon$. For
1942 % now, discard a few irrelevant arguments |#1|, |#2|, |#3|, and find
1943 % the multiple of $0.5\cdot 10^{-16}$ within $[y, y+0.5\cdot
1944 % 10^{-16})$; rather, only the last $4$~digits |#8| of~$y$ are
1945 % considered, and we do not perform any carry yet. The \texttt{auxxi}
1946 % auxiliary sets up \texttt{auxii} with a continuation function
1947 % \texttt{auxxii} instead of \texttt{auxiii} as before. To prevent
1948 % \texttt{auxii} from giving a negative results $a-m^2$, we compute
1949 % $a+10^{-16}-m^2$ instead, always positive since $m<\sqrt{a}+0.5\cdot
1950 % 10^{-16}$ and $a\leq 1-10^{-16}$.
1951 % \begin{macrocode}
1952 \cs_new:Npn \@@_sqrt_auxx_o:Nnnnnnnn #1#2#3 #4#5#6#7#8
1953 {
1954 \exp_after:wN \@@_sqrt_auxxi_o:wwnnN
1955 \int_value:w \@@_int_eval:w
1956 (#8 + 2499) / 5000 * 5000 \@@_sep:
1957 {#4} {#5} {#6} {#7} \@@_sep:
1958 }
1959 \cs_new:Npn \@@_sqrt_auxxi_o:wwnnN #1\@@_sep: #2\@@_sep: #3#4#5
1960 {
1961 \@@_sqrt_auxii_o:NnnnnnnnN
1962 \@@_sqrt_auxxii_o:nnnnnnnnw
1963 #2 {#1}
1964 {#3} { #4 + 1 } #5
1965 }
1966 % \end{macrocode}
1967 % \end{macro}
1968 %
1969 % \begin{macro}[rEXP]
1970 % {\@@_sqrt_auxxii_o:nnnnnnnnw, \@@_sqrt_auxxiii_o:w}
1971 % The difference $0\leq a+10^{-16}-m^2\leq
1972 % 10^{-16}+(\sqrt{a}-m)(\sqrt{a}+m)\leq 2\cdot 10^{-16}$ was just
1973 % computed: its first $8$~digits vanish, as do the next four,~|#1|,
1974 % and most of the following four,~|#2|. The guess~$m$ is an
1975 % overestimate if $a+10^{-16}-m^2 < 10^{-16}$, that is, |#1#2|
1976 % vanishes. Otherwise it is an underestimate, unless
1977 % $a+10^{-16}-m^2=10^{-16}$ exactly. For an underestimate, call the
1978 % \texttt{auxxiv} function with argument~$9998$. For an exact result
1979 % call it with~$9999$, and for an overestimate call it with~$10000$.
1980 % \begin{macrocode}
1981 \cs_new:Npn \@@_sqrt_auxxii_o:nnnnnnnnw 0\@@_sep: #1#2#3#4#5#6#7#8 #9\@@_sep:
1982 {
1983 \if_int_compare:w #1#2 > \c_zero_int
1984 \if_int_compare:w #1#2 = \c_one_int
1985 \if_int_compare:w #3#4 = \c_zero_int
1986 \if_int_compare:w #5#6 = \c_zero_int
1987 \if_int_compare:w #7#8 = \c_zero_int
1988 \@@_sqrt_auxxiii_o:w
1989 \fi:
1990 \fi:
1991 \fi:
1992 \fi:
1993 \exp_after:wN \@@_sqrt_auxxiv_o:wnnnnnnnN
1994 \int_value:w 9998
1995 \else:
1996 \exp_after:wN \@@_sqrt_auxxiv_o:wnnnnnnnN
1997 \int_value:w 10000
1998 \fi:
1999 \@@_sep:
2000 }
2001 \cs_new:Npn \@@_sqrt_auxxiii_o:w \fi: \fi: \fi: \fi: #1 \fi: \@@_sep:
2002 {
2003 \fi: \fi: \fi: \fi: \fi:
2004 \@@_sqrt_auxxiv_o:wnnnnnnnN 9999 \@@_sep:
2005 }
2006 % \end{macrocode}
2007 % \end{macro}
2008 %
2009 % \begin{macro}[rEXP]{\@@_sqrt_auxxiv_o:wnnnnnnnN}
2010 % This receives $9998$, $9999$ or $10000$ as~|#1| when $m$~is an
2011 % underestimate, exact, or an overestimate, respectively. Then
2012 % comes~$m$ as five blocks of~$4$ digits, but where the last
2013 % block~|#6| may be $0$, $5000$, or~$10000$. In the latter case, we
2014 % need to add a carry, unless $m$~is an overestimate (|#1|~is then
2015 % $10000$). Then comes~$a$ as three arguments. Rounding is done by
2016 % \cs{@@_round:NNN}, whose first argument is the final sign~$0$
2017 % (square roots are positive). We fake its second argument. It
2018 % should be the last digit kept, but this is only used when ties are
2019 % \enquote{rounded to even}, and only when the result is exactly
2020 % half-way between two representable numbers rational square roots of
2021 % numbers with $16$~significant digits have: this situation never
2022 % arises for the square root, as any exact square root of a $16$~digit
2023 % number has at most $8$~significant digits. Finally, the last
2024 % argument is the next digit, possibly shifted by~$1$ when there are
2025 % further nonzero digits. This is achieved by \cs{@@_round_digit:Nw},
2026 % which receives (after removal of the $10000$'s digit) one of $0000$,
2027 % $0001$, $4999$, $5000$, $5001$, or~$9999$, which it converts to $0$,
2028 % $1$, $4$, $5$, $6$, and~$9$, respectively.
2029 % \begin{macrocode}
2030 \cs_new:Npn \@@_sqrt_auxxiv_o:wnnnnnnnN #1\@@_sep: #2#3#4#5#6 #7#8#9
2031 {
2032 \exp_after:wN \@@_basics_pack_high:NNNNNw
2033 \int_value:w \@@_int_eval:w 1 0000 0000 + #2#3
2034 \exp_after:wN \@@_basics_pack_low:NNNNNw
2035 \int_value:w \@@_int_eval:w 1 0000 0000
2036 + #4#5
2037 \if_int_compare:w #6 > #1 \exp_stop_f: + 1 \fi:
2038 + \exp_after:wN \@@_round:NNN
2039 \exp_after:wN 0
2040 \exp_after:wN 0
2041 \int_value:w
2042 \exp_after:wN \use_i:nn
2043 \exp_after:wN \@@_round_digit:Nw
2044 \int_value:w \@@_int_eval:w #6 + 19999 - #1 \@@_sep:
2045 \exp_after:wN \@@_sep:
2046 }
2047 % \end{macrocode}
2048 % \end{macro}
2049 %
2050 % \subsection{About the sign and exponent}
2051 %
2052 % \begin{macro}[EXP]{\@@_logb_o:w, \@@_logb_aux_o:w}
2053 % The exponent of a normal number is its \meta{exponent} minus one.
2054 % \begin{macrocode}
2055 \cs_new:Npn \@@_logb_o:w ? \s_@@ \@@_chk:w #1#2\@@_sep: @
2056 {
2057 \if_case:w #1 \exp_stop_f:
2058 \@@_case_use:nw
2059 { \@@_division_by_zero_o:Nnw \c_minus_inf_fp { logb } }
2060 \or: \exp_after:wN \@@_logb_aux_o:w
2061 \or: \@@_case_return_o:Nw \c_inf_fp
2062 \else: \@@_case_return_same_o:w
2063 \fi:
2064 \s_@@ \@@_chk:w #1 #2\@@_sep:
2065 }
2066 \cs_new:Npn \@@_logb_aux_o:w \s_@@ \@@_chk:w #1 #2 #3 #4 \@@_sep:
2067 {
2068 \exp_after:wN \@@_parse:n \exp_after:wN
2069 { \int_value:w \int_eval:w #3 - 1 \exp_after:wN }
2070 }
2071 % \end{macrocode}
2072 % \end{macro}
2073 %
2074 % \begin{macro}[EXP]{\@@_sign_o:w}
2075 % \begin{macro}[EXP]{\@@_sign_aux_o:w}
2076 % Find the sign of the floating point: \texttt{nan}, |+0|, |-0|, |+1| or |-1|.
2077 % \begin{macrocode}
2078 \cs_new:Npn \@@_sign_o:w ? \s_@@ \@@_chk:w #1#2\@@_sep: @
2079 {
2080 \if_case:w #1 \exp_stop_f:
2081 \@@_case_return_same_o:w
2082 \or: \exp_after:wN \@@_sign_aux_o:w
2083 \or: \exp_after:wN \@@_sign_aux_o:w
2084 \else: \@@_case_return_same_o:w
2085 \fi:
2086 \s_@@ \@@_chk:w #1 #2\@@_sep:
2087 }
2088 \cs_new:Npn \@@_sign_aux_o:w \s_@@ \@@_chk:w #1 #2 #3 \@@_sep:
2089 { \exp_after:wN \@@_set_sign_o:w \exp_after:wN #2 \c_one_fp @ }
2090 % \end{macrocode}
2091 % \end{macro}
2092 % \end{macro}
2093 %
2094 % \begin{macro}[EXP]{\@@_set_sign_o:w}
2095 % This function is used for the unary minus and for \texttt{abs}. It
2096 % leaves the sign of \texttt{nan} invariant, turns negative numbers
2097 % (sign~$2$) to positive numbers (sign~$0$) and positive numbers
2098 % (sign~$0$) to positive or negative numbers depending on~|#1|. It
2099 % also expands after itself in the input stream, just like
2100 % \cs{@@_+_o:ww}.
2101 % \begin{macrocode}
2102 \cs_new:Npn \@@_set_sign_o:w #1 \s_@@ \@@_chk:w #2#3#4\@@_sep: @
2103 {
2104 \exp_after:wN \@@_exp_after_o:w
2105 \exp_after:wN \s_@@
2106 \exp_after:wN \@@_chk:w
2107 \exp_after:wN #2
2108 \int_value:w
2109 \if_case:w #3 \exp_stop_f: #1 \or: 1 \or: 0 \fi: \exp_stop_f:
2110 #4\@@_sep:
2111 }
2112 % \end{macrocode}
2113 % \end{macro}
2114 %
2115 % \subsection{Operations on tuples}
2116 %
2117 % \begin{macro}[EXP]{\@@_tuple_set_sign_o:w}
2118 % \begin{macro}[EXP]{\@@_tuple_set_sign_aux_o:Nnw, \@@_tuple_set_sign_aux_o:w}
2119 % Two cases: |abs(|\meta{tuple}|)| for which |#1| is $0$ (invalid for
2120 % tuples) and |-|\meta{tuple} for which |#1| is $2$. In that case,
2121 % map over all items in the tuple an auxiliary that dispatches to the
2122 % type-appropriate sign-flipping function.
2123 % \begin{macrocode}
2124 \cs_new:Npn \@@_tuple_set_sign_o:w #1#2 @
2125 {
2126 \if_meaning:w 2 #1
2127 \exp_after:wN \@@_tuple_set_sign_aux_o:Nnw
2128 \fi:
2129 \@@_invalid_operation_o:nw { abs }
2130 #2
2131 }
2132 \cs_new:Npn \@@_tuple_set_sign_aux_o:Nnw #1#2
2133 { \@@_tuple_map_o:nw \@@_tuple_set_sign_aux_o:w }
2134 \cs_new:Npn \@@_tuple_set_sign_aux_o:w #1#2 \@@_sep:
2135 {
2136 \@@_change_func_type:NNN #1 \@@_set_sign_o:w
2137 \@@_parse_apply_unary_error:NNw
2138 2 #1 #2 \@@_sep: @
2139 }
2140 % \end{macrocode}
2141 % \end{macro}
2142 % \end{macro}
2143 %
2144 % \begin{macro}[EXP]{\@@_*_tuple_o:ww, \@@_tuple_*_o:ww, \@@_tuple_/_o:ww}
2145 % For \meta{number}|*|\meta{tuple} and \meta{tuple}|*|\meta{number}
2146 % and \meta{tuple}|/|\meta{number}, loop through the \meta{tuple} some
2147 % code that multiplies or divides by the appropriate \meta{number}.
2148 % Importantly we need to dispatch according to the type, and we make
2149 % sure to apply the operator in the correct order.
2150 % \begin{macrocode}
2151 \cs_new:cpn { @@_*_tuple_o:ww } #1 \@@_sep:
2152 { \@@_tuple_map_o:nw { \@@_binary_type_o:Nww * #1 \@@_sep: } }
2153 \cs_new:cpn { @@_tuple_*_o:ww } #1 \@@_sep: #2 \@@_sep:
2154 {
2155 \@@_tuple_map_o:nw { \@@_binary_rev_type_o:Nww * #2 \@@_sep: }
2156 #1 \@@_sep:
2157 }
2158 \cs_new:cpn { @@_tuple_/_o:ww } #1 \@@_sep: #2 \@@_sep:
2159 {
2160 \@@_tuple_map_o:nw { \@@_binary_rev_type_o:Nww / #2 \@@_sep: }
2161 #1 \@@_sep:
2162 }
2163 % \end{macrocode}
2164 % \end{macro}
2165 %
2166 % \begin{macro}[EXP]{\@@_tuple_+_tuple_o:ww, \@@_tuple_-_tuple_o:ww}
2167 % Check the two tuples have the same number of items and map through
2168 % these a helper that dispatches appropriately depending on the types.
2169 % This means |(1,2)+((1,1),2)| gives |(nan,4)|.
2170 % \begin{macrocode}
2171 \cs_set_protected:Npn \@@_tmp:w #1
2172 {
2173 \cs_new:cpn { @@_tuple_#1_tuple_o:ww }
2174 \s_@@_tuple \@@_tuple_chk:w ##1 \@@_sep:
2175 \s_@@_tuple \@@_tuple_chk:w ##2 \@@_sep:
2176 {
2177 \int_compare:nNnTF
2178 { \@@_array_count:n {##1} } = { \@@_array_count:n {##2} }
2179 { \@@_tuple_mapthread_o:nww { \@@_binary_type_o:Nww #1 } }
2180 { \@@_invalid_operation_o:nww #1 }
2181 \s_@@_tuple \@@_tuple_chk:w {##1} \@@_sep:
2182 \s_@@_tuple \@@_tuple_chk:w {##2} \@@_sep:
2183 }
2184 }
2185 \@@_tmp:w +
2186 \@@_tmp:w -
2187 % \end{macrocode}
2188 % \end{macro}
2189 %
2190 % \begin{macrocode}
2191 %</code>
2192 % \end{macrocode}
2193 %
2194 % \end{implementation}
2195 %
2196 % \PrintChanges
2197 %
2198 % \PrintIndex