Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3fp-aux.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-aux} module\\ Support for floating points^^A
35 % }
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 %
46 % \date{Released 2026-05-15}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % \end{documentation}
53 %
54 % \begin{implementation}
55 %
56 % \section{\pkg{l3fp-aux} implementation}
57 %
58 % \begin{macrocode}
59 %<*code>
60 % \end{macrocode}
61 %
62 % \begin{macrocode}
63 %<@@=fp>
64 % \end{macrocode}
65 %
66 % ^^A todo: make sanitize and pack more homogeneous between modules.
67 %
68 % \subsection{Access to primitives}
69 %
70 % \begin{macro}{\@@_int_eval:w, \@@_int_eval_end:, \@@_int_to_roman:w}
71 % Largely for performance reasons, we need to directly access primitives
72 % rather than use \cs{int_eval:n}. This happens \emph{a lot}, so we
73 % use private names. The same is true for \tn{romannumeral}, although it
74 % is used much less widely.
75 % \begin{macrocode}
76 \cs_new_eq:NN \@@_int_eval:w \tex_numexpr:D
77 \cs_new_eq:NN \@@_int_eval_end: \scan_stop:
78 \cs_new_eq:NN \@@_int_to_roman:w \tex_romannumeral:D
79 % \end{macrocode}
80 % \end{macro}
81 %
82 % \begin{macro}{\@@_sep:}
83 % \begin{macrocode}
84 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
85 % \end{macrocode}
86 % \end{macro}
87 %
88 % \subsection{Internal representation}
89 %
90 % Internally, a floating point number \meta{X} is a
91 % token list containing
92 % \begin{quote}
93 % \cs{s_@@} \cs{@@_chk:w} \meta{case} \meta{sign} \meta{body} \cs{@@_sep:}
94 % \end{quote}
95 % Let us explain each piece separately.
96 %
97 % Internal floating point numbers are used in expressions,
98 % and in this context are subject to \texttt{f}-expansion. They must
99 % leave a recognizable mark after \texttt{f}-expansion, to prevent the
100 % floating point number from being re-parsed. Thus, \cs{s_@@}
101 % is simply another name for \tn{relax}.
102 %
103 % When used directly without an accessor function, floating points
104 % should produce an error: this is the role of \cs{@@_chk:w}. We could
105 % make floating point variables be protected to prevent them from
106 % expanding under \texttt{e}/\texttt{x}-expansion, but it seems more
107 % convenient to treat them as a subcase of token list variables.
108 %
109 % The (decimal part of the) IEEE-754-2008 standard requires the format
110 % to be able to represent special floating point numbers besides the
111 % usual positive and negative cases. We distinguish the various
112 % possibilities by their \meta{case}, which is a single digit:
113 % \begin{itemize}
114 % \item[0] zeros: |+0| and |-0|,
115 % \item[1] \enquote{normal} numbers (positive and negative),
116 % \item[2] infinities: |+inf| and |-inf|,
117 % \item[3] quiet and signaling \texttt{nan}.
118 % \end{itemize}
119 % The \meta{sign} is |0| (positive) or |2| (negative),
120 % except in the case of \texttt{nan}, which have $\meta{sign} = 1$.
121 % This ensures that changing the \meta{sign} digit to $2-\meta{sign}$
122 % is exactly equivalent to changing the sign of the number.
123 %
124 % Special floating point numbers have the form
125 % \begin{quote}
126 % \cs{s_@@} \cs{@@_chk:w} \meta{case} \meta{sign} \cs[no-index]{s_@@_\ldots} \cs{@@_sep:}
127 % \end{quote}
128 % where \cs[no-index]{s_@@_\ldots} is a scan mark carrying information about how
129 % the number was formed (useful for debugging).
130 %
131 % Normal floating point numbers ($\meta{case} = 1$) have the form
132 % \begin{quote}
133 % \cs{s_@@} \cs{@@_chk:w} 1 \meta{sign} \Arg{exponent}
134 % \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4} \cs{@@_sep:}
135 % \end{quote}
136 % Here, the \meta{exponent} is an integer, between
137 % $-\ExplSyntaxOn\int_use:N\c__fp_minus_min_exponent_int$ and
138 % $\ExplSyntaxOn\int_use:N\c__fp_max_exponent_int$. The body consists
139 % in four blocks of exactly $4$ digits,
140 % $0000 \leq \meta{X_i} \leq 9999$, and the floating point is
141 % \[
142 % (-1)^{\meta{sign}/2} \meta{X_1}\meta{X_2}\meta{X_3}\meta{X_4}\cdot 10^{\meta{exponent}-16}
143 % \]
144 % where we have concatenated the $16$ digits. Currently, floating point numbers
145 % are normalized such that the \meta{exponent} is minimal, in other words,
146 % $1000 \leq \meta{X_1} \leq 9999$.
147 %
148 % \begin{table}\centering
149 % \caption{Internal representation of floating point numbers.}
150 % \label{tab:fp-convert-special}
151 % \begin{tabular}{ll}
152 % \toprule
153 % \multicolumn{1}{c}{Representation} & Meaning \\
154 % \midrule
155 % 0 0 \cs[no-index]{s_@@_\ldots} \cs{@@_sep:} & Positive zero. \\
156 % 0 2 \cs[no-index]{s_@@_\ldots} \cs{@@_sep:} & Negative zero. \\
157 % 1 0 \Arg{exponent} \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4} \cs{@@_sep:}
158 % & Positive floating point. \\
159 % 1 2 \Arg{exponent} \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4} \cs{@@_sep:}
160 % & Negative floating point. \\
161 % 2 0 \cs[no-index]{s_@@_\ldots} \cs{@@_sep:} & Positive infinity. \\
162 % 2 2 \cs[no-index]{s_@@_\ldots} \cs{@@_sep:} & Negative infinity. \\
163 % 3 1 \cs[no-index]{s_@@_\ldots} \cs{@@_sep:} & Quiet \texttt{nan}. \\
164 % 3 1 \cs[no-index]{s_@@_\ldots} \cs{@@_sep:} & Signaling \texttt{nan}. \\
165 % \bottomrule
166 % \end{tabular}
167 % \end{table}
168 %
169 % Calculations are done in base $10000$, i.e. one myriad.
170 %
171 % \subsection{Using arguments and \cs{@@_sep:}s}
172 %
173 % \begin{macro}[EXP]{\@@_use_none_stop_f:n}
174 % This function removes an argument (typically a digit) and replaces
175 % it by \cs{exp_stop_f:}, a marker which stops \texttt{f}-type
176 % expansion.
177 % \begin{macrocode}
178 \cs_new:Npn \@@_use_none_stop_f:n #1 { \exp_stop_f: }
179 % \end{macrocode}
180 % \end{macro}
181 %
182 % \begin{macro}[EXP]{\@@_use_s:n, \@@_use_s:nn}
183 % Those functions place a \cs{@@_sep:} after one or two arguments
184 % (typically digits).
185 % \begin{macrocode}
186 \cs_new:Npn \@@_use_s:n #1 { #1\@@_sep: }
187 \cs_new:Npn \@@_use_s:nn #1#2 { #1#2\@@_sep: }
188 % \end{macrocode}
189 % \end{macro}
190 %
191 % \begin{macro}[EXP]
192 % {\@@_use_none_until_s:w, \@@_use_i_until_s:nw, \@@_use_ii_until_s:nnw}
193 % Those functions select specific arguments among a set of arguments
194 % delimited by a \cs{@@_sep:}.
195 % \begin{macrocode}
196 \cs_new:Npn \@@_use_none_until_s:w #1\@@_sep: { }
197 \cs_new:Npn \@@_use_i_until_s:nw #1#2\@@_sep: {#1}
198 \cs_new:Npn \@@_use_ii_until_s:nnw #1#2#3\@@_sep: {#2}
199 % \end{macrocode}
200 % \end{macro}
201 %
202 % \begin{macro}[EXP]{\@@_reverse_args:Nww}
203 % Many internal functions take arguments delimited by \cs{@@_sep:}s, and
204 % it is occasionally useful to swap two such arguments.
205 % \begin{macrocode}
206 \cs_new:Npn \@@_reverse_args:Nww #1 #2\@@_sep: #3\@@_sep:
207 { #1 #3\@@_sep: #2\@@_sep: }
208 % \end{macrocode}
209 % \end{macro}
210 %
211 % \begin{macro}[EXP]{\@@_rrot:www}
212 % Rotate three arguments delimited by \cs{@@_sep:}s. This is the inverse
213 % (or the square) of the Forth primitive |ROT|, hence the name.
214 % \begin{macrocode}
215 \cs_new:Npn \@@_rrot:www #1\@@_sep: #2\@@_sep: #3\@@_sep:
216 { #2\@@_sep: #3\@@_sep: #1\@@_sep: }
217 % \end{macrocode}
218 % \end{macro}
219 %
220 % \begin{macro}[EXP]{\@@_use_i:ww, \@@_use_i:www}
221 % Many internal functions take arguments delimited by \cs{@@_sep:}s, and
222 % it is occasionally useful to remove one or two such arguments.
223 % \begin{macrocode}
224 \cs_new:Npn \@@_use_i:ww #1\@@_sep: #2\@@_sep: { #1\@@_sep: }
225 \cs_new:Npn \@@_use_i:www #1\@@_sep: #2\@@_sep: #3\@@_sep: { #1\@@_sep: }
226 % \end{macrocode}
227 % \end{macro}
228 %
229 % \subsection{Constants, and structure of floating points}
230 %
231 % \begin{macro}{\@@_misused:n}
232 % This receives a floating point object (floating point number or
233 % tuple) and generates an error stating that it was misused. This is
234 % called when for instance an |fp| variable is left in the input
235 % stream and its contents reach \TeX{}'s stomach.
236 % \begin{macrocode}
237 \cs_new_protected:Npn \@@_misused:n #1
238 { \msg_error:nne { fp } { misused } { \fp_to_tl:n {#1} } }
239 % \end{macrocode}
240 % \end{macro}
241 %
242 % \begin{macro}{\s_@@, \@@_chk:w}
243 % Floating points numbers all start with \cs{s_@@} \cs{@@_chk:w},
244 % where \cs{s_@@} is equal to the \TeX{} primitive \tn{relax}, and
245 % \cs{@@_chk:w} is protected. The rest of the floating point number
246 % is made of characters (or \tn{relax}). This ensures that nothing
247 % expands under \texttt{f}-expansion, nor under
248 % \texttt{e}/\texttt{x}-expansion.
249 % However, when typeset, \cs{s_@@} does nothing, and \cs{@@_chk:w} is
250 % expanded. We define \cs{@@_chk:w} to produce an error.
251 % \begin{macrocode}
252 \scan_new:N \s_@@
253 \cs_new_protected:Npn \@@_chk:w #1 \@@_sep:
254 { \@@_misused:n { \s_@@ \@@_chk:w #1 \@@_sep: } }
255 % \end{macrocode}
256 % \end{macro}
257 %
258 % \begin{variable}{\s_@@_expr_mark, \s_@@_expr_stop}
259 % Aliases of \cs{tex_relax:D}, used to terminate expressions.
260 % \begin{macrocode}
261 \scan_new:N \s_@@_expr_mark
262 \scan_new:N \s_@@_expr_stop
263 % \end{macrocode}
264 % \end{variable}
265 %
266 % \begin{variable}{\s_@@_mark, \s_@@_stop}
267 % Generic scan marks used throughout the module.
268 % \begin{macrocode}
269 \scan_new:N \s_@@_mark
270 \scan_new:N \s_@@_stop
271 % \end{macrocode}
272 % \end{variable}
273 %
274 % \begin{macro}[EXP]{\@@_use_i_delimit_by_s_stop:nw}
275 % Functions to gobble up to a scan mark.
276 % \begin{macrocode}
277 \cs_new:Npn \@@_use_i_delimit_by_s_stop:nw #1 #2 \s_@@_stop {#1}
278 % \end{macrocode}
279 % \end{macro}
280 %
281 % \begin{macro}
282 % {
283 % \s_@@_invalid, \s_@@_underflow, \s_@@_overflow,
284 % \s_@@_division, \s_@@_exact
285 % }
286 % A couple of scan marks used to indicate where special floating point
287 % numbers come from.
288 % \begin{macrocode}
289 \scan_new:N \s_@@_invalid
290 \scan_new:N \s_@@_underflow
291 \scan_new:N \s_@@_overflow
292 \scan_new:N \s_@@_division
293 \scan_new:N \s_@@_exact
294 % \end{macrocode}
295 % \end{macro}
296 %
297 % \begin{variable}
298 % {\c_zero_fp, \c_minus_zero_fp, \c_inf_fp, \c_minus_inf_fp, \c_nan_fp}
299 % The special floating points. We define the floating points here as
300 % \enquote{exact}.
301 % \begin{macrocode}
302 \tl_const:Nn \c_zero_fp { \s_@@ \@@_chk:w 0 0 \s_@@_exact \@@_sep: }
303 \tl_const:Nn \c_minus_zero_fp { \s_@@ \@@_chk:w 0 2 \s_@@_exact \@@_sep: }
304 \tl_const:Nn \c_inf_fp { \s_@@ \@@_chk:w 2 0 \s_@@_exact \@@_sep: }
305 \tl_const:Nn \c_minus_inf_fp { \s_@@ \@@_chk:w 2 2 \s_@@_exact \@@_sep: }
306 \tl_const:Nn \c_nan_fp { \s_@@ \@@_chk:w 3 1 \s_@@_exact \@@_sep: }
307 % \end{macrocode}
308 % \end{variable}
309 %
310 % \begin{variable}{\c_@@_prec_int, \c_@@_half_prec_int, \c_@@_block_int}
311 % The number of digits of floating points.
312 % \begin{macrocode}
313 \int_const:Nn \c_@@_prec_int { 16 }
314 \int_const:Nn \c_@@_half_prec_int { 8 }
315 \int_const:Nn \c_@@_block_int { 4 }
316 % \end{macrocode}
317 % \end{variable}
318 %
319 % \begin{variable}{\c_@@_myriad_int}
320 % Blocks have $4$~digits so this integer is useful.
321 % \begin{macrocode}
322 \int_const:Nn \c_@@_myriad_int { 10000 }
323 % \end{macrocode}
324 % \end{variable}
325 %
326 % \begin{variable}{\c_@@_minus_min_exponent_int, \c_@@_max_exponent_int}
327 % Normal floating point numbers have an exponent between $-$
328 % \texttt{minus_min_exponent} and \texttt{max_exponent} inclusive.
329 % Larger numbers are rounded to $\pm\infty$. Smaller numbers are
330 % rounded to $\pm 0$. It would be more natural to define a
331 % \texttt{min_exponent} with the opposite sign but that would waste
332 % one \TeX{} count.
333 % \begin{macrocode}
334 \int_const:Nn \c_@@_minus_min_exponent_int { 10000 }
335 \int_const:Nn \c_@@_max_exponent_int { 10000 }
336 % \end{macrocode}
337 % \end{variable}
338 %
339 % \begin{variable}{\c_@@_max_exp_exponent_int}
340 % If a number's exponent is larger than that, its exponential
341 % overflows/underflows.
342 % \begin{macrocode}
343 \int_const:Nn \c_@@_max_exp_exponent_int { 5 }
344 % \end{macrocode}
345 % \end{variable}
346 %
347 % \begin{variable}{\c_@@_overflowing_fp}
348 % A floating point number that is bigger than all normal floating
349 % point numbers. This replaces infinities when converting to formats
350 % that do not support infinities.
351 % \begin{macrocode}
352 \tl_const:Ne \c_@@_overflowing_fp
353 {
354 \s_@@ \@@_chk:w 1 0
355 { \int_eval:n { \c_@@_max_exponent_int + 1 } }
356 {1000} {0000} {0000} {0000} \@@_sep:
357 }
358 % \end{macrocode}
359 % \end{variable}
360 %
361 % \begin{macro}[EXP]{\@@_zero_fp:N, \@@_inf_fp:N}
362 % In case of overflow or underflow, we have to output
363 % a zero or infinity with a given sign.
364 % \begin{macrocode}
365 \cs_new:Npn \@@_zero_fp:N #1
366 { \s_@@ \@@_chk:w 0 #1 \s_@@_underflow \@@_sep: }
367 \cs_new:Npn \@@_inf_fp:N #1
368 { \s_@@ \@@_chk:w 2 #1 \s_@@_overflow \@@_sep: }
369 % \end{macrocode}
370 % \end{macro}
371 %
372 % \begin{macro}[EXP]{\@@_exponent:w}
373 % For normal numbers, the function expands to the exponent, otherwise
374 % to $0$. This is used in \pkg{l3str-format}.
375 % \begin{macrocode}
376 \cs_new:Npn \@@_exponent:w \s_@@ \@@_chk:w #1
377 {
378 \if_meaning:w 1 #1
379 \exp_after:wN \@@_use_ii_until_s:nnw
380 \else:
381 \exp_after:wN \@@_use_i_until_s:nw
382 \exp_after:wN 0
383 \fi:
384 }
385 % \end{macrocode}
386 % \end{macro}
387 %
388 % \begin{macro}[EXP]{\@@_neg_sign:N}
389 % When appearing in an integer expression or after \cs{int_value:w},
390 % this expands to the sign opposite to |#1|, namely $0$ (positive) is
391 % turned to $2$ (negative), $1$ (\texttt{nan}) to $1$, and $2$ to $0$.
392 % \begin{macrocode}
393 \cs_new:Npn \@@_neg_sign:N #1
394 { \@@_int_eval:w 2 - #1 \@@_int_eval_end: }
395 % \end{macrocode}
396 % \end{macro}
397 %
398 % \begin{macro}[EXP]{\@@_kind:w}
399 % Expands to $0$ for zeros, $1$ for normal floating point numbers, $2$
400 % for infinities, $3$ for \nan{}, $4$ for tuples.
401 % \begin{macrocode}
402 \cs_new:Npn \@@_kind:w #1
403 {
404 \@@_if_type_fp:NTwFw
405 #1 \@@_use_ii_until_s:nnw
406 \s_@@ { \@@_use_i_until_s:nw 4 }
407 \s_@@_stop
408 }
409 % \end{macrocode}
410 % \end{macro}
411 %
412 % \subsection{Overflow, underflow, and exact zero}
413 %
414 %^^A todo: the sign of exact zeros should depend on the rounding mode.
415 %
416 % \begin{macro}[EXP]{\@@_sanitize:Nw, \@@_sanitize:wN}
417 % \begin{macro}[EXP]{\@@_sanitize_zero:w}
418 % Expects the sign and the exponent in some order, then the
419 % significand (which we don't touch). Outputs the corresponding
420 % floating point number, possibly underflowed to $\pm 0$ or overflowed
421 % to $\pm\infty$. The functions \cs{@@_underflow:w} and
422 % \cs{@@_overflow:w} are defined in \pkg{l3fp-traps}.
423 % \begin{macrocode}
424 \cs_new:Npn \@@_sanitize:Nw #1 #2\@@_sep:
425 {
426 \if_case:w
427 \if_int_compare:w #2 > \c_@@_max_exponent_int 1 ~ \else:
428 \if_int_compare:w #2 < - \c_@@_minus_min_exponent_int 2 ~ \else:
429 \if_meaning:w 1 #1 3 ~ \fi: \fi: \fi: 0 ~
430 \or: \exp_after:wN \@@_overflow:w
431 \or: \exp_after:wN \@@_underflow:w
432 \or: \exp_after:wN \@@_sanitize_zero:w
433 \fi:
434 \s_@@ \@@_chk:w 1 #1 {#2}
435 }
436 \cs_new:Npn \@@_sanitize:wN #1\@@_sep: #2 { \@@_sanitize:Nw #2 #1\@@_sep: }
437 \cs_new:Npn \@@_sanitize_zero:w \s_@@ \@@_chk:w #1 #2 #3\@@_sep:
438 { \c_zero_fp }
439 % \end{macrocode}
440 % \end{macro}
441 % \end{macro}
442 %
443 % \subsection{Expanding after a floating point number}
444 %
445 % \begin{macro}[EXP]{\@@_exp_after_o:w}
446 % \begin{macro}[EXP]{\@@_exp_after_f:nw}
447 % \begin{syntax}
448 % \cs{@@_exp_after_o:w} \meta{floating point}
449 % \cs{@@_exp_after_f:nw} \Arg{tokens} \meta{floating point}
450 % \end{syntax}
451 % Places \meta{tokens} (empty in the case of \cs{@@_exp_after_o:w})
452 % between the \meta{floating point} and the following tokens, then
453 % hits those tokens with \texttt{o} or \texttt{f}-expansion, and
454 % leaves the floating point number unchanged.
455 %
456 % We first distinguish normal floating points, which have a significand,
457 % from the much simpler special floating points.
458 % \begin{macrocode}
459 \cs_new:Npn \@@_exp_after_o:w \s_@@ \@@_chk:w #1
460 {
461 \if_meaning:w 1 #1
462 \exp_after:wN \@@_exp_after_normal:nNNw
463 \else:
464 \exp_after:wN \@@_exp_after_special:nNNw
465 \fi:
466 { }
467 #1
468 }
469 \cs_new:Npn \@@_exp_after_f:nw #1 \s_@@ \@@_chk:w #2
470 {
471 \if_meaning:w 1 #2
472 \exp_after:wN \@@_exp_after_normal:nNNw
473 \else:
474 \exp_after:wN \@@_exp_after_special:nNNw
475 \fi:
476 { \exp:w \exp_end_continue_f:w #1 }
477 #2
478 }
479 % \end{macrocode}
480 % \end{macro}
481 % \end{macro}
482 %
483 % \begin{macro}[EXP]{\@@_exp_after_special:nNNw}
484 % \begin{syntax}
485 % \cs{@@_exp_after_special:nNNw} \Arg{after} \meta{case} \meta{sign} \meta{scan mark} \cs{@@_sep:}
486 % \end{syntax}
487 % Special floating point numbers are easy to jump over since they
488 % contain few tokens.
489 % \begin{macrocode}
490 \cs_new:Npn \@@_exp_after_special:nNNw #1#2#3#4\@@_sep:
491 {
492 \exp_after:wN \s_@@
493 \exp_after:wN \@@_chk:w
494 \exp_after:wN #2
495 \exp_after:wN #3
496 \exp_after:wN #4
497 \exp_after:wN \@@_sep:
498 #1
499 }
500 % \end{macrocode}
501 % \end{macro}
502 %
503 % \begin{macro}[EXP]{\@@_exp_after_normal:nNNw}
504 % For normal floating point numbers, life is slightly harder, since we
505 % have many tokens to jump over. Here it would be slightly better if
506 % the digits were not braced but instead were delimited arguments (for
507 % instance delimited by |,|). That may be changed some day.
508 % \begin{macrocode}
509 \cs_new:Npn \@@_exp_after_normal:nNNw #1 1 #2 #3 #4#5#6#7\@@_sep:
510 {
511 \exp_after:wN \@@_exp_after_normal:Nwwwww
512 \exp_after:wN #2
513 \int_value:w #3 \exp_after:wN \@@_sep:
514 \int_value:w 1 #4 \exp_after:wN \@@_sep:
515 \int_value:w 1 #5 \exp_after:wN \@@_sep:
516 \int_value:w 1 #6 \exp_after:wN \@@_sep:
517 \int_value:w 1 #7 \exp_after:wN \@@_sep: #1
518 }
519 \cs_new:Npn \@@_exp_after_normal:Nwwwww
520 #1 #2\@@_sep: 1 #3 \@@_sep: 1 #4 \@@_sep: 1 #5 \@@_sep: 1 #6 \@@_sep:
521 { \s_@@ \@@_chk:w 1 #1 {#2} {#3} {#4} {#5} {#6} \@@_sep: }
522 % \end{macrocode}
523 % \end{macro}
524 %
525 % \subsection{Other floating point types}
526 %
527 % \begin{macro}{\s_@@_tuple, \@@_tuple_chk:w}
528 % \begin{variable}{\c_@@_empty_tuple_fp}
529 % Floating point tuples take the form \cs{s_@@_tuple}
530 % \cs{@@_tuple_chk:w} |{| \meta{fp 1} \meta{fp 2} \dots |}| \cs{@@_sep:}
531 % where each \meta{fp} is a floating point number or tuple, hence ends with
532 % \cs{@@_sep:} itself. When a tuple is typeset, \cs{@@_tuple_chk:w} produces
533 % an error, just like usual floating point numbers.
534 % Tuples may have zero or one element.
535 % \begin{macrocode}
536 \scan_new:N \s_@@_tuple
537 \cs_new_protected:Npn \@@_tuple_chk:w #1 \@@_sep:
538 { \@@_misused:n { \s_@@_tuple \@@_tuple_chk:w #1 \@@_sep: } }
539 \tl_const:Nn \c_@@_empty_tuple_fp
540 { \s_@@_tuple \@@_tuple_chk:w { } \@@_sep: }
541 % \end{macrocode}
542 % \end{variable}
543 % \end{macro}
544 %
545 % \begin{macro}[EXP]{\@@_tuple_count:w, \@@_array_count:n}
546 % \begin{macro}[EXP]{\@@_tuple_count_loop:Nw}
547 % Count the number of items in a tuple of floating points by counting
548 % \cs{@@_sep:}s. The technique is very similar to \cs{tl_count:n}, but
549 % with the loop built-in. Checking for the end of the loop is done
550 % with the |\use_none:n #1| construction.
551 % \begin{macrocode}
552 \cs_new:Npn \@@_array_count:n #1
553 { \@@_tuple_count:w \s_@@_tuple \@@_tuple_chk:w {#1} \@@_sep: }
554 \cs_new:Npn \@@_tuple_count:w \s_@@_tuple \@@_tuple_chk:w #1 \@@_sep:
555 {
556 \int_value:w \@@_int_eval:w 0
557 \@@_tuple_count_loop:Nw #1 { ? \prg_break: } \@@_sep:
558 \prg_break_point:
559 \@@_int_eval_end:
560 }
561 \cs_new:Npn \@@_tuple_count_loop:Nw #1#2\@@_sep:
562 { \use_none:n #1 + 1 \@@_tuple_count_loop:Nw }
563 % \end{macrocode}
564 % \end{macro}
565 % \end{macro}
566 %
567 % \begin{macro}[EXP]{\@@_if_type_fp:NTwFw}
568 % Used as \cs{@@_if_type_fp:NTwFw} \meta{marker} \Arg{true code}
569 % \cs{s_@@} \Arg{false code} \cs{s_@@_stop}, this test whether the
570 % \meta{marker} is \cs{s_@@} or not and runs the appropriate
571 % \meta{code}. The very unusual syntax is for optimization purposes
572 % as that function is used for all floating point operations.
573 % \begin{macrocode}
574 \cs_new:Npn \@@_if_type_fp:NTwFw #1 \s_@@ #2 #3 \s_@@_stop {#2}
575 % \end{macrocode}
576 % \end{macro}
577 %
578 % \begin{macro}[EXP]{\@@_array_if_all_fp:nTF, \@@_array_if_all_fp_loop:w}
579 % True if all items are floating point numbers. Used for |min|.
580 % \begin{macrocode}
581 \cs_new:Npn \@@_array_if_all_fp:nTF #1
582 {
583 \@@_array_if_all_fp_loop:w #1 { \s_@@ \prg_break: } \@@_sep:
584 \prg_break_point: \use_i:nn
585 }
586 \cs_new:Npn \@@_array_if_all_fp_loop:w #1#2 \@@_sep:
587 {
588 \@@_if_type_fp:NTwFw
589 #1 \@@_array_if_all_fp_loop:w
590 \s_@@ { \prg_break:n \use_iii:nnn }
591 \s_@@_stop
592 }
593 % \end{macrocode}
594 % \end{macro}
595 %
596 % \begin{macro}[EXP]
597 % {\@@_type_from_scan:N, \@@_type_from_scan_other:N, \@@_type_from_scan:w}
598 % Used as \cs{@@_type_from_scan:N} \meta{token}.
599 % Grabs the pieces of the stringified \meta{token} which lies after
600 % the first |s__fp|. If the \meta{token} does not contain that
601 % string, the result is |_?|.
602 % \begin{macrocode}
603 \cs_new:Npn \@@_type_from_scan:N #1
604 {
605 \@@_if_type_fp:NTwFw
606 #1 { }
607 \s_@@ { \@@_type_from_scan_other:N #1 }
608 \s_@@_stop
609 }
610 \cs_new:Npe \@@_type_from_scan_other:N #1
611 {
612 \exp_not:N \exp_after:wN \exp_not:N \@@_type_from_scan:w
613 \exp_not:N \token_to_str:N #1 \s_@@_mark
614 \tl_to_str:n { s_@@ _? } \s_@@_mark \s_@@_stop
615 }
616 \exp_last_unbraced:NNNNo
617 \cs_new:Npn \@@_type_from_scan:w #1
618 { \tl_to_str:n { s_@@ } } #2 \s_@@_mark #3 \s_@@_stop {#2}
619 % \end{macrocode}
620 % \end{macro}
621 %
622 % \begin{macro}[EXP]{\@@_change_func_type:NNN}
623 % \begin{macro}[EXP]{\@@_change_func_type_aux:w, \@@_change_func_type_chk:NNN}
624 % Arguments are \meta{type marker} \meta{function} \meta{recovery}.
625 % This gives the function obtained by placing the type after |@@|. If
626 % the function is not defined then \meta{recovery} \meta{function} is
627 % used instead; however that test is not run when the \meta{type
628 % marker} is \cs{s_@@}.
629 % \begin{macrocode}
630 \cs_new:Npn \@@_change_func_type:NNN #1#2#3
631 {
632 \@@_if_type_fp:NTwFw
633 #1 #2
634 \s_@@
635 {
636 \exp_after:wN \@@_change_func_type_chk:NNN
637 \cs:w
638 @@ \@@_type_from_scan_other:N #1
639 \exp_after:wN \@@_change_func_type_aux:w \token_to_str:N #2
640 \cs_end:
641 #2 #3
642 }
643 \s_@@_stop
644 }
645 \exp_last_unbraced:NNNNo
646 \cs_new:Npn \@@_change_func_type_aux:w #1 { \tl_to_str:n { @@ } } { }
647 \cs_new:Npn \@@_change_func_type_chk:NNN #1#2#3
648 {
649 \if_meaning:w \scan_stop: #1
650 \exp_after:wN #3 \exp_after:wN #2
651 \else:
652 \exp_after:wN #1
653 \fi:
654 }
655 % \end{macrocode}
656 % \end{macro}
657 % \end{macro}
658 %
659 % \begin{macro}[EXP]{\@@_exp_after_any_f:Nnw, \@@_exp_after_any_f:nw}
660 % \begin{macro}[EXP]{\@@_exp_after_expr_stop_f:nw}
661 % The |Nnw| function simply dispatches to the appropriate
662 % \cs[no-index]{@@_exp_after\ldots{}_f:nw} with \enquote{\ldots{}}
663 % (either empty or |_|\meta{type}) extracted from |#1|, which should
664 % start with |\s__fp|. If it doesn't start with |\s__fp| the function
665 % \cs{@@_exp_after_?_f:nw} defined in \pkg{l3fp-parse} gives an error;
666 % another special \meta{type} is |stop|, useful for loops, see below.
667 % The |nw| function has an important optimization for floating points
668 % numbers; it also fetches its type marker |#2| from the floating
669 % point.
670 % \begin{macrocode}
671 \cs_new:Npn \@@_exp_after_any_f:Nnw #1
672 { \cs:w @@_exp_after \@@_type_from_scan_other:N #1 _f:nw \cs_end: }
673 \cs_new:Npn \@@_exp_after_any_f:nw #1#2
674 {
675 \@@_if_type_fp:NTwFw
676 #2 \@@_exp_after_f:nw
677 \s_@@ { \@@_exp_after_any_f:Nnw #2 }
678 \s_@@_stop
679 {#1} #2
680 }
681 \cs_new_eq:NN \@@_exp_after_expr_stop_f:nw \use_none:nn
682 % \end{macrocode}
683 % \end{macro}
684 % \end{macro}
685 %
686 % \begin{macro}[EXP]{\@@_exp_after_tuple_o:w}
687 % \begin{macro}[EXP]{\@@_exp_after_tuple_f:nw, \@@_exp_after_array_f:w}
688 % The loop works by using the |n| argument of
689 % \cs{@@_exp_after_any_f:nw} to place the loop macro after the next
690 % item in the tuple and expand it.
691 % \begin{quote}
692 % \cs{@@_exp_after_array_f:w}\\
693 % \meta{fp_1} \cs{@@_sep:}\\
694 % \ldots{}\\
695 % \meta{fp_n} \cs{@@_sep:}\\
696 % \cs{s_@@_expr_stop}
697 % \end{quote}
698 % \begin{macrocode}
699 \cs_new:Npn \@@_exp_after_tuple_o:w
700 { \@@_exp_after_tuple_f:nw { \exp_after:wN \exp_stop_f: } }
701 \cs_new:Npn \@@_exp_after_tuple_f:nw
702 #1 \s_@@_tuple \@@_tuple_chk:w #2 \@@_sep:
703 {
704 \exp_after:wN \s_@@_tuple
705 \exp_after:wN \@@_tuple_chk:w
706 \exp_after:wN {
707 \exp:w \exp_end_continue_f:w
708 \@@_exp_after_array_f:w #2 \s_@@_expr_stop
709 \exp_after:wN }
710 \exp_after:wN \@@_sep:
711 \exp:w \exp_end_continue_f:w #1
712 }
713 \cs_new:Npn \@@_exp_after_array_f:w
714 { \@@_exp_after_any_f:nw { \@@_exp_after_array_f:w } }
715 % \end{macrocode}
716 % \end{macro}
717 % \end{macro}
718 %
719 % \subsection{Packing digits}
720 %
721 % When a positive integer |#1| is known to be less than $10^8$, the
722 % following trick splits it into two blocks of $4$ digits, padding
723 % with zeros on the left.
724 % \begin{verbatim}
725 % \cs_new:Npn \__fp_pack:NNNNNw #1 #2#3#4#5 #6 \__fp_sep: { {#2#3#4#5} {#6} }
726 % \exp_after:wN \__fp_pack:NNNNNw
727 % \__fp_int_value:w \__fp_int_eval:w 1 0000 0000 + #1 \__fp_sep:
728 % \end{verbatim}
729 % The idea is that adding $10^8$ to the number ensures that it has
730 % exactly $9$ digits, and can then easily find which digits correspond
731 % to what position in the number. Of course, this can be modified
732 % for any number of digits less or equal to~$9$ (we are limited by
733 % \TeX{}'s integers). This method is very heavily relied upon in
734 % \texttt{l3fp-basics}.
735 %
736 % More specifically, the auxiliary inserts |+ #1#2#3#4#5| \cs{@@_sep:} |{#6}|, which
737 % allows us to compute several blocks of $4$ digits in a nested manner,
738 % performing carries on the fly. Say we want to compute $1\,2345 \times
739 % 6677\,8899$. With simplified names, we would do
740 % \begin{verbatim}
741 % \exp_after:wN \post_processing:w
742 % \__fp_int_value:w \__fp_int_eval:w - 5 0000
743 % \exp_after:wN \__fp_pack:NNNNNw
744 % \__fp_int_value:w \__fp_int_eval:w 4 9995 0000
745 % + 12345 * 6677
746 % \exp_after:wN \__fp_pack:NNNNNw
747 % \__fp_int_value:w \__fp_int_eval:w 5 0000 0000
748 % + 12345 * 8899 \__fp_sep:
749 % \end{verbatim}
750 % The \cs{exp_after:wN} triggers \cs{int_value:w} \cs{@@_int_eval:w}, which
751 % starts a first computation, whose initial value is $- 5\,0000$ (the
752 % \enquote{leading shift}). In that computation appears an
753 % \cs{exp_after:wN}, which triggers the nested computation
754 % \cs{int_value:w} \cs{@@_int_eval:w} with starting value $4\,9995\,0000$ (the
755 % \enquote{middle shift}). That, in turn, expands \cs{exp_after:wN}
756 % which triggers the third computation. The third computation's value
757 % is $5\,0000\,0000 + 12345 \times 8899$, which has $9$ digits. Adding
758 % $5\cdot 10^{8}$ to the product allowed us to know how many digits to
759 % expect as long as the numbers to multiply are not too big; it
760 % also works to some extent with negative results. The \texttt{pack}
761 % function puts the last $4$ of those $9$ digits into a brace group,
762 % moves the \cs{@@_sep:} delimiter, and inserts a |+|, which combines the
763 % carry with the previous computation. The shifts nicely combine into
764 % $5\,0000\,0000 / 10^{4} + 4\,9995\,0000 = 5\,0000\,0000$. As long as
765 % the operands are in some range, the result of this second computation
766 % has $9$ digits. The corresponding \texttt{pack} function,
767 % expanded after the result is computed, braces the last $4$ digits, and
768 % leaves |+| \meta{5 digits} for the initial computation. The
769 % \enquote{leading shift} cancels the combination of the other shifts,
770 % and the |\post_processing:w| takes care of packing the last few
771 % digits.
772 %
773 % Admittedly, this is quite intricate. It is probably the key in making
774 % \pkg{l3fp} as fast as other pure \TeX{} floating point units despite
775 % its increased precision. In fact, this is used so much that we
776 % provide different sets of packing functions and shifts, depending on
777 % ranges of input.
778 %
779 % \begin{macro}[EXP]{\@@_pack:NNNNNw}
780 % \begin{variable}
781 % {
782 % \c_@@_trailing_shift_int ,
783 % \c_@@_middle_shift_int ,
784 % \c_@@_leading_shift_int ,
785 % }
786 % This set of shifts allows for computations involving results in the
787 % range $[-4\cdot 10^{8}, 5\cdot 10^{8}-1]$. Shifted values all have
788 % exactly $9$ digits.
789 % \begin{macrocode}
790 \int_const:Nn \c_@@_leading_shift_int { - 5 0000 }
791 \int_const:Nn \c_@@_middle_shift_int { 5 0000 * 9999 }
792 \int_const:Nn \c_@@_trailing_shift_int { 5 0000 * 10000 }
793 \cs_new:Npn \@@_pack:NNNNNw #1 #2#3#4#5 #6\@@_sep:
794 { + #1#2#3#4#5 \@@_sep: {#6} }
795 % \end{macrocode}
796 % \end{variable}
797 % \end{macro}
798 %
799 % \begin{macro}[EXP]{\@@_pack_big:NNNNNNw}
800 % \begin{variable}
801 % {
802 % \c_@@_big_trailing_shift_int ,
803 % \c_@@_big_middle_shift_int ,
804 % \c_@@_big_leading_shift_int ,
805 % }
806 % This set of shifts allows for computations involving results in the
807 % range $[-5\cdot 10^{8}, 6\cdot 10^{8}-1]$ (actually a bit more).
808 % Shifted values all have exactly $10$ digits. Note that the upper
809 % bound is due to \TeX{}'s limit of $2^{31}-1$ on integers. The
810 % shifts are chosen to be roughly the mid-point of $10^{9}$ and
811 % $2^{31}$, the two bounds on $10$-digit integers in \TeX{}.
812 % \begin{macrocode}
813 \int_const:Nn \c_@@_big_leading_shift_int { - 15 2374 }
814 \int_const:Nn \c_@@_big_middle_shift_int { 15 2374 * 9999 }
815 \int_const:Nn \c_@@_big_trailing_shift_int { 15 2374 * 10000 }
816 \cs_new:Npn \@@_pack_big:NNNNNNw #1#2 #3#4#5#6 #7\@@_sep:
817 { + #1#2#3#4#5#6 \@@_sep: {#7} }
818 % \end{macrocode}
819 % \end{variable}
820 % \end{macro}
821 %
822 % ^^A \@@_pack_Bigg:NNNNNNw = \@@_pack_big:NNNNNNw ?
823 % \begin{macro}[EXP]{\@@_pack_Bigg:NNNNNNw}
824 % \begin{variable}
825 % {
826 % \c_@@_Bigg_trailing_shift_int ,
827 % \c_@@_Bigg_middle_shift_int ,
828 % \c_@@_Bigg_leading_shift_int ,
829 % }
830 % This set of shifts allows for computations with results in the
831 % range $[-1\cdot 10^{9}, 147483647]$; the end-point is $2^{31} - 1 -
832 % 2\cdot 10^{9} \simeq 1.47\cdot 10^{8}$. Shifted values all have
833 % exactly $10$ digits.
834 % \begin{macrocode}
835 \int_const:Nn \c_@@_Bigg_leading_shift_int { - 20 0000 }
836 \int_const:Nn \c_@@_Bigg_middle_shift_int { 20 0000 * 9999 }
837 \int_const:Nn \c_@@_Bigg_trailing_shift_int { 20 0000 * 10000 }
838 \cs_new:Npn \@@_pack_Bigg:NNNNNNw #1#2 #3#4#5#6 #7\@@_sep:
839 { + #1#2#3#4#5#6 \@@_sep: {#7} }
840 % \end{macrocode}
841 % \end{variable}
842 % \end{macro}
843 %
844 % \begin{macro}[EXP]{\@@_pack_twice_four:wNNNNNNNN}
845 % \begin{syntax}
846 % \cs{@@_pack_twice_four:wNNNNNNNN} \meta{tokens} \cs{@@_sep:} \meta{$\geq 8$ digits}
847 % \end{syntax}
848 % Grabs two sets of $4$ digits and places them before the \cs{@@_sep:}
849 % delimiter. Putting several copies of this function before a
850 % \cs{@@_sep:} packs more digits since each takes the digits
851 % packed by the others in its first argument.
852 % \begin{macrocode}
853 \cs_new:Npn \@@_pack_twice_four:wNNNNNNNN #1\@@_sep: #2#3#4#5 #6#7#8#9
854 { #1 {#2#3#4#5} {#6#7#8#9} \@@_sep: }
855 % \end{macrocode}
856 % \end{macro}
857 %
858 % \begin{macro}[EXP]{\@@_pack_eight:wNNNNNNNN}
859 % \begin{syntax}
860 % \cs{@@_pack_eight:wNNNNNNNN} \meta{tokens} \cs{@@_sep:} \meta{$\geq 8$ digits}
861 % \end{syntax}
862 % Grabs one set of $8$ digits and places them before the \cs{@@_sep:}
863 % delimiter as a single group. Putting several copies of this
864 % function before a \cs{@@_sep:} packs more digits since each
865 % takes the digits packed by the others in its first argument.
866 % \begin{macrocode}
867 \cs_new:Npn \@@_pack_eight:wNNNNNNNN #1\@@_sep: #2#3#4#5 #6#7#8#9
868 { #1 {#2#3#4#5#6#7#8#9} \@@_sep: }
869 % \end{macrocode}
870 % \end{macro}
871 %
872 % \begin{macro}[EXP]
873 % {
874 % \@@_basics_pack_low:NNNNNw,
875 % \@@_basics_pack_high:NNNNNw,
876 % \@@_basics_pack_high_carry:w
877 % }
878 % Addition and multiplication of significands are done in two steps:
879 % first compute a (more or less) exact result, then round and pack
880 % digits in the final (braced) form. These functions take care of the
881 % packing, with special attention given to the case where rounding has
882 % caused a carry. Since rounding can only shift the final digit by
883 % $1$, a carry always produces an exact power of $10$. Thus,
884 % \cs{@@_basics_pack_high_carry:w} is always followed by four times
885 % |{0000}|.
886 %
887 % This is used in \pkg{l3fp-basics} and \pkg{l3fp-extended}.
888 % \begin{macrocode}
889 \cs_new:Npn \@@_basics_pack_low:NNNNNw #1 #2#3#4#5 #6\@@_sep:
890 { + #1 - 1 \@@_sep: {#2#3#4#5} {#6} \@@_sep: }
891 \cs_new:Npn \@@_basics_pack_high:NNNNNw #1 #2#3#4#5 #6\@@_sep:
892 {
893 \if_meaning:w 2 #1
894 \@@_basics_pack_high_carry:w
895 \fi:
896 \@@_sep: {#2#3#4#5} {#6}
897 }
898 \cs_new:Npn \@@_basics_pack_high_carry:w \fi: \@@_sep: #1
899 { \fi: + 1 \@@_sep: {1000} }
900 % \end{macrocode}
901 % \end{macro}
902 %
903 % \begin{macro}[EXP]
904 % {
905 % \@@_basics_pack_weird_low:NNNNw,
906 % \@@_basics_pack_weird_high:NNNNNNNNw
907 % }
908 % This is used in \pkg{l3fp-basics} for additions and
909 % divisions. Their syntax is confusing, hence the name.
910 % \begin{macrocode}
911 \cs_new:Npn \@@_basics_pack_weird_low:NNNNw #1 #2#3#4 #5\@@_sep:
912 {
913 \if_meaning:w 2 #1
914 + 1
915 \fi:
916 \@@_int_eval_end:
917 #2#3#4\@@_sep: {#5} \@@_sep:
918 }
919 \cs_new:Npn \@@_basics_pack_weird_high:NNNNNNNNw
920 1 #1#2#3#4 #5#6#7#8 #9\@@_sep: { \@@_sep: {#1#2#3#4} {#5#6#7#8} {#9} }
921 % \end{macrocode}
922 % \end{macro}
923 %
924 % \subsection{Decimate (dividing by a power of 10)}
925 %
926 % ^^A begin[todo]
927 % \begin{macro}[EXP]{\@@_decimate:nNnnnn}
928 % \begin{syntax}
929 % \cs{@@_decimate:nNnnnn} \Arg{shift} \meta{f_1}
930 % ~~\Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4}
931 % \end{syntax}
932 % Each \meta{X_i} consists in $4$ digits exactly,
933 % and $1000\leq\meta{X_1}<9999$. The first argument determines
934 % by how much we shift the digits. \meta{f_1} is called as follows:
935 % \begin{syntax}
936 % \meta{f_1} \meta{rounding} \Arg{X'_1} \Arg{X'_2} \meta{extra-digits} \cs{@@_sep:}
937 % \end{syntax}
938 % where $0\leq\meta{X'_i}<10^{8}-1$ are $8$ digit integers,
939 % forming the truncation of our number. In other words,
940 % \[
941 % \left(
942 % \sum_{i=1}^{4} \meta{X_i} \cdot 10^{-4i} \cdot 10^{-\meta{shift}}
943 % \right)
944 % - \bigl( \meta{X'_1} \cdot 10^{-8} + \meta{X'_2} \cdot 10^{-16} \bigr)
945 % = 0.\meta{extra-digits} \cdot 10^{-16}
946 % \in [0,10^{-16}).
947 % \]
948 % To round properly later, we need to remember some information
949 % about the difference. The \meta{rounding} digit is $0$ if and
950 % only if the difference is exactly $0$, and $5$ if and only if
951 % the difference is exactly $0.5\cdot 10^{-16}$. Otherwise, it
952 % is the (non-$0$, non-$5$) digit closest to $10^{17}$ times the
953 % difference. In particular, if the shift is $17$ or more, all
954 % the digits are dropped, \meta{rounding} is $1$ (not $0$), and
955 % \meta{X'_1} and \meta{X'_2} are both zero.
956 %
957 % If the shift is $1$, the \meta{rounding} digit is simply the
958 % only digit that was pushed out of the brace groups (this is
959 % important for subtraction). It would be more natural for the
960 % \meta{rounding} digit to be placed after the \meta{X'_i},
961 % but the choice we make involves less reshuffling.
962 %
963 % Note that this function treats negative \meta{shift} as $0$.
964 % \begin{macrocode}
965 \cs_new:Npn \@@_decimate:nNnnnn #1
966 {
967 \cs:w
968 @@_decimate_
969 \if_int_compare:w \@@_int_eval:w #1 > \c_@@_prec_int
970 tiny
971 \else:
972 \@@_int_to_roman:w \@@_int_eval:w #1
973 \fi:
974 :Nnnnn
975 \cs_end:
976 }
977 % \end{macrocode}
978 % Each of the auxiliaries see the function \meta{f_1},
979 % followed by $4$ blocks of $4$ digits.
980 % \end{macro}
981 %
982 % \begin{macro}[EXP]{\@@_decimate_:Nnnnn, \@@_decimate_tiny:Nnnnn}
983 % If the \meta{shift} is zero, or too big, life is very easy.
984 % \begin{macrocode}
985 \cs_new:Npn \@@_decimate_:Nnnnn #1 #2#3#4#5
986 { #1 0 {#2#3} {#4#5} \@@_sep: }
987 \cs_new:Npn \@@_decimate_tiny:Nnnnn #1 #2#3#4#5
988 { #1 1 { 0000 0000 } { 0000 0000 } 0 #2#3#4#5 \@@_sep: }
989 % \end{macrocode}
990 % \end{macro}
991 %
992 % \begin{macro}[EXP]
993 % {
994 % \@@_decimate_auxi:Nnnnn, \@@_decimate_auxii:Nnnnn,
995 % \@@_decimate_auxiii:Nnnnn, \@@_decimate_auxiv:Nnnnn,
996 % \@@_decimate_auxv:Nnnnn, \@@_decimate_auxvi:Nnnnn,
997 % \@@_decimate_auxvii:Nnnnn, \@@_decimate_auxviii:Nnnnn,
998 % \@@_decimate_auxix:Nnnnn, \@@_decimate_auxx:Nnnnn,
999 % \@@_decimate_auxxi:Nnnnn, \@@_decimate_auxxii:Nnnnn,
1000 % \@@_decimate_auxxiii:Nnnnn, \@@_decimate_auxxiv:Nnnnn,
1001 % \@@_decimate_auxxv:Nnnnn, \@@_decimate_auxxvi:Nnnnn
1002 % }
1003 % \begin{syntax}
1004 % \cs{@@_decimate_auxi:Nnnnn} \meta{f_1} \Arg{X_1} \Arg{X_2} \Arg{X_3} \Arg{X_4}
1005 % \end{syntax}
1006 % Shifting happens in two steps: compute the \meta{rounding} digit,
1007 % and repack digits into two blocks of $8$. The sixteen functions
1008 % are very similar, and defined through \cs{@@_tmp:w}.
1009 % The arguments are as follows: |#1| indicates which function is
1010 % being defined; after one step of expansion, |#2| yields the
1011 % \enquote{extra digits} which are then converted by
1012 % \cs{@@_round_digit:Nw} to the \meta{rounding} digit (note the |+|
1013 % separating blocks of digits to avoid overflowing \TeX{}'s integers).
1014 % This triggers the \texttt{f}-expansion of
1015 % \cs{@@_decimate_pack:nnnnnnnnnnw},\footnote{No, the argument
1016 % spec is not a mistake: the function calls an auxiliary to
1017 % do half of the job.} responsible for building two blocks of
1018 % $8$ digits, and removing the rest. For this to work, |#3|
1019 % alternates between braced and unbraced blocks of $4$ digits,
1020 % in such a way that the $5$ first and $5$ next token groups
1021 % yield the correct blocks of $8$ digits.
1022 % \begin{macrocode}
1023 \cs_new:Npn \@@_tmp:w #1 #2 #3
1024 {
1025 \cs_new:cpn { @@_decimate_ #1 :Nnnnn } ##1 ##2##3##4##5
1026 {
1027 \exp_after:wN ##1
1028 \int_value:w
1029 \exp_after:wN \@@_round_digit:Nw #2 \@@_sep:
1030 \@@_decimate_pack:nnnnnnnnnnw #3 \@@_sep:
1031 }
1032 }
1033 \@@_tmp:w {i} {\use_none:nnn #50}{ 0{#2}#3{#4}#5 }
1034 \@@_tmp:w {ii} {\use_none:nn #5 }{ 00{#2}#3{#4}#5 }
1035 \@@_tmp:w {iii} {\use_none:n #5 }{ 000{#2}#3{#4}#5 }
1036 \@@_tmp:w {iv} { #5 }{ {0000}#2{#3}#4 #5 }
1037 \@@_tmp:w {v} {\use_none:nnn #4#5 }{ 0{0000}#2{#3}#4 #5 }
1038 \@@_tmp:w {vi} {\use_none:nn #4#5 }{ 00{0000}#2{#3}#4 #5 }
1039 \@@_tmp:w {vii} {\use_none:n #4#5 }{ 000{0000}#2{#3}#4 #5 }
1040 \@@_tmp:w {viii}{ #4#5 }{ {0000}0000{#2}#3 #4 #5 }
1041 \@@_tmp:w {ix} {\use_none:nnn #3#4+#5}{ 0{0000}0000{#2}#3 #4 #5 }
1042 \@@_tmp:w {x} {\use_none:nn #3#4+#5}{ 00{0000}0000{#2}#3 #4 #5 }
1043 \@@_tmp:w {xi} {\use_none:n #3#4+#5}{ 000{0000}0000{#2}#3 #4 #5 }
1044 \@@_tmp:w {xii} { #3#4+#5}{ {0000}0000{0000}#2 #3 #4 #5 }
1045 \@@_tmp:w {xiii}{\use_none:nnn#2#3+#4#5}{ 0{0000}0000{0000}#2 #3 #4 #5 }
1046 \@@_tmp:w {xiv} {\use_none:nn #2#3+#4#5}{ 00{0000}0000{0000}#2 #3 #4 #5 }
1047 \@@_tmp:w {xv} {\use_none:n #2#3+#4#5}{ 000{0000}0000{0000}#2 #3 #4 #5 }
1048 \@@_tmp:w {xvi} { #2#3+#4#5}{{0000}0000{0000}0000 #2 #3 #4 #5}
1049 % \end{macrocode}
1050 % \end{macro}
1051 %
1052 % \begin{macro}[EXP]{\@@_decimate_pack:nnnnnnnnnnw}
1053 % The computation of the \meta{rounding} digit leaves an unfinished
1054 % \cs{int_value:w}, which expands the following functions. This
1055 % allows us to repack nicely the digits we keep. Those digits come
1056 % as an alternation of unbraced and braced blocks of $4$ digits,
1057 % such that the first $5$ groups of token consist in $4$ single digits,
1058 % and one brace group (in some order), and the next $5$ have the same
1059 % structure. This is followed by some digits and a \cs{@@_sep:}.
1060 % \begin{macrocode}
1061 \cs_new:Npn \@@_decimate_pack:nnnnnnnnnnw #1#2#3#4#5
1062 { \@@_decimate_pack:nnnnnnw { #1#2#3#4#5 } }
1063 \cs_new:Npn \@@_decimate_pack:nnnnnnw #1 #2#3#4#5#6
1064 { {#1} {#2#3#4#5#6} }
1065 % \end{macrocode}
1066 % \end{macro}
1067 % ^^A end[todo]
1068 %
1069 % \subsection{Functions for use within primitive conditional branches}
1070 %
1071 % The functions described in this section are not pretty and can easily
1072 % be misused. When correctly used, each of them removes one \cs{fi:} as
1073 % part of its parameter text, and puts one back as part of its
1074 % replacement text.
1075 %
1076 % Many computation functions in \pkg{l3fp} must perform tests on the
1077 % type of floating points that they receive. This is often done in an
1078 % \cs{if_case:w} statement or another conditional statement, and only a
1079 % few cases lead to actual computations: most of the special cases are
1080 % treated using a few standard functions which we define now. A typical
1081 % use context for those functions would be
1082 % \begin{syntax}
1083 % \cs{if_case:w} \meta{integer} \cs{exp_stop_f:}
1084 % | |\cs{@@_case_return_o:Nw} \meta{fp var}
1085 % \cs{or:} \cs{@@_case_use:nw} \Arg{some computation}
1086 % \cs{or:} \cs{@@_case_return_same_o:w}
1087 % \cs{or:} \cs{@@_case_return:nw} \Arg{something}
1088 % \cs{fi:}
1089 % \meta{junk}
1090 % \meta{floating point}
1091 % \end{syntax}
1092 % In this example, the case $0$ returns the floating point
1093 % \meta{fp~var}, expanding once after that floating point. Case $1$
1094 % does \meta{some computation} using the \meta{floating point}
1095 % (presumably compute the operation requested by the user in that
1096 % non-trivial case). Case $2$ returns the \meta{floating point}
1097 % without modifying it, removing the \meta{junk} and expanding once
1098 % after. Case $3$ closes the conditional, removes the \meta{junk}
1099 % and the \meta{floating point}, and expands \meta{something} next. In
1100 % other cases, the \enquote{\meta{junk}} is expanded, performing some
1101 % other operation on the \meta{floating point}. We provide similar
1102 % functions with two trailing \meta{floating points}.
1103 %
1104 % \begin{macro}[EXP]{\@@_case_use:nw}
1105 % This function ends a \TeX{} conditional, removes junk until the next
1106 % floating point, and places its first argument before that floating
1107 % point, to perform some operation on the floating point.
1108 % \begin{macrocode}
1109 \cs_new:Npn \@@_case_use:nw #1#2 \fi: #3 \s_@@ { \fi: #1 \s_@@ }
1110 % \end{macrocode}
1111 % \end{macro}
1112 %
1113 % \begin{macro}[EXP]{\@@_case_return:nw}
1114 % This function ends a \TeX{} conditional, removes junk and a floating
1115 % point, and places its first argument in the input stream. A quirk
1116 % is that we don't define this function requiring a floating point to
1117 % follow, simply anything ending in a \cs{@@_sep:}. This, in turn, means
1118 % that the \meta{junk} may not contain \cs{@@_sep:}s.
1119 % \begin{macrocode}
1120 \cs_new:Npn \@@_case_return:nw #1#2 \fi: #3 \@@_sep: { \fi: #1 }
1121 % \end{macrocode}
1122 % \end{macro}
1123 %
1124 % \begin{macro}[EXP]{\@@_case_return_o:Nw}
1125 % This function ends a \TeX{} conditional, removes junk and a floating
1126 % point, and returns its first argument (an \meta{fp~var}) then expands
1127 % once after it.
1128 % \begin{macrocode}
1129 \cs_new:Npn \@@_case_return_o:Nw #1#2 \fi: #3 \s_@@ #4 \@@_sep:
1130 { \fi: \exp_after:wN #1 }
1131 % \end{macrocode}
1132 % \end{macro}
1133 %
1134 % \begin{macro}[EXP]{\@@_case_return_same_o:w}
1135 % This function ends a \TeX{} conditional, removes junk, and returns
1136 % the following floating point, expanding once after it.
1137 % \begin{macrocode}
1138 \cs_new:Npn \@@_case_return_same_o:w #1 \fi: #2 \s_@@
1139 { \fi: \@@_exp_after_o:w \s_@@ }
1140 % \end{macrocode}
1141 % \end{macro}
1142 %
1143 % \begin{macro}[EXP]{\@@_case_return_o:Nww}
1144 % Same as \cs{@@_case_return_o:Nw} but with two trailing floating
1145 % points.
1146 % \begin{macrocode}
1147 \cs_new:Npn \@@_case_return_o:Nww #1#2 \fi: #3 \s_@@ #4 \@@_sep: #5 \@@_sep:
1148 { \fi: \exp_after:wN #1 }
1149 % \end{macrocode}
1150 % \end{macro}
1151 %
1152 % \begin{macro}[EXP]{\@@_case_return_i_o:ww, \@@_case_return_ii_o:ww}
1153 % Similar to \cs{@@_case_return_same_o:w}, but this returns the first
1154 % or second of two trailing floating point numbers, expanding once
1155 % after the result.
1156 % \begin{macrocode}
1157 \cs_new:Npn \@@_case_return_i_o:ww
1158 #1 \fi: #2 \s_@@ #3 \@@_sep: \s_@@ #4 \@@_sep:
1159 { \fi: \@@_exp_after_o:w \s_@@ #3 \@@_sep: }
1160 \cs_new:Npn \@@_case_return_ii_o:ww #1 \fi: #2 \s_@@ #3 \@@_sep:
1161 { \fi: \@@_exp_after_o:w }
1162 % \end{macrocode}
1163 % \end{macro}
1164 %
1165 % \subsection{Integer floating points}
1166 %
1167 % \begin{macro}[EXP, pTF]{\@@_int:w}
1168 % Tests if the floating point argument is an integer. For normal
1169 % floating point numbers, this holds if the rounding digit resulting
1170 % from \cs{@@_decimate:nNnnnn} is~$0$.
1171 % \begin{macrocode}
1172 \prg_new_conditional:Npnn \@@_int:w \s_@@ \@@_chk:w #1 #2 #3 #4\@@_sep:
1173 { TF , T , F , p }
1174 {
1175 \if_case:w #1 \exp_stop_f:
1176 \prg_return_true:
1177 \or:
1178 \if_charcode:w 0
1179 \@@_decimate:nNnnnn { \c_@@_prec_int - #3 }
1180 \@@_use_i_until_s:nw #4
1181 \prg_return_true:
1182 \else:
1183 \prg_return_false:
1184 \fi:
1185 \else: \prg_return_false:
1186 \fi:
1187 }
1188 % \end{macrocode}
1189 % \end{macro}
1190 %
1191 % \subsection{Small integer floating points}
1192 %
1193 % \begin{macro}[EXP]{\@@_small_int:wTF}
1194 % \begin{macro}[EXP]
1195 % {
1196 % \@@_small_int_true:wTF,
1197 % \@@_small_int_normal:NnwTF,
1198 % \@@_small_int_test:NnnwNTF
1199 % }
1200 % Tests if the floating point argument is an integer or $\pm\infty$.
1201 % If so, it is clipped to an integer in the range $[-10^{8},10^{8}]$
1202 % and fed as a braced argument to the \meta{true code}.
1203 % Otherwise, the \meta{false code} is performed.
1204 %
1205 % First filter special cases: zeros and infinities are integers,
1206 % \texttt{nan} is not. For normal numbers, decimate. If the rounding
1207 % digit is not $0$ run the \meta{false code}. If it is, then the
1208 % integer is |#2| |#3|; use |#3| if |#2| vanishes and otherwise
1209 % $10^{8}$.
1210 % \begin{macrocode}
1211 \cs_new:Npn \@@_small_int:wTF \s_@@ \@@_chk:w #1#2
1212 {
1213 \if_case:w #1 \exp_stop_f:
1214 \@@_case_return:nw { \@@_small_int_true:wTF 0 \@@_sep: }
1215 \or: \exp_after:wN \@@_small_int_normal:NnwTF
1216 \or:
1217 \@@_case_return:nw
1218 {
1219 \exp_after:wN \@@_small_int_true:wTF \int_value:w
1220 \if_meaning:w 2 #2 - \fi: 1 0000 0000 \@@_sep:
1221 }
1222 \else: \@@_case_return:nw \use_ii:nn
1223 \fi:
1224 #2
1225 }
1226 \cs_new:Npn \@@_small_int_true:wTF #1\@@_sep: #2#3 { #2 {#1} }
1227 \cs_new:Npn \@@_small_int_normal:NnwTF #1#2#3\@@_sep:
1228 {
1229 \@@_decimate:nNnnnn { \c_@@_prec_int - #2 }
1230 \@@_small_int_test:NnnwNw
1231 #3 #1
1232 }
1233 \cs_new:Npn \@@_small_int_test:NnnwNw #1#2#3#4\@@_sep: #5
1234 {
1235 \if_meaning:w 0 #1
1236 \exp_after:wN \@@_small_int_true:wTF
1237 \int_value:w \if_meaning:w 2 #5 - \fi:
1238 \if_int_compare:w #2 > \c_zero_int
1239 1 0000 0000
1240 \else:
1241 #3
1242 \fi:
1243 \exp_after:wN \@@_sep:
1244 \else:
1245 \exp_after:wN \use_ii:nn
1246 \fi:
1247 }
1248 % \end{macrocode}
1249 % \end{macro}
1250 % \end{macro}
1251 %
1252 % \subsection{Fast string comparison}
1253 %
1254 % \begin{macro}{\@@_str_if_eq:nn}
1255 % A private version of the low-level string comparison function.
1256 % \begin{macrocode}
1257 \cs_new_eq:NN \@@_str_if_eq:nn \tex_strcmp:D
1258 % \end{macrocode}
1259 % \end{macro}
1260 %
1261 % \subsection{Name of a function from its \pkg{l3fp-parse} name}
1262 %
1263 % \begin{macro}[EXP]{\@@_func_to_name:N, \@@_func_to_name_aux:w}
1264 % The goal is to convert for instance \cs{@@_sin_o:w} to |sin|.
1265 % This is used in error messages hence does not need to be fast.
1266 % \begin{macrocode}
1267 \cs_new:Npn \@@_func_to_name:N #1
1268 {
1269 \exp_last_unbraced:Nf
1270 \@@_func_to_name_aux:w { \cs_to_str:N #1 } X
1271 }
1272 \cs_set_protected:Npn \@@_tmp:w #1 #2
1273 { \cs_new:Npn \@@_func_to_name_aux:w ##1 #1 ##2 #2 ##3 X {##2} }
1274 \exp_args:Nff \@@_tmp:w { \tl_to_str:n { @@_ } }
1275 { \tl_to_str:n { _o: } }
1276 % \end{macrocode}
1277 % \end{macro}
1278 %
1279 % \subsection{Messages}
1280 %
1281 % Using a floating point directly is an error.
1282 % \begin{macrocode}
1283 \msg_new:nnnn { fp } { misused }
1284 { A~floating~point~with~value~'#1'~was~misused. }
1285 {
1286 To~obtain~the~value~of~a~floating~point~variable,~use~
1287 '\token_to_str:N \fp_to_decimal:N',~
1288 '\token_to_str:N \fp_to_tl:N',~or~other~
1289 conversion~functions.
1290 }
1291 \prop_gput:Nnn \g_msg_module_name_prop { fp } { LaTeX }
1292 \prop_gput:Nnn \g_msg_module_type_prop { fp } { }
1293 % \end{macrocode}
1294 %
1295 % \begin{macrocode}
1296 %</code>
1297 % \end{macrocode}
1298 %
1299 % \end{implementation}
1300 %
1301 % \PrintChanges
1302 %
1303 % \PrintIndex