Debian Codegraph @

texmf-dist/source/latex/siunitx/siunitx-number.dtx

raw
1 % \iffalse meta-comment
2 %
3 % File: siunitx-number.dtx Copyright (C) 2014-2019,2021-2026 Joseph Wright
4 %
5 % It may be distributed and/or modified under the conditions of the
6 % LaTeX Project Public License (LPPL), either version 1.3c of this
7 % license or (at your option) any later version. The latest version
8 % of this license is in the file
9 %
10 % https://www.latex-project.org/lppl.txt
11 %
12 % This file is part of the "siunitx bundle" (The Work in LPPL)
13 % and all files in that bundle must be distributed together.
14 %
15 % The released version of this bundle is available from CTAN.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/josephwright/siunitx
22 %
23 % for those people who are interested.
24 %
25 % -----------------------------------------------------------------------
26 %
27 %<*driver>
28 \documentclass{l3doc}
29 % Additional commands needed in this source
30 \ProvideDocumentCommand\email{m}{\href{mailto:#1}{\nolinkurl{#1}}}
31 \ProvideDocumentCommand\foreign{m}{\textit{#1}}
32 % The next line is needed so that \GetFileInfo will be able to pick up
33 % version data
34 \usepackage{siunitx}
35 \begin{document}
36 \DocInput{\jobname.dtx}
37 \end{document}
38 %</driver>
39 % \fi
40 %
41 % \GetFileInfo{siunitx.sty}
42 %
43 % \title{^^A
44 % \pkg{siunitx-number} -- Parsing and formatting numbers^^A
45 % \thanks{This file describes \fileversion,
46 % last revised \filedate.}^^A
47 % }
48 %
49 % \author{^^A
50 % Joseph Wright^^A
51 % \thanks{^^A
52 % E-mail:
53 % \email{[email protected]}^^A
54 % }^^A
55 % }
56 %
57 % \date{Released \filedate}
58 %
59 % \maketitle
60 %
61 % \begin{documentation}
62 %
63 % \section{Parsing and formatting numbers}
64 %
65 % This submodule is dedicated to parsing and formatting numbers. A small number
66 % of \LaTeXe{} math mode commands are assumed to be available as part of the
67 % formatted output. The sign commands \cs{mp}, \cs{pm}, \cs{ll}, \cs{le},
68 % \cs{gg} and \cs{ge} are used to replace two-character input; \cs{pm}
69 % is also required for the output of uncertainties, and \cs{sim} for
70 % approximate values. The standard settings require \cs{times}. For the display
71 % of colored negative numbers, the command \cs{color} is assumed to be
72 % available. Where the latter may apply, numbers should be printed inside a
73 % group: note that \TeX{} grouping is not added \emph{within} formatted numbers
74 % as they may need to be decomposed into parts (see
75 % \cs{siunitx_number_output:NN}). Such a color will be the \emph{first} part of
76 % the result, meaning that a test for an initial |\color| and following
77 % brace group may be used to detect/remove/adjust this part.
78 %
79 % \begin{function}{\siunitx_number_parse:nN, \siunitx_number_parse:VN}
80 % \begin{syntax}
81 % \cs{siunitx_number_parse:nN} \Arg{number} \meta{tl~var}
82 % \end{syntax}
83 % Parses the \emph{number} and stores the resulting internal representation
84 % in the \meta{tl~var}. The parsing is influenced by the various key--value
85 % settings for numerical input. The \meta{number} should comprise a single
86 % real value, possibly with comparator, uncertainty and exponent parts.
87 % If the number is invalid, or if number parsing is disabled, the result will
88 % be an entirely empty \meta{tl~var}.
89 %
90 % The structure of a valid number is:
91 % \begin{quote}
92 % \marg{comparator}\marg{sign}\marg{integer}\marg{decimal}
93 % \marg{uncertainty}\\
94 % \marg{exponent sign}\marg{exponent}
95 % \end{quote}
96 % where the two sign parts must be single tokens if present, and all other
97 % components must be given in braces. The number will have at least one digit
98 % for either the \meta{integer} and \meta{exponent} parts. The
99 % \meta{uncertainty} part should either be blank or contain an
100 % \meta{identifier} (as a brace group), followed by one or more data entries.
101 % Valid uncertainty \meta{identifiers} currently are
102 % \begin{itemize}
103 % \item[\texttt{S}] A single symmetrical uncertainty (\foreign{e.g.}~a
104 % statistical standard uncertainty). The data item here is a single
105 % value representing the uncertainty in the least-significant digits.
106 % \item[\texttt{A}] A single unsymmetrical uncertainty. The data item here
107 % contains two brace groups, each using the same least-significant digit
108 % approach as the \texttt{S} type. The positive component is given first
109 % and the negative second, and neither has a sign.
110 % \item A combination of \texttt{S} and \texttt{A} entries, with one data
111 % item per entry. These are then iterated over to be output in order.
112 % \end{itemize}
113 % If a decimal marker should be explicitly recorded as present for a value
114 % with no decimal digits, the \meta{decimal} part should contain
115 % \cs{empty}.
116 % \end{function}
117 %
118 % \begin{function}{\siunitx_number_process:NN, \siunitx_number_process:cc}
119 % \begin{syntax}
120 % \cs{siunitx_number_process:N} \meta{tl~var1} \meta{tl~var2}
121 % \end{syntax}
122 % Applies a set of number processing operations to the \meta{internal
123 % number} stored in the \meta{tl~var1}, \foreign{viz.}~in order
124 % \begin{enumerate}
125 % \item Dropping uncertainty
126 % \item Converting to scientific mode (or similar)
127 % \item Rounding
128 % \item Dropping zero decimal part
129 % \item Forcing a minimum number of digits
130 % \end{enumerate}
131 % with the result stored in \meta{tl~var2}.
132 % \end{function}
133 %
134 % \begin{function}[EXP]
135 % {
136 % \siunitx_number_output:N, \siunitx_number_output:c
137 % \siunitx_number_output:n,
138 % \siunitx_number_output:NN, \siunitx_number_output:cN
139 % \siunitx_number_output:nN
140 % }
141 % \begin{syntax}
142 % \cs{siunitx_number_output:N} \meta{number}
143 % \cs{siunitx_number_output:NN} \meta{number} \meta{marker}
144 % \end{syntax}
145 % Formats the \meta{number} (in the \pkg{siunitx} internal format),
146 % producing the result in a form suitable for typesetting in math mode.
147 % The details for the formatting are controlled by a number of key--value
148 % options. Note that \emph{formatting} does not apply any manipulation
149 % (processing) to the number. This function is usable in an \texttt{e}-
150 % or \texttt{x}-type expansion, and further uncontrolled expansion is
151 % prevented by appropriate use of |\exp_not:n| internally.
152 %
153 % In the \texttt{NN} version, the \meta{marker} token is inserted at each
154 % possible alignment position in the output, \foreign{viz.}
155 % \begin{itemize}
156 % \item Between the comparator and the integer (\emph{before} any
157 % sign for the integer)
158 % \item Between the sign and the first digit of the integer
159 % \item Both sides of the decimal marker
160 % \item Both sides of the separated uncertainty sign (\foreign{i.e.}~after
161 % the decimal part and before any integer uncertainty part)
162 % \item Both sides of the decimal marker for a separated uncertainty
163 % \item Both sides of the multiplication symbol for the exponent part.
164 % \end{itemize}
165 %
166 % The \texttt{n} and \texttt{nN} version take a token list, which should
167 % be in the internal \pkg{siunitx} format.
168 % \end{function}
169 %
170 % \begin{function}{\siunitx_number_format:nN}
171 % \begin{syntax}
172 % \cs{siunitx_number_format:nN} \Arg{number} \meta{tl~var}
173 % \end{syntax}
174 % Carries out a combination of \cs{siunitx_number_parse:nN},
175 % \cs{siunitx_number_process:NN} and \cs{siunitx_number_output:N} using
176 % \texttt{x}-type expansion to place the result in the \meta{tl~var}. If
177 % \cs{l_siunitx_number_parse_bool} if \texttt{false}, the input is simply
178 % stored inside the \meta{tl~var} inside \cs{ensuremath}.
179 % \end{function}
180 %
181 % \begin{function}[EXP]
182 % {
183 % \siunitx_number_adjust_exponent:Nn ,
184 % \siunitx_number_adjust_exponent:nn ,
185 % \siunitx_number_adjust_exponent:Vn
186 % }
187 % \begin{syntax}
188 % \cs{siunitx_number_adjust_exponent:Nn} \meta{number} \Arg{fp~expr}
189 % \end{syntax}
190 % Adjusts the exponent of the \meta{number} (in internal format) by the
191 % \meta{fp~expr} and leaves the result in the input stream.
192 % \end{function}
193 %
194 % \begin{function}{\siunitx_number_normalize_symbols:N}
195 % \begin{syntax}
196 % \cs{siunitx_number_normalize_symbols:N} \meta{tl~var}
197 % \end{syntax}
198 % Replaces all multi-token signs and comparators in the \meta{tl~var}
199 % with their single-token equivalents. Replaces any active hyphen tokens
200 % with non-active versions.
201 % \end{function}
202 %
203 % \begin{function}[pTF, EXP]{\siunitx_if_number:n}
204 % \begin{syntax}
205 % \cs{siunitx_if_number_token:NTF} \Arg{tokens}
206 % \Arg{true code} \Arg{false code}
207 % \end{syntax}
208 % Determines if the \meta{tokens} form a valid number which can be fully
209 % parsed by \pkg{siunitx}.
210 % \end{function}
211 %
212 % \begin{function}[TF]{\siunitx_if_number_token:N}
213 % \begin{syntax}
214 % \cs{siunitx_if_number_token:NTF} \Arg{token}
215 % \Arg{true code} \Arg{false code}
216 % \end{syntax}
217 % Determines if the \meta{token} is valid in a number based on those
218 % tokens currently set up for detection in a number.
219 % \end{function}
220 %
221 % \begin{variable}{\l_siunitx_bracket_ambiguous_bool}
222 % A switch to control whether ambiguous numbers are bracketed: this can
223 % also be covered in quantity formatting by a setting there.
224 % \end{variable}
225 %
226 % \begin{variable}{\l_siunitx_number_parse_bool}
227 % A switch to control whether any parsing is attempted for numbers.
228 % \end{variable}
229 %
230 % \begin{variable}
231 % {
232 % \l_siunitx_number_comparator_tl ,
233 % \l_siunitx_number_exponent_tl ,
234 % \l_siunitx_number_sign_tl
235 % }
236 % The list of possible input comparators, exponent markers and signs.
237 % \end{variable}
238 %
239 % \begin{variable}
240 % {\l_siunitx_number_input_decimal_tl, \l_siunitx_number_output_decimal_tl}
241 % The list of possible input decimal marker(s), and the output marker.
242 % \end{variable}
243 %
244 % \begin{function}{allow-uncertainty-breaks}
245 % \begin{syntax}
246 % |allow-uncertainty-breaks| = |true|\verb"|"|false|
247 % \end{syntax}
248 % Specifies whether breaks are permitted for a (separated) uncertainties. The
249 % standard setting is |true|.
250 % \end{function}
251 %
252 % \begin{function}{bracket-ambiguous-numbers}
253 % \begin{syntax}
254 % |bracket-ambiguous-numbers| = |true|\verb"|"|false|
255 % \end{syntax}
256 % \end{function}
257 %
258 % \begin{function}{bracket-negative-numbers}
259 % \begin{syntax}
260 % |bracket-negative-numbers| = |true|\verb"|"|false|
261 % \end{syntax}
262 % \end{function}
263 %
264 % \begin{function}{drop-exponent}
265 % \begin{syntax}
266 % |drop-exponent| = |true|\verb"|"|false|
267 % \end{syntax}
268 % \end{function}
269 %
270 % \begin{function}{drop-uncertainty}
271 % \begin{syntax}
272 % |drop-uncertainty| = |true|\verb"|"|false|
273 % \end{syntax}
274 % \end{function}
275 %
276 % \begin{function}{drop-zero-decimal}
277 % \begin{syntax}
278 % |drop-zero-decimal| = |true|\verb"|"|false|
279 % \end{syntax}
280 % \end{function}
281 %
282 % \begin{function}{evaluate-expression}
283 % \begin{syntax}
284 % |evaluate-expression| = |true|\verb"|"|false|
285 % \end{syntax}
286 % \end{function}
287 %
288 % \begin{function}{exponent-base}
289 % \begin{syntax}
290 % |exponent-base| = \meta{base}
291 % \end{syntax}
292 % \end{function}
293 %
294 % \begin{function}{exponent-mode}
295 % \begin{syntax}
296 % |exponent-mode| = |engineering|\verb"|"|fixed|\verb"|"|input|\verb"|"|scientific|\verb"|"|threshold|
297 % \end{syntax}
298 % Choice which determines whether numbers are converted to exponent form. The
299 % option |engineering| forces exponent form with an exponent which is the
300 % smallest power of three which gives a mantissa with an integer part. The
301 % option |fixed| uses a fixed exponent (set in |fixed-exponent|). The
302 % option |input| leaves the input unchanged (which will therefore produce an
303 % exponent only if the input contained one). The choice |scientific| gives
304 % an exponent with the mantissa~$m$ in the range $1 \le m < 10$. Finally,
305 % the option |threshold| will apply |scientific| if the exponent of input
306 % is outside of the range stored in |exponent-thresholds|. The standard
307 % setting is |input|.
308 % \end{function}
309 %
310 % \begin{function}{exponent-product}
311 % \begin{syntax}
312 % |exponent-product| = \meta{symbol}
313 % \end{syntax}
314 % \end{function}
315 %
316 % \begin{function}{expression}
317 % \begin{syntax}
318 % |expression| = \meta{expression}
319 % \end{syntax}
320 % \end{function}
321 %
322 % \begin{function}{final-digit-group-min-size}
323 % \begin{syntax}
324 % |final-digit-group-min-size| = \meta{integer}
325 % \end{syntax}
326 % Minimum number of digits which must be present in a group of digits; if this
327 % is not met, the digits of the final group will be combined with those from the
328 % immediately-preceding one.
329 % \end{function}
330 %
331 % \begin{function}{fixed-exponent}
332 % \begin{syntax}
333 % |fixed-exponent| = \meta{exponent}
334 % \end{syntax}
335 % \end{function}
336 %
337 % \begin{function}
338 % {digit-group-size, digit-group-first-size, digit-group-other-size}
339 % \begin{syntax}
340 % |digit-group-number| = \meta{integer}
341 % \end{syntax}
342 % Sets the size of the block (the number of digits) used when grouping
343 % digits. The option |digit-group-first-size| applies to the first grouping,
344 % \foreign{i.e.}~immediately next to the decimal marker, while
345 % |digit-group-other-size| applies to all other groups. Both can be set
346 % using |digit-group-size|. The standard setting for both options is $3$.
347 % \end{function}
348 %
349 % \begin{function}{group-digits}
350 % \begin{syntax}
351 % |group-digits| = |all|\verb"|"|decimal|\verb"|"|integer|\verb"|"|none|
352 % \end{syntax}
353 % Choice to specify whether digits in a number are grouped. The option |none|
354 % entirely disables this, while |all| means that both the integer and decimal
355 % parts are grouped. The settings |integer| and |decimal| activate grouping
356 % for the relevant part only. The standard setting is |all|.
357 % \end{function}
358 %
359 % \begin{function}{group-minimum-digits}
360 % \begin{syntax}
361 % |group-minimum-digits| = \meta{value}
362 % \end{syntax}
363 % The number of digits that must be present in a numerical part (integer or
364 % decimal) before digit grouping is attempted. The standard setting is $4$.
365 % \end{function}
366 %
367 % \begin{function}{group-separator}
368 % \begin{syntax}
369 % |group-separator| = \meta{symbol}
370 % \end{syntax}
371 % Sets the symbol inserted between groups of digits. The standard setting is
372 % a thin space (\cs{,}).
373 % \end{function}
374 %
375 % \begin{function}{input-close-uncertainty}
376 % \begin{syntax}
377 % |input-close-uncertainty| = \meta{tokens}
378 % \end{syntax}
379 % \end{function}
380 %
381 % \begin{function}{input-comparators}
382 % \begin{syntax}
383 % |input-comparators| = \meta{tokens}
384 % \end{syntax}
385 % \end{function}
386 %
387 % \begin{function}{input-close-uncertainty}
388 % \begin{syntax}
389 % |input-close-uncertainty| = \meta{tokens}
390 % \end{syntax}
391 % \end{function}
392 %
393 % \begin{function}{input-decimal-markers}
394 % \begin{syntax}
395 % |input-decimal-markers| = \meta{tokens}
396 % \end{syntax}
397 % \end{function}
398 %
399 % \begin{function}{input-digits}
400 % \begin{syntax}
401 % |input-digits| = \meta{tokens}
402 % \end{syntax}
403 % \end{function}
404 %
405 % \begin{function}{input-exponent-markers}
406 % \begin{syntax}
407 % |input-exponent-markers| = \meta{tokens}
408 % \end{syntax}
409 % \end{function}
410 %
411 % \begin{function}{input-open-uncertainty}
412 % \begin{syntax}
413 % |input-open-uncertainty| = \meta{tokens}
414 % \end{syntax}
415 % \end{function}
416 %
417 % \begin{function}{input-signs}
418 % \begin{syntax}
419 % |input-signs| = \meta{tokens}
420 % \end{syntax}
421 % \end{function}
422 %
423 % \begin{function}{input-uncertainty-signs}
424 % \begin{syntax}
425 % |input-uncertainty-signs| = \meta{tokens}
426 % \end{syntax}
427 % \end{function}
428 %
429 % \begin{function}{input-uncertainty-divider}
430 % \begin{syntax}
431 % |input-uncertainty-divider| = \meta{tokens}
432 % \end{syntax}
433 % \end{function}
434 %
435 % \begin{function}{minimum-decimal-digits}
436 % \begin{syntax}
437 % |minimum-decimal-digits| = \meta{min}
438 % \end{syntax}
439 % \end{function}
440 %
441 % \begin{function}{minimum-integer-digits}
442 % \begin{syntax}
443 % |minimum-integer-digits| = \meta{min}
444 % \end{syntax}
445 % \end{function}
446 %
447 % \begin{function}{negative-color}
448 % \begin{syntax}
449 % |negative-color| = \meta{color}
450 % \end{syntax}
451 % \end{function}
452 %
453 % \begin{function}{output-close-uncertainty}
454 % \begin{syntax}
455 % |output-close-uncertainty| = \meta{symbol}
456 % \end{syntax}
457 % \end{function}
458 %
459 % \begin{function}{output-decimal-marker}
460 % \begin{syntax}
461 % |output-decimal-marker| = \meta{symbol}
462 % \end{syntax}
463 % \end{function}
464 %
465 % \begin{function}{output-open-uncertainty}
466 % \begin{syntax}
467 % |output-open-uncertainty| = \meta{symbol}
468 % \end{syntax}
469 % \end{function}
470 %
471 % \begin{function}{parse-numbers}
472 % \begin{syntax}
473 % |parse-numbers| = |true|\verb"|"|false|
474 % \end{syntax}
475 % \end{function}
476 %
477 % \begin{function}
478 % {
479 % print-implicit-plus ,
480 % print-mantissa-implicit-plus ,
481 % print-exponent-implicit-plus
482 % }
483 % \begin{syntax}
484 % |print-implicit-plus| = |true|\verb"|"|false|
485 % |print-mantissa-implicit-plus| = |true|\verb"|"|false|
486 % |print-exponent-implicit-plus| = |true|\verb"|"|false|
487 % \end{syntax}
488 % Controls whether the plus sign implicit in a positive number is printed;
489 % this can be controlled at the level of the mantissa or exponent, or
490 % can be activated for both.
491 % \end{function}
492 %
493 % \begin{function}{print-unity-mantissa}
494 % \begin{syntax}
495 % |print-unity-mantissa| = |true|\verb"|"|false|
496 % \end{syntax}
497 % \end{function}
498 %
499 % \begin{function}{print-zero-exponent}
500 % \begin{syntax}
501 % |print-zero-exponent| = |true|\verb"|"|false|
502 % \end{syntax}
503 % \end{function}
504 %
505 % \begin{function}{print-zero-integer}
506 % \begin{syntax}
507 % |print-zero-integer| = |true|\verb"|"|false|
508 % \end{syntax}
509 % \end{function}
510 %
511 % \begin{function}{retain-explicit-plus}
512 % \begin{syntax}
513 % |retain-explicit-plus| = |true|\verb"|"|false|
514 % \end{syntax}
515 % Switch which determines if an explicit |+| is retained as a sign when
516 % parsing. The standard setting is |false|.
517 % \end{function}
518 %
519 % \begin{function}{retain-explicit-decimal-marker}
520 % \begin{syntax}
521 % |retain-explicit-decimal-marker| = |true|\verb"|"|false|
522 % \end{syntax}
523 % Switch which determines if an explicit decimal marker is retained when
524 % parsing a number where there is no decimal part to a number
525 % (\foreign{i.e.}~whether to differentiate |10| and |10.|). The standard
526 % setting is |false|.
527 % \end{function}
528 %
529 % \begin{function}{retain-negative-zero}
530 % \begin{syntax}
531 % |retain-negative-zero| = |true|\verb"|"|false|
532 % \end{syntax}
533 % Switch which determines if a negative sign is retained where the value of
534 % a parsed number is exactly zero. The standard setting is |false|.
535 % \end{function}
536 %
537 % \begin{function}{retain-zero-uncertainty}
538 % \begin{syntax}
539 % |retain-zero-uncertainty| = |true|\verb"|"|false|
540 % \end{syntax}
541 % Switch which determines if an entirely zero uncertainty part is retained
542 % on parsing, or whether this is normalised to remove the uncertainty.
543 % The standard setting is |false|.
544 % \end{function}
545 %
546 % \begin{function}{round-direction}
547 % \begin{syntax}
548 % |round-direction| = |down|\verb"|"|nearest|\verb"|"|up|
549 % \end{syntax}
550 % Choice which determines how values are rounded. The setting
551 % |up| means that the value is always rounded away from zero, whereas the
552 % setting |down| means that the value will be rounded toward zero. The
553 % setting |nearest| means that the value will be rounded to the nearest
554 % (either up or down), taking account of the setting of |round-half|.
555 % The standard setting is |nearest|.
556 % \end{function}
557 %
558 % \begin{function}{round-half}
559 % \begin{syntax}
560 % |round-half| = |even|\verb"|"|up|
561 % \end{syntax}
562 % Choice which determines how values of exactly half are rounded. The setting
563 % |up| means that the value is always rounded away from zero, whereas the
564 % setting |even| means that the value will be rounded to the closes even
565 % number. The standard setting is |up|.
566 % \end{function}
567 %
568 % \begin{function}{round-minimum}
569 % \begin{syntax}
570 % |round-minimum| = \meta{min}
571 % \end{syntax}
572 % Literal which sets a minimum value below which rounded values will be
573 % replaced by this value and a |>| or |<|, as appropriate for the sign of
574 % the value. The standard setting is empty, \foreign{i.e.}~there is no
575 % minimum.
576 % \end{function}
577 %
578 % \begin{function}{round-mode}
579 % \begin{syntax}
580 % |round-mode| = |figures|\verb"|"|none|\verb"|"|places|\verb"|"|uncertainty|
581 % \end{syntax}
582 % Choice which specifies the rounding approach used for numbers. The choice
583 % |figures| means that values are rounding to the number of significant
584 % figures specified by |round-precision|. The setting |places| rounds to
585 % |round-precision| interpreted as a number of decimal places: this may be
586 % negative (rounding to an integer). The setting |none| disables rounding.
587 % The setting |uncertainty| first rounds the uncertainty to the number of
588 % significant figures specified by |round-precision|, then rounds the main
589 % value such that its accuracy is correctly specified by this updated
590 % uncertainty. The standard setting is |none|.
591 % \end{function}
592 %
593 % \begin{function}{round-pad}
594 % \begin{syntax}
595 % |round-pad| = |true|\verb"|"|false|
596 % \end{syntax}
597 % Switch which specifies if values should be padded to the required number
598 % length when rounding to a number of decimal places. The standard setting is
599 % |true|.
600 % \end{function}
601 %
602 % \begin{function}{round-precision}
603 % \begin{syntax}
604 % |round-precision| = \meta{precision}
605 % \end{syntax}
606 % Integer specifying the number of digits used as a target when rounding:
607 % this may be interpreted as decimal places or significant figures,
608 % depending on active |round-mode|. The standard setting is $2$.
609 % \end{function}
610 %
611 % \begin{function}{round-zero-positive}
612 % \begin{syntax}
613 % |round-zero-positive| = |true|\verb"|"|false|
614 % \end{syntax}
615 % Switch to control whether a value rounded to zero is regarded as a positive
616 % number if the input was negative. The standard setting is |true|.
617 % \end{function}
618 %
619 % \begin{function}{simplify-uncertainty}
620 % \begin{syntax}
621 % |simplify-uncertainty| = |true|\verb"|"|false|
622 % \end{syntax}
623 % Switch to control whether uncertainties given with two equal components
624 % are printed as a single value.
625 % \end{function}
626 %
627 % \begin{function}{tight-spacing}
628 % \begin{syntax}
629 % |tight-spacing| = |true|\verb"|"|false|
630 % \end{syntax}
631 % \end{function}
632 %
633 % \begin{function}{uncertainty-descriptor-mode}
634 % \begin{syntax}
635 % |uncertainty-descriptor| = |bracket|\verb"|"|bracket-separator|\verb"|"|separator|\verb"|"|subscript|
636 % \end{syntax}
637 % Selects how uncertainty descriptors are formatted: a choice from the
638 % options |bracket|, |text| and |subscript|. The option |bracket| wraps the
639 % descriptor in parenthesis, |bracket-separator| does the same but also
640 % includes a separator between the uncertainty and opening bracket,
641 % |separator| places the descriptor after the uncertainty and a separator,
642 % and |subscript| formats the descriptor as a subscript. The
643 % standard setting is |bracket-separator|.
644 % \end{function}
645 %
646 % \begin{function}{uncertainty-descriptor-separator}
647 % \begin{syntax}
648 % |uncertainty-descriptor-separator| = \meta{separator}
649 % \end{syntax}
650 % Separateor inserted between the uncertainty and descriptor when one is
651 % required by |uncertainty-separator-mode|. The standard setting is
652 % \verb*|\ |.
653 % \end{function}
654 %
655 % \begin{function}{uncertainty-descriptors}
656 % \begin{syntax}
657 % |uncertainty-descriptors| = \meta{clist}
658 % \end{syntax}
659 % Stores the list of descriptors used when there are multiple uncertainty
660 % components given. This is not used when there is only a single uncertainty
661 % component present. The standard setting is |empty|.
662 % \end{function}
663 %
664 % \begin{function}{uncertainty-mode}
665 % \begin{syntax}
666 % |uncertainty-mode| = |compact|\verb"|"|compact-marker|\verb"|"|full|\verb"|"|separate|
667 % \end{syntax}
668 % Switch to determine how single symmetrical uncertainties are formatted.
669 % When this is set to |separate|, the uncertainty is printed as an entirely
670 % separate number preceded by \cs{pm}. Other settings all place the
671 % uncertainty in parentheses directly attached to the main value. The
672 % standard setting of |compact| prints digits of uncertainty in the
673 % least-significant digits. It does \emph{not} print a decimal marker if the
674 % uncertainty crosses the decimal. The setting |full| prints the full value
675 % of the uncertainty. The setting |compact-marker| is available to print in
676 % the |compact| style except where the uncertainty crosses the decimal, in
677 % which case the |full| style is used. The standard setting is |compact|.
678 % \end{function}
679 %
680 % \begin{function}{uncertainty-round-direction}
681 % \begin{syntax}
682 % |uncertainty-round-direction| = |down|\verb"|"|nearest|\verb"|"|up|
683 % \end{syntax}
684 % Choice which determines how uncertainty values are rounded. The setting
685 % |up| means that the uncertainty is always rounded away from zero, whereas
686 % the setting |down| means that the uncertainty will be rounded toward zero.
687 % The setting |nearest| means that the uncertainty will be rounded to the
688 % nearest (either up or down), taking account of the setting of |round-half|.
689 % The standard setting is |nearest|.
690 % \end{function}
691 %
692 % \begin{function}{uncertainty-separator}
693 % \begin{syntax}
694 % |uncertainty-separator| = \meta{separator}
695 % \end{syntax}
696 % Stores the separator used between the main value and uncertainty when using
697 % the |compact| or |compact-marker| style setting for |uncertainty-mode|.
698 % \end{function}
699 %
700 % \begin{function}{zero-decimal-as-symbol}
701 % \begin{syntax}
702 % |zero-decimal-as-symbol| = |true|\verb"|"|false|
703 % \end{syntax}
704 % Switch to determine if an entirely zero decimal part is replaced by a
705 % symbol. Does not apply if the decimal part is marked as entirely absent.
706 % \end{function}
707 %
708 % \begin{function}{zero-symbol}
709 % \begin{syntax}
710 % |zero-symbol| = \meta{symbol}
711 % \end{syntax}
712 % Material printed when a zero numerical component is replaced by a symbol.
713 % \end{function}
714 %
715 % \end{documentation}
716 %
717 % \begin{implementation}
718 %
719 % \section{\pkg{siunitx-number} implementation}
720 %
721 % Start the \pkg{DocStrip} guards.
722 % \begin{macrocode}
723 %<*package>
724 % \end{macrocode}
725 %
726 % Identify the internal prefix.
727 % \begin{macrocode}
728 %<@@=siunitx_number>
729 % \end{macrocode}
730 %
731 % \subsection{Initial set-up}
732 %
733 % Variants not provided by \pkg{expl3}.
734 % \begin{macrocode}
735 \cs_generate_variant:Nn \tl_if_blank:nTF { e }
736 \cs_generate_variant:Nn \tl_if_in:NnTF { NV }
737 \cs_generate_variant:Nn \tl_if_in:nnTF { nV }
738 \cs_generate_variant:Nn \tl_remove_all:Nn { NV }
739 \cs_generate_variant:Nn \tl_replace_all:Nnn { NnV }
740 % \end{macrocode}
741 %
742 % \begin{variable}{\l_@@_tmp_tl}
743 % Scratch space.
744 % \begin{macrocode}
745 \tl_new:N \l_@@_tmp_tl
746 % \end{macrocode}
747 % \end{variable}
748 %
749 % \subsection{Main formatting routine}
750 %
751 % \begin{variable}{\l_@@_outputted_tl}
752 % A token list for the final formatted result: may or may not be generated
753 % by the parser, depending on settings which are active.
754 % \begin{macrocode}
755 \tl_new:N \l_@@_outputted_tl
756 % \end{macrocode}
757 % \end{variable}
758 %
759 % \begin{variable}{\l_siunitx_number_parse_bool}
760 % Tracks whether to parse numbers: public as this may affect other
761 % behaviors.
762 % \begin{macrocode}
763 \bool_new:N \l_siunitx_number_parse_bool
764 % \end{macrocode}
765 % \end{variable}
766 %
767 % \begin{variable}{\l_siunitx_number_parse_bool}
768 % Top-level options.
769 % \begin{macrocode}
770 \keys_define:nn { siunitx }
771 {
772 parse-numbers .bool_set:N = \l_siunitx_number_parse_bool
773 }
774 % \end{macrocode}
775 % \end{variable}
776 %
777 % \begin{macro}{\siunitx_number_format:nN}
778 % \begin{macrocode}
779 \cs_new_protected:Npn \siunitx_number_format:nN #1#2
780 {
781 \group_begin:
782 \bool_if:NTF \l_siunitx_number_parse_bool
783 {
784 \siunitx_number_parse:nN {#1} \l_@@_parsed_tl
785 \tl_if_empty:NF \l_@@_parsed_tl
786 {
787 \siunitx_number_process:NN \l_@@_parsed_tl \l_@@_parsed_tl
788 \tl_set:Ne \l_@@_outputted_tl
789 { \siunitx_number_output:N \l_@@_parsed_tl }
790 }
791 }
792 { \tl_set:Nn \l_@@_outputted_tl { \ensuremath {#1} } }
793 \exp_args:NNNV \group_end:
794 \tl_set:Nn #2 \l_@@_outputted_tl
795 }
796 % \end{macrocode}
797 % \end{macro}
798 %
799 % \subsection{Parsing numbers}
800 %
801 % Before numbers can be manipulated or formatted they need to be parsed into
802 % an internal form. In particular, if multiple code paths are to be avoided,
803 % it is necessary to do such parsing even for relatively simple cases such
804 % as converting |1e10| to |1 \times 10^{10}|.
805 %
806 % Storing the result of such parsing can be done in a number of ways. In the
807 % first version of \pkg{siunitx} a series of separate data stores were used.
808 % This is potentially quite fast (as recovery of items relies only on \TeX{}'s
809 % hash table) but makes managing the various data entries somewhat tedious and
810 % error-prone. For version two of the package, a single data structure
811 % (property list) was used for each part of the parsed number. Whilst this is
812 % easy to manage and extend, it is somewhat slower as at a \TeX{} level there
813 % are repeated pack--unpack steps. In particular, the fact that there are a
814 % limited number of items to track for a \enquote{number} means that a more
815 % efficient approach is desirable (contrast parsing units, which is open-ended
816 % and therefore fits well with using a property list).
817 %
818 % In this release, the structure of a valid number is:
819 % \begin{quote}
820 % \marg{comparator}\meta{sign}\marg{integer}\marg{decimal}
821 % \marg{uncertainty}\\
822 % \meta{exponent sign}\marg{exponent}
823 % \end{quote}
824 % where all components must be given in braces. \emph{All} of the components
825 % must be present in a stored number (\foreign{i.e.}~at the end of parsing).
826 %
827 % A non-empty \meta{uncertainty} must contain one leading brace group
828 % containing an identifier, then zero or more brace groups which contain
829 % the uncertainty data. In this release, the known uncertainty types are
830 % \begin{itemize}
831 % \item \texttt{A}: An unsymmetrical uncertainty made up of two values.
832 % Each is stored as uncertainty in significant digits, with no radix point
833 % in the stored value. The two parts are stored within one brace group,
834 % within which are two brace groups: the first contains the positive
835 % component, the second the negative component. Neither component has a
836 % sign.
837 % \item \texttt{S}: A symmetrical statistical uncertainty made up of
838 % a single value. These are stored as uncertainty in significant digits,
839 % with no radix point in the stored value.
840 % \end{itemize}
841 %
842 % \begin{variable}{\l_siunitx_number_input_decimal_tl}
843 % The input decimal markers(s).
844 % \begin{macrocode}
845 \tl_new:N \l_siunitx_number_input_decimal_tl
846 % \end{macrocode}
847 % \end{variable}
848 %
849 % \begin{variable}
850 % {
851 % \l_@@_expression_bool ,
852 % \l_@@_input_uncert_close_tl ,
853 % \l_siunitx_number_input_comparator_tl ,
854 % \l_@@_input_digit_tl ,
855 % \l_siunitx_number_input_exponent_tl ,
856 % \l_@@_input_ignore_tl ,
857 % \l_@@_input_uncert_open_tl ,
858 % \l_@@_input_uncert_divide_tl ,
859 % \l_siunitx_number_input_sign_tl ,
860 % \l_@@_input_uncert_sign_tl ,
861 % \l_@@_explicit_decimal_bool ,
862 % \l_@@_explicit_plus_bool ,
863 % \l_@@_negative_zero_bool ,
864 % \l_@@_zero_uncert_bool
865 % }
866 % \begin{macro}[EXP]{\@@_expression:n}
867 % Options which determine the various valid parts of a parsed number.
868 % \begin{macrocode}
869 \keys_define:nn { siunitx }
870 {
871 evaluate-expression .bool_set:N =
872 \l_@@_expression_bool ,
873 expression .code:n =
874 \cs_set:Npn \@@_expression:n ##1 {#1} ,
875 input-close-uncertainty .tl_set:N =
876 \l_@@_input_uncert_close_tl ,
877 input-comparators .tl_set:N =
878 \l_siunitx_number_input_comparator_tl ,
879 input-decimal-markers .tl_set:N =
880 \l_siunitx_number_input_decimal_tl ,
881 input-digits .tl_set:N =
882 \l_@@_input_digit_tl ,
883 input-exponent-markers .tl_set:N =
884 \l_siunitx_number_input_exponent_tl ,
885 input-ignore .tl_set:N =
886 \l_@@_input_ignore_tl ,
887 input-open-uncertainty .tl_set:N =
888 \l_@@_input_uncert_open_tl ,
889 input-signs .tl_set:N =
890 \l_siunitx_number_input_sign_tl ,
891 input-uncertainty-signs .code:n =
892 {
893 \tl_set:Nn \l_@@_input_uncert_sign_tl {#1}
894 \tl_map_inline:nn {#1}
895 {
896 \tl_if_in:NnF \l_siunitx_number_input_sign_tl {##1}
897 { \tl_put_right:Nn \l_siunitx_number_input_sign_tl {##1} }
898 }
899 } ,
900 input-uncertainty-divider .tl_set:N =
901 \l_@@_input_uncert_divide_tl ,
902 parse-numbers .bool_set:N =
903 \l_siunitx_number_parse_bool ,
904 retain-explicit-decimal-marker .bool_set:N =
905 \l_@@_explicit_decimal_bool ,
906 retain-explicit-plus .bool_set:N =
907 \l_@@_explicit_plus_bool ,
908 retain-negative-zero .bool_set:N =
909 \l_@@_negative_zero_bool ,
910 retain-zero-uncertainty .bool_set:N =
911 \l_@@_zero_uncert_bool
912 }
913 \cs_new:Npn \@@_expression:n #1 { }
914 \tl_new:N \l_@@_input_uncert_sign_tl
915 % \end{macrocode}
916 % \end{macro}
917 % \end{variable}
918 %
919 % \begin{variable}{\l_@@_arg_tl}
920 % The input argument or a part thereof, depending on the position in
921 % the parsing routine.
922 % \begin{macrocode}
923 \tl_new:N \l_@@_arg_tl
924 % \end{macrocode}
925 % \end{variable}
926 %
927 % \begin{variable}{\l_@@_comparator_tl}
928 % A comparator, if found, is held here.
929 % \begin{macrocode}
930 \tl_new:N \l_@@_comparator_tl
931 % \end{macrocode}
932 % \end{variable}
933 %
934 % \begin{variable}{\l_@@_exponent_tl}
935 % The exponent part of a parsed number. It is easiest to find this
936 % relatively early in the parsing process, but as it needs to go at
937 % the end of the internal format is held separately until required.
938 % \begin{macrocode}
939 \tl_new:N \l_@@_exponent_tl
940 % \end{macrocode}
941 % \end{variable}
942 %
943 % \begin{variable}{\l_@@_flex_tl}
944 % In a number with an uncertainty, the exact meaning of a second part is
945 % not fully resolved until parsing is complete. That is handled using
946 % this \enquote{flexible} store.
947 % \begin{macrocode}
948 \tl_new:N \l_@@_flex_tl
949 % \end{macrocode}
950 % \end{variable}
951 %
952 % \begin{variable}{\l_@@_parsed_tl}
953 % The number parsed into internal format.
954 % \begin{macrocode}
955 \tl_new:N \l_@@_parsed_tl
956 % \end{macrocode}
957 % \end{variable}
958 %
959 % \begin{variable}{\l_@@_input_tl}
960 % The numerical input exactly as given by the user.
961 % \begin{macrocode}
962 \tl_new:N \l_@@_input_tl
963 % \end{macrocode}
964 % \end{variable}
965 %
966 % \begin{variable}{\l_@@_partial_tl}
967 % To avoid needing to worry about the fact that the final data stores are
968 % somewhat tricky to add to token-by-token, a simple store is used to build
969 % up the parsed part of a number before transferring in one go.
970 % \begin{macrocode}
971 \tl_new:N \l_@@_partial_tl
972 % \end{macrocode}
973 % \end{variable}
974 %
975 % \begin{variable}
976 % {
977 % \l_@@_tolerance_tl ,
978 % \l_@@_uncert_tl ,
979 % \l_@@_uncert_offset_int,
980 % \l_@@_uncert_types_tl
981 % }
982 % To allow multiple uncertainties, we want to build this up as possibly
983 % multiple entries. For asymmetrical uncertainties/tolerances, we need
984 % an additional store.
985 % \begin{macrocode}
986 \tl_new:N \l_@@_tolerance_tl
987 \tl_new:N \l_@@_uncert_tl
988 \int_new:N \l_@@_uncert_offset_int
989 \tl_new:N \l_@@_uncert_types_tl
990 % \end{macrocode}
991 % \end{variable}
992 %
993 % \begin{variable}{\l_@@_validate_bool}
994 % Used to set up for validation with no error production.
995 % \begin{macrocode}
996 \bool_new:N \l_@@_validate_bool
997 % \end{macrocode}
998 % \end{variable}
999 %
1000 % \begin{macro}{\siunitx_number_normalize_symbols:N}
1001 % \begin{macro}{\@@_normalize_aux:nN}
1002 % \begin{macro}{\@@_normalize_actives:N}
1003 % \begin{variable}{\c_@@_normalize_tl}
1004 % There are two parts to the replacement code. First, any actives
1005 % are normalised: these can come up with some packages and
1006 % cause issues. Multi-token signs then are converted to the single token
1007 % equivalents so that everything else can work on a one token basis.
1008 % \begin{macrocode}
1009 \cs_new_protected:Npn \siunitx_number_normalize_symbols:N #1
1010 {
1011 \@@_normalize_actives:N #1
1012 \exp_after:wN \@@_normalize_aux:NnN \exp_after:wN #1
1013 \c_@@_normalize_tl
1014 { ? } \q_recursion_tail
1015 \q_recursion_stop
1016 }
1017 \cs_set_protected:Npn \@@_normalize_aux:NnN #1#2#3
1018 {
1019 \quark_if_recursion_tail_stop:N #3
1020 \tl_replace_all:Nnn #1 {#2} {#3}
1021 \@@_normalize_aux:NnN #1
1022 }
1023 \group_begin:
1024 \char_set_catcode_other:N \~
1025 \tl_const:Nn \c_@@_normalize_tl
1026 {
1027 { -+ } \mp
1028 { +- } \pm
1029 { << } \ll
1030 { <= } \le
1031 { >> } \gg
1032 { >= } \ge
1033 { ~ } \sim
1034 }
1035 \group_end:
1036 \group_begin:
1037 \char_set_catcode_active:N \-
1038 \char_set_catcode_active:N \<
1039 \char_set_catcode_active:N \>
1040 \char_set_catcode_active:N \~
1041 \cs_new_protected:Npe \@@_normalize_actives:N #1
1042 {
1043 \tl_replace_all:Nnn #1
1044 { \exp_not:N - } { \token_to_str:N - }
1045 \tl_replace_all:Nnn #1
1046 { \exp_not:N < } { \token_to_str:N < }
1047 \tl_replace_all:Nnn #1
1048 { \exp_not:N > } { \token_to_str:N > }
1049 \tl_replace_all:Nnn #1
1050 { \exp_not:N ~ } { \token_to_str:N ~ }
1051 }
1052 \group_end:
1053 % \end{macrocode}
1054 % \end{variable}
1055 % \end{macro}
1056 % \end{macro}
1057 % \end{macro}
1058 %
1059 % \begin{macro}{\siunitx_number_parse:nN, \siunitx_number_parse:VN}
1060 % \begin{macro}{\@@_parse:nN}
1061 % After some initial set up, the parser expands the input and then replaces
1062 % as far as possible tricky tokens with ones that can be handled using
1063 % delimited arguments. To avoid multiple conditionals here, the parser is
1064 % set up as a chain of commands initially, with a loop only later. This
1065 % avoids more conditionals than are necessary.
1066 % \begin{macrocode}
1067 \cs_new_protected:Npn \siunitx_number_parse:nN #1#2
1068 {
1069 \bool_if:NTF \l_siunitx_number_parse_bool
1070 { \@@_parse:nN {#1} #2 }
1071 { \tl_clear:N #2 }
1072 }
1073 \cs_generate_variant:Nn \siunitx_number_parse:nN { V }
1074 \cs_new_protected:Npn \@@_parse:nN #1#2
1075 {
1076 \group_begin:
1077 \tl_clear:N \l_@@_parsed_tl
1078 \tl_clear:N \l_@@_uncert_tl
1079 \tl_clear:N \l_@@_uncert_types_tl
1080 \tl_map_inline:Nn \l_@@_input_ignore_tl
1081 {
1082 \token_if_macro:NT ##1
1083 { \cs_set_eq:NN ##1 \scan_stop: }
1084 }
1085 \protected@edef \l_@@_arg_tl
1086 {
1087 \bool_if:NTF \l_@@_expression_bool
1088 { \fp_eval:n { \@@_expression:n {#1} } }
1089 {#1}
1090 }
1091 \tl_set_eq:NN \l_@@_input_tl \l_@@_arg_tl
1092 \siunitx_number_normalize_symbols:N \l_@@_arg_tl
1093 \tl_map_inline:Nn \l_@@_input_ignore_tl
1094 { \tl_remove_all:Nn \l_@@_arg_tl {##1} }
1095 \protected@edef \l_@@_input_digit_tl
1096 { \l_@@_input_digit_tl }
1097 \tl_if_empty:NF \l_@@_arg_tl
1098 {
1099 \@@_parse_comparator:
1100 \@@_parse_check:
1101 }
1102 \exp_args:NNNV \group_end:
1103 \tl_set:Nn #2 \l_@@_parsed_tl
1104 }
1105 % \end{macrocode}
1106 % \end{macro}
1107 % \end{macro}
1108 %
1109 % \begin{macro}{\@@_parse_check:}
1110 % After the loop there is one case that might need tidying up. If a
1111 % separated uncertainty was found it will be currently in \cs{l_@@_flex_tl}
1112 % and needs moving. A series of tests pick up that case, then the check is
1113 % made that some content was found
1114 % \begin{macrocode}
1115 \cs_new_protected:Npn \@@_parse_check:
1116 {
1117 \tl_put_right:NV \l_@@_uncert_tl \l_@@_flex_tl
1118 \tl_if_empty:NF \l_@@_uncert_tl
1119 {
1120 \tl_if_blank:eTF { \exp_after:wN \use_iv:nnnn \l_@@_parsed_tl }
1121 { \@@_parse_combine_uncert: }
1122 { \tl_clear:N \l_@@_parsed_tl }
1123 }
1124 \tl_if_empty:NTF \l_@@_parsed_tl
1125 {
1126 \bool_if:NF \l_@@_validate_bool
1127 {
1128 \msg_error:nne { siunitx } { invalid-number }
1129 { \exp_not:V \l_@@_input_tl }
1130 }
1131 }
1132 { \@@_parse_finalise: }
1133 }
1134 % \end{macrocode}
1135 % \end{macro}
1136 %
1137 % \begin{macro}{\@@_parse_combine_uncert:}
1138 % \begin{macro}[EXP]{\@@_parse_combine_uncert:nnnn}
1139 % \begin{macro}[EXP]{\@@_parse_combine_uncert:nn}
1140 % \begin{macro}[EXP]
1141 % {
1142 % \@@_parse_combine_uncert_loop:nnnnnnn ,
1143 % \@@_parse_combine_uncert_loop:nnnnnnn
1144 % }
1145 % \begin{macro}[EXP]
1146 % {\@@_parse_combine_uncert:nnnnnn, \@@_parse_combine_uncert:ennnnn}
1147 % \begin{macro}[EXP]{\@@_parse_combine_uncert_aux:nn}
1148 % \begin{macro}[EXP]
1149 % {\@@_parse_combine_uncert:nnnnnnn, \@@_parse_combine_uncert:ennnnnn}
1150 % \begin{macro}[EXP]
1151 % {\@@_parse_combine_uncert:nnnnn, \@@_parse_combine_uncert:nnenn}
1152 % \begin{macro}[EXP]
1153 % {
1154 % \@@_parse_combine_uncert_aux:nnnn ,
1155 % \@@_parse_combine_uncert_aux:eenn
1156 % }
1157 % \begin{macro}[EXP]{\@@_parse_combine_uncert:N}
1158 % \begin{macro}[EXP]{\@@_parse_combine_uncert:w}
1159 % \begin{macro}[EXP]{\@@_parse_combine_uncert_end:nnn}
1160 % \begin{macro}[EXP]{\@@_parse_combine_uncert_end:nnnn}
1161 % Conversion of a second numerical part to an uncertainty needs a bit of
1162 % work. The first step is to extract the useful information from the two
1163 % stores: the sign, integer and decimal parts from the real number. Then
1164 % set up a loop to deal with each uncertainty to combine. Everything is done
1165 % by expansion to avoid repeated assignments. To allow for the case where
1166 % there is an error, the setup doesn't insert any tokens until the end of
1167 % the loop, which means a bit of work on the stack.
1168 % \begin{macrocode}
1169 \cs_new_protected:Npn \@@_parse_combine_uncert:
1170 {
1171 \tl_set:Ne \l_@@_parsed_tl
1172 {
1173 \exp_after:wN \@@_parse_combine_uncert:nnnn
1174 \l_@@_parsed_tl
1175 }
1176 }
1177 % \end{macrocode}
1178 % Here, |#1| and |#2| are not required at this stage, while |#4| is junk.
1179 % Thus |#3| is the only item needed for the rest of the process. After a
1180 % little argument shuffling, the main loop can begin.
1181 % \begin{macrocode}
1182 \cs_new:Npn \@@_parse_combine_uncert:nnnn #1#2#3#4
1183 {
1184 \exp_after:wN \@@_parse_combine_uncert:nn
1185 \exp_after:wN { \l_@@_uncert_tl } {#3}
1186 }
1187 \cs_new:Npn \@@_parse_combine_uncert:nn #1#2
1188 {
1189 \@@_parse_combine_uncert_loop:nnnnnnn {#2} { } { }
1190 #1
1191 { \q_recursion_tail } { } { } { } \q_recursion_stop
1192 }
1193 % \end{macrocode}
1194 % Here, |#4| and |#7| are junk arguments simply there to mop up
1195 % tokens, with the exception that |#4| is also used for the end-of-loop
1196 % test.
1197 % \begin{macrocode}
1198 \cs_new:Npn \@@_parse_combine_uncert_loop:nnnnnnn #1#2#3#4#5#6#7
1199 {
1200 \quark_if_recursion_tail_stop_do:nn {#4}
1201 { \@@_parse_combine_uncert_end:nnn {#1} {#2} {#3} }
1202 \@@_parse_combine_uncert:ennnnn
1203 { \int_eval:n { \tl_count:n {#1} - \tl_count:n {#6} } }
1204 {#1} {#2} {#3} {#5} {#6}
1205 }
1206 \cs_generate_variant:Nn \@@_parse_combine_uncert_loop:nnnnnnn { nee }
1207 % \end{macrocode}
1208 % The difference in places between the two decimal parts is now found: this
1209 % is done just once to avoid having to parse token lists twice. The value
1210 % is then used to generate a number of filler |0| tokens, and these are added
1211 % to the appropriate part of the number.
1212 % \begin{macrocode}
1213 \cs_new:Npn \@@_parse_combine_uncert:nnnnnn #1
1214 {
1215 \@@_parse_combine_uncert:ennnnnn
1216 { \prg_replicate:nn { \int_abs:n {#1} } { 0 } }
1217 {#1}
1218 }
1219 \cs_generate_variant:Nn \@@_parse_combine_uncert:nnnnnn { e }
1220 \cs_new:Npn \@@_parse_combine_uncert:nnnnnnn #1#2#3#4#5#6#7
1221 {
1222 \int_compare:nNnTF {#2} > 0
1223 {
1224 \@@_parse_combine_uncert:nnnnn
1225 {#3} {#4} {#5} {#6} { #7 #1 }
1226 }
1227 {
1228 \@@_parse_combine_uncert:nnenn { #3 #1 } {#4}
1229 {
1230 \tl_map_tokens:nn {#5}
1231 { \@@_parse_combine_uncert_aux:nn {#1} }
1232 }
1233 {#6} {#7}
1234 }
1235 }
1236 \cs_generate_variant:Nn \@@_parse_combine_uncert:nnnnnnn { e }
1237 \cs_new:Npn \@@_parse_combine_uncert_aux:nn #1#2 { {#2#1} }
1238 % \end{macrocode}
1239 % We now ensure that the decimal part is never entirely blank \emph{if} there
1240 % are decimal-part uncertainty digits. There is also a need to handle the
1241 % possibly of an entirely empty uncertainty, where the value is zero and that
1242 % is not being retained.
1243 % \begin{macrocode}
1244 \cs_new:Npn \@@_parse_combine_uncert:nnnnn #1#2#3#4#5
1245 {
1246 \@@_parse_combine_uncert_aux:eenn
1247 {
1248 \bool_lazy_and:nnTF
1249 { \tl_if_blank_p:n {#1} }
1250 { ! \tl_if_blank_p:n {#5} }
1251 { 0 }
1252 { \exp_not:n {#1} }
1253 }
1254 {
1255 \@@_parse_combine_uncert:N #4#5
1256 \q_recursion_tail \q_recursion_stop
1257 }
1258 {#2}
1259 {#3}
1260 }
1261 \cs_generate_variant:Nn \@@_parse_combine_uncert:nnnnn { nne }
1262 \cs_new:Npn \@@_parse_combine_uncert_aux:nnnn #1#2#3#4
1263 {
1264 \@@_parse_combine_uncert_loop:neennnn
1265 {#1}
1266 {
1267 \exp_not:n {#3}
1268 \tl_if_blank:nF {#2} { S }
1269 }
1270 {
1271 \exp_not:n {#4}
1272 \tl_if_blank:nF {#2} { { \exp_not:n {#2} } }
1273 }
1274 }
1275 \cs_generate_variant:Nn \@@_parse_combine_uncert_aux:nnnn { ee }
1276 % \end{macrocode}
1277 % A short routine to remove any leading zeros in the uncertainty part,
1278 % which are not needed for the compact representation used by the module.
1279 % \begin{macrocode}
1280 \cs_new:Npn \@@_parse_combine_uncert:N #1
1281 {
1282 \quark_if_recursion_tail_stop_do:Nn #1
1283 { \bool_if:NT \l_@@_zero_uncert_bool { 0 } }
1284 \str_if_eq:nnTF {#1} { 0 }
1285 { \@@_parse_combine_uncert:N }
1286 { \@@_parse_combine_uncert:w #1 }
1287 }
1288 \cs_new:Npn \@@_parse_combine_uncert:w
1289 #1 \q_recursion_tail \q_recursion_stop
1290 { \exp_not:n {#1} }
1291 % \end{macrocode}
1292 % Recover the sign and integer part, then add in the new decimal and the
1293 % uncertainties.
1294 % \begin{macrocode}
1295 \cs_new:Npn \@@_parse_combine_uncert_end:nnn #1#2#3
1296 {
1297 \exp_after:wN \@@_parse_combine_uncert_end:nnnn
1298 \l_@@_parsed_tl
1299 { \exp_not:n {#1} }
1300 {
1301 \tl_if_blank:nF {#2}
1302 { \exp_not:n { {#2} #3 } }
1303 }
1304 }
1305 \cs_new:Npn \@@_parse_combine_uncert_end:nnnn #1#2#3#4
1306 { \exp_not:n { {#1} {#2} } }
1307 % \end{macrocode}
1308 % \end{macro}
1309 % \end{macro}
1310 % \end{macro}
1311 % \end{macro}
1312 % \end{macro}
1313 % \end{macro}
1314 % \end{macro}
1315 % \end{macro}
1316 % \end{macro}
1317 % \end{macro}
1318 % \end{macro}
1319 % \end{macro}
1320 % \end{macro}
1321 %
1322 % \begin{macro}{\@@_parse_comparator:}
1323 % \begin{macro}{\@@_parse_comparator_aux:Nw}
1324 % A comparator has to be the very first token in the input. A such, the
1325 % test for this can be very fast: grab the first token, do a check and
1326 % if appropriate store the result.
1327 % \begin{macrocode}
1328 \cs_new_protected:Npn \@@_parse_comparator:
1329 {
1330 \exp_after:wN \@@_parse_comparator_aux:Nw
1331 \l_@@_arg_tl \q_stop
1332 }
1333 \cs_new_protected:Npn \@@_parse_comparator_aux:Nw #1#2 \q_stop
1334 {
1335 \tl_if_in:NnTF \l_siunitx_number_input_comparator_tl {#1}
1336 {
1337 \tl_set:Nn \l_@@_comparator_tl {#1}
1338 \tl_set:Nn \l_@@_arg_tl {#2}
1339 }
1340 { \tl_clear:N \l_@@_comparator_tl }
1341 \tl_if_empty:NF \l_@@_arg_tl
1342 { \@@_parse_sign: }
1343 }
1344 % \end{macrocode}
1345 % \end{macro}
1346 % \end{macro}
1347 %
1348 % \begin{macro}{\@@_parse_exponent:}
1349 % \begin{macro}{\@@_parse_exponent_auxi:w}
1350 % \begin{macro}{\@@_parse_exponent_auxii:nn}
1351 % \begin{macro}{\@@_parse_exponent_auxiii:Nw}
1352 % \begin{macro}{\@@_parse_exponent_auxiv:nn}
1353 % \begin{macro}
1354 % {\@@_parse_exponent_zero_test:N, \@@_parse_exponent_check:N}
1355 % \begin{macro}{\@@_parse_exponent_cleanup:N}
1356 % An exponent part of a number has to come at the end and can only occur
1357 % once. Thus it is relatively easy to parse. First, there is a check that
1358 % an exponent part is allowed, and if so a split is made (the previous
1359 % part of the chain checks that there is some content in \cs{l_@@_arg_tl}
1360 % before calling this function). After splitting, if there is no exponent
1361 % then simply save a default. Otherwise, check for a sign and then store
1362 % either this or an implicit plus, and the digits after a check that nothing
1363 % else is present after the~|e|. The only slight complication to all of
1364 % this is allowing an arbitrary token in the input to represent the exponent:
1365 % this is done by setting any exponent tokens to the first of the allowed
1366 % list, then using that in a delimited argument set up. Once an exponent
1367 % part is found, there is a loop to check that each of the tokens is a digit
1368 % then a tidy up step to remove any leading zeros.
1369 % \begin{macrocode}
1370 \cs_new_protected:Npn \@@_parse_exponent:
1371 {
1372 \tl_if_empty:NTF \l_siunitx_number_input_exponent_tl
1373 {
1374 \tl_set:Nn \l_@@_exponent_tl { { } { } }
1375 \tl_if_empty:NF \l_@@_parsed_tl
1376 { \@@_parse_loop: }
1377 }
1378 {
1379 \tl_set:Ne \l_@@_tmp_tl
1380 { \tl_head:V \l_siunitx_number_input_exponent_tl }
1381 \tl_map_inline:Nn \l_siunitx_number_input_exponent_tl
1382 {
1383 \tl_replace_all:NnV \l_@@_arg_tl
1384 {##1} \l_@@_tmp_tl
1385 }
1386 \use:e
1387 {
1388 \cs_set_protected:Npn
1389 \exp_not:N \@@_parse_exponent_auxi:w
1390 ##1 \exp_not:V \l_@@_tmp_tl
1391 ##2 \exp_not:V \l_@@_tmp_tl
1392 ##3 \exp_not:N \q_stop
1393 }
1394 { \@@_parse_exponent_auxii:nn {##1} {##2} }
1395 \use:e
1396 {
1397 \@@_parse_exponent_auxi:w
1398 \exp_not:V \l_@@_arg_tl
1399 \exp_not:V \l_@@_tmp_tl \exp_not:N \q_nil
1400 \exp_not:V \l_@@_tmp_tl \exp_not:N \q_stop
1401 }
1402 }
1403 }
1404 \cs_new_protected:Npn \@@_parse_exponent_auxi:w { }
1405 \cs_new_protected:Npn \@@_parse_exponent_auxii:nn #1#2
1406 {
1407 \quark_if_nil:nTF {#2}
1408 { \tl_set:Nn \l_@@_exponent_tl { { } { } } }
1409 {
1410 \tl_set:Nn \l_@@_arg_tl {#1}
1411 \tl_if_blank:nTF {#2}
1412 { \tl_clear:N \l_@@_parsed_tl }
1413 { \@@_parse_exponent_auxiii:Nw #2 \q_stop }
1414 }
1415 \tl_if_empty:NF \l_@@_parsed_tl
1416 { \@@_parse_loop: }
1417 }
1418 \cs_new_protected:Npn \@@_parse_exponent_auxiii:Nw #1#2 \q_stop
1419 {
1420 \tl_if_in:NnTF \l_siunitx_number_input_sign_tl {#1}
1421 { \@@_parse_exponent_auxiv:nn {#1} {#2} }
1422 { \@@_parse_exponent_auxiv:nn { } {#1#2} }
1423 \tl_if_empty:NT \l_@@_exponent_tl
1424 { \tl_clear:N \l_@@_parsed_tl }
1425 }
1426 \cs_new_protected:Npn \@@_parse_exponent_auxiv:nn #1#2
1427 {
1428 \bool_lazy_or:nnTF
1429 { \l_@@_explicit_plus_bool }
1430 { ! \str_if_eq_p:nn {#1} { + } }
1431 { \tl_set:Nn \l_@@_exponent_tl { {#1} } }
1432 { \tl_set:Nn \l_@@_exponent_tl { { } } }
1433 \tl_if_blank:nTF {#2}
1434 { \tl_clear:N \l_@@_parsed_tl }
1435 {
1436 \@@_parse_exponent_zero_test:N #2
1437 \q_recursion_tail \q_recursion_stop
1438 }
1439 }
1440 \cs_new_protected:Npn \@@_parse_exponent_zero_test:N #1
1441 {
1442 \quark_if_recursion_tail_stop_do:Nn #1
1443 { \tl_set:Nn \l_@@_exponent_tl { { } 0 } }
1444 \str_if_eq:nnTF {#1} { 0 }
1445 { \@@_parse_exponent_zero_test:N }
1446 { \@@_parse_exponent_check:N #1 }
1447 }
1448 \cs_new_protected:Npn \@@_parse_exponent_check:N #1
1449 {
1450 \quark_if_recursion_tail_stop:N #1
1451 \tl_if_in:NnTF \l_@@_input_digit_tl {#1}
1452 {
1453 \tl_put_right:Nn \l_@@_exponent_tl {#1}
1454 \@@_parse_exponent_check:N
1455 }
1456 { \@@_parse_exponent_cleanup:wN }
1457 }
1458 \cs_new_protected:Npn \@@_parse_exponent_cleanup:wN
1459 #1 \q_recursion_stop
1460 { \tl_clear:N \l_@@_parsed_tl }
1461 % \end{macrocode}
1462 % \end{macro}
1463 % \end{macro}
1464 % \end{macro}
1465 % \end{macro}
1466 % \end{macro}
1467 % \end{macro}
1468 % \end{macro}
1469 %
1470 % \begin{macro}{\@@_parse_finalise:}
1471 % \begin{macro}[EXP]{\@@_parse_finalise:nnnn}
1472 % \begin{macro}[EXP]{\@@_parse_finalise:nw}
1473 % Combine all of the bits of a number together, dealing with negative zero
1474 % if required.
1475 % \begin{macrocode}
1476 \cs_new_protected:Npn \@@_parse_finalise:
1477 {
1478 \tl_if_empty:NF \l_@@_parsed_tl
1479 {
1480 \tl_set:Ne \l_@@_parsed_tl
1481 {
1482 { \exp_not:V \l_@@_comparator_tl }
1483 \exp_after:wN \@@_parse_finalise:nnnn \l_@@_parsed_tl
1484 \exp_after:wN \@@_parse_finalise:nw
1485 \l_@@_exponent_tl \q_stop
1486 }
1487 }
1488 }
1489 \cs_new:Npn \@@_parse_finalise:nnnn #1#2#3#4
1490 {
1491 \bool_lazy_all:nTF
1492 {
1493 { ! \l_@@_negative_zero_bool }
1494 { \str_if_eq_p:nn {#1} { - } }
1495 { ! \tl_if_blank_p:n {#2#3} }
1496 {
1497 \str_if_eq_p:ee
1498 { \exp_not:n {#2#3} }
1499 { \prg_replicate:nn { \tl_count:n {#2#3} } { 0 } }
1500 }
1501 }
1502 { \exp_not:n { { } {#2} {#3} {#4} } }
1503 { \exp_not:n { {#1} {#2} {#3} {#4} } }
1504 }
1505 \cs_new:Npn \@@_parse_finalise:nw #1#2 \q_stop
1506 {
1507 { \exp_not:n {#1} }
1508 { \exp_not:n {#2} }
1509 }
1510 % \end{macrocode}
1511 % \end{macro}
1512 % \end{macro}
1513 % \end{macro}
1514 %
1515 % \begin{macro}{\@@_parse_loop:}
1516 % \begin{macro}{\@@_parse_loop_first:NNN}
1517 % \begin{macro}{\@@_parse_loop_main:NNNN}
1518 % \begin{macro}{\@@_parse_loop_main_end:NN}
1519 % \begin{macro}{\@@_parse_loop_main_digit:NNNN}
1520 % \begin{macro}{\@@_parse_loop_main_decimal:N}
1521 % \begin{macro}{\@@_parse_loop_main_uncert:NN}
1522 % \begin{macro}{\@@_parse_loop_main_sign:NNN}
1523 % \begin{macro}{\@@_parse_loop_main_store:NNN}
1524 % \begin{macro}{\@@_parse_loop_after_decimal:NN}
1525 % \begin{macro}{\@@_parse_loop_break:w}
1526 % At this stage, the partial input \cs{l_@@_arg_tl} will contain any
1527 % mantissa, which may contain one or more uncertainties. Parsing this
1528 % and allowing for all of the different formats possible is best done using
1529 % a token-by-token approach. However, as at each stage only a subset of
1530 % tokens are valid, the approach take is to use a set of semi-dedicated
1531 % functions to parse different components along with switches to allow a
1532 % sensible amount of code sharing.
1533 % \begin{macrocode}
1534 \cs_new_protected:Npn \@@_parse_loop:
1535 {
1536 \tl_clear:N \l_@@_partial_tl
1537 \exp_after:wN \@@_parse_loop_first:NNN
1538 \exp_after:wN \l_@@_parsed_tl \exp_after:wN \c_true_bool
1539 \l_@@_arg_tl
1540 \q_recursion_tail \q_recursion_stop
1541 }
1542 % \end{macrocode}
1543 % The very first token of the input is handled with a dedicated function.
1544 % Valid cases here are
1545 % \begin{itemize}
1546 % \item Entirely blank if the original input was for example |+e10|:
1547 % simply clean up if in the integer part or issue an error if in
1548 % a second part (uncertainty).
1549 % \item An integer part digit: pass through to the main collection
1550 % routine.
1551 % \item A decimal marker: store an empty integer part and move to
1552 % the main collection routine for a decimal part.
1553 % \end{itemize}
1554 % Anything else is invalid and sends the code to the abort function.
1555 % \begin{macrocode}
1556 \cs_new_protected:Npn \@@_parse_loop_first:NNN #1#2#3
1557 {
1558 \quark_if_recursion_tail_stop_do:Nn #3
1559 {
1560 \bool_if:NTF #2
1561 { \tl_put_right:Nn #1 { { } { } { } } }
1562 { \@@_parse_loop_break:w \q_recursion_stop }
1563 }
1564 \tl_if_in:NnTF \l_@@_input_digit_tl {#3}
1565 {
1566 \@@_parse_loop_main:NNNN
1567 #1 \c_true_bool \c_false_bool #3
1568 }
1569 {
1570 \tl_if_in:NnTF \l_siunitx_number_input_decimal_tl {#3}
1571 {
1572 \tl_put_right:Nn #1 { { 0 } }
1573 \@@_parse_loop_after_decimal:NN #1
1574 }
1575 { \@@_parse_loop_break:w }
1576 }
1577 }
1578 % \end{macrocode}
1579 % A single function is used to cover the \enquote{main} part of numbers:
1580 % finding the main or separated uncertainty parts and covering both
1581 % the integer and decimal components. This works because these elements
1582 % share a lot of concepts: a small number of switches can be used to
1583 % differentiate between them. To keep the code at least somewhat readable,
1584 % this main function deals with the validity testing but hands off other
1585 % tasks to dedicated auxiliaries for each case.
1586 %
1587 % The possibilities are
1588 % \begin{itemize}
1589 % \item The number terminates, meaning that some digits were collected
1590 % and everything is simply tidied up (as far as the loop is concerned).
1591 % \item A digit is found: this is the common case and leads to a storage
1592 % auxiliary (which handles non-significant zeros).
1593 % \item A decimal marker is found: only valid in the integer part and
1594 % there leading to a store-and-switch situation.
1595 % \item An open-uncertainty token: switch to the dedicated collector
1596 % for uncertainties.
1597 % \item A sign token: stop collecting this number and
1598 % restart collection for the next part.
1599 % \end{itemize}
1600 % \begin{macrocode}
1601 \cs_new_protected:Npn \@@_parse_loop_main:NNNN #1#2#3#4
1602 {
1603 \quark_if_recursion_tail_stop_do:Nn #4
1604 { \@@_parse_loop_main_end:NN #1#2 }
1605 \tl_if_in:NnTF \l_@@_input_digit_tl {#4}
1606 { \@@_parse_loop_main_digit:NNNN #1#2#3#4 }
1607 {
1608 \tl_if_in:NnTF \l_siunitx_number_input_decimal_tl {#4}
1609 {
1610 \bool_if:NTF #2
1611 { \@@_parse_loop_main_decimal:N #1 }
1612 { \@@_parse_loop_break:w }
1613 }
1614 {
1615 \tl_if_in:NnTF \l_@@_input_uncert_open_tl {#4}
1616 { \@@_parse_loop_main_uncert:NN #1 #2 }
1617 {
1618 \tl_if_in:NnTF \l_@@_input_uncert_sign_tl {#4}
1619 {
1620 \@@_parse_loop_main_sign:NNN
1621 #1 #2 #4
1622 }
1623 { \@@_parse_loop_break:w }
1624 }
1625 }
1626 }
1627 }
1628 % \end{macrocode}
1629 % If the main loop finds the end marker then there is a tidy up phase.
1630 % The current partial number is stored either as the integer or decimal,
1631 % depending on the setting for the indicator switch. For the integer
1632 % part, if no number has been collected then one or more non-significant
1633 % zeros have been dropped. Exactly one zero is therefore needed to make
1634 % sure the parsed result is correct. On the other hand, if the decimal
1635 % is empty \emph{and} we are asked to track the presence of a marker,
1636 % we store the indicator value \cs{empty}.
1637 % \begin{macrocode}
1638 \cs_new_protected:Npn \@@_parse_loop_main_end:NN #1#2
1639 {
1640 \tl_if_empty:NT \l_@@_partial_tl
1641 {
1642 \bool_if:NTF #2
1643 { \tl_set:Nn \l_@@_partial_tl { 0 } }
1644 {
1645 \bool_if:NT \l_@@_explicit_decimal_bool
1646 { \tl_set:Nn \l_@@_partial_tl { \empty } }
1647 }
1648 }
1649 \tl_put_right:Ne #1
1650 {
1651 { \exp_not:V \l_@@_partial_tl }
1652 \bool_if:NT #2 { { } }
1653 { }
1654 }
1655 }
1656 % \end{macrocode}
1657 % The most common case for the main loop collector is to find a digit.
1658 % Here, in the integer part it is possible that zeros are non-significant:
1659 % that is handled using a combination of a switch and a string test. Other
1660 % than that, the situation here is simple: store the input and loop.
1661 % \begin{macrocode}
1662 \cs_new_protected:Npn \@@_parse_loop_main_digit:NNNN #1#2#3#4
1663 {
1664 \bool_lazy_or:nnTF
1665 {#3} { ! \str_if_eq_p:nn {#4} { 0 } }
1666 {
1667 \tl_put_right:Nn \l_@@_partial_tl {#4}
1668 \@@_parse_loop_main:NNNN #1 #2 \c_true_bool
1669 }
1670 { \@@_parse_loop_main:NNNN #1 #2 \c_false_bool }
1671 }
1672 % \end{macrocode}
1673 % When a decimal marker was found, move the integer part to the
1674 % store and then go back to the loop with the flags set correctly.
1675 % There is the case of non-significant zeros to cover before that, of course.
1676 % \begin{macrocode}
1677 \cs_new_protected:Npn \@@_parse_loop_main_decimal:N #1
1678 {
1679 \@@_parse_loop_main_store:NNN #1 \c_false_bool \c_false_bool
1680 \@@_parse_loop_after_decimal:NN #1
1681 }
1682 % \end{macrocode}
1683 % Starting an uncertainty part means storing the number to date as in other
1684 % cases, with the possibility of a blank decimal part allowed for. The
1685 % uncertainty itself is collected by a dedicated function as it is extremely
1686 % restricted.
1687 % \begin{macrocode}
1688 \cs_new_protected:Npn \@@_parse_loop_main_uncert:NN #1#2
1689 {
1690 \@@_parse_loop_main_store:NNN #1 #2 \c_false_bool
1691 \@@_parse_uncert:N
1692 }
1693 % \end{macrocode}
1694 % If a sign is found, terminate the current number, store the sign as the
1695 % first token of the second part and go back to do the dedicated first-token
1696 % function.
1697 % \begin{macrocode}
1698 \cs_new_protected:Npn \@@_parse_loop_main_sign:NNN #1#2#3
1699 {
1700 \@@_parse_loop_main_store:NNN #1 #2 \c_true_bool
1701 \tl_put_right:NV \l_@@_uncert_tl \l_@@_flex_tl
1702 \tl_set:Nn \l_@@_flex_tl { {#3} }
1703 \@@_parse_loop_first:NNN
1704 \l_@@_flex_tl \c_false_bool
1705 }
1706 % \end{macrocode}
1707 % A common auxiliary for the various non-digit token functions: tidy up the
1708 % integer and decimal parts of a number. Here, the two flags are used to
1709 % indicate if empty decimal and uncertainty parts should be included in
1710 % the storage cycle.
1711 % \begin{macrocode}
1712 \cs_new_protected:Npn \@@_parse_loop_main_store:NNN #1#2#3
1713 {
1714 \tl_put_right:Ne #1
1715 {
1716 {
1717 \tl_if_empty:NTF \l_@@_partial_tl
1718 { 0 }
1719 { \exp_not:V \l_@@_partial_tl }
1720 }
1721 \bool_if:NT #2 { { } }
1722 \bool_if:NT #3 { { } }
1723 }
1724 \tl_clear:N \l_@@_partial_tl
1725 }
1726 % \end{macrocode}
1727 % After a decimal marker there has to be a digit if there wasn't one before
1728 % it. That is handled by using a dedicated function, which checks for
1729 % an empty integer part first then either simply hands off or looks for
1730 % a digit.
1731 % \begin{macrocode}
1732 \cs_new_protected:Npn \@@_parse_loop_after_decimal:NN #1#2
1733 {
1734 \tl_if_blank:eTF { \exp_after:wN \use_none:n #1 }
1735 {
1736 \quark_if_recursion_tail_stop_do:Nn #2
1737 { \@@_parse_loop_break:w \q_recursion_stop }
1738 \tl_if_in:NnTF \l_@@_input_digit_tl {#1}
1739 {
1740 \tl_put_right:Nn \l_@@_partial_tl {#2}
1741 \@@_parse_loop_main:NNNN
1742 #1 \c_false_bool \c_true_bool
1743 }
1744 { \@@_parse_loop_break:w }
1745 }
1746 {
1747 \@@_parse_loop_main:NNNN
1748 #1 \c_false_bool \c_true_bool #2
1749 }
1750 }
1751 % \end{macrocode}
1752 % Something is not right: remove all of the remaining tokens from the
1753 % number and clear the storage areas as a signal for the next part of the
1754 % code.
1755 % \begin{macrocode}
1756 \cs_new_protected:Npn \@@_parse_loop_break:w
1757 #1 \q_recursion_stop
1758 {
1759 \tl_clear:N \l_@@_flex_tl
1760 \tl_clear:N \l_@@_parsed_tl
1761 \tl_clear:N \l_@@_uncert_tl
1762 }
1763 % \end{macrocode}
1764 % \end{macro}
1765 % \end{macro}
1766 % \end{macro}
1767 % \end{macro}
1768 % \end{macro}
1769 % \end{macro}
1770 % \end{macro}
1771 % \end{macro}
1772 % \end{macro}
1773 % \end{macro}
1774 % \end{macro}
1775 %
1776 % \begin{macro}{\@@_parse_sign:}
1777 % \begin{macro}{\@@_parse_sign_aux:Nw}
1778 % The first token of a number after a comparator could be a sign. A quick
1779 % check is made and if found stored. For the number to be valid it has to be
1780 % more than just a sign, so the next part of the chain is only called if that
1781 % is the case.
1782 % \begin{macrocode}
1783 \cs_new_protected:Npn \@@_parse_sign:
1784 {
1785 \exp_after:wN \@@_parse_sign_aux:Nw
1786 \l_@@_arg_tl \q_stop
1787 }
1788 \cs_new_protected:Npn \@@_parse_sign_aux:Nw #1#2 \q_stop
1789 {
1790 \tl_if_in:NnTF \l_siunitx_number_input_sign_tl {#1}
1791 {
1792 \tl_set:Nn \l_@@_arg_tl {#2}
1793 \bool_lazy_and:nnTF
1794 { \token_if_eq_charcode_p:NN #1 + }
1795 { ! \l_@@_explicit_plus_bool }
1796 { \tl_set:Nn \l_@@_parsed_tl { { } } }
1797 { \tl_set:Nn \l_@@_parsed_tl { {#1} } }
1798 }
1799 { \tl_set:Nn \l_@@_parsed_tl { { } } }
1800 \tl_if_empty:NTF \l_@@_arg_tl
1801 { \tl_clear:N \l_@@_parsed_tl }
1802 { \@@_parse_exponent: }
1803 }
1804 % \end{macrocode}
1805 % \end{macro}
1806 % \end{macro}
1807 %
1808 % \begin{macro}{\@@_parse_uncert:N}
1809 % \begin{macro}{\@@_parse_uncert:NNN}
1810 % \begin{macro}
1811 % {
1812 % \@@_parse_uncert_auxi:N ,
1813 % \@@_parse_uncert_auxii:N ,
1814 % \@@_parse_uncert_auxiv:N
1815 % }
1816 % \begin{macro}{\@@_parse_uncert_auxiv:}
1817 % \begin{macro}{\@@_parse_uncert_marker:}
1818 % \begin{macro}{\@@_parse_uncert_marker:nnn}
1819 % \begin{macro}{\@@_parse_uncert_marker:nnnw}
1820 % \begin{macro}{\@@_parse_uncert_marker:Nnnn}
1821 % \begin{macro}{\@@_parse_uncert_marker:w}
1822 % \begin{macro}{\@@_parse_uncert_marker:nnnnN}
1823 % \begin{macro}{\@@_parse_uncert_marker:nnnnnN}
1824 % \begin{macro}
1825 % {
1826 % \@@_parse_uncert_marker:nnnnnnN ,
1827 % \@@_parse_uncert_marker:ennnnnN
1828 % }
1829 % \begin{macro}{\@@_parse_uncert_after:N}
1830 % Parsing a combined uncertainty has a very restricted range of allowed
1831 % tokens. A closing uncertainty token in the first place is an error,
1832 % so we filter that out explicitly. After that, we check for digits,
1833 % which require checking for significant digits. The non-digit function
1834 % is separate to make the flow clearer.
1835 % \begin{macrocode}
1836 \cs_new_protected:Npn \@@_parse_uncert:N #1
1837 {
1838 \quark_if_recursion_tail_stop_do:Nn #1
1839 { \@@_parse_loop_break:w \q_recursion_stop }
1840 \tl_if_in:NnTF \l_@@_input_uncert_close_tl {#1}
1841 { \@@_parse_loop_break:w }
1842 {
1843 \@@_parse_uncert:NNN
1844 \c_false_bool \@@_parse_uncert_auxi:N #1
1845 }
1846 }
1847 % \end{macrocode}
1848 % Deal with digits: a simple question of whether they are significant.
1849 % \begin{macrocode}
1850 \cs_new_protected:Npn \@@_parse_uncert:NNN #1#2#3
1851 {
1852 \quark_if_recursion_tail_stop_do:Nn #3
1853 { \@@_parse_loop_break:w \q_recursion_stop }
1854 \tl_if_in:NnTF \l_@@_input_digit_tl {#3}
1855 {
1856 \bool_lazy_or:nnTF
1857 {#1} { ! \str_if_eq_p:nn {#3} { 0 } }
1858 {
1859 \tl_put_right:Nn \l_@@_partial_tl {#3}
1860 \@@_parse_uncert:NNN \c_true_bool #2
1861 }
1862 { \@@_parse_uncert:NNN \c_false_bool #2 }
1863 }
1864 { #2 #3 }
1865 }
1866 % \end{macrocode}
1867 % For the two auxiliaries, the difference is the handling of a
1868 % decimal marker: one may be present, but only exactly one.
1869 % \begin{macrocode}
1870 \cs_new_protected:Npn \@@_parse_uncert_auxi:N #1
1871 {
1872 \tl_if_in:NnTF \l_@@_input_uncert_close_tl {#1}
1873 {
1874 \@@_parse_uncert_auxiii:
1875 \@@_parse_uncert_after:N
1876 }
1877 {
1878 \tl_if_in:NnTF \l_siunitx_number_input_decimal_tl {#1}
1879 { \@@_parse_uncert_marker: }
1880 { \@@_parse_uncert_auxiv:N #1 }
1881 }
1882 }
1883 \cs_new_protected:Npn \@@_parse_uncert_auxii:N #1
1884 {
1885 \tl_if_in:NnTF \l_@@_input_uncert_close_tl {#1}
1886 {
1887 \@@_parse_uncert_auxiii:
1888 \@@_parse_uncert_after:N
1889 }
1890 { \@@_parse_uncert_auxiv:N #1 }
1891 }
1892 % \end{macrocode}
1893 % Deal with the closing bracket, which might leave us with nothing if there
1894 % were no significant digits.
1895 % \begin{macrocode}
1896 \cs_new_protected:Npn \@@_parse_uncert_auxiii:
1897 {
1898 \tl_if_empty:NT \l_@@_partial_tl
1899 {
1900 \bool_lazy_or:nnT
1901 { \l_@@_zero_uncert_bool }
1902 {
1903 ! \bool_lazy_or_p:nn
1904 { \tl_if_empty_p:N \l_@@_tolerance_tl }
1905 { \str_if_eq_p:Vn \l_@@_tolerance_tl { 0 } }
1906 }
1907 { \tl_set:Nn \l_@@_partial_tl { 0 } }
1908 }
1909 }
1910 % \end{macrocode}
1911 % A common auxiliary for the case where the token is not a digit, a
1912 % decimal mark or a closing parenthesis.
1913 % \begin{macrocode}
1914 \cs_new_protected:Npn \@@_parse_uncert_auxiv:N #1
1915 {
1916 \tl_if_empty:NTF \l_@@_tolerance_tl
1917 {
1918 \tl_if_in:NnTF \l_@@_input_uncert_divide_tl {#1}
1919 {
1920 \tl_if_empty:NT \l_@@_partial_tl
1921 { \tl_set:Nn \l_@@_partial_tl { 0 } }
1922 \tl_set_eq:NN \l_@@_tolerance_tl \l_@@_partial_tl
1923 \tl_clear:N \l_@@_partial_tl
1924 \@@_parse_uncert:N
1925 }
1926 { \@@_parse_loop_break:w }
1927 }
1928 { \@@_parse_loop_break:w }
1929 }
1930 % \end{macrocode}
1931 % Handling a decimal marker in the uncertainty is a bit tricky: we need to
1932 % make sure it's valid. First, we need to be sure that the integer part of
1933 % the captured uncertainty is not too long. Then we need to check that the
1934 % decimal part is not too long, and extend the decimal if it is. Both of
1935 % these require data from the collected partial number, so we extract that
1936 % first. Checking the decimal part needs the length of the not-yet-collected
1937 % uncertainty. There may be one or more parts to that, so we have to grab up
1938 % to the \emph{first} closing token to count the length: just grabbing
1939 % everything only works if there is only a single uncertainty. We take
1940 % protective steps against an entirely-missing closing token so that there is
1941 % not an uncontrolled error.
1942 % \begin{macrocode}
1943 \cs_new_protected:Npn \@@_parse_uncert_marker:
1944 {
1945 \exp_after:wN \@@_parse_uncert_marker:nnn
1946 \l_@@_parsed_tl
1947 }
1948 \cs_new_protected:Npn \@@_parse_uncert_marker:nnn #1#2#3
1949 {
1950 \int_compare:nNnTF
1951 { \tl_count:N \l_@@_partial_tl } > { \tl_count:n {#2} }
1952 { \@@_parse_loop_break:w }
1953 { \@@_parse_uncert_marker:nnnw {#1} {#2} {#3} }
1954 }
1955 \cs_new_protected:Npn \@@_parse_uncert_marker:nnnw
1956 #1#2#3#4 \q_recursion_tail \q_recursion_stop
1957 {
1958 \tl_if_in:nVTF {#4} \l_@@_input_uncert_close_tl
1959 {
1960 \exp_after:wN \@@_parse_uncert_marker:Nnnn
1961 \l_@@_input_uncert_close_tl
1962 {#1} {#2} {#3}
1963 }
1964 { \@@_parse_loop_break:w }
1965 #4 \q_recursion_tail \q_recursion_stop
1966 }
1967 \cs_new_protected:Npn \@@_parse_uncert_marker:Nnnn
1968 #1#2#3#4
1969 {
1970 \cs_set_protected:Npn \@@_parse_uncert_marker:w ##1 #1
1971 { \@@_parse_uncert_marker:nnnnN {#2} {#3} {#4} {##1} #1 }
1972 \@@_parse_uncert_marker:w
1973 }
1974 \cs_new_protected:Npn \@@_parse_uncert_marker:w { }
1975 % \end{macrocode}
1976 % With the separated out main part and the current uncertainty part
1977 % available, we can pad the main part if required then restart the
1978 % collection of the uncertainty having thrown away the decimal marker
1979 % and with the lengths lined up. To account for asymmetrical
1980 % uncertainties, there is a bit of work in tracking.
1981 % \begin{macrocode}
1982 \cs_new_protected:Npn \@@_parse_uncert_marker:nnnnN #1#2#3#4#5
1983 {
1984 \tl_set:Ne \l_@@_tmp_tl { \tl_head:V \l_@@_input_uncert_divide_tl }
1985 \tl_if_empty:NTF \l_@@_tmp_tl
1986 { \@@_parse_uncert_marker:nnnnnN {#1} {#2} {#3} {#4} { } #5 }
1987 {
1988 \use:e
1989 {
1990 \cs_set_protected:Npn \exp_not:N \@@_parse_uncert_marker:w
1991 ##1 \l_@@_tmp_tl ##2 \l_@@_tmp_tl ##3 \exp_not:N \q_stop
1992 {
1993 \exp_not:N \tl_if_blank:nTF {##2}
1994 {
1995 \@@_parse_uncert_marker:nnnnnN
1996 \exp_not:n { {#1} {#2} {#3} }
1997 {##1} { }
1998 }
1999 {
2000 \@@_parse_uncert_marker:nnnnnN
2001 \exp_not:n { {#1} {#2} {#3} }
2002 {##1} { \l_@@_tmp_tl ##2 }
2003 }
2004 \exp_not:N #5
2005 }
2006 \exp_not:N \@@_parse_uncert_marker:w #4
2007 \l_@@_tmp_tl \l_@@_tmp_tl \exp_not:N \q_stop
2008 }
2009 }
2010 }
2011 \cs_new_protected:Npn \@@_parse_uncert_marker:nnnnnN #1#2#3#4#5#6
2012 {
2013 \@@_parse_uncert_marker:ennnnnN
2014 { \int_eval:n { \tl_count:n {#4} } }
2015 {#1} {#2} {#3} {#4} {#5} #6
2016 }
2017 \cs_new_protected:Npn \@@_parse_uncert_marker:nnnnnnN #1#2#3#4#5#6#7
2018 {
2019 \bool_lazy_and:nnF
2020 { \tl_if_empty_p:N \l_@@_tolerance_tl }
2021 { \tl_if_empty_p:N \l_@@_uncert_types_tl }
2022 {
2023 \int_compare:nNnF {#1} = \l_@@_uncert_offset_int
2024 { \@@_parse_loop_break:w }
2025 }
2026 \int_set:Nn \l_@@_uncert_offset_int {#1}
2027 \tl_set:Ne \l_@@_parsed_tl
2028 {
2029 {#2}
2030 {#3}
2031 {
2032 #4
2033 \prg_replicate:nn
2034 { \int_max:nn { 0 } { #1 - \tl_count:n {#4} } }
2035 { 0 }
2036 }
2037 }
2038 \tl_if_empty:NTF \l_@@_partial_tl
2039 {
2040 \@@_parse_uncert:NNN \c_false_bool
2041 \@@_parse_uncert_auxii:N
2042 }
2043 {
2044 \@@_parse_uncert:NNN \c_true_bool
2045 \@@_parse_uncert_auxii:N
2046 }
2047 #5#6#7
2048 }
2049 \cs_generate_variant:Nn \@@_parse_uncert_marker:nnnnnnN { e }
2050 % \end{macrocode}
2051 % At the end of collection, we can either start another one or be done:
2052 % either way we move the data around.
2053 % \begin{macrocode}
2054 \cs_new_protected:Npn \@@_parse_uncert_after:N #1
2055 {
2056 \tl_set:Ne \l_@@_uncert_tl
2057 {
2058 \exp_not:V \l_@@_uncert_tl
2059 \tl_if_empty:NF \l_@@_partial_tl
2060 {
2061 {
2062 \tl_if_empty:NTF \l_@@_tolerance_tl
2063 { \exp_not:V \l_@@_partial_tl }
2064 {
2065 { \exp_not:V \l_@@_tolerance_tl }
2066 { \exp_not:V \l_@@_partial_tl }
2067 }
2068 }
2069 }
2070 }
2071 \tl_set:Ne \l_@@_uncert_types_tl
2072 {
2073 \l_@@_uncert_types_tl
2074 \tl_if_empty:NTF \l_@@_tolerance_tl
2075 { S }
2076 { A }
2077 }
2078 \tl_clear:N \l_@@_partial_tl
2079 \tl_clear:N \l_@@_tolerance_tl
2080 \quark_if_recursion_tail_stop_do:Nn #1
2081 {
2082 \tl_set:Ne \l_@@_parsed_tl
2083 {
2084 \exp_not:V \l_@@_parsed_tl
2085 {
2086 \tl_if_empty:NF \l_@@_uncert_tl
2087 {
2088 { \l_@@_uncert_types_tl }
2089 \exp_not:V \l_@@_uncert_tl
2090 }
2091 }
2092 }
2093 \tl_clear:N \l_@@_partial_tl
2094 \tl_clear:N \l_@@_uncert_tl
2095 \int_zero:N \l_@@_uncert_offset_int
2096 }
2097 \tl_if_in:NnTF \l_@@_input_uncert_open_tl {#1}
2098 { \@@_parse_uncert:N }
2099 { \@@_parse_loop_break:w }
2100 }
2101 % \end{macrocode}
2102 % \end{macro}
2103 % \end{macro}
2104 % \end{macro}
2105 % \end{macro}
2106 % \end{macro}
2107 % \end{macro}
2108 % \end{macro}
2109 % \end{macro}
2110 % \end{macro}
2111 % \end{macro}
2112 % \end{macro}
2113 % \end{macro}
2114 % \end{macro}
2115 %
2116 % \subsection{Processing numbers}
2117 %
2118 % \begin{variable}
2119 % {
2120 % \l_@@_drop_exponent_bool ,
2121 % \l_@@_drop_uncertainty_bool ,
2122 % \l_@@_drop_zero_decimal_bool ,
2123 % \l_@@_exponent_mode_str ,
2124 % \l_@@_exponent_fixed_int ,
2125 % \l_@@_min_decimal_int ,
2126 % \l_@@_min_integer_int ,
2127 % \l_@@_round_dir_str ,
2128 % \l_@@_round_half_even_bool ,
2129 % \l_@@_round_mode_str ,
2130 % \l_@@_round_pad_bool ,
2131 % \l_@@_round_precision_int ,
2132 % \l_@@_round_positive_bool ,
2133 % \l_@@_round_uncert_dir_str
2134 % }
2135 % \begin{macrocode}
2136 \keys_define:nn { siunitx }
2137 {
2138 drop-exponent .bool_set:N =
2139 \l_@@_drop_exponent_bool ,
2140 drop-uncertainty .bool_set:N =
2141 \l_@@_drop_uncertainty_bool ,
2142 drop-zero-decimal .bool_set:N =
2143 \l_@@_drop_zero_decimal_bool ,
2144 exponent-mode .choices:nn =
2145 { engineering , fixed , input , scientific , threshold }
2146 { \str_set_eq:NN \l_@@_exponent_mode_str \l_keys_choice_str } ,
2147 exponent-thresholds .code:n =
2148 { \@@_set_thresholds:n {#1} } ,
2149 fixed-exponent .int_set:N =
2150 \l_@@_exponent_fixed_int ,
2151 minimum-decimal-digits .int_set:N =
2152 \l_@@_min_decimal_int ,
2153 minimum-integer-digits .int_set:N =
2154 \l_@@_min_integer_int ,
2155 round-direction .choices:nn =
2156 { down , nearest , up }
2157 { \str_set_eq:NN \l_@@_round_dir_str \l_keys_choice_str } ,
2158 round-half .choice: ,
2159 round-half / even .code:n =
2160 { \bool_set_true:N \l_@@_round_half_even_bool } ,
2161 round-half / up .code:n =
2162 { \bool_set_false:N \l_@@_round_half_even_bool } ,
2163 round-minimum .code:n =
2164 { \@@_set_round_min:n {#1} } ,
2165 round-mode .choices:nn =
2166 { figures , none , places, uncertainty }
2167 { \str_set_eq:NN \l_@@_round_mode_str \l_keys_choice_str } ,
2168 round-pad .bool_set:N =
2169 \l_@@_round_pad_bool ,
2170 round-precision .int_set:N =
2171 \l_@@_round_precision_int ,
2172 round-zero-positive .bool_set:N =
2173 \l_@@_round_positive_bool ,
2174 uncertainty-round-direction .choices:nn =
2175 { down , nearest , up }
2176 { \str_set_eq:NN \l_@@_round_uncert_dir_str \l_keys_choice_str } ,
2177 }
2178 \bool_new:N \l_@@_round_half_even_bool
2179 \str_new:N \l_@@_exponent_mode_str
2180 \str_new:N \l_@@_round_dir_str
2181 \str_new:N \l_@@_round_mode_str
2182 \str_new:N \l_@@_round_uncert_dir_str
2183 % \end{macrocode}
2184 % \end{variable}
2185 %
2186 % \begin{variable}{\l_@@_round_min_tl}
2187 % For storing the minimum for rounding.
2188 % \begin{macrocode}
2189 \tl_new:N \l_@@_round_min_tl
2190 % \end{macrocode}
2191 % \end{variable}
2192 %
2193 % \begin{macro}{\@@_set_round_min:n}
2194 % \begin{macro}{\@@_set_round_min:nnnnnnn}
2195 % For setting the rounding minimum, the aim is to do as much of the work
2196 % now as possible. That's mainly a question of checking if there are any
2197 % significant digits in the mantissa given.
2198 % \begin{macrocode}
2199 \cs_new_protected:Npn \@@_set_round_min:n #1
2200 {
2201 \siunitx_number_parse:nN {#1} \l_@@_tmp_tl
2202 \exp_after:wN \@@_set_round_min:nnnnnnn \l_@@_tmp_tl
2203 }
2204 \cs_new_protected:Npn \@@_set_round_min:nnnnnnn #1#2#3#4#5#6#7
2205 {
2206 \tl_set:Ne \l_@@_round_min_tl
2207 {
2208 \bool_lazy_and:nnF
2209 { \str_if_eq_p:nn {#3} { 0 } }
2210 {
2211 \str_if_eq_p:ee
2212 { \exp_not:n {#4} }
2213 { \prg_replicate:nn { \tl_count:n {#4} } { 0 } }
2214 }
2215 { \exp_not:n { {#3} {#4} } }
2216 }
2217 }
2218 % \end{macrocode}
2219 % \end{macro}
2220 % \end{macro}
2221 %
2222 % \begin{variable}{\l_@@_lower_threshold_int, \l_@@_upper_threshold_int}
2223 % For storing the range for exponents.
2224 % \begin{macrocode}
2225 \int_new:N \l_@@_lower_threshold_int
2226 \int_new:N \l_@@_upper_threshold_int
2227 % \end{macrocode}
2228 % \end{variable}
2229 %
2230 % \begin{macro}{\@@_set_round_thresholds:n}
2231 % \begin{macro}{\@@_set_round_thresholds:w}
2232 % \begin{macro}{\@@_set_round_threshold:nn}
2233 % Split the range, parse each part, set the data structures.
2234 % \begin{macrocode}
2235 \cs_new_protected:Npe \@@_set_thresholds:n #1
2236 {
2237 \exp_not:N \@@_set_thresholds:w
2238 #1 \token_to_str:N : \token_to_str:N : \exp_not:N \q_stop
2239 }
2240 \use:e
2241 {
2242 \cs_new_protected:Npn \exp_not:N \@@_set_thresholds:w
2243 #1 \token_to_str:N : #2 \token_to_str:N : #3 \exp_not:N \q_stop
2244 }
2245 {
2246 \@@_set_threshold:nn { lower } {#1}
2247 \@@_set_threshold:nn { upper } {#2}
2248 }
2249 \cs_new_protected:Npn \@@_set_threshold:nn #1#2
2250 { \int_set:cn { l__@@_ #1 _threshold_int } { 0 #2 } }
2251 % \end{macrocode}
2252 % \end{macro}
2253 % \end{macro}
2254 % \end{macro}
2255 %
2256 % \begin{macro}{\@@_if_threshold_exceeded:nTF}
2257 % \begin{macro}{\@@_if_threshold_exceeded_p:n}
2258 % A short auxiliary.
2259 % \begin{macrocode}
2260 \prg_new_conditional:Npnn \@@_if_threshold_exceeded:n #1 { TF , p }
2261 {
2262 \bool_lazy_and:nnTF
2263 { \int_compare_p:nNn {#1} > \l_@@_lower_threshold_int }
2264 { \int_compare_p:nNn {#1} < \l_@@_upper_threshold_int }
2265 \prg_return_true:
2266 \prg_return_false:
2267 }
2268 % \end{macrocode}
2269 % \end{macro}
2270 % \end{macro}
2271 %
2272 % \begin{macro}{\siunitx_number_process:NN, \siunitx_number_process:cc}
2273 % \begin{macro}{\@@_process:nnnnnnnNN}
2274 % A top-level interface for the processing tools. Rounding happens in all
2275 % cases, but exponents are only processed if the value is not $0$.
2276 % \begin{macrocode}
2277 \cs_new_protected:Npn \siunitx_number_process:NN #1#2
2278 {
2279 \tl_if_empty:NTF #1
2280 { \tl_clear:N #2 }
2281 {
2282 \@@_drop_uncertainty:NN #1 #2
2283 \exp_after:wN \@@_process:nnnnnnnNN #2 #2 #2
2284 \@@_drop_exponent:NN #2 #2
2285 \@@_zero_decimal:NN #2 #2
2286 \@@_digits:NN #2 #2
2287 }
2288 }
2289 \cs_generate_variant:Nn \siunitx_number_process:NN { cc }
2290 \cs_new_protected:Npn \@@_process:nnnnnnnNN #1#2#3#4#5#6#7#8#9
2291 {
2292 \bool_lazy_and:nnTF
2293 { \str_if_eq_p:nn {#3} { 0 } }
2294 {
2295 \str_if_eq_p:ee
2296 { \exp_not:n {#4} } { \prg_replicate:nn { \tl_count:n {#4} } { 0 } }
2297 }
2298 { \@@_round:NN #8 #9 }
2299 {
2300 \@@_exponent:NN #8 #9
2301 \@@_round:NN #9 #9
2302 }
2303 }
2304 % \end{macrocode}
2305 % \end{macro}
2306 % \end{macro}
2307 %
2308 % \begin{macro}{\@@_exponent:NN}
2309 % \begin{macro}[EXP]
2310 % {
2311 % \@@_exponent:nnnnnnn ,
2312 % \@@_exponent_aux:nnnnnnn ,
2313 % \@@_exponent_engineering:nnnnnnn ,
2314 % \@@_exponent_fixed:nnnnnnn ,
2315 % \@@_exponent_scientific:nnnnnnn
2316 % }
2317 % \begin{macro}[EXP]
2318 % {
2319 % \@@_exponent_fixed:nnnnnnnn ,
2320 % \@@_exponent_fixed:ennnnnnn ,
2321 % \@@_exponent_scientific:nnnnnnnn ,
2322 % \@@_exponent_scientific:ennnnnnn
2323 % }
2324 % \begin{macro}[EXP]{\@@_exponent_scientific:nnnw}
2325 % \begin{macro}[EXP]{\@@_exponent_shift:nnn, \@@_exponent_shift:nne}
2326 % \begin{macro}[EXP]{\@@_exponent_shift_down:nnnw}
2327 % \begin{macro}[EXP]{\@@_exponent_shift_down:nnn}
2328 % \begin{macro}[EXP]{\@@_exponent_shift_down:nw}
2329 % \begin{macro}[EXP]{\@@_exponent_shift_up:nnn}
2330 % \begin{macro}[EXP]{\@@_exponent_shift_up:nnw}
2331 % \begin{macro}[EXP]
2332 % {\@@_exponent_shift_up_aux:nnn, \@@_exponent_shift_up_aux:een}
2333 % \begin{macro}[EXP]{\@@_exponent_shift_uncert:nw}
2334 % \begin{macro}[EXP]
2335 % {\@@_exponent_shift_uncert_S:nnnn, \@@_exponent_shift_uncert_S:ennn}
2336 % \begin{macro}[EXP]{\@@_exponent_uncert:n}
2337 % \begin{macro}[EXP]{\@@_exponent_finalise:n}
2338 % \begin{macro}[EXP]{\@@_exponentcd te_engineering_aux:nnnnnnn}
2339 % \begin{macro}[EXP]
2340 % {
2341 % \@@_exponent_engineering_0:nnnn ,
2342 % \@@_exponent_engineering_1:nnnn ,
2343 % \@@_exponent_engineering_2:nnnn
2344 % }
2345 % \begin{macro}[EXP]{\@@_exponent_engineering:nnNw}
2346 % \begin{macro}[EXP]{\@@_exponent_engineering_uncert:nn}
2347 % \begin{macro}[EXP]{\@@_exponent_engineering_uncert_S:nnn}
2348 % \begin{macro}[EXP]
2349 % {\@@_exponent_threshold:nnnnnnn, \@@_exponent_threshold_aux:nnnnnnn}
2350 % \begin{macro}[EXP]{\@@_exponent_threshold:n, \@@_exponent_threshold:e}
2351 % Manipulating an exponent is done using a single expansion function
2352 % \emph{unless} dealing with engineering-style output. The latter is easier
2353 % to handle by first converting to scientific output, then post-processing.
2354 % (Once \texttt{e}-type expansion is generally available, this will be
2355 % handling using a single \cs{tl_set:Ne}.)
2356 % \begin{macrocode}
2357 \cs_new_protected:Npn \@@_exponent:NN #1#2
2358 {
2359 \tl_set:Ne #2
2360 { \exp_after:wN \@@_exponent:nnnnnnn #1 }
2361 \str_if_eq:VnT \l_@@_exponent_mode_str { engineering }
2362 {
2363 \tl_set:Ne #2
2364 { \exp_after:wN \@@_exponent_engineering_aux:nnnnnnn #2 }
2365 }
2366 }
2367 \cs_new:Npn \@@_exponent:nnnnnnn #1#2#3#4#5#6#7
2368 {
2369 \str_if_eq:VnTF \l_@@_exponent_mode_str { input }
2370 { \exp_not:n { {#1} {#2} {#3} {#4} {#5} {#6} {#7} } }
2371 {
2372 \tl_if_blank:nTF {#7}
2373 { \@@_exponent_aux:nnnnnnn {#1} {#2} {#3} {#4} {#5} {#6} { 0 } }
2374 { \@@_exponent_aux:nnnnnnn {#1} {#2} {#3} {#4} {#5} {#6} {#7} }
2375 }
2376 }
2377 \cs_new:Npn \@@_exponent_aux:nnnnnnn #1#2#3#4#5#6#7
2378 {
2379 \use:c { @@_exponent_ \l_@@_exponent_mode_str :nnnnnnn }
2380 {#1} {#2} {#3} {#4} {#5} {#6} {#7}
2381 }
2382 \cs_new:Npn \@@_exponent_fixed:nnnnnnn #1#2#3#4#5#6#7
2383 {
2384 \@@_exponent_fixed:ennnnnnn
2385 { \int_eval:n { \l_@@_exponent_fixed_int - (#6#7) } }
2386 {#1} {#2} {#3} {#4} {#5} {#6} {#7}
2387 }
2388 \cs_new:Npn \@@_exponent_fixed:nnnnnnnn #1#2#3#4#5#6#7#8
2389 {
2390 \exp_not:n { {#2} {#3} }
2391 \@@_exponent_shift:nnn {#1} {#4} {#5}
2392 \@@_exponent_uncert:n {#6}
2393 { \int_compare:nNnT \l_@@_exponent_fixed_int < 0 { - } }
2394 { \int_abs:n \l_@@_exponent_fixed_int }
2395 }
2396 \cs_generate_variant:Nn \@@_exponent_fixed:nnnnnnnn { e }
2397 % \end{macrocode}
2398 % To convert to scientific notation, the key question is to find the number
2399 % of significant places. That is easy enough if the number has a non-zero
2400 % integer component. For a pure decimal, we have to trim off leading
2401 % zeros in a loop.
2402 % \begin{macrocode}
2403 \cs_new:Npn \@@_exponent_scientific:nnnnnnn #1#2#3#4#5#6#7
2404 {
2405 \@@_exponent_scientific:ennnnnnn
2406 { \int_eval:n { \tl_count:n {#3} } }
2407 {#1} {#2} {#3} {#4} {#5} {#6} {#7}
2408 }
2409 \cs_new:Npn \@@_exponent_scientific:nnnnnnnn #1#2#3#4#5#6#7#8
2410 {
2411 \exp_not:n { {#2} {#3} }
2412 \int_compare:nNnTF {#1} = 1
2413 {
2414 \str_if_eq:nnTF {#4} { 0 }
2415 {
2416 \@@_exponent_scientific:nnnw
2417 { 0 } {#6} { #7#8 } #5 \q_stop
2418 }
2419 { \exp_not:n { {#4} {#5} {#6} {#7} {#8} } }
2420 }
2421 {
2422 \@@_exponent_shift:nnn { #1 - 1 } {#4} {#5}
2423 \@@_exponent_uncert:n {#6}
2424 \@@_exponent_finalise:n { #1 + #7#8 - 1 }
2425 }
2426 }
2427 \cs_generate_variant:Nn \@@_exponent_scientific:nnnnnnnn { e }
2428 \cs_new_eq:NN \@@_exponent_engineering:nnnnnnn
2429 \@@_exponent_scientific:nnnnnnn
2430 \cs_new:Npn \@@_exponent_scientific:nnnw #1#2#3#4#5 \q_stop
2431 {
2432 \str_if_eq:nnTF {#4} { 0 }
2433 {
2434 \@@_exponent_scientific:nnnw
2435 { #1 - 1 } {#2} {#3} #5 \q_stop
2436 }
2437 {
2438 \exp_not:n { {#4} {#5} {#2} }
2439 \@@_exponent_finalise:n { #1 + #3 - 1 }
2440 }
2441 }
2442 % \end{macrocode}
2443 % When adjusting the exponent position, there are two paths depending on
2444 % which way the shift takes place.
2445 % \begin{macrocode}
2446 \cs_new:Npn \@@_exponent_shift:nnn #1#2#3
2447 {
2448 \int_compare:nNnTF {#1} > 0
2449 { \@@_exponent_shift_down:nnnw {#1} {#3} { } #2 \q_stop }
2450 {
2451 \int_compare:nNnTF {#1} < 0
2452 { \@@_exponent_shift_up:nnn {#1} {#2} {#3} }
2453 { {#2} {#3} }
2454 }
2455 }
2456 \cs_generate_variant:Nn \@@_exponent_shift:nnn { nne }
2457 % \end{macrocode}
2458 % For shifting the exponent down, there is first a loop to reserve the
2459 % integer part before doing the work: that of course has to be undone
2460 % for any remainder at he end of the process.
2461 % \begin{macrocode}
2462 \cs_new:Npn \@@_exponent_shift_down:nnnw #1#2#3#4#5 \q_stop
2463 {
2464 \tl_if_blank:nTF {#5}
2465 { \@@_exponent_shift_down:nnn {#1} { #4 #3 } {#2} }
2466 { \@@_exponent_shift_down:nnnw {#1} {#2} { #4 #3 } #5 \q_stop }
2467 }
2468 \cs_new:Npn \@@_exponent_shift_down:nnn #1#2#3
2469 {
2470 \int_compare:nNnTF {#1} = 0
2471 { { \tl_reverse:n {#2} } \exp_not:n { {#3} } }
2472 { \@@_exponent_shift_down:nw {#1} #2 \q_stop {#3} }
2473 }
2474 \cs_new:Npn \@@_exponent_shift_down:nw #1#2#3 \q_stop #4
2475 {
2476 \tl_if_blank:nTF {#3}
2477 { \@@_exponent_shift_down:nnn { #1 - 1 } { 0 } { #2#4 } }
2478 { \@@_exponent_shift_down:nnn { #1 - 1 } {#3} { #2#4 } }
2479 }
2480 % \end{macrocode}
2481 % For shifting the exponent up, we can run out of decimal digits, at which
2482 % point filling is easy. Other than that a simple loop as we are picking
2483 % input off the front of the decimal part. We also need to deal with leading
2484 % zeros: these cannot accumulate.
2485 % \begin{macrocode}
2486 \cs_new:Npn \@@_exponent_shift_up:nnn #1#2#3
2487 {
2488 \tl_if_blank:nTF {#3}
2489 {
2490 \@@_exponent_shift_up_aux:een
2491 { \int_eval:n { #1 + 1 } }
2492 { \str_if_eq:nnF {#2} { 0 } {#2} 0 }
2493 { }
2494 \@@_exponent_shift_uncert:nw { 1 }
2495 }
2496 { \@@_exponent_shift_up:nnw {#1} {#2} #3 \q_stop }
2497 }
2498 \cs_new:Npn \@@_exponent_shift_up:nnw #1#2#3#4 \q_stop
2499 {
2500 \@@_exponent_shift_up_aux:een
2501 { \int_eval:n { #1 + 1 } }
2502 { \str_if_eq:nnF {#2} { 0 } {#2} #3 }
2503 {#4}
2504 }
2505 \cs_new:Npn \@@_exponent_shift_up_aux:nnn #1#2#3
2506 {
2507 \int_compare:nNnTF {#1} = 0
2508 { \exp_not:n { {#2} {#3} } }
2509 {
2510 \tl_if_blank:nTF {#3}
2511 {
2512 {
2513 \exp_not:n {#2}
2514 \prg_replicate:nn { \int_abs:n {#1} } { 0 }
2515 }
2516 { }
2517 \@@_exponent_shift_uncert:nw { \int_abs:n {#1} }
2518 }
2519 { \@@_exponent_shift_up:nnn {#1} {#2} {#3} }
2520 }
2521 }
2522 \cs_generate_variant:Nn \@@_exponent_shift_up_aux:nnn { ee }
2523 % \end{macrocode}
2524 % If the shift has put digits into the integer part, we have to adjust the
2525 % uncertainty accordingly. First, we grab the data, then adjust by the
2526 % number of places that have been transferred.
2527 % \begin{macrocode}
2528 \cs_new:Npn \@@_exponent_shift_uncert:nw
2529 #1#2 \@@_exponent_uncert:n #3
2530 {
2531 \tl_if_blank:nTF {#3}
2532 {
2533 #2
2534 \@@_exponent_uncert:n { }
2535 }
2536 {
2537 \str_if_eq:nnTF {#3} { 0 }
2538 {
2539 #2
2540 \@@_exponent_uncert:n { { S } { 0 } }
2541 }
2542 {
2543 \@@_exponent_shift_uncert_S:ennn
2544 { \prg_replicate:nn {#1} { 0 } }
2545 {#2}
2546 #3
2547 }
2548 }
2549 }
2550 \cs_new:Npn \@@_exponent_shift_uncert_S:nnnn #1#2#3#4
2551 {
2552 #2
2553 \@@_exponent_uncert:n { { S } { #4#1 } }
2554 }
2555 \cs_generate_variant:Nn \@@_exponent_shift_uncert_S:nnnn { e }
2556 \cs_new:Npn \@@_exponent_uncert:n #1 { { \exp_not:n {#1} } }
2557 % \end{macrocode}
2558 % Tidy up the exponent to put the sign in the right place.
2559 % \begin{macrocode}
2560 \cs_new:Npn \@@_exponent_finalise:n #1
2561 {
2562 \int_compare:nNnTF {#1} < 0
2563 { { - } }
2564 { { } }
2565 { \int_abs:n {#1} }
2566 }
2567 % \end{macrocode}
2568 % This could (and eventually will) be combined with the main function above:
2569 % that will need \texttt{e}-type expansion. The input has already been
2570 % normalised such that the integer part is in the range $1 \le n < 10$.
2571 % Thus there are only three cases to deal with, depending on the required
2572 % adjustment to the exponent.
2573 % \begin{macrocode}
2574 \cs_new:Npn \@@_exponent_engineering_aux:nnnnnnn #1#2#3#4#5#6#7
2575 {
2576 \exp_not:n { {#1} {#2} }
2577 \use:c
2578 {
2579 @@_exponent_engineering_
2580 \int_compare:nNnTF {#6#7} < 0
2581 {
2582 \int_case:nnF { \int_mod:nn { #7 } { 3 } }
2583 {
2584 { 1 } { 2 }
2585 { 2 } { 1 }
2586 }
2587 { 0 }
2588 }
2589 { \int_mod:nn {#7} { 3 } }
2590 :nnnn
2591 }
2592 {#3} {#4} {#5} {#6#7}
2593 }
2594 \cs_new:cpn { @@_exponent_engineering_0:nnnn } #1#2#3#4
2595 {
2596 \exp_not:n { {#1} {#2} {#3} }
2597 \@@_exponent_finalise:n {#4}
2598 }
2599 \cs_new:cpn { @@_exponent_engineering_1:nnnn } #1#2#3#4
2600 {
2601 \tl_if_blank:nTF {#2}
2602 {
2603 { \exp_not:n { #1 0 } } { }
2604 { \@@_exponent_engineering_uncert:nn {#3} { 0 } }
2605 }
2606 {
2607 { \exp_not:n {#1} \exp_not:o { \tl_head:w #2 \q_stop } }
2608 { \tl_tail:n {#2} }
2609 { \exp_not:n {#3} }
2610 }
2611 \@@_exponent_finalise:n { #4 - 1 }
2612 }
2613 \cs_new:cpn { @@_exponent_engineering_2:nnnn } #1#2#3#4
2614 {
2615 \tl_if_blank:nTF {#2}
2616 {
2617 { \exp_not:n { #1 00 } } { }
2618 { \@@_exponent_engineering_uncert:nn {#3} { 00 } }
2619 }
2620 { \@@_exponent_engineering:nnNw {#1} {#3} #2 \q_stop }
2621 \@@_exponent_finalise:n { #4 - 2 }
2622 }
2623 \cs_new:Npn \@@_exponent_engineering:nnNw #1#2#3#4 \q_stop
2624 {
2625 \tl_if_blank:nTF {#4}
2626 {
2627 { \exp_not:n { #1#3 0 } } { }
2628 { \@@_exponent_engineering_uncert:nn {#2} { 0 } }
2629 }
2630 {
2631 { \exp_not:n {#1#3} \exp_not:o { \tl_head:w #4 \q_stop } }
2632 { \tl_tail:n {#4} }
2633 { \exp_not:n {#2} }
2634 }
2635 }
2636 \cs_new:Npn \@@_exponent_engineering_uncert:nn #1#2
2637 {
2638 \tl_if_blank:nF {#1}
2639 {
2640 \use:c { @@_exponent_engineering_uncert_ \use_i:nn #1 :nnn }
2641 #1 {#2}
2642 }
2643 }
2644 \cs_new:Npn \@@_exponent_engineering_uncert_S:nnn #1#2#3
2645 {
2646 { S }
2647 {
2648 \exp_not:n {#2}
2649 \str_if_eq:nnF {#2} { 0 } {#3}
2650 }
2651 }
2652 % \end{macrocode}
2653 % To branch here we need to work out the scientific notation form.
2654 % \begin{macrocode}
2655 \cs_new:Npn \@@_exponent_threshold:nnnnnnn #1#2#3#4#5#6#7
2656 {
2657 \@@_exponent_threshold:e
2658 {
2659 \@@_exponent_scientific:nnnnnnn
2660 {#1} {#2} {#3} {#4} {#5} {#6} {#7}
2661 }
2662 }
2663 \cs_new:Npn \@@_exponent_threshold:n #1
2664 { \@@_exponent_threshold_aux:nnnnnnn #1 }
2665 \cs_generate_variant:Nn \@@_exponent_threshold:n { e }
2666 \cs_new:Npn \@@_exponent_threshold_aux:nnnnnnn #1#2#3#4#5#6#7
2667 {
2668 \@@_if_threshold_exceeded:nTF {#6#7}
2669 {
2670 \exp_not:n { {#1} {#2} }
2671 \@@_exponent_shift:nnn { -#6#7 } {#3} {#4}
2672 \@@_exponent_uncert:n {#5}
2673 { }
2674 { 0 }
2675 }
2676 { \exp_not:n { {#1} {#2} {#3} {#4} {#5} {#6} {#7} } }
2677 }
2678 % \end{macrocode}
2679 % \end{macro}
2680 % \end{macro}
2681 % \end{macro}
2682 % \end{macro}
2683 % \end{macro}
2684 % \end{macro}
2685 % \end{macro}
2686 % \end{macro}
2687 % \end{macro}
2688 % \end{macro}
2689 % \end{macro}
2690 % \end{macro}
2691 % \end{macro}
2692 % \end{macro}
2693 % \end{macro}
2694 % \end{macro}
2695 % \end{macro}
2696 % \end{macro}
2697 % \end{macro}
2698 % \end{macro}
2699 % \end{macro}
2700 % \end{macro}
2701 %
2702 % \begin{macro}{\@@_digits:NN}
2703 % \begin{macro}[EXP]{\@@_digits:nnnnnnn}
2704 % \begin{macro}[EXP]{\@@_digits:nn}
2705 % \begin{macro}[EXP]{\@@_digits_uncert:nnw}
2706 % \begin{macro}[EXP]
2707 % {\@@_digits_uncert_A:nn, \@@_digits_uncert_S:nn, \@@_digits_uncert_aux:nn}
2708 % \begin{macro}[EXP]{\@@_digits_uncert_A:nnn}
2709 % \begin{macro}[EXP]{\@@_digits_uncert:nN}
2710 % \begin{macro}[EXP]{\@@_digits_uncert:nNw}
2711 % Forcing a minimum number of digits in each part is quite easy. As
2712 % the common case is that we don't do anything here, there is no real need
2713 % to optimise the calculation (normally also numbers have only a few digits).
2714 % \begin{macrocode}
2715 \cs_new_protected:Npn \@@_digits:NN #1#2
2716 {
2717 \tl_set:Ne #2
2718 { \exp_after:wN \@@_digits:nnnnnnn #1 }
2719 }
2720 \cs_new:Npn \@@_digits:nnnnnnn #1#2#3#4#5#6#7
2721 {
2722 \exp_not:n { {#1} {#2} }
2723 {
2724 \@@_digits:nn \l_@@_min_integer_int {#3}
2725 \exp_not:n {#3}
2726 }
2727 {
2728 \exp_not:n {#4}
2729 \@@_digits:nn \l_@@_min_decimal_int {#4}
2730 }
2731 {
2732 \tl_if_blank:nF {#5}
2733 { \@@_digits_uncert:nnw {#4} #5 \q_stop }
2734 }
2735 \exp_not:n { {#6} {#7} }
2736 }
2737 \cs_new:Npn \@@_digits:nn #1#2
2738 {
2739 \int_compare:nNnT
2740 { #1 - \tl_count:n {#2} } > 0
2741 { \prg_replicate:nn { #1 - \tl_count:n {#2} } { 0 } }
2742 }
2743 \cs_new:Npn \@@_digits_uncert:nnw #1#2#3 \q_stop
2744 {
2745 { #2 }
2746 \cs_if_exist:cTF { @@_digits_uncert_ #2 :nn }
2747 { { \use:c { @@_digits_uncert_ #2 :nn } {#1} {#3} } }
2748 {
2749 \@@_digits_uncert:nN {#1} #2 \q_recursion_tail
2750 #3 \q_recursion_stop
2751 }
2752 }
2753 \cs_new:Npn \@@_digits_uncert_A:nn #1#2
2754 { \@@_digits_uncert_A:nnn {#1} #2 }
2755 \cs_new:Npn \@@_digits_uncert_A:nnn #1#2#3
2756 {
2757 { \@@_digits_uncert_aux:nn {#1} {#2} }
2758 { \@@_digits_uncert_aux:nn {#1} {#3} }
2759 }
2760 \cs_new:Npn \@@_digits_uncert_S:nn #1#2
2761 { \@@_digits_uncert_aux:nn {#1} {#2} }
2762 \cs_new:Npn \@@_digits_uncert_aux:nn #1#2
2763 {
2764 \exp_not:n {#2}
2765 \int_compare:nNnT
2766 { \l_@@_min_decimal_int - \tl_count:n {#1} } > 0
2767 {
2768 \prg_replicate:nn
2769 { \l_@@_min_decimal_int - \tl_count:n {#1} } { 0 }
2770 }
2771 }
2772 \cs_new:Npn \@@_digits_uncert:nN #1#2
2773 {
2774 \quark_if_recursion_tail_stop:N #2
2775 \@@_digits_uncert:nNw {#1} #2
2776 }
2777 \cs_new:Npn \@@_digits_uncert:nNw #1#2#3 \q_recursion_tail #4
2778 {
2779 { \use:c { @@_digits_uncert_ #2 :nn } {#1} {#4} }
2780 \@@_digits_uncert:nN {#1} #3 \q_recursion_tail
2781 }
2782 % \end{macrocode}
2783 % \end{macro}
2784 % \end{macro}
2785 % \end{macro}
2786 % \end{macro}
2787 % \end{macro}
2788 % \end{macro}
2789 % \end{macro}
2790 % \end{macro}
2791 %
2792 % \begin{macro}{\@@_drop_exponent:NN}
2793 % \begin{macro}[EXP]{\@@_drop_exponent:nnnnnnn}
2794 % Simple stripping of the exponent.
2795 % \begin{macrocode}
2796 \cs_new_protected:Npn \@@_drop_exponent:NN #1#2
2797 {
2798 \bool_if:NT \l_@@_drop_exponent_bool
2799 {
2800 \str_if_eq:VnT \l_@@_exponent_mode_str { input }
2801 { \msg_warning:nn { siunitx } { ambiguous-dropped-exponent } }
2802 \tl_set:Ne #2
2803 { \exp_after:wN \@@_drop_exponent:nnnnnnn #1 }
2804 }
2805 }
2806 \cs_new:Npn \@@_drop_exponent:nnnnnnn #1#2#3#4#5#6#7
2807 { \exp_not:n { {#1} {#2} {#3} {#4} {#5} { } { 0 } } }
2808 % \end{macrocode}
2809 % \end{macro}
2810 % \end{macro}
2811 %
2812 % \begin{macro}{\@@_drop_uncertainty:NN}
2813 % \begin{macro}[EXP]{\@@_drop_uncertainty:nnnnnnn}
2814 % Simple stripping of the uncertainty.
2815 % \begin{macrocode}
2816 \cs_new_protected:Npn \@@_drop_uncertainty:NN #1#2
2817 {
2818 \bool_if:NTF \l_@@_drop_uncertainty_bool
2819 {
2820 \tl_set:Ne #2
2821 { \exp_after:wN \@@_drop_uncertainty:nnnnnnn #1 }
2822 }
2823 { \tl_set_eq:NN #2 #1 }
2824
2825 }
2826 \cs_new:Npn \@@_drop_uncertainty:nnnnnnn #1#2#3#4#5#6#7
2827 { \exp_not:n { {#1} {#2} {#3} {#4} { } {#6} {#7} } }
2828 % \end{macrocode}
2829 % \end{macro}
2830 % \end{macro}
2831 %
2832 % \begin{macro}{\@@_round:NN}
2833 % \begin{macro}[EXP]{\@@_round:nnnnnnn}
2834 % Rounding is at the top level simple enough: fire off the expandable
2835 % set up which does the work.
2836 % \begin{macrocode}
2837 \cs_new_protected:Npn \@@_round:NN #1#2
2838 {
2839 \tl_set:Ne #2
2840 {
2841 \str_if_eq:VnTF \l_@@_round_mode_str { none }
2842 { \exp_not:V #1 }
2843 { \exp_after:wN \@@_round:nnnnnnn #1 }
2844 }
2845 }
2846 \cs_new:Npn \@@_round:nnnnnnn #1#2#3#4#5#6#7
2847 {
2848 \tl_if_blank:nTF { #3#4 }
2849 {
2850 \exp_not:n { {#1} {#2} { } { } {#5} {#6} {#7} }
2851 }
2852 {
2853 \str_if_eq:nnTF {#4} { \empty }
2854 {
2855 \use:c { @@_round_ \l_@@_round_mode_str :nnnnnnn }
2856 {#1} {#2} {#3} { }
2857 }
2858 {
2859 \use:c { @@_round_ \l_@@_round_mode_str :nnnnnnn }
2860 {#1} {#2} {#3} {#4}
2861 }
2862 {#5} {#6} {#7}
2863 }
2864 }
2865 % \end{macrocode}
2866 % \end{macro}
2867 % \end{macro}
2868 %
2869 % \begin{macro}[EXP]{\@@_round:nnn, \@@_round:enn}
2870 % \begin{macro}[EXP]{\@@_round:nnnn, \@@_round:Vnnn}
2871 % \begin{macro}[EXP]
2872 % {
2873 % \@@_round_auxi:nnnnN ,
2874 % \@@_round_auxii:nnnnN ,
2875 % \@@_round_auxiii:nnnnN ,
2876 % \@@_round_auxiii:ennnN ,
2877 % \@@_round_auxiv:nnnnN ,
2878 % \@@_round_auxiv:ennnN
2879 % }
2880 % \begin{macro}[EXP]{\@@_round_auxv:nnnN, \@@_round_auxvi:nnnN}
2881 % \begin{macro}[EXP]{\@@_round_auxvii:nnnTF}
2882 % \begin{macro}[EXP]{\@@_round_auxviii:nnN, \@@_round_auxix:nnN}
2883 % \begin{macro}[EXP]{\@@_round_final_integer:nnw, \@@_round_final_decimal:nnw}
2884 % \begin{macro}[EXP]
2885 % {\@@_round_final_significant:n, \@@_round_final_significant:e}
2886 % \begin{macro}[EXP]{\@@_round_final_significant:N}
2887 % \begin{macro}[EXP]{\@@_round_final_significant:w}
2888 % \begin{macro}[EXP]
2889 % {
2890 % \@@_round_final_output:nn ,
2891 % \@@_round_final_output:ne ,
2892 % \@@_round_final_output:en ,
2893 % \@@_round_final_output:ee
2894 % }
2895 % \begin{macro}[EXP]{\@@_round_final:nn, \@@_round_final:en}
2896 % \begin{macro}[EXP]
2897 % {
2898 % \@@_round_final_shift:nn ,
2899 % \@@_round_final_shift:en ,
2900 % \@@_round_final_shift:ee
2901 % }
2902 % \begin{macro}[EXP]{\@@_round_final_shift:Nw}
2903 % \begin{macro}[EXP]
2904 % {
2905 % \@@_round_engineering:nn ,
2906 % \@@_round_fixed:nn ,
2907 % \@@_round_input:nn ,
2908 % \@@_round_scientific:nn ,
2909 % \@@_round_threshold:nn
2910 % }
2911 % \begin{macro}[EXP]{\@@_round_engineering:NNNNn}
2912 % \begin{macro}[EXP]{\@@_round_engineering:nnN, \@@_round_engineering:VnN}
2913 % \begin{macro}[EXP]{\@@_round_truncate:n, \@@_round_truncate_direct:n}
2914 % \begin{macro}[EXP]{\@@_round_truncate:nnN}
2915 % Actually doing the rounding needs us to work from the least significant
2916 % digit, so we start by reversing the input. We \emph{could} also drop
2917 % digits in this phase, but tracking everything would be horrible, so
2918 % we go slightly slower but clearer and split the steps. First we reverse
2919 % the decimal part, then the integer.
2920 % \begin{macrocode}
2921 \cs_new:Npn \@@_round:nnn #1#2#3
2922 {
2923 \@@_round:Vnnn \l_@@_round_dir_str
2924 {#1} {#2} {#3}
2925 }
2926 \cs_generate_variant:Nn \@@_round:nnn { e }
2927 \cs_new:Npn \@@_round:nnnn #1#2#3#4
2928 {
2929 \@@_round_auxi:nnnnN {#1} {#2} {#3} { }
2930 #4 \q_recursion_tail \q_recursion_stop
2931 }
2932 \cs_generate_variant:Nn \@@_round:nnnn { V }
2933 \cs_new:Npn \@@_round_auxi:nnnnN #1#2#3#4#5
2934 {
2935 \quark_if_recursion_tail_stop_do:Nn #5
2936 {
2937 \@@_round_auxii:nnnnN {#1} {#2} {#4} { } #3
2938 \q_recursion_tail \q_recursion_stop
2939 }
2940 \@@_round_auxi:nnnnN {#1} {#2} {#3} {#5#4}
2941 }
2942 \cs_new:Npn \@@_round_auxii:nnnnN #1#2#3#4#5
2943 {
2944 \quark_if_recursion_tail_stop_do:Nn #5
2945 {
2946 \tl_if_blank:nTF {#3}
2947 {
2948 \@@_round_auxiv:nnnnN {#2} {#1} { } { } #4
2949 \q_recursion_tail \q_recursion_stop
2950 }
2951 {
2952 \@@_round_auxiii:nnnnN {#2} {#1} {#4} { } #3
2953 \q_recursion_tail \q_recursion_stop
2954 }
2955 }
2956 \@@_round_auxii:nnnnN {#1} {#2} {#3} {#5#4}
2957 }
2958 % \end{macrocode}
2959 % We now have the input reversed plus how many digits we need to discard
2960 % (|#1|). We have two functions, one which deals with the decimal part,
2961 % one of which deals with the integer. In the latter, we should never hit
2962 % the end before we've dropped all the digits: the fixed-zero is a
2963 % fall-back in case something weird happens. For the integer case, we need
2964 % to collect up zeros to pad the length back out correctly later. We also
2965 % have to cover the case where we round to exactly place above the length
2966 % of the integer: that may product a value of $1\dots$: we tidy up the
2967 % case where it comes out as $0$ later.
2968 % \begin{macrocode}
2969 \cs_new:Npn \@@_round_auxiii:nnnnN #1#2#3#4#5
2970 {
2971 \quark_if_recursion_tail_stop_do:Nn #5
2972 {
2973 \@@_round_auxiv:nnnnN {#1} {#2} { } {#4} #3
2974 \q_recursion_tail \q_recursion_stop
2975 }
2976 \int_compare:nNnTF {#1} > 0
2977 {
2978 \@@_round_auxiii:ennnN
2979 { \int_eval:n { #1 - 1 } } {#2} {#3} { #5#4 }
2980 }
2981 { \@@_round_auxv:nnnN {#2} {#4} {#3} #5 }
2982 }
2983 \cs_generate_variant:Nn \@@_round_auxiii:nnnnN { e }
2984 \cs_new:Npn \@@_round_auxiv:nnnnN #1#2#3#4#5
2985 {
2986 \quark_if_recursion_tail_stop_do:Nn #5
2987 {
2988 \int_compare:nNnTF {#1} = 0
2989 {
2990 \@@_round_auxvi:nnnN {#2} {#4} {#3}
2991 0 \q_recursion_tail \q_recursion_stop
2992 }
2993 { { 0 } { } }
2994 }
2995 \int_compare:nNnTF {#1} > 0
2996 {
2997 \@@_round_auxiv:ennnN
2998 { \int_eval:n { #1 - 1 } } {#2} { #3 0 } { #5#4 }
2999 }
3000 { \@@_round_auxvi:nnnN {#2} {#4} {#3} #5 }
3001 }
3002 \cs_generate_variant:Nn \@@_round_auxiv:nnnnN { e }
3003 % \end{macrocode}
3004 % We now have the discarded digits, the matching filler and the
3005 % next digit. So we can make the decision on rounding: that
3006 % is handled by a common auxiliary.
3007 % \begin{macrocode}
3008 \cs_new:Npn \@@_round_auxv:nnnN #1#2#3#4
3009 {
3010 \quark_if_recursion_tail_stop_do:Nn #4
3011 {
3012 \@@_round_auxvi:nnnN
3013 {#1} {#2} { } #3 \q_recursion_tail \q_recursion_stop
3014 }
3015 \@@_round_auxvii:nnnTF {#1} {#2} {#4}
3016 { \@@_round_final_decimal:nnw }
3017 { \@@_round_auxviii:nnN }
3018 {#3} { } #4
3019 }
3020 \cs_new:Npn \@@_round_auxvi:nnnN #1#2#3#4
3021 {
3022 \quark_if_recursion_tail_stop_do:Nn #4
3023 { { 0 } { } }
3024 \@@_round_auxvii:nnnTF {#1} {#2} {#4}
3025 { \@@_round_final_integer:nnw }
3026 { \@@_round_auxix:nnN }
3027 { } {#3} #4
3028 }
3029 % \end{macrocode}
3030 % For rounding up or down, the decision here is easy: pick the appropriate
3031 % branch. For rounding to nearest, we need to deal with the half-even rule:
3032 % it can only apply at this stage, when the \emph{discarded} value can
3033 % be exactly half.
3034 % \begin{macrocode}
3035 \cs_new:Npn \@@_round_auxvii:nnnTF #1#2#3
3036 {
3037 \str_case:nnF {#1}
3038 {
3039 { down } { \use_i:nn }
3040 { up }
3041 {
3042 \str_if_eq:eeTF
3043 {#2} { \prg_replicate:nn { \tl_count:n {#2} } { 0 } }
3044 { \use_i:nn }
3045 { \use_ii:nn }
3046 }
3047 }
3048 {
3049 \bool_lazy_or:nnTF
3050 { \int_compare_p:nNn { 0 \tl_head:n {#2} } < 5 }
3051 {
3052 \bool_lazy_all_p:n
3053 {
3054 { \l_@@_round_half_even_bool }
3055 { ! \int_if_odd_p:n {#3} }
3056 { \@@_round_if_half_p:n {#2} }
3057 }
3058 }
3059 }
3060 }
3061 % \end{macrocode}
3062 % The main rounding routines. These are only every called when there is
3063 % rounding to do, so there is no need to carry a flag forward. Thus the
3064 % question to ask is simple: is the next value a $9$ or not (as that
3065 % continues the sequence). There is a general need to handle the case
3066 % where a zero is rounded up: that automatically means a need to trim
3067 % the other end.
3068 % \begin{macrocode}
3069 \cs_new:Npn \@@_round_auxviii:nnN #1#2#3
3070 {
3071 \quark_if_recursion_tail_stop_do:Nn #3
3072 {
3073 \str_if_eq:nnTF {#1} { 0 }
3074 {
3075 \@@_round_final_output:ne
3076 { 1 }
3077 { \@@_round_truncate:n {#2} }
3078 }
3079 {
3080 \@@_round_auxix:nnN {#2} { } #1
3081 \q_recursion_tail \q_recursion_stop
3082 }
3083 }
3084 \int_compare:nNnTF {#3} = 9
3085 { \@@_round_auxviii:nnN {#1} { 0 #2 } }
3086 {
3087 \int_compare:nNnTF {#3} = 0
3088 {
3089 \bool_lazy_or:nnTF
3090 { \tl_if_blank_p:n {#1} }
3091 { \int_compare_p:nNn {#1} = { 0 } }
3092 {
3093 \@@_round_final_decimal:nnw
3094 {#1} { 1 \@@_round_truncate:n {#2} }
3095 }
3096 {
3097 \@@_round_final_decimal:nnw
3098 {#1} { 1 #2 }
3099 }
3100 }
3101 {
3102 \@@_round_final:en
3103 { \int_eval:n { #3 + 1 } }
3104 { \@@_round_final_decimal:nnw {#1} {#2} }
3105 }
3106 }
3107 }
3108 \cs_new:Npn \@@_round_auxix:nnN #1#2#3
3109 {
3110 \quark_if_recursion_tail_stop_do:Nn #3
3111 {
3112 \tl_if_blank:nTF {#1}
3113 {
3114 \@@_round_final_shift:en
3115 { 1 \@@_round_truncate_direct:n {#2} 0 }
3116 { }
3117 }
3118 {
3119 \@@_round_final_shift:ee
3120 { 1 #2 }
3121 { \@@_round_truncate:n {#1} }
3122 }
3123 }
3124 \int_compare:nNnTF {#3} = 9
3125 { \@@_round_auxix:nnN {#1} { 0 #2 } }
3126 {
3127 \@@_round_final:en
3128 { \int_eval:n { #3 + 1 } }
3129 { \@@_round_final_integer:nnw {#1} {#2} }
3130 }
3131 }
3132 % \end{macrocode}
3133 % Tidying up means grabbing the remaining digits and undoing the reversal.
3134 % \begin{macrocode}
3135 \cs_new:Npn \@@_round_final_decimal:nnw
3136 #1#2#3 \q_recursion_tail \q_recursion_stop
3137 {
3138 \@@_round_final_output:ee
3139 { \tl_reverse:n {#1} }
3140 { \tl_reverse:n {#3} #2 }
3141 }
3142 \cs_new:Npn \@@_round_final_integer:nnw
3143 #1#2#3 \q_recursion_tail \q_recursion_stop
3144 {
3145 \@@_round_final_output:en
3146 { \@@_round_final_significant:e { \tl_reverse:n {#3} #2 } }
3147 {#1}
3148 }
3149 \cs_new:Npn \@@_round_final_significant:n #1
3150 {
3151 \@@_round_final_significant:N #1
3152 \q_recursion_tail \q_recursion_stop
3153 }
3154 \cs_generate_variant:Nn \@@_round_final_significant:n { e }
3155 \cs_new:Npn \@@_round_final_significant:N #1
3156 {
3157 \quark_if_recursion_tail_stop_do:Nn #1 { 0 }
3158 \int_compare:nNnTF {#1} = 0
3159 { \@@_round_final_significant:N }
3160 {
3161 #1
3162 \@@_round_final_significant:w
3163 }
3164 }
3165 \cs_new:Npn \@@_round_final_significant:w
3166 #1 \q_recursion_tail \q_recursion_stop
3167 {#1}
3168 \cs_new:Npn \@@_round_final_output:nn #1#2 { {#1} {#2} }
3169 \cs_generate_variant:Nn \@@_round_final_output:nn { ne , e , ee }
3170 \cs_new:Npn \@@_round_final:nn #1#2
3171 { #2 #1 }
3172 \cs_generate_variant:Nn \@@_round_final:nn { e }
3173 % \end{macrocode}
3174 % Here we deal with the case where rounding applies along with an
3175 % exponent set based on number of places. We can only get here if an
3176 % additional integer digit has been added, so there is no need to test for
3177 % that. There are two cases for action: when using |scientific| mode, where
3178 % we always need to shift by one, and when using |engineering| mode if
3179 % we now have four digits. The latter is a bit more work: we need to trim
3180 % digits off as required.
3181 % \begin{macrocode}
3182 \cs_new:Npn \@@_round_final_shift:nn #1#2
3183 {
3184 \bool_lazy_or:nnTF
3185 { \str_if_eq_p:Vn \l_@@_round_mode_str { figures } }
3186 { \str_if_eq_p:Vn \l_@@_round_mode_str { places } }
3187 {
3188 \use:c
3189 { @@_round_ \l_@@_exponent_mode_str :nn }
3190 {#1} {#2}
3191 }
3192 { {#1} {#2} }
3193 }
3194 \cs_generate_variant:Nn \@@_round_final_shift:nn { en , ee }
3195 \cs_new:Npn \@@_round_engineering:nn #1#2
3196 {
3197 \int_compare:nNnTF { \tl_count:n {#1} } = 4
3198 {
3199 \bool_lazy_and:nnTF
3200 { \int_compare_p:nNn \l_@@_round_precision_int = 1 }
3201 { \str_if_eq_p:Vn \l_@@_round_mode_str { figures } }
3202 { { 1 } { } }
3203 { \@@_round_engineering:NNNNn #1 {#2} }
3204 \@@_round_final_shift:Nw 3
3205 }
3206 { {#1} {#2} }
3207 }
3208 \cs_new:Npn \@@_round_engineering:NNNNn #1#2#3#4#5
3209 {
3210 {#1}
3211 \@@_round_engineering:VnN
3212 \l_@@_round_precision_int { }
3213 #2#3#4#5 \q_recursion_tail \q_recursion_stop
3214 }
3215 \cs_new:Npn \@@_round_engineering:nnN #1#2#3
3216 {
3217 \quark_if_recursion_tail_stop_do:Nn #3 { {#2} }
3218 \int_compare:nNnTF {#1} = { 0 }
3219 { \use_i_delimit_by_q_recursion_stop:nw { {#2} } }
3220 { \@@_round_engineering:nnN { #1 - 1 } { #2#3 } }
3221 }
3222 \cs_generate_variant:Nn \@@_round_engineering:nnN { V }
3223 \cs_new:Npn \@@_round_fixed:nn #1#2 { {#1} {#2} }
3224 \cs_new:Npn \@@_round_input:nn #1#2 { {#1} {#2} }
3225 \cs_new:Npn \@@_round_scientific:nn #1#2
3226 {
3227 \bool_lazy_and:nnTF
3228 { \int_compare_p:nNn \l_@@_round_precision_int = 1 }
3229 { \str_if_eq_p:Vn \l_@@_round_mode_str { figures } }
3230 { { 1 } { } }
3231 {
3232 \@@_exponent_shift:nne
3233 { 1 } {#1} { \@@_round_truncate_direct:n {#2} }
3234 }
3235 \@@_round_final_shift:Nw 1
3236 }
3237 \cs_new:Npn \@@_round_threshold:nn #1#2
3238 {
3239 \@@_if_threshold_exceeded:nTF {#1}
3240 { \@@_round_input:nn }
3241 { \@@_round_scientific:nn }
3242 {#1} {#2}
3243 }
3244 \cs_new:Npn \@@_round_final_shift:Nw #1#2 \@@_round_end:nnn #3#4#5
3245 {
3246 \exp_not:n { {#3} }
3247 \@@_exponent_finalise:n { #4#5 + #1 }
3248 }
3249 % \end{macrocode}
3250 % When we have rounded up to the next power of ten, we need to go back and
3251 % remove one more digit. That only happens when rounding to a number of
3252 % figures or when dealing with an integer part.
3253 % \begin{macrocode}
3254 \cs_new:Npn \@@_round_truncate:n #1
3255 {
3256 \str_if_eq:VnTF \l_@@_round_mode_str { figures }
3257 { \@@_round_truncate_direct:n {#1} }
3258 {#1}
3259 }
3260 \cs_new:Npn \@@_round_truncate_direct:n #1
3261 {
3262 \@@_round_truncate:nnN { } { }
3263 #1 \q_recursion_tail \q_recursion_stop
3264 }
3265 \cs_new:Npn \@@_round_truncate:nnN #1#2#3
3266 {
3267 \quark_if_recursion_tail_stop_do:Nn #3 { #1 }
3268 \@@_round_truncate:nnN {#1#2} {#3}
3269 }
3270 % \end{macrocode}
3271 % \end{macro}
3272 % \end{macro}
3273 % \end{macro}
3274 % \end{macro}
3275 % \end{macro}
3276 % \end{macro}
3277 % \end{macro}
3278 % \end{macro}
3279 % \end{macro}
3280 % \end{macro}
3281 % \end{macro}
3282 % \end{macro}
3283 % \end{macro}
3284 % \end{macro}
3285 % \end{macro}
3286 % \end{macro}
3287 % \end{macro}
3288 % \end{macro}
3289 % \end{macro}
3290 %
3291 % \begin{macro}[EXP]{\@@_round_if_half_p:n}
3292 % \begin{macro}[EXP]{\@@_round_if_half:N}
3293 % A simple test for a valuing being exactly half: we can only test
3294 % digit-by-digit as there is no limit on the size of the value given.
3295 % \begin{macrocode}
3296 \prg_new_conditional:Npnn \@@_round_if_half:n #1 { p }
3297 {
3298 \int_compare:nNnTF { \tl_head:n { #1 0 } } = 5
3299 {
3300 \exp_after:wN \@@_round_if_half:N \use_none:n #1 0
3301 \q_recursion_tail \q_recursion_stop
3302 }
3303 { \prg_return_false: }
3304 }
3305 \cs_new:Npn \@@_round_if_half:N #1
3306 {
3307 \quark_if_recursion_tail_stop_do:Nn #1
3308 { \prg_return_true: }
3309 \int_compare:nNnTF {#1} = 0
3310 { \@@_round_if_half:N }
3311 { \use_i_delimit_by_q_recursion_stop:nw { \prg_return_false: } }
3312 }
3313 % \end{macrocode}
3314 % \end{macro}
3315 % \end{macro}
3316 %
3317 % \begin{macro}[EXP]{\@@_round_pad:nnn}
3318 % The case where we are short of digits is easy enough to handle:
3319 % generate zeros to pad it out.
3320 % \begin{macrocode}
3321 \cs_new:Npn \@@_round_pad:nnn #1#2#3
3322 {
3323 {#2}
3324 {
3325 #3
3326 \bool_if:NT \l_@@_round_pad_bool
3327 { \prg_replicate:nn {#1} { 0 } }
3328 }
3329 }
3330 % \end{macrocode}
3331 % \end{macro}
3332 %
3333 % \begin{macro}[EXP]{\@@_round_end:nnn}
3334 % Used as a marker to allow adjustment when we pass a power of ten.
3335 % \begin{macrocode}
3336 \cs_new:Npn \@@_round_end:nnn #1#2#3 { \exp_not:n { {#1} {#2} {#3} } }
3337 % \end{macrocode}
3338 % \end{macro}
3339 %
3340 % \begin{macro}[EXP]{\@@_round_figures:nnnnnnn, \@@_round_figures_aux:nnnnnnn}
3341 % \begin{macro}[EXP]{\@@_round_figures_count:nnN}
3342 % \begin{macro}[EXP]
3343 % {\@@_round_figures_count:nnnN, \@@_round_figures_count:ennN}
3344 % Rounding to figures only makes sense if the number is not $0$, so we start
3345 % by filtering out that case. We then check that
3346 % there is no uncertainty, and that the number of figures requested is
3347 % positive: if not, the result is always fixed at zero.
3348 % \begin{macrocode}
3349 \cs_new:Npn \@@_round_figures:nnnnnnn #1#2#3#4#5#6#7
3350 {
3351 \bool_lazy_and:nnTF
3352 { \str_if_eq_p:nn {#3} { 0 } }
3353 {
3354 \str_if_eq_p:ee
3355 { \exp_not:n {#4} } { \prg_replicate:nn { \tl_count:n {#4} } { 0 } }
3356 }
3357 { \exp_not:n { {#1} {#2} {#3} {#4} {#5} {#6} {#7} } }
3358 { \@@_round_figures_aux:nnnnnnn {#1} {#2} {#3} {#4} {#5} {#6} {#7} }
3359 }
3360 \cs_new:Npn \@@_round_figures_aux:nnnnnnn #1#2#3#4#5#6#7
3361 {
3362 \tl_if_blank:nTF {#5}
3363 {
3364 \int_compare:nNnTF \l_@@_round_precision_int > 0
3365 {
3366 \exp_not:n { {#1} {#2} }
3367 \@@_round_figures_count:nnN {#3} {#4} #3#4
3368 \q_recursion_tail \q_recursion_stop
3369 \@@_round_end:nnn { } {#6} {#7}
3370 }
3371 { { } { } { 0 } { } { } { } { 0 } }
3372 }
3373 { \exp_not:n { {#1} {#2} {#3} {#4} {#5} {#6} {#7} } }
3374 }
3375 % \end{macrocode}
3376 % The first real step is to count up the number of significant figures.
3377 % The only tricky issue here is dealing with leading zeros.
3378 % \begin{macrocode}
3379 \cs_new:Npn \@@_round_figures_count:nnN #1#2#3
3380 {
3381 \quark_if_recursion_tail_stop_do:Nn #3
3382 { { } { } { 0 } { } { } { } { 0 } }
3383 \int_compare:nNnTF {#3} = 0
3384 { \@@_round_figures_count:nnN {#1} {#2} }
3385 { \@@_round_figures_count:nnnN { 1 } {#1} {#2} }
3386 }
3387 \cs_new:Npn \@@_round_figures_count:nnnN #1#2#3#4
3388 {
3389 \quark_if_recursion_tail_stop_do:Nn #4
3390 {
3391 \int_compare:nNnTF {#1} > \l_@@_round_precision_int
3392 {
3393 \@@_round:enn
3394 { \int_eval:n { #1 - \l_@@_round_precision_int } }
3395 {#2} {#3}
3396 }
3397 {
3398 \@@_round_pad:nnn
3399 { \l_@@_round_precision_int - (#1) } {#2} {#3}
3400 }
3401 }
3402 \@@_round_figures_count:ennN
3403 { \int_eval:n { #1 + 1 } } {#2} {#3}
3404 }
3405 \cs_generate_variant:Nn \@@_round_figures_count:nnnN { e }
3406 % \end{macrocode}
3407 % \end{macro}
3408 % \end{macro}
3409 % \end{macro}
3410 %
3411 % \begin{macro}[EXP]{\@@_round_places:nnnnnnn}
3412 % \begin{macro}[EXP]{\@@_round_places_decimal:nn, \@@_round_places_integer:nn}
3413 % \begin{macro}[EXP]{\@@_round_places_finalise:n, \@@_round_places_finalise:e}
3414 % \begin{macro}[EXP]{\@@_round_places_finalise:nnnnnnn}
3415 % \begin{macro}[EXP]{\@@_round_places_finalise:nnnnn}
3416 % The first step when rounding to a fixed number of places is to establish
3417 % if this is in the decimal or integer parts. The two require different
3418 % calculations for how many digits to drop from the input. The no-op end
3419 % function here is to allow tidying up in some cases: see the finalisation
3420 % of rounding.
3421 % \begin{macrocode}
3422 \cs_new:Npn \@@_round_places:nnnnnnn #1#2#3#4#5#6#7
3423 {
3424 \tl_if_blank:nTF {#5}
3425 {
3426 \@@_round_places_finalise:e
3427 {
3428 \exp_not:n { {#1} {#2} }
3429 \int_compare:nNnTF \l_@@_round_precision_int > 0
3430 { \@@_round_places_decimal:nn }
3431 { \@@_round_places_integer:nn }
3432 {#3} {#4}
3433 \@@_round_end:nnn { } {#6} {#7}
3434 }
3435 }
3436 { \exp_not:n { {#1} {#2} {#3} {#4} {#5} {#6} {#7} } }
3437 }
3438 \cs_new:Npn \@@_round_places_decimal:nn #1#2
3439 {
3440 \int_compare:nNnTF
3441 { \l_@@_round_precision_int - 0 \tl_count:n {#2} } > 0
3442 {
3443 \@@_round_pad:nnn
3444 { \l_@@_round_precision_int - 0 \tl_count:n {#2} }
3445 {#1} {#2}
3446 }
3447 {
3448 \@@_round:enn
3449 {
3450 \int_eval:n
3451 { 0 \tl_count:n {#2} - \l_@@_round_precision_int }
3452 }
3453 {#1} {#2}
3454 }
3455 }
3456 \cs_new:Npn \@@_round_places_integer:nn #1#2
3457 {
3458 \@@_round:enn
3459 {
3460 \int_eval:n
3461 { 0 \tl_count:n {#2} - \l_@@_round_precision_int }
3462 }
3463 {#1} {#2}
3464 }
3465 % \end{macrocode}
3466 % To finalise rounding to places, we have to worry about a minimum value:
3467 % that is basically a case of looking for value of zero and rearranging. We
3468 % also need to worry about a \enquote{negative zero} arising.
3469 % \begin{macrocode}
3470 \cs_new:Npn \@@_round_places_finalise:n #1
3471 { \@@_round_places_finalise:nnnnnnn #1 }
3472 \cs_new:Npn \@@_round_places_finalise:nnnnnnn #1#2#3#4#5#6#7
3473 {
3474 \str_if_eq:eeTF
3475 { \exp_not:n {#3#4} }
3476 { \prg_replicate:nn { \tl_count:n {#3#4} } { 0 } }
3477 {
3478 \tl_if_empty:NTF \l_@@_round_min_tl
3479 {
3480 \exp_not:n { {#1} }
3481 {
3482 \bool_lazy_and:nnF
3483 { \l_@@_round_positive_bool }
3484 { \str_if_eq_p:nn {#2} { - } }
3485 { \exp_not:n {#2} }
3486 }
3487 \exp_not:n { {#3} {#4} {#5} {#6} {#7} }
3488 }
3489 {
3490 \exp_after:wN \@@_round_places_finalise:nnnnn
3491 \l_@@_round_min_tl {#2} {#6} {#7}
3492 }
3493 }
3494 { \exp_not:n { {#1} {#2} {#3} {#4} {#5} {#6} {#7} } }
3495 }
3496 \cs_generate_variant:Nn \@@_round_places_finalise:n { e }
3497 \cs_new:Npn \@@_round_places_finalise:nnnnn #1#2#3#4#5
3498 {
3499 {
3500 \str_if_eq:nnTF {#3} { - }
3501 { > }
3502 { < }
3503 }
3504 \exp_not:n { {#3} {#1} {#2} { } {#4} {#5} }
3505 }
3506 % \end{macrocode}
3507 % \end{macro}
3508 % \end{macro}
3509 % \end{macro}
3510 % \end{macro}
3511 % \end{macro}
3512 %
3513 % \begin{macro}[EXP]{\@@_round_uncertainty:nnnnnnn}
3514 % \begin{macro}[EXP]{\@@_round_uncertainty:nnn, \@@_round_uncertainty:nno}
3515 % \begin{macro}[EXP]
3516 % {\@@_round_uncertainty:nnnn, \@@_round_uncertainty:ennn}
3517 % \begin{macro}[EXP]{\@@_round_uncertainty:nnnnnn}
3518 % \begin{macro}[EXP]
3519 % {
3520 % \@@_round_uncertainty_auxi:nnnnn ,
3521 % \@@_round_uncertainty_auxii:nnnnn
3522 % }
3523 % \begin{macro}[EXP]
3524 % {
3525 % \@@_round_uncertainty_auxiii:nnnnnnn ,
3526 % \@@_round_uncertainty_auxiii:eennnnn
3527 % }
3528 % \begin{macro}[EXP]{\@@_round_uncertainty_zeros:nnnn}
3529 % \begin{macro}[EXP]
3530 % {
3531 % \@@_round_uncertainty_auxiv:nnnnnnN ,
3532 % \@@_round_uncertainty_auxiv:nennnnN ,
3533 % \@@_round_uncertainty_auxiv:ennnnnN ,
3534 % \@@_round_uncertainty_auxiv:eennnnN
3535 % }
3536 % \begin{macro}[EXP]{\@@_round_uncertainty_auxv:nnnN}
3537 % \begin{macro}[EXP]{\@@_round_uncertainty_auxvi:nn}
3538 % \begin{macro}[EXP]{\@@_round_uncertainty_auxvii:nnNnnn}
3539 % \begin{macro}[EXP]
3540 % {
3541 % \@@_round_uncertainty_engineering:nnn ,
3542 % \@@_round_uncertainty_fixed:nnn ,
3543 % \@@_round_uncertainty_input:nnn ,
3544 % \@@_round_uncertainty_scientific:nnn ,
3545 % \@@_round_uncertainty_threshold:nnn
3546 % }
3547 % \begin{macro}[EXP]
3548 % {
3549 % \@@_round_uncertainty_engineering_2:n ,
3550 % \@@_round_uncertainty_engineering_3:n ,
3551 % \@@_round_uncertainty_engineering_4:n
3552 % }
3553 % Rounding to an uncertainty can only happen where the result will have some
3554 % uncertainty left: otherwise we simply drop the uncertainty entirely. Only
3555 % |S|-type uncertainties can be used for rounding.
3556 % \begin{macrocode}
3557 \cs_new:Npn \@@_round_uncertainty:nnnnnnn #1#2#3#4#5#6#7
3558 {
3559 \bool_lazy_or:nnTF
3560 { \tl_if_blank_p:n {#5} }
3561 { ! \int_compare_p:nNn \l_@@_round_precision_int > 0 }
3562 { \exp_not:n { {#1} {#2} {#3} {#4} { } {#6} {#7} } }
3563 {
3564 \str_if_eq:eeTF { \tl_head:n {#5} } { S }
3565 {
3566 \exp_not:n { {#1} {#2} }
3567 \@@_round_uncertainty:nno
3568 {#3} {#4} { \use_ii:nn #5 }
3569 {#6} {#7}
3570 }
3571 { \exp_not:n { {#1} {#2} {#3} {#4} {#5} {#6} {#7} } }
3572 }
3573 }
3574 % \end{macrocode}
3575 % Round the uncertainty first: this is needed to get the number of places
3576 % correct. Once that is done, it's just a question of working out the digits
3577 % in the main part.
3578 % \begin{macrocode}
3579 \cs_new:Npn \@@_round_uncertainty:nnn #1#2#3
3580 {
3581 \@@_round_uncertainty:ennn
3582 {
3583 \int_eval:n
3584 { \tl_count:n {#3} - \l_@@_round_precision_int }
3585 }
3586 {#1} {#2} {#3}
3587 }
3588 \cs_generate_variant:Nn \@@_round_uncertainty:nnn { nno }
3589 \cs_new:Npn \@@_round_uncertainty:nnnn #1#2#3#4
3590 {
3591 \use:e
3592 {
3593 \exp_not:N \@@_round_uncertainty:nnnnnn
3594 \@@_round:Vnnn
3595 \l_@@_round_uncert_dir_str {#1} { } {#4}
3596 {#2} {#3} {#1} {#4}
3597 }
3598 }
3599 \cs_generate_variant:Nn \@@_round_uncertainty:nnnn { e }
3600 % \end{macrocode}
3601 % The number of digits to remove from the main part and the detail of
3602 % zero filling depends on whether we rounded up the uncertainty. So there
3603 % is a split here.
3604 % \begin{macrocode}
3605 \cs_new:Npn \@@_round_uncertainty:nnnnnn #1#2#3#4#5#6
3606 {
3607 \tl_if_blank:nTF {#1}
3608 { \@@_round_uncertainty_auxi:nnnnn }
3609 { \@@_round_uncertainty_auxii:nnnnn }
3610 {#2} {#3} {#4} {#5} {#6}
3611 }
3612 % \end{macrocode}
3613 % The simple case: just round to the same number of digits and do zero
3614 % filling.
3615 % \begin{macrocode}
3616 \cs_new:Npn \@@_round_uncertainty_auxi:nnnnn #1#2#3#4#5
3617 {
3618 \@@_round_uncertainty_auxiv:ennnnnN
3619 { \@@_round_uncertainty_zeros:nnnn {#1} {#3} {#4} {#5} }
3620 {#4}
3621 {#2} {#3} {#1} { }
3622 \@@_round_uncertainty_auxvi:nn
3623 }
3624 % \end{macrocode}
3625 % When the uncertainty rounds up, zero filling is dependent on whether we
3626 % cross the boundary for the integer part.
3627 % \begin{macrocode}
3628 \cs_new:Npn \@@_round_uncertainty_auxii:nnnnn #1#2#3#4#5
3629 {
3630 \@@_round_uncertainty_auxiii:eennnnn
3631 { \tl_count:n {#5} }
3632 { \tl_count:n {#3} }
3633 {#1} {#2} {#3} {#4} {#5}
3634 }
3635 \cs_new:Npn \@@_round_uncertainty_auxiii:nnnnnnn #1#2#3#4#5#6#7
3636 {
3637 \int_compare:nNnTF {#1} < { #2 + 1 }
3638 {
3639 \bool_lazy_and:nnTF
3640 { \int_compare_p:nNn \l_@@_round_precision_int = 2 }
3641 { \int_compare_p:nNn {#1} = {#2} }
3642 { \@@_round_uncertainty_auxiv:nennnnN { 0 } }
3643 { \@@_round_uncertainty_auxiv:nennnnN { } }
3644 { \int_eval:n { #6 + 1 } }
3645 {#4} {#5} { 1 } { }
3646 \@@_round_uncertainty_auxvii:nnNnnn
3647 }
3648 {
3649 \@@_round_uncertainty_auxiv:eennnnN
3650 { \@@_round_uncertainty_zeros:nnnn {#3} {#5} {#6} {#7} }
3651 { \int_eval:n { #6 + 1 } }
3652 {#4} {#5} { 1 } {#3}
3653 \@@_round_uncertainty_auxvii:nnNnnn
3654 }
3655 }
3656 \cs_generate_variant:Nn \@@_round_uncertainty_auxiii:nnnnnnn { ee }
3657 \cs_new:Npn \@@_round_uncertainty_zeros:nnnn #1#2#3#4
3658 {
3659 \prg_replicate:nn
3660 {
3661 \int_max:nn
3662 {
3663 \tl_if_blank:nTF {#2}
3664 { #3 }
3665 {
3666 ( \tl_count:n {#4} - \tl_count:n {#2} )
3667 - \tl_count:n {#1}
3668 }
3669 }
3670 { 0 }
3671 }
3672 { 0 }
3673 }
3674 % \end{macrocode}
3675 % Back together for the business end. If there has been a shift, we may need
3676 % to tidy up.
3677 % \begin{macrocode}
3678 \cs_new:Npn \@@_round_uncertainty_auxiv:nnnnnnN #1#2#3#4#5#6#7
3679 {
3680 \use:e
3681 {
3682 \exp_not:N \@@_round_uncertainty_auxv:nnnN
3683 \@@_round:nnn {#2} {#3} {#4}
3684 }
3685 {#3} #7
3686 \@@_round_end:nnn { { S } { #5#6 #1 } }
3687 }
3688 \cs_generate_variant:Nn \@@_round_uncertainty_auxiv:nnnnnnN { ne , e , ee }
3689 \cs_new:Npn \@@_round_uncertainty_auxv:nnnN #1#2#3#4
3690 {
3691 \int_compare:nNnT { \tl_count:n {#1} } > { \tl_count:n {#3} }
3692 { #4 }
3693 {#1} {#2}
3694 }
3695 \cs_new:Npn \@@_round_uncertainty_auxvi:nn #1#2
3696 { \use:c { @@_round_ \l_@@_exponent_mode_str :nn } {#1} {#2} }
3697 % \end{macrocode}
3698 % If there was a rounding up in the uncertainty, there is a bit more to do.
3699 % \begin{macrocode}
3700 \cs_new:Npn \@@_round_uncertainty_auxvii:nnNnnn #1#2#3#4#5#6
3701 {
3702 \bool_lazy_any:nTF
3703 {
3704 { \str_if_eq_p:Vn \l_@@_exponent_mode_str { engineering } }
3705 { \str_if_eq_p:Vn \l_@@_exponent_mode_str { scientific } }
3706 {
3707 \bool_lazy_and_p:nn
3708 { \str_if_eq_p:Vn \l_@@_exponent_mode_str { threshold } }
3709 { ! \@@_if_threshold_exceeded_p:n {#5#6} }
3710 }
3711 }
3712 {
3713 \use:c
3714 { @@_round_uncertainty_ \l_@@_exponent_mode_str :nnn }
3715 {#1}
3716 }
3717 {
3718 \@@_round_uncertainty_auxvi:nn {#1} {#2}
3719 \@@_round_end:nnn {#4}
3720 }
3721 {#5} {#6}
3722 }
3723 \cs_new:Npn \@@_round_uncertainty_engineering:nnn #1#2#3
3724 {
3725 \use:c
3726 {
3727 @@_round_uncertainty_
3728 \l_@@_exponent_mode_str _
3729 \tl_count:n {#1}
3730 :n
3731 }
3732 {#2#3}
3733 }
3734 \cs_new:cpn { @@_round_uncertainty_engineering_2:n } #1
3735 {
3736 { 10 } { } { { S } { 10 } }
3737 \@@_exponent_finalise:n {#1}
3738 }
3739 \cs_new:cpn { @@_round_uncertainty_engineering_3:n } #1
3740 {
3741 { 100 } { } { { S } { 100 } }
3742 \@@_exponent_finalise:n {#1}
3743 }
3744 \cs_new:cpn { @@_round_uncertainty_engineering_4:n } #1
3745 {
3746 { 1 } { } { { S } { 1 } }
3747 \@@_exponent_finalise:n { #1 + 3 }
3748 }
3749 \cs_new:Npn \@@_round_uncertainty_fixed:nnn #1#2#3
3750 {
3751 {#1} { }
3752 { { S } { 1 \prg_replicate:nn { \tl_count:n {#1} - 1 } { 0 } } }
3753 {#2} {#3}
3754 }
3755 \cs_new_eq:NN \@@_round_uncertainty_input:nnn
3756 \@@_round_uncertainty_fixed:nnn
3757 \cs_new:Npn \@@_round_uncertainty_scientific:nnn #1#2#3
3758 {
3759 { 1 } { } { { S } { 1 } }
3760 \@@_exponent_finalise:n { #2#3 + \tl_count:n {#1} - 1 }
3761 }
3762 % \end{macrocode}
3763 % We need to branch here based on the order of magnitude.
3764 % \begin{macrocode}
3765 \cs_new:Npn \@@_round_uncertainty_threshold:nnn #1#2#3
3766 {
3767 \@@_if_threshold_exceeded:nTF {#2#3}
3768 { \@@_round_uncertainty_input:nnn }
3769 { \@@_round_uncertainty_scientific:nnn }
3770 {#1} {#2} {#3}
3771 }
3772 % \end{macrocode}
3773 % \end{macro}
3774 % \end{macro}
3775 % \end{macro}
3776 % \end{macro}
3777 % \end{macro}
3778 % \end{macro}
3779 % \end{macro}
3780 % \end{macro}
3781 % \end{macro}
3782 % \end{macro}
3783 % \end{macro}
3784 % \end{macro}
3785 % \end{macro}
3786 %
3787 % \begin{macro}{\@@_zero_decimal:NN}
3788 % \begin{macro}[EXP]{\@@_zero_decimal:nnnnnnn}
3789 % Simple stripping of the decimal part if zero.
3790 % \begin{macrocode}
3791 \cs_new_protected:Npn \@@_zero_decimal:NN #1#2
3792 {
3793 \bool_if:NT \l_@@_drop_zero_decimal_bool
3794 {
3795 \tl_set:Ne #2
3796 { \exp_after:wN \@@_zero_decimal:nnnnnnn #1 }
3797 }
3798 }
3799 \cs_new:Npn \@@_zero_decimal:nnnnnnn #1#2#3#4#5#6#7
3800 {
3801 \exp_not:n { {#1} {#2} {#3} }
3802 \bool_lazy_and:nnTF
3803 { \tl_if_empty_p:n {#5} }
3804 {
3805 \str_if_eq_p:ee
3806 { \exp_not:n {#4} }
3807 { \prg_replicate:nn { \tl_count:n {#4} } { 0 } }
3808 }
3809 { { } }
3810 { \exp_not:n { {#4} } }
3811 \exp_not:n { {#5} {#6} {#7} }
3812 }
3813 % \end{macrocode}
3814 % \end{macro}
3815 % \end{macro}
3816 %
3817 % \subsection{Number modification}
3818 %
3819 % \begin{macro}[EXP]
3820 % {\siunitx_number_adjust_exponent:nn, \siunitx_number_adjust_exponent:Vn}
3821 % \begin{macro}[EXP]{\siunitx_number_adjust_exponent:Nn}
3822 % \begin{macro}[EXP]{\@@_adjust_exp:nnnnnnnn}
3823 % \begin{macro}[EXP]{\@@_adjust_exp:nn, \@@_adjust_exp:en}
3824 % \begin{macro}[EXP]{\@@_adjust_exp:nNw}
3825 % A simply case of breaking down and rebuilding the number.
3826 % \begin{macrocode}
3827 \cs_new:Npn \siunitx_number_adjust_exponent:nn #1#2
3828 { \@@_adjust_exp:nnnnnnnn #1 {#2} }
3829 \cs_generate_variant:Nn \siunitx_number_adjust_exponent:nn { V }
3830 \cs_new:Npn \siunitx_number_adjust_exponent:Nn #1#2
3831 {
3832 \tl_if_empty:NF #1
3833 { \siunitx_number_adjust_exponent:Vn #1 {#2} }
3834 }
3835 \cs_new:Npn \@@_adjust_exp:nnnnnnnn #1#2#3#4#5#6#7#8
3836 {
3837 \exp_not:n { {#1} {#2} {#3} {#4} {#5} }
3838 \@@_adjust_exp:en { \fp_eval:n { #6#7 + #8 } } {#6}
3839 }
3840 \cs_new:Npn \@@_adjust_exp:nn #1#2
3841 { \@@_adjust_exp:nNw {#2} #1 \q_stop }
3842 \cs_generate_variant:Nn \@@_adjust_exp:nn { e }
3843 \cs_new:Npn \@@_adjust_exp:nNw #1#2#3 \q_stop
3844 {
3845 \token_if_eq_meaning:NNTF #2 -
3846 { { - } { \exp_not:n {#3} } }
3847 { { \str_if_eq:nnT {#1} { + } { + } } { \exp_not:n {#2#3} } }
3848 }
3849 % \end{macrocode}
3850 % \end{macro}
3851 % \end{macro}
3852 % \end{macro}
3853 % \end{macro}
3854 % \end{macro}
3855 %
3856 % \subsection{Outputting parsed numbers}
3857 %
3858 % \begin{variable}{\l_@@_bracket_close_tl, \l_@@_bracket_open_tl}
3859 % Purely internal for the present.
3860 % \begin{macrocode}
3861 \tl_new:N \l_@@_bracket_close_tl
3862 \tl_new:N \l_@@_bracket_open_tl
3863 \tl_set:Nn \l_@@_bracket_open_tl { ( }
3864 \tl_set:Nn \l_@@_bracket_close_tl { ) }
3865 % \end{macrocode}
3866 % \end{variable}
3867 %
3868 % \begin{variable}{\l_siunitx_number_bracket_ambiguous_bool}
3869 % \begin{macrocode}
3870 \bool_new:N \l_siunitx_number_bracket_ambiguous_bool
3871 % \end{macrocode}
3872 % \end{variable}
3873 %
3874 % \begin{variable}{\l_siunitx_number_output_decimal_tl}
3875 % \begin{macrocode}
3876 \tl_new:N \l_siunitx_number_output_decimal_tl
3877 % \end{macrocode}
3878 % \end{variable}
3879 %
3880 % \begin{variable}
3881 % {
3882 % \l_@@_bracket_negative_bool ,
3883 % \l_@@_implicit_plus_mant_bool ,
3884 % \l_@@_implicit_plus__exp_bool ,
3885 % \l_@@_exponent_base_tl ,
3886 % \l_@@_exponent_product_tl ,
3887 % \l_@@_group_size_int ,
3888 % \l_@@_group_first_int ,
3889 % \l_@@_group_min_int ,
3890 % \l_@@_group_decimal_bool ,
3891 % \l_@@_group_integer_bool ,
3892 % \l_@@_group_minimum_int ,
3893 % \l_@@_group_separator_tl ,
3894 % \l_@@_negative_color_tl ,
3895 % \l_@@_output_exp_marker_tl ,
3896 % \l_@@_output_uncert_close_tl ,
3897 % \l_@@_output_uncert_open_tl ,
3898 % \l_@@_uncert_break_bool ,
3899 % \l_@@_uncert_desc_mode_str ,
3900 % \l_@@_uncert_desc_separator_tl ,
3901 % \l_@@_uncert_desc_clist ,
3902 % \l_@@_uncert_mode_str ,
3903 % \l_@@_uncert_separator_tl ,
3904 % \l_@@_uncert_simplify_bool ,
3905 % \l_@@_tight_bool ,
3906 % \l_@@_unity_mantissa_bool ,
3907 % \l_@@_zero_exponent_bool ,
3908 % \l_@@_zero_integer_bool
3909 % }
3910 % Keys producing tokens in the output.
3911 % \begin{macrocode}
3912 \keys_define:nn { siunitx }
3913 {
3914 allow-uncertainty-breaks .bool_set:N =
3915 \l_@@_uncert_break_bool ,
3916 bracket-ambiguous-numbers .bool_set:N =
3917 \l_siunitx_number_bracket_ambiguous_bool ,
3918 bracket-negative-numbers .bool_set:N =
3919 \l_@@_bracket_negative_bool ,
3920 exponent-base .tl_set:N =
3921 \l_@@_exponent_base_tl ,
3922 exponent-product .tl_set:N =
3923 \l_@@_exponent_product_tl ,
3924 digit-group-size .meta:n =
3925 {
3926 digit-group-first-size = {#1} ,
3927 digit-group-other-size = {#1}
3928 } ,
3929 digit-group-first-size .int_set:N =
3930 \l_@@_group_first_int ,
3931 digit-group-other-size .int_set:N =
3932 \l_@@_group_size_int ,
3933 final-digit-group-min-size .int_set:N =
3934 \l_@@_group_min_int ,
3935 group-digits .choice: ,
3936 group-digits / all .code:n =
3937 {
3938 \bool_set_true:N \l_@@_group_decimal_bool
3939 \bool_set_true:N \l_@@_group_integer_bool
3940 } ,
3941 group-digits / decimal .code:n =
3942 {
3943 \bool_set_true:N \l_@@_group_decimal_bool
3944 \bool_set_false:N \l_@@_group_integer_bool
3945 } ,
3946 group-digits / integer .code:n =
3947 {
3948 \bool_set_false:N \l_@@_group_decimal_bool
3949 \bool_set_true:N \l_@@_group_integer_bool
3950 } ,
3951 group-digits / none .code:n =
3952 {
3953 \bool_set_false:N \l_@@_group_decimal_bool
3954 \bool_set_false:N \l_@@_group_integer_bool
3955 } ,
3956 group-digits .default:n = all ,
3957 group-minimum-digits .int_set:N =
3958 \l_@@_group_minimum_int ,
3959 group-separator .tl_set:N =
3960 \l_@@_group_separator_tl ,
3961 negative-color .tl_set:N =
3962 \l_@@_negative_color_tl ,
3963 output-close-uncertainty .tl_set:N =
3964 \l_@@_output_uncert_close_tl ,
3965 output-decimal-marker .tl_set:N =
3966 \l_siunitx_number_output_decimal_tl ,
3967 output-exponent-marker .tl_set:N =
3968 \l_@@_output_exp_marker_tl ,
3969 output-open-uncertainty .tl_set:N =
3970 \l_@@_output_uncert_open_tl ,
3971 print-implicit-plus .meta:n =
3972 {
3973 print-mantissa-implicit-plus = {#1} ,
3974 print-exponent-implicit-plus = {#1}
3975 } ,
3976 print-implicit-plus .default:n = true ,
3977 print-mantissa-implicit-plus .bool_set:N =
3978 \l_@@_implicit_plus_mant_bool ,
3979 print-exponent-implicit-plus .bool_set:N =
3980 \l_@@_implicit_plus_exp_bool ,
3981 print-unity-mantissa .bool_set:N =
3982 \l_@@_unity_mantissa_bool ,
3983 print-zero-exponent .bool_set:N =
3984 \l_@@_zero_exponent_bool ,
3985 print-zero-integer .bool_set:N =
3986 \l_@@_zero_integer_bool ,
3987 simplify-uncertainty .bool_set:N =
3988 \l_@@_uncert_simplify_bool ,
3989 tight-spacing .bool_set:N =
3990 \l_@@_tight_bool ,
3991 uncertainty-descriptor-mode .choices:nn =
3992 { bracket , bracket-separator , separator , subscript }
3993 { \str_set_eq:NN \l_@@_uncert_desc_mode_str \l_keys_choice_str } ,
3994 uncertainty-descriptor-separator .tl_set:N =
3995 \l_@@_uncert_desc_separator_tl ,
3996 uncertainty-descriptors .clist_set:N =
3997 \l_@@_uncert_desc_clist ,
3998 uncertainty-mode .choices:nn =
3999 { compact , compact-marker , full , separate }
4000 { \str_set_eq:NN \l_@@_uncert_mode_str \l_keys_choice_str } ,
4001 uncertainty-separator .tl_set:N =
4002 \l_@@_uncert_separator_tl ,
4003 zero-decimal-as-symbol .bool_set:N =
4004 \l_@@_zero_symbol_bool ,
4005 zero-symbol .tl_set:N = \l_@@_zero_symbol_tl
4006 }
4007 \bool_new:N \l_@@_group_decimal_bool
4008 \bool_new:N \l_@@_group_integer_bool
4009 \str_new:N \l_@@_uncert_desc_mode_str
4010 \str_new:N \l_@@_uncert_mode_str
4011 % \end{macrocode}
4012 % \end{variable}
4013 %
4014 % \begin{macro}[EXP]{\siunitx_number_output:N, \siunitx_number_output:c}
4015 % \begin{macro}[EXP]{\siunitx_number_output:n}
4016 % \begin{macro}[EXP]{\siunitx_number_output:NN, \siunitx_number_output:cN}
4017 % \begin{macro}[EXP]{\siunitx_number_output:nN}
4018 % \begin{macro}[EXP]{\@@_output:Nn}
4019 % \begin{macro}[EXP]{\@@_output:nn}
4020 % \begin{macro}[EXP]{\@@_output:nnnnnnn}
4021 % \begin{macro}[EXP]{\@@_output_bracket:nn}
4022 % \begin{macro}[EXP]{\@@_output_bracket:w}
4023 % \begin{macro}[EXP]{\@@_output_comparator:nn}
4024 % \begin{macro}[EXP]{\@@_output_sign:nnn}
4025 % \begin{macro}[EXP]{\@@_output_sign:nN}
4026 % \begin{macro}[EXP]
4027 % {\@@_output_sign_color:w, \@@_output_sign_brackets:w}
4028 % \begin{macro}[EXP]{\@@_output_integer:nnn}
4029 % \begin{macro}[EXP]{\@@_output_decimal:nn, \@@_output_decimal:en}
4030 % \begin{macro}[EXP]{\@@_output_digits:nn}
4031 % \begin{macro}[EXP]{\@@_output_digit_separator:n}
4032 % \begin{macro}[EXP]{\@@_output_digit_separator:N}
4033 % \begin{macro}[EXP]{\@@_output_integer_3_3:n}
4034 % \begin{macro}[EXP]
4035 % {
4036 % \@@_output_integer_aux_0:n,
4037 % \@@_output_integer_aux_1:n,
4038 % \@@_output_integer_aux_2:n
4039 % }
4040 % \begin{macro}[EXP]{\@@_output_integer_first:nnNN}
4041 % \begin{macro}[EXP]{\@@_output_integer_loop:NNNN}
4042 % \begin{macro}[EXP]{\@@_output_integer_first:n}
4043 % \begin{macro}[EXP]{\@@_output_integer_aux:n, \@@_output_integer_aux:e}
4044 % \begin{macro}[EXP]{\@@_output_integer_loop:NnnnN}
4045 % \begin{macro}[EXP]{\@@_output_decimal_3_3:n}
4046 % \begin{macro}[EXP]{\@@_output_decimal_loop:NNNN}
4047 % \begin{macro}[EXP]{\@@_output_decimal_3_2:n}
4048 % \begin{macro}[EXP]{\@@_output_decimal:NNNw}
4049 % \begin{macro}[EXP]{\@@_output_decimal_loop:NN}
4050 % \begin{macro}[EXP]{\@@_output_decimal_first:n}
4051 % \begin{macro}[EXP]{\@@_output_decimal_loop:NnnnN}
4052 % \begin{macro}[EXP]{\@@_output_uncertainty:nnn}
4053 % \begin{macro}[EXP]{\@@_output_uncertainty_unaligned:n}
4054 % \begin{macro}[EXP]{\@@_output_uncert:nnnn}
4055 % \begin{macro}[EXP]{\@@_output_uncert:nnnnn, \@@_output_uncert:Vnnnn}
4056 % \begin{macro}[EXP]{\@@_output_uncert_first:nnN, \@@_output_uncert_loop:nnN}
4057 % \begin{macro}[EXP]{\@@_output_uncert_loop:nnNw}
4058 % \begin{macro}[EXP]
4059 % {
4060 % \@@_output_uncert_desc_bracket:n ,
4061 % \@@_output_uncert_desc_bracket-separator:n ,
4062 % \@@_output_uncert_desc_separator:n ,
4063 % \@@_output_uncert_desc_subscript:n
4064 % }
4065 % \begin{macro}[EXP]{\@@_output_uncert_A_loop:nnn, \@@_output_uncert_S_loop:nnn}
4066 % \begin{macro}[EXP]{\@@_output_uncert_S_loop:w}
4067 % \begin{macro}[EXP]{\@@_output_uncert_A:nnnn, \@@_output_uncert_A_multi:nnnn}
4068 % \begin{macro}[EXP]{\@@_output_uncert_A:nnnnn}
4069 % \begin{macro}[EXP]{\@@_output_uncert_A_aux:nnnn}
4070 % \begin{macro}[EXP]
4071 % {
4072 % \@@_output_uncert_S:nnnn ,
4073 % \@@_output_uncert_S_sep:nnnn ,
4074 % \@@_output_uncert_S_multi:nnnn
4075 % }
4076 % \begin{macro}[EXP]
4077 % {
4078 % \@@_output_uncert_S_compact:nn ,
4079 % \@@_output_uncert_S_compact-marker:nn ,
4080 % \@@_output_uncert_S_full:nn
4081 % }
4082 % \begin{macro}[EXP]
4083 % {\@@_output_uncert_augment:nnnn}
4084 % \begin{macro}[EXP]
4085 % {\@@_output_uncert_augment:nnn, \@@_output_uncert_augment:enn}
4086 % \begin{macro}[EXP]
4087 % {\@@_output_uncert_augment:nnnw, \@@_output_uncert_augment:ennw}
4088 % \begin{macro}[EXP]{\@@_output_uncert_augment:nnw}
4089 % \begin{macro}[EXP]
4090 % {
4091 % \@@_output_exponent:nnnnn ,
4092 % \@@_output_exponent_auxi:nnnnn ,
4093 % \@@_output_exponent_auxii:nnnnn ,
4094 % \@@_output_exponent_auxiii:nnnnn
4095 % }
4096 % \begin{macro}[EXP]{\@@_output_exponent_auxiv:nn}
4097 % \begin{macro}[EXP]{\@@_output_end:}
4098 % The approach to formatting a single number is to split into
4099 % the constituent parts. All of the parts are assembled including
4100 % inserting tabular alignment markers (which may be empty) for each
4101 % separate unit.
4102 % \begin{macrocode}
4103 \cs_new:Npn \siunitx_number_output:N #1
4104 { \@@_output:Nn #1 { } }
4105 \cs_generate_variant:Nn \siunitx_number_output:N { c }
4106 \cs_new:Npn \siunitx_number_output:n #1
4107 { \@@_output:nn #1 { } }
4108 \cs_new:Npn \siunitx_number_output:NN #1#2
4109 { \@@_output:Nn #1 {#2} }
4110 \cs_generate_variant:Nn \siunitx_number_output:NN { c }
4111 \cs_new:Npn \siunitx_number_output:nN #1#2
4112 { \@@_output:nn #1 {#2} }
4113 \cs_new:Npn \@@_output:Nn #1#2
4114 {
4115 \tl_if_empty:NF #1
4116 { \exp_after:wN \@@_output:nnnnnnn #1 {#2} }
4117 }
4118 \cs_new:Npn \@@_output:nn #1#2
4119 {
4120 \tl_if_empty:nF {#1}
4121 { \@@_output:nnnnnnn #1 {#2} }
4122 }
4123 \cs_new:Npn \@@_output:nnnnnnn #1#2#3#4#5#6#7#8
4124 {
4125 \@@_output_color:n {#2}
4126 \@@_output_comparator:nn {#1} {#8}
4127 \@@_output_bracket:nn {#5} {#7}
4128 \@@_output_sign:nnn {#1} {#2} {#8}
4129 \@@_output_integer:nnn {#3} {#4} {#7}
4130 \@@_output_decimal:nn {#4} {#8}
4131 \@@_output_uncertainty:nnn {#5} {#4} {#8}
4132 \@@_output_exponent:nnnnn {#6} {#7} {#3} {#4} {#8}
4133 \@@_output_end:
4134 }
4135 % \end{macrocode}
4136 % Adding brackets for the combination of a separate uncertainty with an
4137 % exponent may need brackets. This needs testing up-front, so has to come
4138 % before the main formatting routines.
4139 % \begin{macrocode}
4140 \cs_new:Npn \@@_output_bracket:nn #1#2
4141 {
4142 \bool_lazy_all:nT
4143 {
4144 { \str_if_eq_p:Vn \l_@@_uncert_mode_str { separate } }
4145 { \l_siunitx_number_bracket_ambiguous_bool }
4146 { ! \tl_if_blank_p:n {#1} }
4147 {
4148 \bool_lazy_or_p:nn
4149 { \l_@@_zero_exponent_bool }
4150 {
4151 ! \bool_lazy_or_p:nn
4152 { \tl_if_blank_p:n {#2} }
4153 { \str_if_eq_p:nn {#2} { 0 } }
4154 }
4155 }
4156 }
4157 \@@_output_bracket:w
4158 }
4159 \cs_new:Npn \@@_output_bracket:w #1 \@@_output_exponent:nnnnn
4160 {
4161 \exp_not:V \l_@@_bracket_open_tl
4162 #1
4163 \exp_not:V \l_@@_bracket_close_tl
4164 \@@_output_exponent:nnnnn
4165 }
4166 % \end{macrocode}
4167 % As color for negative values applies to the \emph{whole} output, we have
4168 % to deal with it before anything else.
4169 % \begin{macrocode}
4170 \cs_new:Npn \@@_output_color:n #1
4171 {
4172 \bool_lazy_and:nnT
4173 { \str_if_eq_p:nn {#1} { - } }
4174 { ! \tl_if_empty_p:N \l_@@_negative_color_tl }
4175 { \exp_not:N \color { \exp_not:V \l_@@_negative_color_tl } }
4176 }
4177 % \end{macrocode}
4178 % To get the spacing correct this needs to be an ordinary math character.
4179 % \begin{macrocode}
4180 \cs_new:Npn \@@_output_comparator:nn #1#2
4181 {
4182 \tl_if_blank:nF {#1}
4183 { \exp_not:n { \mathord {#1} } }
4184 \exp_not:n {#2}
4185 }
4186 % \end{macrocode}
4187 % Formatting signs has to deal with some additional formatting requirements
4188 % for negative numbers. Making such numbers by bracketing them needs some
4189 % rearrangement of the order of tokens, which is set up in the main
4190 % formatting macro by the dedicated do-nothing end function. We also have
4191 % the comparator passed here: if it is present, we need to deal with
4192 % tighter spacing.
4193 % \begin{macrocode}
4194 \cs_new:Npn \@@_output_sign:nnn #1#2#3
4195 {
4196 \tl_if_blank:nTF {#2}
4197 {
4198 \bool_if:NT \l_@@_implicit_plus_mant_bool
4199 { \@@_output_sign:nN {#1} + }
4200 }
4201 {
4202 \str_if_eq:nnTF {#2} { - }
4203 {
4204 \bool_if:NTF \l_@@_bracket_negative_bool
4205 { \@@_output_sign_brackets:w }
4206 { \@@_output_sign:nN {#1} - }
4207 }
4208 { \@@_output_sign:nN {#1} #2 }
4209 }
4210 \exp_not:n {#3}
4211 }
4212 \cs_new:Npn \@@_output_sign:nN #1#2
4213 {
4214 \tl_if_blank:nTF {#1}
4215 { \use:n }
4216 { \exp_not:N \mathord }
4217 { \exp_not:n {#2} }
4218 }
4219 \cs_new:Npn
4220 \@@_output_sign_brackets:w #1 \@@_output_end:
4221 {
4222 \exp_not:V \l_@@_bracket_open_tl
4223 #1
4224 \exp_not:V \l_@@_bracket_close_tl
4225 \@@_output_end:
4226 }
4227 % \end{macrocode}
4228 % Digit formatting leads off with separate functions to allow for a few
4229 % \enquote{up front} items before using a common set of tests for some common
4230 % cases. The code then splits again as the two types of grouping need
4231 % different strategies.
4232 % \begin{macrocode}
4233 \cs_new:Npn \@@_output_integer:nnn #1#2#3
4234 {
4235 \tl_if_blank:nTF {#1}
4236 {
4237 \bool_lazy_and:nnT
4238 { \str_if_eq_p:nn {#3} { 0 } }
4239 { ! \l_@@_zero_exponent_bool }
4240 { \@@_output_digits:nn { integer } { 1 } }
4241 }
4242 {
4243 \bool_lazy_or:nnT
4244 { \l_@@_zero_integer_bool }
4245 { ! \str_if_eq_p:nn {#1} { 0 } }
4246 {
4247 \bool_lazy_any:nT
4248 {
4249 { \l_@@_unity_mantissa_bool }
4250 { ! \str_if_eq_p:nn { #1 . #2 } { 1. } }
4251 {
4252 \bool_lazy_and_p:nn
4253 {
4254 \bool_lazy_or_p:nn
4255 { \tl_if_blank_p:n {#3} }
4256 { \str_if_eq_p:nn {#3} { 0 } }
4257 }
4258 { ! \l_@@_zero_exponent_bool }
4259 }
4260 {
4261 \bool_lazy_and_p:nn
4262 { \str_if_eq_p:nn {#1} { 0 } }
4263 { \l_@@_zero_integer_bool }
4264 }
4265 }
4266 { \@@_output_digits:nn { integer } {#1} }
4267 }
4268 }
4269 }
4270 \cs_new:Npn \@@_output_decimal:nn #1#2
4271 {
4272 \exp_not:n {#2}
4273 \tl_if_blank:nF {#1}
4274 {
4275 \str_if_eq:VnTF \l_siunitx_number_output_decimal_tl { , }
4276 { \exp_not:N \mathord }
4277 { \use:n }
4278 { \exp_not:V \l_siunitx_number_output_decimal_tl }
4279 }
4280 \exp_not:n {#2}
4281 \str_if_eq:nnF {#1} { \empty }
4282 {
4283 \bool_lazy_and:nnTF
4284 { \l_@@_zero_symbol_bool }
4285 {
4286 \str_if_eq_p:ee
4287 {#1}
4288 { \prg_replicate:nn { \tl_count:n {#1} } { 0 } }
4289 }
4290 { \exp_not:V \l_@@_zero_symbol_tl }
4291 { \@@_output_digits:nn { decimal } {#1} }
4292 }
4293 }
4294 \cs_generate_variant:Nn \@@_output_decimal:nn { e }
4295 \cs_new:Npn \@@_output_digits:nn #1#2
4296 {
4297 \bool_if:cTF { l_@@_group_ #1 _ bool }
4298 {
4299 \int_compare:nNnTF
4300 { \tl_count:n {#2} } < \l_@@_group_minimum_int
4301 { \exp_not:n {#2} }
4302 {
4303 \int_compare:nNnTF \l_@@_group_min_int < 2
4304 {
4305 \cs_if_exist_use:cF
4306 {
4307 @@_output_ #1 _
4308 \int_use:N \l_@@_group_first_int
4309 _
4310 \int_use:N \l_@@_group_size_int
4311 :n
4312 }
4313 { \use:c { @@_output_ #1 _first:n } }
4314 }
4315 { \use:c { @@_output_ #1 _first:n } }
4316 {#2}
4317 }
4318 }
4319 { \exp_not:n {#2} }
4320 }
4321 % \end{macrocode}
4322 % Conditionally add the separator.
4323 % \begin{macrocode}
4324 \cs_new:Npn \@@_output_digit_separator:n #1
4325 {
4326 \int_compare:nNnF { \tl_count:n {#1} } < \l_@@_group_min_int
4327 { \@@_output_digit_separator:N \l_@@_group_separator_tl }
4328 }
4329 % \end{macrocode}
4330 % An auxiliary to ensure spacing is correct.
4331 % \begin{macrocode}
4332 \cs_new:Npn \@@_output_digit_separator:N #1
4333 {
4334 \str_if_eq:VnTF #1 { , }
4335 { \exp_not:N \mathord }
4336 { \use:n }
4337 { \exp_not:V #1 }
4338 }
4339 % \end{macrocode}
4340 % For standard grouping of integers, we need to know how many digits there
4341 % are to allow for the correct insertion of separators. That is done using
4342 % a two-part set up such that there is no separator on the first pass.
4343 % \begin{macrocode}
4344 \cs_new:cpn { @@_output_integer_3_3:n } #1
4345 {
4346 \use:c
4347 {
4348 @@_output_integer_aux_
4349 \int_eval:n { \int_mod:nn { \tl_count:n {#1} } { 3 } }
4350 :n
4351 } {#1}
4352 }
4353 \cs_new:cpn { @@_output_integer_aux_0:n } #1
4354 { \@@_output_integer_first:nnNN #1 \q_nil }
4355 \cs_new:cpn { @@_output_integer_aux_1:n } #1
4356 { \@@_output_integer_first:nnNN { } { } #1 \q_nil }
4357 \cs_new:cpn { @@_output_integer_aux_2:n } #1
4358 { \@@_output_integer_first:nnNN { } #1 \q_nil }
4359 \cs_new:Npn \@@_output_integer_first:nnNN #1#2#3#4
4360 {
4361 \exp_not:n {#1#2#3}
4362 \quark_if_nil:NF #4
4363 { \@@_output_integer_loop:NNNN #4 }
4364 }
4365 \cs_new:Npn \@@_output_integer_loop:NNNN #1#2#3#4
4366 {
4367 \@@_output_digit_separator:N \l_@@_group_separator_tl
4368 \exp_not:n {#1#2#3}
4369 \quark_if_nil:NF #4
4370 { \@@_output_integer_loop:NNNN #4 }
4371 }
4372 % \end{macrocode}
4373 % There is no clever way of doing an uneven integer grouping, so just provide
4374 % a slow generic approach. This is more-or-less the same as the generic
4375 % decimal code, but with the output reversed. The only wrinkle is we need to
4376 % reverse the entire input again, so it has to be carried as an argument.
4377 % \begin{macrocode}
4378 \cs_new:Npn \@@_output_integer_first:n #1
4379 {
4380 \@@_output_integer_aux:e { \tl_reverse:n {#1} }
4381 }
4382 \cs_new:Npn \@@_output_integer_aux:n #1
4383 {
4384 \@@_output_integer_loop:NnnnN \l_@@_group_first_int { 0 } { } { }
4385 #1 \q_recursion_tail \q_recursion_stop
4386 }
4387 \cs_generate_variant:Nn \@@_output_integer_aux:n { e }
4388 \cs_new:Npn \@@_output_integer_loop:NnnnN #1#2#3#4#5
4389 {
4390 \quark_if_recursion_tail_stop_do:Nn #5
4391 {
4392 \exp_not:n {#4}
4393 \@@_output_digit_separator:n {#4}
4394 #3
4395 }
4396 \int_compare:nNnTF { #2 + 1 } > #1
4397 {
4398 \@@_output_integer_loop:NnnnN
4399 \l_@@_group_size_int { 1 }
4400 {
4401 \exp_not:n {#4}
4402 \tl_if_blank:nF {#3}
4403 { \@@_output_digit_separator:n {#4} }
4404 #3
4405 }
4406 {#5}
4407 }
4408 { \@@_output_integer_loop:NnnnN #1 { #2 + 1 } {#3} { #5 #4 } }
4409 }
4410 % \end{macrocode}
4411 % For standard decimal grouping, no need to do any counting, just loop using
4412 % enough markers to find the end of the list. By passing the decimal marker,
4413 % it is possible not to have to use a check on the content of the rest of
4414 % the number. The |\use_none:n(n)| mop up the remaining |\q_nil| tokens.
4415 % \begin{macrocode}
4416 \cs_new:cpn { @@_output_decimal_3_3:n } #1
4417 {
4418 \@@_output_decimal_loop:NNNN \c_empty_tl
4419 #1 \q_nil \q_nil \q_nil
4420 }
4421 \cs_new:Npn \@@_output_decimal_loop:NNNN #1#2#3#4
4422 {
4423 \quark_if_nil:NF #2
4424 {
4425 \@@_output_digit_separator:N #1
4426 \exp_not:n {#2}
4427 \quark_if_nil:NTF #3
4428 { \use_none:n }
4429 {
4430 \exp_not:n {#3}
4431 \quark_if_nil:NTF #4
4432 { \use_none:nn }
4433 {
4434 \exp_not:n {#4}
4435 \@@_output_decimal_loop:NNNN
4436 \l_@@_group_separator_tl
4437 }
4438 }
4439 }
4440 }
4441 % \end{macrocode}
4442 % The same trick for the group-of-two case, but as the first pass is already
4443 % done, there is not need to worry about the separator.
4444 % \begin{macrocode}
4445 \cs_new:cpn { @@_output_decimal_3_2:n } #1
4446 {
4447 \int_compare:nNnTF { \tl_count:n {#1} } > 2
4448 { \@@_output_decimal:NNNw #1 \q_stop }
4449 { \exp_not:n {#1} }
4450 }
4451 \cs_new:Npn \@@_output_decimal:NNNw #1#2#3#4 \q_stop
4452 {
4453 \exp_not:n {#1#2#3}
4454 \@@_output_decimal_loop:NN #4 \q_nil \q_nil
4455 }
4456 \cs_new:Npn \@@_output_decimal_loop:NN #1#2
4457 {
4458 \quark_if_nil:NF #1
4459 {
4460 \@@_output_digit_separator:N
4461 \l_@@_group_separator_tl
4462 \exp_not:n {#1}
4463 \quark_if_nil:NTF #2
4464 { \use_none:n }
4465 {
4466 \exp_not:n {#2}
4467 \@@_output_decimal_loop:NN
4468 }
4469 }
4470 }
4471 % \end{macrocode}
4472 % The generic decimal separator: simply count the digits.
4473 % \begin{macrocode}
4474 \cs_new:Npn \@@_output_decimal_first:n #1
4475 {
4476 \@@_output_decimal_loop:NnnnN \l_@@_group_first_int { 0 } { } { }
4477 #1 \q_recursion_tail \q_recursion_stop
4478 }
4479 \cs_new:Npn \@@_output_decimal_loop:NnnnN #1#2#3#4#5
4480 {
4481 \quark_if_recursion_tail_stop_do:Nn #5
4482 {
4483 \exp_not:n {#3}
4484 \@@_output_digit_separator:n {#4}
4485 \exp_not:n {#4}
4486 }
4487 \int_compare:nNnTF { #2 + 1 } > #1
4488 {
4489 \exp_not:n {#3}
4490 \tl_if_blank:nF {#3}
4491 { \@@_output_digit_separator:n {#4} }
4492 \@@_output_decimal_loop:NnnnN \l_@@_group_size_int { 1 } {#4} {#5}
4493 }
4494 { \@@_output_decimal_loop:NnnnN #1 { #2 + 1 } {#3} { #4#5 } }
4495 }
4496 % \end{macrocode}
4497 % The lead-off here is to deal with the common cases: no uncertainty at all
4498 % or a single uncertainty. Otherwise we have to move to a mapping.
4499 % \begin{macrocode}
4500 \cs_new:Npn \@@_output_uncertainty:nnn #1#2#3
4501 {
4502 \tl_if_blank:nTF {#1}
4503 { \@@_output_uncertainty_unaligned:n {#3} }
4504 {
4505 \cs_if_exist:cTF
4506 { @@_output_uncert_ \tl_head:n {#1} :nnnn }
4507 {
4508 \use:c { @@_output_uncert_ \tl_head:n {#1} :nnnn }
4509 {#2} {#3} #1
4510 }
4511 { \@@_output_uncert:nnnn {#2} {#3} #1 }
4512 }
4513 }
4514 \cs_new:Npn \@@_output_uncertainty_unaligned:n #1
4515 { \exp_not:n { #1 #1 #1 #1 } }
4516 % \end{macrocode}
4517 % For handling a list of uncertainties, we also need the descriptors. There
4518 % is no way to reasonably deal with alignment of an open-ended list, so
4519 % the treatment is as-for no uncertainty at all.
4520 % \begin{macrocode}
4521 \cs_new:Npn \@@_output_uncert:nnnn #1#2#3#4
4522 {
4523 \@@_output_uncert:Vnnnn \l_@@_uncert_desc_clist
4524 {#1} {#2} {#3} {#4}
4525 }
4526 \cs_new:Npn \@@_output_uncert:nnnnn #1#2#3#4#5
4527 {
4528 \@@_output_uncert_first:nnN {#2} {#3}
4529 #4 \q_recursion_tail #1 , \q_recursion_stop {#5}
4530 }
4531 \cs_generate_variant:Nn \@@_output_uncert:nnnnn { V }
4532 \cs_new:Npn \@@_output_uncert_first:nnN #1#2#3
4533 { \@@_output_uncert_loop:nnNw {#1} {#2} #3 }
4534 \cs_new:Npn \@@_output_uncert_loop:nnN #1#2#3
4535 {
4536 \quark_if_recursion_tail_stop:N #3
4537 \@@_output_uncert_loop:nnNw {#1} { } #3
4538 }
4539 \cs_new:Npn \@@_output_uncert_loop:nnNw
4540 #1#2#3#4 \q_recursion_tail #5 , #6 \q_recursion_stop #7
4541 {
4542 \use:c { @@_output_uncert_ #3 _loop:nnn } {#1} {#2} {#7}
4543 \tl_if_blank:nF {#5}
4544 { \use:c { @@_output_uncert_desc_ \l_@@_uncert_desc_mode_str :n } {#5} }
4545 \@@_output_uncert_loop:nnN {#1} {#2} #4
4546 \q_recursion_tail #6 , \q_recursion_stop
4547 }
4548 \cs_new:Npn \@@_output_uncert_desc_bracket:n #1
4549 {
4550 \exp_not:V \l_@@_bracket_open_tl
4551 \exp_not:V \l_siunitx_unit_font_tl
4552 { \exp_not:n {#1} }
4553 \exp_not:V \l_@@_bracket_close_tl
4554 }
4555 \cs_new:cpn { @@_output_uncert_desc_bracket-separator:n } #1
4556 {
4557 \exp_not:V \l_@@_uncert_desc_separator_tl
4558 \exp_not:V \l_@@_bracket_open_tl
4559 \exp_not:V \l_siunitx_unit_font_tl
4560 { \exp_not:n {#1} }
4561 \exp_not:V \l_@@_bracket_close_tl
4562 }
4563 \cs_new:Npn \@@_output_uncert_desc_separator:n #1
4564 {
4565 \exp_not:V \l_@@_uncert_desc_separator_tl
4566 \exp_not:V \l_siunitx_unit_font_tl
4567 { \exp_not:n {#1} }
4568 }
4569 \cs_new:Npe \@@_output_uncert_desc_subscript:n #1
4570 {
4571 { }
4572 \char_generate:nn { `\_ } { 8 }
4573 {
4574 \exp_not:N \exp_not:V \exp_not:N \l_siunitx_unit_font_tl
4575 { \exp_not:N \exp_not:n {#1} }
4576 }
4577 }
4578 % \end{macrocode}
4579 % Here, we have to tidy up so the alignment point only applies to the first
4580 % |S|-type uncertainty.
4581 % \begin{macrocode}
4582 \cs_new:Npn \@@_output_uncert_A_loop:nnn #1#2#3
4583 { \@@_output_uncert_A_multi:nnnn {#1} {#2} { A } {#3} }
4584 \cs_new:Npn \@@_output_uncert_S_loop:nnn #1#2#3
4585 {
4586 \@@_output_uncert_S_multi:nnnn {#1} {#2} { S } {#3}
4587 \@@_output_uncert_S_loop:w
4588 }
4589 \cs_new:Npn \@@_output_uncert_S_loop:w #1 \@@_output_uncert_loop:nnN #2#3
4590 {
4591 #1
4592 \@@_output_uncert_loop:nnN {#2} { }
4593 }
4594 % \end{macrocode}
4595 % Printing an asymmetrical uncertainty is more straight-forward as they are
4596 % always given in a single format. The only issue is handling the catcode.
4597 % \begin{macrocode}
4598 \cs_new:Npn \@@_output_uncert_A:nnnn #1#2#3#4
4599 { \@@_output_uncert_A:nnnnn {#1} {#2} {#3} #4 }
4600 \cs_new_eq:NN \@@_output_uncert_A_multi:nnnn
4601 \@@_output_uncert_A:nnnn
4602 \cs_new:Npn \@@_output_uncert_A:nnnnn #1#2#3#4#5
4603 {
4604 \bool_lazy_and:nnTF
4605 { \l_@@_uncert_simplify_bool }
4606 { \str_if_eq_p:nn {#4} {#5} }
4607 { \@@_output_uncert_S_sep:nnnn {#1} {#2} { } {#4} }
4608 { \@@_output_uncert_A_aux:nnnn {#1} {#2} {#4} {#5} }
4609 }
4610 \cs_new:Npe \@@_output_uncert_A_aux:nnnn #1#2#3#4
4611 {
4612 { }
4613 ^
4614 {
4615 +
4616 \exp_not:N \@@_output_uncert_augment:nnnn
4617 {#3} {#1} {#3} { }
4618 }
4619 \char_generate:nn { `\_ } { 8 }
4620 {
4621 -
4622 \exp_not:N \@@_output_uncert_augment:nnnn
4623 {#4} {#1} {#4} { }
4624 }
4625 \exp_not:N \@@_output_uncertainty_unaligned:n {#2}
4626 }
4627 % \end{macrocode}
4628 % Uncertainties which are directly attached are easy to deal with. For those
4629 % that are separated, the first step is to find if they are entirely
4630 % contained within the decimal part, and to pad if they are. For the case
4631 % where the boundary is crossed to the integer part, the correct number of
4632 % digit tokens need to be removed from the start of the uncertainty and
4633 % the split result sent to the appropriate auxiliaries.
4634 % \begin{macrocode}
4635 \cs_new:Npn \@@_output_uncert_S:nnnn #1#2#3#4
4636 {
4637 \str_if_eq:VnTF \l_@@_uncert_mode_str { separate }
4638 { \@@_output_uncert_S_sep:nnnn {#1} {#2} {#3} {#4} }
4639 {
4640 \exp_not:V \l_@@_uncert_separator_tl
4641 \exp_not:V \l_@@_output_uncert_open_tl
4642 \use:c { @@_output_uncert_S_ \l_@@_uncert_mode_str :nn } {#1} {#4}
4643 \exp_not:V \l_@@_output_uncert_close_tl
4644 \@@_output_uncertainty_unaligned:n {#2}
4645 }
4646 }
4647 \cs_new:Npn \@@_output_uncert_S_sep:nnnn #1#2#3#4
4648 {
4649 \exp_not:n {#2}
4650 \bool_if:NTF \l_@@_tight_bool
4651 { \exp_not:N \mathord }
4652 { \use:n }
4653 { \pm }
4654 \bool_if:NF \l_@@_uncert_break_bool { \exp_not:N \nobreak }
4655 \exp_not:n {#2}
4656 \@@_output_uncert_augment:nnnn {#4} {#1} {#4} {#2}
4657 }
4658 \cs_new_eq:NN \@@_output_uncert_S_multi:nnnn
4659 \@@_output_uncert_S_sep:nnnn
4660 % \end{macrocode}
4661 % Handle the content of brackets: the only complex case is the
4662 % mixed situation.
4663 % \begin{macrocode}
4664 \cs_new:Npn \@@_output_uncert_S_compact:nn #1#2
4665 { \exp_not:n {#2} }
4666 \cs_new:cpn { @@_output_uncert_S_compact-marker:nn } #1#2
4667 {
4668 \bool_lazy_or:nnTF
4669 { \tl_if_blank_p:n {#1} }
4670 { ! \int_compare_p:nNn { \tl_count:n {#2} } > { \tl_count:n {#1} } }
4671 { \@@_output_uncert_S_compact:nn }
4672 { \@@_output_uncert_S_full:nn }
4673 {#1} {#2}
4674 }
4675 \cs_new:Npn \@@_output_uncert_S_full:nn #1#2
4676 {
4677 \@@_output_uncert_augment:nnnn {#2} {#1} {#2} { }
4678 }
4679 % \end{macrocode}
4680 % Convert the internal (short) form of an uncertainty to the longer form.
4681 % \begin{macrocode}
4682 \cs_new:Npn \@@_output_uncert_augment:nnnn #1#2#3#4
4683 {
4684 \@@_output_uncert_augment:enn
4685 { \int_eval:n { \tl_count:n {#1} - \tl_count:n {#2} } }
4686 {#3} {#4}
4687 }
4688 \cs_new:Npn \@@_output_uncert_augment:nnn #1#2#3
4689 {
4690 \int_compare:nNnTF {#1} > 0
4691 {
4692 \@@_output_uncert_augment:ennw
4693 { \int_eval:n { #1 - 1 } }
4694 {#3}
4695 { }
4696 #2 \q_nil
4697 }
4698 {
4699 0
4700 \@@_output_decimal:en
4701 {
4702 \prg_replicate:nn { \int_abs:n {#1} } { 0 }
4703 #2
4704 }
4705 {#3}
4706 }
4707 }
4708 \cs_generate_variant:Nn \@@_output_uncert_augment:nnn { e }
4709 \cs_new:Npn \@@_output_uncert_augment:nnnw #1#2#3#4
4710 {
4711 \quark_if_nil:NF #4
4712 {
4713 \int_compare:nNnTF {#1} = 0
4714 { \@@_output_uncert_augment:nnw {#3#4} {#2} }
4715 {
4716 \@@_output_uncert_augment:ennw
4717 { \int_eval:n { #1 - 1 } }
4718 {#2}
4719 {#3#4}
4720 }
4721 }
4722 }
4723 \cs_generate_variant:Nn \@@_output_uncert_augment:nnnw { e }
4724 \cs_new:Npn \@@_output_uncert_augment:nnw #1#2#3 \q_nil
4725 {
4726 \@@_output_digits:nn { integer } {#1}
4727 \@@_output_decimal:nn {#3} {#2}
4728 }
4729 % \end{macrocode}
4730 % Setting the exponent part requires some information about the mantissa:
4731 % was it there or not. This means that whilst only the sign and value for
4732 % the exponent are typeset here, there is a need to also have access to the
4733 % combined mantissa part (with a decimal marker). The rest of the work is
4734 % about picking up the various options and getting the combinations right.
4735 % For signs, the auxiliary from the main sign routine can be used, but not
4736 % the main function: negative exponents don't have special handling.
4737 % \begin{macrocode}
4738 \cs_new:Npn \@@_output_exponent:nnnnn #1#2#3#4#5
4739 {
4740 \exp_not:n {#5}
4741 \str_if_empty:nTF {#2}
4742 {
4743 \bool_if:NTF \l_@@_zero_exponent_bool
4744 { \@@_output_exponent_auxi:nnnnn {#1} { 0 } {#3} {#4} {#5} }
4745 { \exp_not:n {#5} }
4746 }
4747 { \@@_output_exponent_auxi:nnnnn {#1} {#2} {#3} {#4} {#5} }
4748 }
4749 \cs_new:Npn \@@_output_exponent_auxi:nnnnn #1#2#3#4#5
4750 {
4751 \bool_lazy_or:nnTF
4752 { \l_@@_zero_exponent_bool }
4753 { ! \str_if_eq_p:nn {#2} { 0 } }
4754 {
4755 \tl_if_empty:NTF \l_@@_output_exp_marker_tl
4756 { \@@_output_exponent_auxii:nnnnn }
4757 { \@@_output_exponent_auxiii:nnnnn }
4758 {#1} {#2} {#3} {#4} {#5}
4759 }
4760 { \exp_not:n {#5} }
4761 }
4762 \cs_new:Npn \@@_output_exponent_auxii:nnnnn #1#2#3#4#5
4763 {
4764 \bool_lazy_or:nnT
4765 {
4766 \bool_lazy_and_p:nn
4767 { \l_@@_unity_mantissa_bool }
4768 { \str_if_eq_p:nn { #3 . #4 } { 1. } }
4769 }
4770 {
4771 ! \bool_lazy_or_p:nn
4772 { \str_if_eq_p:nn { #3 . #4 } { 1. } }
4773 { \tl_if_blank_p:n {#3} }
4774 }
4775 {
4776 \bool_if:NTF \l_@@_tight_bool
4777 { \exp_not:N \mathord }
4778 { \use:n }
4779 { \exp_not:V \l_@@_exponent_product_tl }
4780 }
4781 \exp_not:n {#5}
4782 \exp_not:V \l_@@_exponent_base_tl
4783 ^
4784 { \@@_output_exponent_auxiv:nnn { } {#1} {#2} }
4785 }
4786 \cs_new:Npn \@@_output_exponent_auxiii:nnnnn #1#2#3#4#5
4787 {
4788 \exp_not:n {#5}
4789 \exp_not:V \l_@@_output_exp_marker_tl
4790 \@@_output_exponent_auxiv:nnn { \mathord } {#1} {#2}
4791 }
4792 \cs_new:Npn \@@_output_exponent_auxiv:nnn #1#2#3
4793 {
4794 \tl_if_blank:nTF {#2}
4795 {
4796 \bool_lazy_and:nnT
4797 { \l_@@_implicit_plus_exp_bool }
4798 { ! \str_if_eq_p:nn {#3} { 0 } }
4799 { #1 + }
4800 }
4801 { \exp_not:n {#1#2} }
4802 \@@_output_digits:nn { integer } {#3}
4803 }
4804 % \end{macrocode}
4805 % A do-nothing marker used to allow shuffling of the output and so expandable
4806 % operations for formatting.
4807 % \begin{macrocode}
4808 \cs_new:Npn \@@_output_end: { }
4809 % \end{macrocode}
4810 % \end{macro}
4811 % \end{macro}
4812 % \end{macro}
4813 % \end{macro}
4814 % \end{macro}
4815 % \end{macro}
4816 % \end{macro}
4817 % \end{macro}
4818 % \end{macro}
4819 % \end{macro}
4820 % \end{macro}
4821 % \end{macro}
4822 % \end{macro}
4823 % \end{macro}
4824 % \end{macro}
4825 % \end{macro}
4826 % \end{macro}
4827 % \end{macro}
4828 % \end{macro}
4829 % \end{macro}
4830 % \end{macro}
4831 % \end{macro}
4832 % \end{macro}
4833 % \end{macro}
4834 % \end{macro}
4835 % \end{macro}
4836 % \end{macro}
4837 % \end{macro}
4838 % \end{macro}
4839 % \end{macro}
4840 % \end{macro}
4841 % \end{macro}
4842 % \end{macro}
4843 % \end{macro}
4844 % \end{macro}
4845 % \end{macro}
4846 % \end{macro}
4847 % \end{macro}
4848 % \end{macro}
4849 % \end{macro}
4850 % \end{macro}
4851 % \end{macro}
4852 % \end{macro}
4853 % \end{macro}
4854 % \end{macro}
4855 % \end{macro}
4856 % \end{macro}
4857 % \end{macro}
4858 % \end{macro}
4859 % \end{macro}
4860 % \end{macro}
4861 % \end{macro}
4862 % \end{macro}
4863 %
4864 % \subsection{Miscellaneous tools}
4865 %
4866 % \begin{variable}{\l_@@_valid_tl}
4867 % The list of valid tokens.
4868 % \begin{macrocode}
4869 \tl_new:N \l_@@_valid_tl
4870 % \end{macrocode}
4871 % \end{variable}
4872 %
4873 % \begin{macro}[TF]{\siunitx_if_number:n}
4874 % Test if an entire number is valid: this means parsing the number but not
4875 % returning anything.
4876 % \begin{macrocode}
4877 \prg_new_protected_conditional:Npnn \siunitx_if_number:n #1
4878 { T , F , TF }
4879 {
4880 \group_begin:
4881 \bool_set_true:N \l_@@_validate_bool
4882 \bool_set_true:N \l_siunitx_number_parse_bool
4883 \siunitx_number_parse:nN {#1} \l_@@_parsed_tl
4884 \tl_if_empty:NTF \l_@@_parsed_tl
4885 {
4886 \group_end:
4887 \prg_return_false:
4888 }
4889 {
4890 \group_end:
4891 \prg_return_true:
4892 }
4893 }
4894 % \end{macrocode}
4895 % \end{macro}
4896 %
4897 % \begin{macro}[pTF, EXP]{\siunitx_if_number_token:N}
4898 % \begin{macro}[EXP]
4899 % {
4900 % \@@_if_token_auxi:NN ,
4901 % \@@_if_token_auxii:NN ,
4902 % \@@_if_token_auxiii:NN
4903 % }
4904 % A simple conditional to answer the question of whether a specific token is
4905 % possibly valid in a number.
4906 % \begin{macrocode}
4907 \prg_new_conditional:Npnn \siunitx_if_number_token:N #1
4908 { p , T , F , TF }
4909 {
4910 \@@_token_auxi:NN #1
4911 \l_siunitx_number_input_decimal_tl
4912 \l_@@_input_uncert_close_tl
4913 \l_siunitx_number_input_comparator_tl
4914 \l_@@_input_digit_tl
4915 \l_siunitx_number_input_exponent_tl
4916 \l_@@_input_ignore_tl
4917 \l_@@_input_uncert_open_tl
4918 \l_siunitx_number_input_sign_tl
4919 \l_@@_input_uncert_sign_tl
4920 \l_@@_input_uncert_divide_tl
4921 \q_recursion_tail
4922 \q_recursion_stop
4923 }
4924 \cs_new:Npn \@@_token_auxi:NN #1#2
4925 {
4926 \quark_if_recursion_tail_stop_do:Nn #2 { \prg_return_false: }
4927 \@@_token_auxii:NN #1 #2
4928 \@@_token_auxi:NN #1
4929 }
4930 \cs_new:Npn \@@_token_auxii:NN #1#2
4931 {
4932 \exp_after:wN \@@_token_auxiii:NN \exp_after:wN #1
4933 #2 \q_recursion_tail \q_recursion_stop
4934 }
4935 \cs_new:Npn \@@_token_auxiii:NN #1#2
4936 {
4937 \quark_if_recursion_tail_stop:N #2
4938 \str_if_eq:nnT {#1} {#2}
4939 {
4940 \use_i_delimit_by_q_recursion_stop:nw
4941 {
4942 \use_i_delimit_by_q_recursion_stop:nw
4943 { \prg_return_true: }
4944 }
4945 }
4946 \@@_token_auxiii:NN #1
4947 }
4948 % \end{macrocode}
4949 % \end{macro}
4950 % \end{macro}
4951 %
4952 % \subsection{Messages}
4953 %
4954 % \begin{macrocode}
4955 \msg_new:nnnn { siunitx } { invalid-number }
4956 { Invalid~number~'#1'. }
4957 {
4958 The~input~'#1'~could~not~be~parsed~as~a~number~following~the~
4959 format~defined~in~module~documentation.
4960 }
4961 \msg_new:nnnn { siunitx } { ambiguous-dropped-exponent }
4962 { Potentially~ambiguous~dropping~of~exponent. }
4963 {
4964 The~option~"drop-exponent"~is~active~but~values~do~not~have~an~
4965 exponent~fixed~by~"exponent-mode".~The~result~could~be~misleading.
4966 }
4967 % \end{macrocode}
4968 %
4969 % \subsection{Standard settings for module options}
4970 %
4971 % Some of these follow naturally from the point of definition
4972 % (\foreign{e.g.}~boolean variables are always |false| to begin with),
4973 % but for clarity everything is set here.
4974 % \begin{macrocode}
4975 \keys_set:nn { siunitx }
4976 {
4977 allow-uncertainty-breaks = true ,
4978 bracket-ambiguous-numbers = true ,
4979 bracket-negative-numbers = false ,
4980 drop-exponent = false ,
4981 drop-uncertainty = false ,
4982 drop-zero-decimal = false ,
4983 evaluate-expression = false ,
4984 exponent-base = 10 ,
4985 exponent-mode = input ,
4986 exponent-product = \times ,
4987 expression = #1 ,
4988 fixed-exponent = 0 ,
4989 digit-group-size = 3 ,
4990 digit-group-first-size = 3 ,
4991 digit-group-other-size = 3 ,
4992 final-digit-group-min-size = 1 ,
4993 group-digits = all ,
4994 group-minimum-digits = 5 ,
4995 group-separator = \, , % (
4996 input-close-uncertainty = ) ,
4997 input-comparators = { <=>\approx\ge\geq\gg\le\leq\ll\sim } ,
4998 input-decimal-markers = { ., } ,
4999 input-digits = 0123456789 ,
5000 input-exponent-markers = dDeE ,
5001 input-ignore = \, ,
5002 input-open-uncertainty = ( , % )
5003 input-signs = +-\mp\pm ,
5004 input-uncertainty-signs = \pm ,
5005 minimum-decimal-digits = 0 ,
5006 minimum-integer-digits = 0 ,
5007 negative-color = , % (
5008 output-close-uncertainty = ) ,
5009 output-decimal-marker = . ,
5010 output-open-uncertainty = ( , % )
5011 parse-numbers = true ,
5012 print-exponent-implicit-plus = false ,
5013 print-implicit-plus = false ,
5014 print-mantissa-implicit-plus = false ,
5015 print-unity-mantissa = true ,
5016 print-zero-exponent = false ,
5017 print-zero-integer = true ,
5018 retain-explicit-decimal-marker = false ,
5019 retain-explicit-plus = false ,
5020 retain-negative-zero = false ,
5021 retain-zero-uncertainty = false ,
5022 round-direction = nearest ,
5023 round-half = up ,
5024 round-minimum = 0 ,
5025 round-mode = none ,
5026 round-pad = true ,
5027 round-precision = 2 ,
5028 round-zero-positive = true ,
5029 simplify-uncertainty = false ,
5030 tight-spacing = false ,
5031 uncertainty-descriptor-mode = bracket-separator ,
5032 uncertainty-descriptor-separator = \ ,
5033 uncertainty-descriptors = ,
5034 uncertainty-mode = compact ,
5035 uncertainty-round-direction = nearest ,
5036 uncertainty-separator = ,
5037 zero-decimal-as-symbol = false ,
5038 zero-symbol = \mbox { --- }
5039 }
5040 % \end{macrocode}
5041 % Two awkward settings.
5042 % \begin{macrocode}
5043 \keys_set:ne { siunitx }
5044 {
5045 exponent-thresholds = -3 \c_colon_str 3 ,
5046 input-uncertainty-divider = \c_colon_str
5047 }
5048 % \end{macrocode}
5049 %
5050 % \begin{macrocode}
5051 %</package>
5052 % \end{macrocode}
5053 %
5054 % \end{implementation}
5055 %
5056 % \PrintIndex